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.handler;
21
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.node.ArrayNode;
24 import com.fasterxml.jackson.databind.node.ObjectNode;
25 import org.waarp.common.database.DbPreparedStatement;
26 import org.waarp.common.database.data.AbstractDbData;
27 import org.waarp.common.database.exception.WaarpDatabaseException;
28 import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
29 import org.waarp.common.database.exception.WaarpDatabaseSqlException;
30 import org.waarp.common.exception.InvalidArgumentException;
31 import org.waarp.common.json.JsonHandler;
32 import org.waarp.common.role.RoleDefault.ROLE;
33 import org.waarp.common.utility.ParametersChecker;
34 import org.waarp.gateway.kernel.exception.HttpForbiddenRequestException;
35 import org.waarp.gateway.kernel.exception.HttpIncorrectRequestException;
36 import org.waarp.gateway.kernel.exception.HttpInvalidAuthenticationException;
37 import org.waarp.gateway.kernel.exception.HttpNotFoundRequestException;
38 import org.waarp.gateway.kernel.rest.DataModelRestMethodHandler;
39 import org.waarp.gateway.kernel.rest.HttpRestHandler;
40 import org.waarp.gateway.kernel.rest.HttpRestHandler.METHOD;
41 import org.waarp.gateway.kernel.rest.RestArgument;
42 import org.waarp.gateway.kernel.rest.RestConfiguration;
43 import org.waarp.openr66.context.R66Session;
44 import org.waarp.openr66.database.data.DbConfiguration;
45 import org.waarp.openr66.database.data.DbConfiguration.Columns;
46 import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler;
47
48
49
50
51 public class DbConfigurationR66RestMethodHandler
52 extends DataModelRestMethodHandler<DbConfiguration> {
53 private static final String HOST_ID_AS_VARCHAR_IN_URI_AS =
54 "HostId as VARCHAR in URI as ";
55 public static final String BASEURI = "configurations";
56
57 public enum FILTER_ARGS {
58 HOSTID("host name"), BANDWIDTH(
59 "<0 for no filter, =0 for no bandwidth, >0 for a limit greater than value");
60
61 public final String type;
62
63 FILTER_ARGS(final String type) {
64 this.type = type;
65 }
66 }
67
68
69
70
71
72 public DbConfigurationR66RestMethodHandler(final RestConfiguration config,
73 final METHOD... method) {
74 super(BASEURI, config, method);
75 }
76
77 @Override
78 protected final DbConfiguration getItem(final HttpRestHandler handler,
79 final RestArgument arguments,
80 final RestArgument result,
81 final Object body)
82 throws HttpNotFoundRequestException {
83 try {
84 HttpRestV1Utils.checkSanity(arguments);
85 } catch (final InvalidArgumentException e) {
86 throw new HttpNotFoundRequestException("Issue on values", e);
87 }
88 final ObjectNode arg = arguments.getUriArgs().deepCopy();
89 arg.setAll(arguments.getBody());
90 try {
91 final JsonNode node = RestArgument.getId(arg);
92 final String id;
93 if (node.isMissingNode()) {
94
95 id = arg.path(DbConfiguration.Columns.HOSTID.name()).asText();
96 } else {
97 id = node.asText();
98 }
99 return new DbConfiguration(id);
100 } catch (final WaarpDatabaseException e) {
101 throw new HttpNotFoundRequestException(
102 "Issue while reading from database " + arg, e);
103 }
104 }
105
106 @Override
107 protected final DbConfiguration createItem(final HttpRestHandler handler,
108 final RestArgument arguments,
109 final RestArgument result,
110 final Object body)
111 throws HttpIncorrectRequestException {
112 try {
113 HttpRestV1Utils.checkSanity(arguments);
114 } catch (final InvalidArgumentException e) {
115 throw new HttpIncorrectRequestException("Issue on values", e);
116 }
117 final ObjectNode arg = arguments.getUriArgs().deepCopy();
118 arg.setAll(arguments.getBody());
119 try {
120 return new DbConfiguration(arg);
121 } catch (final WaarpDatabaseException e) {
122 throw new HttpIncorrectRequestException(
123 "Issue while inserting into database", e);
124 }
125 }
126
127 @Override
128 protected final DbPreparedStatement getPreparedStatement(
129 final HttpRestHandler handler, final RestArgument arguments,
130 final RestArgument result, final Object body)
131 throws HttpIncorrectRequestException {
132 try {
133 HttpRestV1Utils.checkSanity(arguments);
134 } catch (final InvalidArgumentException e) {
135 throw new HttpIncorrectRequestException("Issue on values", e);
136 }
137 final ObjectNode arg = arguments.getUriArgs().deepCopy();
138 arg.setAll(arguments.getBody());
139 String host = arg.path(FILTER_ARGS.HOSTID.name()).asText();
140 if (ParametersChecker.isEmpty(host)) {
141 host = null;
142 }
143 final int limit = arg.path(FILTER_ARGS.BANDWIDTH.name()).asInt(-1);
144 try {
145 return DbConfiguration.getFilterPrepareStament(handler.getDbSession(),
146 host, limit);
147 } catch (final WaarpDatabaseNoConnectionException e) {
148 throw new HttpIncorrectRequestException(
149 "Issue while reading from database", e);
150 } catch (final WaarpDatabaseSqlException e) {
151 throw new HttpIncorrectRequestException(
152 "Issue while reading from database", e);
153 }
154 }
155
156 @Override
157 protected final DbConfiguration getItemPreparedStatement(
158 final DbPreparedStatement statement)
159 throws HttpIncorrectRequestException, HttpNotFoundRequestException {
160 try {
161 return DbConfiguration.getFromStatement(statement);
162 } catch (final WaarpDatabaseNoConnectionException e) {
163 throw new HttpIncorrectRequestException(
164 "Issue while selecting from database", e);
165 } catch (final WaarpDatabaseSqlException e) {
166 throw new HttpNotFoundRequestException(
167 "Issue while selecting from database", e);
168 }
169 }
170
171 @Override
172 protected final ArrayNode getDetailedAllow() {
173 final ArrayNode node = JsonHandler.createArrayNode();
174
175 final ObjectNode node1 = JsonHandler.createObjectNode();
176 node1.put(AbstractDbData.JSON_MODEL, DbConfiguration.class.getSimpleName());
177
178 for (final DbConfiguration.Columns column : DbConfiguration.Columns.values()) {
179 node1.put(column.name(), DbConfiguration.dbTypes[column.ordinal()]);
180 }
181 ObjectNode node2;
182 ObjectNode node3;
183 if (methods.contains(METHOD.GET)) {
184 node2 = RestArgument.fillDetailedAllow(METHOD.GET, path + "/id",
185 COMMAND_TYPE.GET.name(),
186 JsonHandler.createObjectNode().put(
187 DbConfiguration.Columns.HOSTID.name(),
188 HOST_ID_AS_VARCHAR_IN_URI_AS +
189 path + "/id"), node1);
190 node.add(node2);
191
192 node3 = JsonHandler.createObjectNode();
193 for (final FILTER_ARGS arg : FILTER_ARGS.values()) {
194 node3.put(arg.name(), arg.type);
195 }
196 node2 = RestArgument.fillDetailedAllow(METHOD.GET, path,
197 COMMAND_TYPE.MULTIGET.name(),
198 node3,
199 JsonHandler.createArrayNode()
200 .add(node1));
201 node.add(node2);
202 }
203 if (methods.contains(METHOD.PUT)) {
204 node3 = JsonHandler.createObjectNode();
205 node3.put(DbConfiguration.Columns.HOSTID.name(),
206 HOST_ID_AS_VARCHAR_IN_URI_AS + path + "/id");
207 for (final DbConfiguration.Columns column : DbConfiguration.Columns.values()) {
208 if (column.name()
209 .equalsIgnoreCase(DbConfiguration.Columns.HOSTID.name())) {
210 continue;
211 }
212 node3.put(column.name(), DbConfiguration.dbTypes[column.ordinal()]);
213 }
214 node2 = RestArgument.fillDetailedAllow(METHOD.PUT, path + "/id",
215 COMMAND_TYPE.UPDATE.name(), node3,
216 node1);
217 node.add(node2);
218 }
219 if (methods.contains(METHOD.DELETE)) {
220 node3 = JsonHandler.createObjectNode();
221 node3.put(DbConfiguration.Columns.HOSTID.name(),
222 HOST_ID_AS_VARCHAR_IN_URI_AS + path + "/id");
223 node2 = RestArgument.fillDetailedAllow(METHOD.DELETE, path + "/id",
224 COMMAND_TYPE.DELETE.name(), node3,
225 node1);
226 node.add(node2);
227 }
228 if (methods.contains(METHOD.POST)) {
229 node3 = JsonHandler.createObjectNode();
230 for (final DbConfiguration.Columns column : DbConfiguration.Columns.values()) {
231 node3.put(column.name(), DbConfiguration.dbTypes[column.ordinal()]);
232 }
233 node2 = RestArgument.fillDetailedAllow(METHOD.POST, path,
234 COMMAND_TYPE.CREATE.name(), node3,
235 node1);
236 node.add(node2);
237 }
238 node2 = RestArgument.fillDetailedAllow(METHOD.OPTIONS, path,
239 COMMAND_TYPE.OPTIONS.name(), null,
240 null);
241 node.add(node2);
242
243 return node;
244 }
245
246 @Override
247 public final String getPrimaryPropertyName() {
248 return Columns.HOSTID.name();
249 }
250
251 @Override
252 protected final void put(final HttpRestHandler handler,
253 final RestArgument arguments,
254 final RestArgument result, final Object body)
255 throws HttpIncorrectRequestException, HttpInvalidAuthenticationException,
256 HttpNotFoundRequestException {
257 try {
258 HttpRestV1Utils.checkSanity(arguments);
259 } catch (final InvalidArgumentException e) {
260 throw new HttpIncorrectRequestException("Issue on values", e);
261 }
262 super.put(handler, arguments, result, body);
263 final DbConfiguration item = getItem(handler, arguments, result, body);
264 if (item.isOwnConfiguration()) {
265 item.updateConfiguration();
266 }
267 }
268
269 @Override
270 protected final void checkAuthorization(final HttpRestHandler handler,
271 final RestArgument arguments,
272 final RestArgument result,
273 final METHOD method)
274 throws HttpForbiddenRequestException {
275 try {
276 HttpRestV1Utils.checkSanity(arguments);
277 } catch (final InvalidArgumentException e) {
278 throw new HttpForbiddenRequestException("Issue on values", e);
279 }
280 final HttpRestR66Handler r66handler = (HttpRestR66Handler) handler;
281 final R66Session session = r66handler.getServerHandler().getSession();
282 if (!session.getAuth().isValidRole(ROLE.CONFIGADMIN)) {
283 throw new HttpForbiddenRequestException(
284 "Partner must have ConfigAdmin role");
285 }
286 }
287
288 }