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