1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.protocol.http.restv2.converters;
22
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.node.ObjectNode;
25 import org.waarp.common.database.exception.WaarpDatabaseSqlException;
26 import org.waarp.common.json.JsonHandler;
27 import org.waarp.common.logging.SysErrLogger;
28 import org.waarp.common.utility.ParametersChecker;
29 import org.waarp.common.utility.WaarpStringUtils;
30 import org.waarp.openr66.pojo.Host;
31 import org.waarp.openr66.protocol.http.restv2.errors.RestError;
32 import org.waarp.openr66.protocol.http.restv2.errors.RestErrorException;
33
34 import javax.ws.rs.InternalServerErrorException;
35 import java.util.ArrayList;
36 import java.util.Comparator;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40
41 import static org.waarp.common.file.FileUtils.*;
42 import static org.waarp.openr66.protocol.configuration.Configuration.*;
43 import static org.waarp.openr66.protocol.http.restv2.RestConstants.HostFields.*;
44 import static org.waarp.openr66.protocol.http.restv2.errors.RestErrors.*;
45
46
47
48
49
50
51 public final class HostConverter {
52
53
54
55
56 private HostConverter() throws InstantiationException {
57 throw new InstantiationException(
58 getClass().getName() + " cannot be instantiated.");
59 }
60
61
62
63
64
65
66 public enum Order {
67
68
69
70 ascId(new Comparator<Host>() {
71 @Override
72 public final int compare(final Host t1, final Host t2) {
73 return t1.getHostid().compareTo(t2.getHostid());
74 }
75 }),
76
77
78
79 descId(new Comparator<Host>() {
80 @Override
81 public final int compare(final Host t1, final Host t2) {
82 return -t1.getHostid().compareTo(t2.getHostid());
83 }
84 }),
85
86
87
88 ascAddress(new Comparator<Host>() {
89 @Override
90 public final int compare(final Host t1, final Host t2) {
91 return t1.getAddress().compareTo(t2.getAddress());
92 }
93 }),
94
95
96
97 descAddress(new Comparator<Host>() {
98 @Override
99 public final int compare(final Host t1, final Host t2) {
100 return -t1.getAddress().compareTo(t2.getAddress());
101 }
102 });
103
104
105
106
107 public final Comparator<Host> comparator;
108
109 Order(final Comparator<Host> comparator) {
110 this.comparator = comparator;
111 }
112 }
113
114
115
116
117
118
119
120
121
122
123 public static ObjectNode hostToNode(final Host host) {
124 final ObjectNode node = JsonHandler.createObjectNode();
125 node.put(HOST_NAME, host.getHostid());
126 node.put(ADDRESS, host.getAddress());
127 node.put(PORT, host.getPort());
128 node.put(PASSWORD, host.getHostkey());
129 node.put(IS_SSL, host.isSSL());
130 node.put(IS_CLIENT, host.isClient());
131 node.put(IS_ADMIN, host.isAdmin());
132 node.put(IS_ACTIVE, host.isActive());
133 node.put(IS_PROXY, host.isProxified());
134
135 return node;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public static Host nodeToNewHost(final ObjectNode object) {
151 Host emptyHost = null;
152 try {
153 emptyHost =
154 new Host(null, null, -1, null, false, false, false, false, true);
155 } catch (final WaarpDatabaseSqlException e) {
156 SysErrLogger.FAKE_LOGGER.syserr(e);
157 }
158
159 return nodeToUpdatedHost(object, emptyHost);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 public static Host nodeToUpdatedHost(final ObjectNode object,
179 final Host oldHost) {
180
181 final List<RestError> errors = new ArrayList<RestError>();
182
183 final Iterator<Map.Entry<String, JsonNode>> fields = object.fields();
184 while (fields.hasNext()) {
185 final Map.Entry<String, JsonNode> field = fields.next();
186 final String name = field.getKey();
187 final JsonNode value = field.getValue();
188
189 if (name.equalsIgnoreCase(HOST_NAME)) {
190 if (value.isTextual()) {
191 if (oldHost.getHostid() == null) {
192 oldHost.setHostid(value.asText());
193 } else if (!oldHost.getHostid().equals(value.asText())) {
194 errors.add(FIELD_NOT_ALLOWED(HOST_NAME));
195 }
196 } else {
197 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
198 }
199 } else if (name.equalsIgnoreCase(ADDRESS)) {
200 if (value.isTextual()) {
201 oldHost.setAddress(value.asText());
202 } else {
203 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
204 }
205 } else if (name.equalsIgnoreCase(PORT)) {
206 if (value.canConvertToInt() && value.asInt() >= 0 &&
207 value.asInt() < ZERO_COPY_CHUNK_SIZE) {
208 oldHost.setPort(value.asInt());
209 } else {
210 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
211 }
212 } else if (name.equalsIgnoreCase(PASSWORD)) {
213 if (value.isTextual()) {
214 oldHost.setHostkey(encryptPassword(value.asText()));
215 } else {
216 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
217 }
218 } else if (name.equalsIgnoreCase(IS_SSL)) {
219 if (value.isBoolean()) {
220 oldHost.setSSL(value.asBoolean());
221 } else {
222 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
223 }
224 } else if (name.equalsIgnoreCase(IS_CLIENT)) {
225 if (value.isBoolean()) {
226 oldHost.setClient(value.asBoolean());
227 } else {
228 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
229 }
230 } else if (name.equalsIgnoreCase(IS_ADMIN)) {
231 if (value.isBoolean()) {
232 oldHost.setAdmin(value.asBoolean());
233 } else {
234 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
235 }
236 } else if (name.equalsIgnoreCase(IS_ACTIVE)) {
237 if (value.isBoolean()) {
238 oldHost.setActive(value.asBoolean());
239 } else {
240 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
241 }
242 } else if (name.equalsIgnoreCase(IS_PROXY)) {
243 if (value.isBoolean()) {
244 oldHost.setProxified(value.asBoolean());
245 } else {
246 errors.add(ILLEGAL_FIELD_VALUE(name, value.toString()));
247 }
248 } else {
249 errors.add(UNKNOWN_FIELD(name));
250 }
251 }
252
253 errors.addAll(checkRequiredFields(oldHost));
254
255 if (errors.isEmpty()) {
256 return oldHost;
257 } else {
258 throw new RestErrorException(errors);
259 }
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274 private static byte[] encryptPassword(final String password) {
275 try {
276 return configuration.getCryptoKey().cryptToHex(password)
277 .getBytes(WaarpStringUtils.UTF8);
278 } catch (final Exception e) {
279 throw new InternalServerErrorException(
280 "Failed to encrypt the host password", e);
281 }
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295 private static List<RestError> checkRequiredFields(final Host host) {
296 final List<RestError> errors = new ArrayList<RestError>();
297 if (ParametersChecker.isEmpty(host.getHostid())) {
298 errors.add(MISSING_FIELD(HOST_NAME));
299 }
300 if (ParametersChecker.isEmpty(host.getAddress())) {
301 errors.add(MISSING_FIELD(ADDRESS));
302 }
303 if (host.getPort() == -1) {
304 errors.add(MISSING_FIELD(PORT));
305 }
306 if (host.getHostkey() == null || host.getHostkey().length == 0) {
307 errors.add(MISSING_FIELD(PASSWORD));
308 }
309
310 return errors;
311 }
312 }