2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.service;
26 import java.security.cert.X509Certificate;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.security.auth.x500.X500Principal;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.DELETE;
36 import javax.ws.rs.Encoded;
37 import javax.ws.rs.GET;
38 import javax.ws.rs.POST;
39 import javax.ws.rs.PUT;
40 import javax.ws.rs.Path;
41 import javax.ws.rs.PathParam;
42 import javax.ws.rs.Produces;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.HttpHeaders;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import javax.ws.rs.core.Response.Status;
48 import javax.ws.rs.core.UriInfo;
50 import org.apache.cxf.jaxrs.ext.PATCH;
51 import org.onap.aaiauth.auth.Auth;
52 import org.onap.aai.cl.api.Logger;
53 import org.onap.aai.cl.eelf.LoggerFactory;
54 import org.onap.crud.exception.CrudException;
55 import org.onap.crud.logging.CrudServiceMsgs;
56 import org.onap.crud.logging.LoggingUtil;
57 import org.onap.crud.util.CrudServiceConstants;
58 import org.onap.crud.util.CrudServiceUtil;
61 import com.google.gson.JsonElement;
63 public class CrudRestService {
65 private AbstractGraphDataService graphDataService;
66 Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName());
67 Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName());
70 private String mediaType = MediaType.APPLICATION_JSON;
71 public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
73 public CrudRestService(AbstractGraphDataService graphDataService) throws Exception {
74 this.graphDataService = graphDataService;
75 this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
79 POST, GET, PUT, DELETE, PATCH
82 public void startup() {
87 @Path("/{version}/{type}/{id}")
88 @Consumes({ MediaType.APPLICATION_JSON })
89 @Produces({ MediaType.APPLICATION_JSON })
90 public Response getVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
91 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
92 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
93 LoggingUtil.initMdcContext(req, headers);
95 logger.debug("Incoming request..." + content);
96 Response response = null;
98 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
101 String result = graphDataService.getVertex(version, id, type);
102 response = Response.status(Status.OK).entity(result).type(mediaType).build();
103 } catch (CrudException ce) {
104 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
105 } catch (Exception e) {
106 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
109 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
112 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
117 @Path("/{version}/{type}/")
118 @Consumes({ MediaType.APPLICATION_JSON })
119 @Produces({ MediaType.APPLICATION_JSON })
120 public Response getVertices(String content, @PathParam("version") String version, @PathParam("type") String type,
121 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
122 @Context HttpServletRequest req) {
124 LoggingUtil.initMdcContext(req, headers);
126 logger.debug("Incoming request..." + content);
127 Response response = null;
128 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
130 Map<String, String> filter = new HashMap<String, String>();
131 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
132 filter.put(e.getKey(), e.getValue().get(0));
136 String result = graphDataService.getVertices(version, type, filter);
137 response = Response.status(Status.OK).entity(result).type(mediaType).build();
138 } catch (CrudException ce) {
139 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
140 } catch (Exception e) {
141 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
144 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
147 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
152 @Path("/relationships/{version}/{type}/{id}")
153 @Consumes({ MediaType.APPLICATION_JSON })
154 @Produces({ MediaType.APPLICATION_JSON })
155 public Response getEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
156 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
157 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
158 LoggingUtil.initMdcContext(req, headers);
160 logger.debug("Incoming request..." + content);
161 Response response = null;
163 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
167 String result = graphDataService.getEdge(version, id, type);
168 response = Response.status(Status.OK).entity(result).type(mediaType).build();
169 } catch (CrudException ce) {
170 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
171 } catch (Exception e) {
172 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
175 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
178 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
183 @Path("/relationships/{version}/{type}/")
184 @Consumes({ MediaType.APPLICATION_JSON })
185 @Produces({ MediaType.APPLICATION_JSON })
186 public Response getEdges(String content, @PathParam("version") String version, @PathParam("type") String type,
187 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
188 @Context HttpServletRequest req) {
190 LoggingUtil.initMdcContext(req, headers);
192 logger.debug("Incoming request..." + content);
193 Response response = null;
195 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
197 Map<String, String> filter = new HashMap<String, String>();
198 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
199 filter.put(e.getKey(), e.getValue().get(0));
203 String result = graphDataService.getEdges(version, type, filter);
204 response = Response.status(Status.OK).entity(result).type(mediaType).build();
205 } catch (CrudException ce) {
206 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
207 } catch (Exception e) {
208 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
211 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
215 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
220 @Path("/relationships/{version}/{type}/{id}")
221 @Consumes({ MediaType.APPLICATION_JSON })
222 @Produces({ MediaType.APPLICATION_JSON })
223 public Response updateEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
224 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
225 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
227 LoggingUtil.initMdcContext(req, headers);
229 logger.debug("Incoming request..." + content);
230 Response response = null;
232 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
235 EdgePayload payload = EdgePayload.fromJson(content);
236 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
237 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
239 if (payload.getId() != null && !payload.getId().equals(id)) {
240 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
244 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
245 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
246 result = graphDataService.patchEdge(version, id, type, payload);
249 result = graphDataService.updateEdge(version, id, type, payload);
252 response = Response.status(Status.OK).entity(result).type(mediaType).build();
253 } catch (CrudException ce) {
254 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
255 } catch (Exception e) {
256 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
259 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
263 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
268 @Path("/relationships/{version}/{type}/{id}")
269 @Consumes({ "application/merge-patch+json" })
270 @Produces({ MediaType.APPLICATION_JSON })
271 public Response patchEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
272 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
273 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
275 LoggingUtil.initMdcContext(req, headers);
277 logger.debug("Incoming request..." + content);
278 Response response = null;
279 if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
282 EdgePayload payload = EdgePayload.fromJson(content);
283 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
284 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
286 if (payload.getId() != null && !payload.getId().equals(id)) {
287 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
290 String result = graphDataService.patchEdge(version, id, type, payload);
291 response = Response.status(Status.OK).entity(result).type(mediaType).build();
292 } catch (CrudException ce) {
293 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
294 } catch (Exception e) {
295 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
298 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
301 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
306 @Path("/{version}/{type}/{id}")
307 @Consumes({ MediaType.APPLICATION_JSON })
308 @Produces({ MediaType.APPLICATION_JSON })
309 public Response updateVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
310 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
311 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
313 LoggingUtil.initMdcContext(req, headers);
315 logger.debug("Incoming request..." + content);
316 Response response = null;
318 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
321 VertexPayload payload = VertexPayload.fromJson(content);
322 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
323 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
325 if (payload.getId() != null && !payload.getId().equals(id)) {
326 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
330 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false));
332 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
333 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
334 result = graphDataService.patchVertex(version, id, type, payload);
337 result = graphDataService.updateVertex(version, id, type, payload);
339 response = Response.status(Status.OK).entity(result).type(mediaType).build();
340 } catch (CrudException ce) {
341 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
342 } catch (Exception e) {
343 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
346 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
349 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
354 @Path("/{version}/{type}/{id}")
355 @Consumes({ "application/merge-patch+json" })
356 @Produces({ MediaType.APPLICATION_JSON })
357 public Response patchVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
358 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
359 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
361 LoggingUtil.initMdcContext(req, headers);
363 logger.debug("Incoming request..." + content);
364 Response response = null;
366 if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
368 VertexPayload payload = VertexPayload.fromJson(content);
369 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
370 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
372 if (payload.getId() != null && !payload.getId().equals(id)) {
373 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
376 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false));
378 String result = graphDataService.patchVertex(version, id, type, payload);
379 response = Response.status(Status.OK).entity(result).type(mediaType).build();
380 } catch (CrudException ce) {
381 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
382 } catch (Exception e) {
383 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
386 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
389 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
394 @Path("/{version}/{type}/")
395 @Consumes({ MediaType.APPLICATION_JSON })
396 @Produces({ MediaType.APPLICATION_JSON })
397 public Response addVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
398 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
399 @Context HttpServletRequest req) {
401 LoggingUtil.initMdcContext(req, headers);
403 logger.debug("Incoming request..." + content);
404 Response response = null;
406 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
409 VertexPayload payload = VertexPayload.fromJson(content);
410 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
411 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
413 if (payload.getId() != null) {
414 throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST);
417 if (payload.getType() != null && !payload.getType().equals(type)) {
418 throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
421 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true));
423 String result = graphDataService.addVertex(version, type, payload);
424 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
425 } catch (CrudException ce) {
426 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
427 } catch (Exception e) {
428 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
431 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
434 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
438 private void validateBulkPayload(BulkPayload payload) throws CrudException {
439 List<String> vertices = new ArrayList<String>();
440 List<String> edges = new ArrayList<String>();
442 for (JsonElement v : payload.getObjects()) {
443 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
444 v.getAsJsonObject().entrySet());
446 if (entries.size() != 2) {
447 throw new CrudException("", Status.BAD_REQUEST);
449 Map.Entry<String, JsonElement> opr = entries.get(0);
450 Map.Entry<String, JsonElement> item = entries.get(1);
452 if (vertices.contains(item.getKey())) {
453 throw new CrudException("duplicate vertex in payload: " + item.getKey(), Status.BAD_REQUEST);
455 VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
456 if (vertexPayload.getType() == null) {
457 throw new CrudException("Vertex Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
460 if (!opr.getKey().equalsIgnoreCase("operation")) {
461 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
464 if (!opr.getValue().getAsString().equalsIgnoreCase("add")
465 && !opr.getValue().getAsString().equalsIgnoreCase("modify")
466 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
467 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
469 // check if ID is populate for modify/delete operation
470 if ((opr.getValue().getAsString().equalsIgnoreCase("modify")
471 || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) {
473 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
477 vertices.add(item.getKey());
480 for (JsonElement v : payload.getRelationships()) {
481 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
482 v.getAsJsonObject().entrySet());
484 if (entries.size() != 2) {
485 throw new CrudException("", Status.BAD_REQUEST);
487 Map.Entry<String, JsonElement> opr = entries.get(0);
488 Map.Entry<String, JsonElement> item = entries.get(1);
490 if (edges.contains(item.getKey())) {
491 throw new CrudException("duplicate Edge in payload: " + item.getKey(), Status.BAD_REQUEST);
494 EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
496 if (edgePayload.getType() == null) {
497 throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
500 if (!opr.getKey().equalsIgnoreCase("operation")) {
501 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
504 if (!opr.getValue().getAsString().equalsIgnoreCase("add")
505 && !opr.getValue().getAsString().equalsIgnoreCase("modify")
506 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
507 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
509 // check if ID is populate for modify/delete operation
510 if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify")
511 || opr.getValue().getAsString().equalsIgnoreCase("delete"))) {
513 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
516 if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
517 if (edgePayload.getSource() == null || edgePayload.getTarget() == null) {
518 throw new CrudException("Source/Target cannot be null for edge: " + item.getKey(), Status.BAD_REQUEST);
520 if (edgePayload.getSource().startsWith("$") && !vertices.contains(edgePayload.getSource().substring(1))) {
521 throw new CrudException(
522 "Source Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(),
526 if (edgePayload.getTarget().startsWith("$") && !vertices.contains(edgePayload.getTarget().substring(1))) {
527 throw new CrudException(
528 "Target Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(),
532 edges.add(item.getKey());
539 @Path("/{version}/bulk/")
540 @Consumes({ MediaType.APPLICATION_JSON })
541 @Produces({ MediaType.APPLICATION_JSON })
542 public Response addBulk(String content, @PathParam("version") String version, @PathParam("type") String type,
543 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
544 @Context HttpServletRequest req) {
546 LoggingUtil.initMdcContext(req, headers);
548 logger.debug("Incoming request..." + content);
549 Response response = null;
551 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
554 BulkPayload payload = BulkPayload.fromJson(content);
555 if ((payload.getObjects() == null && payload.getRelationships() == null)
556 || (payload.getObjects() != null && payload.getObjects().isEmpty() && payload.getRelationships() != null
557 && payload.getRelationships().isEmpty())) {
558 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
561 validateBulkPayload(payload);
562 String result = graphDataService.addBulk(version, payload, headers);
563 response = Response.status(Status.OK).entity(result).type(mediaType).build();
564 } catch (CrudException ce) {
565 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
566 } catch (Exception e) {
567 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
570 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
573 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
579 @Consumes({ MediaType.APPLICATION_JSON })
580 @Produces({ MediaType.APPLICATION_JSON })
581 public Response addVertex(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
582 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
584 LoggingUtil.initMdcContext(req, headers);
586 logger.debug("Incoming request..." + content);
587 Response response = null;
589 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
592 VertexPayload payload = VertexPayload.fromJson(content);
593 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
594 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
596 if (payload.getId() != null) {
597 throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST);
600 if (payload.getType() == null || payload.getType().isEmpty()) {
601 throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST);
604 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true));
606 String result = graphDataService.addVertex(version, payload.getType(), payload);
607 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
608 } catch (CrudException ce) {
609 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
610 } catch (Exception e) {
611 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
614 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
617 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
622 @Path("/relationships/{version}/{type}/")
623 @Consumes({ MediaType.APPLICATION_JSON })
624 @Produces({ MediaType.APPLICATION_JSON })
625 public Response addEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
626 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
627 @Context HttpServletRequest req) {
629 LoggingUtil.initMdcContext(req, headers);
631 logger.debug("Incoming request..." + content);
632 Response response = null;
634 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
637 EdgePayload payload = EdgePayload.fromJson(content);
638 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
639 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
641 if (payload.getId() != null) {
642 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
645 if (payload.getType() != null && !payload.getType().equals(type)) {
646 throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
648 String result = graphDataService.addEdge(version, type, payload);
649 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
650 } catch (CrudException ce) {
651 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
652 } catch (Exception e) {
653 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
656 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
659 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
664 @Path("/relationships/{version}/")
665 @Consumes({ MediaType.APPLICATION_JSON })
666 @Produces({ MediaType.APPLICATION_JSON })
667 public Response addEdge(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
668 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
670 LoggingUtil.initMdcContext(req, headers);
672 logger.debug("Incoming request..." + content);
673 Response response = null;
675 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
678 EdgePayload payload = EdgePayload.fromJson(content);
679 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
680 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
682 if (payload.getId() != null) {
683 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
686 if (payload.getType() == null || payload.getType().isEmpty()) {
687 throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
689 String result = graphDataService.addEdge(version, payload.getType(), payload);
691 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
692 } catch (CrudException ce) {
693 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
694 } catch (Exception e) {
695 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
698 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
701 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
706 @Path("/{version}/{type}/{id}")
707 @Consumes({ MediaType.APPLICATION_JSON })
708 @Produces({ MediaType.APPLICATION_JSON })
709 public Response deleteVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
710 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
711 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
713 LoggingUtil.initMdcContext(req, headers);
715 logger.debug("Incoming request..." + content);
716 Response response = null;
718 if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
721 String result = graphDataService.deleteVertex(version, id, type);
722 response = Response.status(Status.OK).entity(result).type(mediaType).build();
723 } catch (CrudException ce) {
724 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
725 } catch (Exception e) {
726 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
729 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
732 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
737 @Path("/relationships/{version}/{type}/{id}")
738 @Consumes({ MediaType.APPLICATION_JSON })
739 @Produces({ MediaType.APPLICATION_JSON })
740 public Response deleteEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
741 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
742 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
744 LoggingUtil.initMdcContext(req, headers);
746 logger.debug("Incoming request..." + content);
747 Response response = null;
748 if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
751 String result = graphDataService.deleteEdge(version, id, type);
752 response = Response.status(Status.OK).entity(result).type(mediaType).build();
753 } catch (CrudException ce) {
754 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
755 } catch (Exception e) {
756 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
759 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
762 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
766 protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action,
767 String authPolicyFunctionName) {
769 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
770 String authUser = null;
771 if (cipherSuite != null) {
772 X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
773 X509Certificate clientCert = certChain[0];
774 X500Principal subjectDn = clientCert.getSubjectX500Principal();
775 authUser = subjectDn.toString();
777 return this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
778 } catch (Exception e) {
779 logResult(action, uri, e);
784 void logResult(Action op, String uri, Exception e) {
786 logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, e.getStackTrace().toString());
788 // Clear the MDC context so that no other transaction inadvertently
789 // uses our transaction id.