1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.protocol.http.rest.client;
21
22 import com.fasterxml.jackson.databind.node.ObjectNode;
23 import io.netty.channel.Channel;
24 import io.netty.channel.ChannelInitializer;
25 import io.netty.channel.socket.SocketChannel;
26 import io.netty.handler.codec.http.HttpMethod;
27 import org.waarp.common.database.data.AbstractDbData;
28 import org.waarp.common.json.JsonHandler;
29 import org.waarp.common.utility.ParametersChecker;
30 import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
31 import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler;
32 import org.waarp.gateway.kernel.rest.HttpRestHandler;
33 import org.waarp.gateway.kernel.rest.RestArgument;
34 import org.waarp.gateway.kernel.rest.RestConfiguration;
35 import org.waarp.gateway.kernel.rest.client.HttpRestClientHelper;
36 import org.waarp.gateway.kernel.rest.client.RestFuture;
37 import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler.RESTHANDLERS;
38 import org.waarp.openr66.protocol.localhandler.packet.json.JsonPacket;
39
40 import java.util.Map;
41
42
43
44
45 public class HttpRestR66Client extends HttpRestClientHelper {
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public RestFuture sendQuery(final RestConfiguration config,
72 final Channel channel, final HttpMethod method,
73 final String host, final String addedUri,
74 final String user, final String pwd,
75 final Map<String, String> uriArgs,
76 final String json) {
77 if (config.isRestSignature()) {
78 return sendQuery(config.getHmacSha256(), channel, method, host, addedUri,
79 user, pwd, uriArgs, json);
80 } else {
81 return sendQuery(channel, method, host, addedUri, user, uriArgs, json);
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94 public HttpRestR66Client(final String baseUri,
95 final ChannelInitializer<SocketChannel> initializer,
96 final int client, final long timeout) {
97 super(baseUri, client, timeout, initializer);
98 }
99
100
101
102
103
104
105 public RESTHANDLERS getRestHandler(final RestArgument bodyResponse) {
106 final ObjectNode node = bodyResponse.getAnswer();
107 final String model = node.path(AbstractDbData.JSON_MODEL).asText();
108 try {
109 if (ParametersChecker.isNotEmpty(model)) {
110 return RESTHANDLERS.valueOf(model);
111 }
112 } catch (final Exception ignored) {
113
114 }
115 return null;
116 }
117
118
119
120
121
122
123
124 public String getPrimaryProperty(final RestArgument bodyResponse) {
125 final ObjectNode answer = bodyResponse.getAnswer();
126 final String model = answer.path(AbstractDbData.JSON_MODEL).asText();
127 final String property = getPrimaryPropertyName(model);
128 if (property == null) {
129 return null;
130 }
131 return answer.path(property).asText();
132 }
133
134
135
136
137
138
139 public String getPrimaryPropertyName(final String model) {
140 try {
141 if (ParametersChecker.isNotEmpty(model)) {
142 final RESTHANDLERS dbdata = RESTHANDLERS.valueOf(model);
143 final DataModelRestMethodHandler<?> handler =
144 (DataModelRestMethodHandler<?>) HttpRestHandler.defaultConfiguration.restHashMap.get(
145 dbdata.uri);
146 return handler.getPrimaryPropertyName();
147 }
148 } catch (final Exception ignored) {
149
150 }
151 return null;
152 }
153
154
155
156
157
158
159
160
161 @SuppressWarnings("unchecked")
162 public AbstractDbData getDbDataFromFuture(final RestFuture future)
163 throws HttpIncorrectRequestException {
164 if (future.getRestArgument() != null) {
165 final RestArgument arg = future.getRestArgument();
166 final ObjectNode node = arg.getAnswer();
167 final String model = node.path(AbstractDbData.JSON_MODEL).asText();
168 try {
169 if (ParametersChecker.isNotEmpty(model)) {
170 final RESTHANDLERS rmodel = RESTHANDLERS.valueOf(model);
171 try {
172 return (AbstractDbData) rmodel.clasz.getConstructor(
173 ObjectNode.class).newInstance(node);
174 } catch (final Exception e) {
175 throw new HttpIncorrectRequestException(e);
176 }
177 }
178 } catch (final Exception ignored) {
179
180 }
181 }
182 return null;
183 }
184
185
186
187
188
189
190
191
192 public JsonPacket getJsonPacketFromFuture(final RestFuture future)
193 throws HttpIncorrectRequestException {
194 if (future.getRestArgument() != null) {
195 final RestArgument arg = future.getRestArgument();
196 final ObjectNode node = arg.getAnswer();
197 try {
198 return JsonPacket.createFromBuffer(JsonHandler.writeAsString(node));
199 } catch (final Exception e) {
200 throw new HttpIncorrectRequestException(e);
201 }
202 }
203 return null;
204 }
205
206 }