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.openr66.pojo.Limit;
29 import org.waarp.openr66.protocol.http.restv2.errors.RestError;
30 import org.waarp.openr66.protocol.http.restv2.errors.RestErrorException;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.waarp.openr66.protocol.http.restv2.RestConstants.LimitsFields.*;
37 import static org.waarp.openr66.protocol.http.restv2.RestConstants.*;
38 import static org.waarp.openr66.protocol.http.restv2.errors.RestErrors.*;
39
40
41
42
43
44
45 public final class LimitsConverter {
46
47
48
49
50 private LimitsConverter() throws InstantiationException {
51 throw new InstantiationException(
52 getClass().getName() + " cannot be instantiated.");
53 }
54
55
56
57
58
59
60
61
62
63
64 public static ObjectNode limitToNode(final Limit limits) {
65 final ObjectNode node = JsonHandler.createObjectNode();
66 node.put(READ_GLOBAL_LIMIT, limits.getReadGlobalLimit());
67 node.put(WRITE_GLOBAL_LIMIT, limits.getWriteGlobalLimit());
68 node.put(READ_SESSION_LIMIT, limits.getReadSessionLimit());
69 node.put(WRITE_SESSION_LIMIT, limits.getWriteSessionLimit());
70 node.put(DELAY_LIMIT, limits.getDelayLimit());
71
72 return node;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public static Limit nodeToNewLimit(final ObjectNode object) {
86 Limit emptyLimits = null;
87 try {
88 emptyLimits = new Limit(serverName(), 0, 0, 0, 0, 0);
89 } catch (final WaarpDatabaseSqlException e) {
90 SysErrLogger.FAKE_LOGGER.syserr(e);
91 }
92 return nodeToUpdatedLimit(object, emptyLimits);
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public static Limit nodeToUpdatedLimit(final ObjectNode object,
110 final Limit oldLimits) {
111 final List<RestError> errors = new ArrayList<RestError>();
112
113 while (object.fields().hasNext()) {
114 final Map.Entry<String, JsonNode> field = object.fields().next();
115 final String name = field.getKey();
116 final JsonNode value = field.getValue();
117
118 if (name.equalsIgnoreCase(READ_GLOBAL_LIMIT)) {
119 if (value.canConvertToLong() && value.asLong() >= 0) {
120 oldLimits.setReadGlobalLimit(value.asLong());
121 } else {
122 errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
123 }
124 } else if (name.equalsIgnoreCase(WRITE_GLOBAL_LIMIT)) {
125 if (value.canConvertToLong() && value.asLong() >= 0) {
126 oldLimits.setWriteGlobalLimit(value.asLong());
127 } else {
128 errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
129 }
130 } else if (name.equalsIgnoreCase(READ_SESSION_LIMIT)) {
131 if (value.canConvertToLong() && value.asLong() >= 0) {
132 oldLimits.setReadSessionLimit(value.asLong());
133 } else {
134 errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
135 }
136 } else if (name.equalsIgnoreCase(WRITE_SESSION_LIMIT)) {
137 if (value.canConvertToLong() && value.asLong() >= 0) {
138 oldLimits.setWriteSessionLimit(value.asLong());
139 } else {
140 errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
141 }
142 } else if (name.equalsIgnoreCase(DELAY_LIMIT)) {
143 if (value.canConvertToLong() && value.asLong() >= 0) {
144 oldLimits.setDelayLimit(value.asLong());
145 } else {
146 errors.add(ILLEGAL_PARAMETER_VALUE(name, value.toString()));
147 }
148 } else {
149 errors.add(UNKNOWN_FIELD(name));
150 }
151 }
152
153 if (errors.isEmpty()) {
154 return oldLimits;
155 } else {
156 throw new RestErrorException(errors);
157 }
158 }
159 }