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 com.google.gson.JsonElement;
28 import org.apache.cxf.jaxrs.ext.PATCH;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aaiauth.auth.Auth;
32 import org.onap.crud.exception.CrudException;
33 import org.onap.crud.logging.CrudServiceMsgs;
34 import org.onap.crud.logging.LoggingUtil;
35 import org.onap.crud.util.CrudServiceConstants;
36 import org.onap.crud.util.CrudServiceUtil;
39 import java.security.cert.X509Certificate;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
44 import javax.security.auth.x500.X500Principal;
45 import javax.servlet.http.HttpServletRequest;
47 import javax.ws.rs.core.*;
48 import javax.ws.rs.core.Response.Status;
50 public class CrudRestService {
52 private AbstractGraphDataService graphDataService;
53 Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName());
54 Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName());
57 private String mediaType = MediaType.APPLICATION_JSON;
58 public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
60 public CrudRestService(AbstractGraphDataService graphDataService) throws Exception {
61 this.graphDataService = graphDataService;
62 this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
66 POST, GET, PUT, DELETE, PATCH
69 public void startup() {
74 @Path("/{version}/{type}/{id}")
75 @Consumes({MediaType.APPLICATION_JSON})
76 @Produces({MediaType.APPLICATION_JSON})
77 public Response getVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
78 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
79 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
80 LoggingUtil.initMdcContext(req, headers);
82 logger.debug("Incoming request..." + content);
83 Response response = null;
87 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
88 String result = graphDataService.getVertex(version, id, type);
89 response = Response.status(Status.OK).entity(result).type(mediaType).build();
91 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
93 } catch (CrudException ce) {
94 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
95 } catch (Exception e) {
96 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
100 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
105 @Path("/{version}/{type}/")
106 @Consumes({MediaType.APPLICATION_JSON})
107 @Produces({MediaType.APPLICATION_JSON})
108 public Response getVertices(String content, @PathParam("version") String version, @PathParam("type") String type,
109 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
110 @Context HttpServletRequest req) {
112 LoggingUtil.initMdcContext(req, headers);
114 logger.debug("Incoming request..." + content);
115 Response response = null;
117 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
119 Map<String, String> filter = new HashMap<String, String>();
120 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
121 filter.put(e.getKey(), e.getValue().get(0));
124 String result = graphDataService.getVertices(version, type, filter);
125 response = Response.status(Status.OK).entity(result).type(mediaType).build();
127 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
129 } catch (CrudException ce) {
130 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
131 } catch (Exception e) {
132 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
136 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
141 @Path("/relationships/{version}/{type}/{id}")
142 @Consumes({MediaType.APPLICATION_JSON})
143 @Produces({MediaType.APPLICATION_JSON})
144 public Response getEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
145 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
146 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
147 LoggingUtil.initMdcContext(req, headers);
149 logger.debug("Incoming request..." + content);
150 Response response = null;
154 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
156 String result = graphDataService.getEdge(version, id, type);
157 response = Response.status(Status.OK).entity(result).type(mediaType).build();
159 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
161 } catch (CrudException ce) {
162 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
163 } catch (Exception e) {
164 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
167 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
172 @Path("/relationships/{version}/{type}/")
173 @Consumes({MediaType.APPLICATION_JSON})
174 @Produces({MediaType.APPLICATION_JSON})
175 public Response getEdges(String content, @PathParam("version") String version, @PathParam("type") String type,
176 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
177 @Context HttpServletRequest req) {
179 LoggingUtil.initMdcContext(req, headers);
181 logger.debug("Incoming request..." + content);
182 Response response = null;
185 Map<String, String> filter = new HashMap<String, String>();
186 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
187 filter.put(e.getKey(), e.getValue().get(0));
191 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
192 String result = graphDataService.getEdges(version, type, filter);
193 response = Response.status(Status.OK).entity(result).type(mediaType).build();
195 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
197 } catch (CrudException ce) {
198 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
199 } catch (Exception e) {
200 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
203 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
208 @Path("/relationships/{version}/{type}/{id}")
209 @Consumes({MediaType.APPLICATION_JSON})
210 @Produces({MediaType.APPLICATION_JSON})
211 public Response updateEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
212 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
213 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
215 LoggingUtil.initMdcContext(req, headers);
217 logger.debug("Incoming request..." + content);
218 Response response = null;
222 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
223 EdgePayload payload = EdgePayload.fromJson(content);
224 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
225 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
227 if (payload.getId() != null && !payload.getId().equals(id)) {
228 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
232 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
233 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
234 result = graphDataService.patchEdge(version, id, type, payload);
237 result = graphDataService.updateEdge(version, id, type, payload);
240 response = Response.status(Status.OK).entity(result).type(mediaType).build();
242 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
244 } catch (CrudException ce) {
245 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
246 } catch (Exception e) {
247 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
250 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
255 @Path("/relationships/{version}/{type}/{id}")
256 @Consumes({"application/merge-patch+json"})
257 @Produces({MediaType.APPLICATION_JSON})
258 public Response patchEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
259 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
260 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
262 LoggingUtil.initMdcContext(req, headers);
264 logger.debug("Incoming request..." + content);
265 Response response = null;
268 if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
269 EdgePayload payload = EdgePayload.fromJson(content);
270 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
271 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
273 if (payload.getId() != null && !payload.getId().equals(id)) {
274 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
277 String result = graphDataService.patchEdge(version, id, type, payload);
278 response = Response.status(Status.OK).entity(result).type(mediaType).build();
280 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
282 } catch (CrudException ce) {
283 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
284 } catch (Exception e) {
285 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
288 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
293 @Path("/{version}/{type}/{id}")
294 @Consumes({MediaType.APPLICATION_JSON})
295 @Produces({MediaType.APPLICATION_JSON})
296 public Response updateVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
297 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
298 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
300 LoggingUtil.initMdcContext(req, headers);
302 logger.debug("Incoming request..." + content);
303 Response response = null;
307 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
308 VertexPayload payload = VertexPayload.fromJson(content);
309 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
310 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
312 if (payload.getId() != null && !payload.getId().equals(id)) {
313 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
318 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false));
320 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
321 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
322 result = graphDataService.patchVertex(version, id, type, payload);
325 result = graphDataService.updateVertex(version, id, type, payload);
327 response = Response.status(Status.OK).entity(result).type(mediaType).build();
329 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
331 } catch (CrudException ce) {
332 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
333 } catch (Exception e) {
334 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
337 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
342 @Path("/{version}/{type}/{id}")
343 @Consumes({"application/merge-patch+json"})
344 @Produces({MediaType.APPLICATION_JSON})
345 public Response patchVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
346 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
347 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
349 LoggingUtil.initMdcContext(req, headers);
351 logger.debug("Incoming request..." + content);
352 Response response = null;
355 if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
356 VertexPayload payload = VertexPayload.fromJson(content);
357 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
358 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
360 if (payload.getId() != null && !payload.getId().equals(id)) {
361 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
364 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false));
366 String result = graphDataService.patchVertex(version, id, type, payload);
367 response = Response.status(Status.OK).entity(result).type(mediaType).build();
369 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
371 } catch (CrudException ce) {
372 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
373 } catch (Exception e) {
374 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
377 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
382 @Path("/{version}/{type}/")
383 @Consumes({MediaType.APPLICATION_JSON})
384 @Produces({MediaType.APPLICATION_JSON})
385 public Response addVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
386 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
387 @Context HttpServletRequest req) {
389 LoggingUtil.initMdcContext(req, headers);
391 logger.debug("Incoming request..." + content);
392 Response response = null;
396 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
397 VertexPayload payload = VertexPayload.fromJson(content);
398 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
399 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
401 if (payload.getId() != null) {
402 throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST);
405 if (payload.getType() != null && !payload.getType().equals(type)) {
406 throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
409 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true));
411 String result = graphDataService.addVertex(version, type, payload);
412 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
414 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
416 } catch (CrudException ce) {
417 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
418 } catch (Exception e) {
419 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
422 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
426 private void validateBulkPayload(BulkPayload payload) throws CrudException {
427 List<String> vertices = new ArrayList<String>();
428 List<String> edges = new ArrayList<String>();
430 for (JsonElement v : payload.getObjects()) {
431 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
432 v.getAsJsonObject().entrySet());
434 if (entries.size() != 2) {
435 throw new CrudException("", Status.BAD_REQUEST);
437 Map.Entry<String, JsonElement> opr = entries.get(0);
438 Map.Entry<String, JsonElement> item = entries.get(1);
440 if (vertices.contains(item.getKey())) {
441 throw new CrudException("duplicate vertex in payload: " + item.getKey(), Status.BAD_REQUEST);
443 VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
444 if (vertexPayload.getType() == null) {
445 throw new CrudException("Vertex Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
448 if (!opr.getKey().equalsIgnoreCase("operation")) {
449 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
452 if (!opr.getValue().getAsString().equalsIgnoreCase("add")
453 && !opr.getValue().getAsString().equalsIgnoreCase("modify")
454 && !opr.getValue().getAsString().equalsIgnoreCase("patch")
455 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
456 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
458 // check if ID is populate for modify/patch/delete operation
459 if ((opr.getValue().getAsString().equalsIgnoreCase("modify")
460 || opr.getValue().getAsString().equalsIgnoreCase("patch")
461 || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) {
463 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
467 vertices.add(item.getKey());
470 for (JsonElement v : payload.getRelationships()) {
471 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
472 v.getAsJsonObject().entrySet());
474 if (entries.size() != 2) {
475 throw new CrudException("", Status.BAD_REQUEST);
477 Map.Entry<String, JsonElement> opr = entries.get(0);
478 Map.Entry<String, JsonElement> item = entries.get(1);
480 if (edges.contains(item.getKey())) {
481 throw new CrudException("duplicate Edge in payload: " + item.getKey(), Status.BAD_REQUEST);
484 EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
486 if (edgePayload.getType() == null) {
487 throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
490 if (!opr.getKey().equalsIgnoreCase("operation")) {
491 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
494 if (!opr.getValue().getAsString().equalsIgnoreCase("add")
495 && !opr.getValue().getAsString().equalsIgnoreCase("modify")
496 && !opr.getValue().getAsString().equalsIgnoreCase("patch")
497 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
498 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
500 // check if ID is populate for modify/patch/delete operation
501 if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify")
502 || opr.getValue().getAsString().equalsIgnoreCase("patch")
503 || opr.getValue().getAsString().equalsIgnoreCase("delete"))) {
505 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
508 if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
509 if (edgePayload.getSource() == null || edgePayload.getTarget() == null) {
510 throw new CrudException("Source/Target cannot be null for edge: " + item.getKey(), Status.BAD_REQUEST);
512 if (edgePayload.getSource().startsWith("$") && !vertices.contains(edgePayload.getSource().substring(1))) {
513 throw new CrudException(
514 "Source Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(),
518 if (edgePayload.getTarget().startsWith("$") && !vertices.contains(edgePayload.getTarget().substring(1))) {
519 throw new CrudException(
520 "Target Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(),
524 edges.add(item.getKey());
531 @Path("/{version}/bulk/")
532 @Consumes({MediaType.APPLICATION_JSON})
533 @Produces({MediaType.APPLICATION_JSON})
534 public Response addBulk(String content, @PathParam("version") String version, @PathParam("type") String type,
535 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
536 @Context HttpServletRequest req) {
538 LoggingUtil.initMdcContext(req, headers);
540 logger.debug("Incoming request..." + content);
541 Response response = null;
545 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
546 BulkPayload payload = BulkPayload.fromJson(content);
547 if ((payload.getObjects() == null && payload.getRelationships() == null)
548 || (payload.getObjects() != null && payload.getObjects().isEmpty() && payload.getRelationships() != null
549 && payload.getRelationships().isEmpty())) {
550 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
553 validateBulkPayload(payload);
554 String result = graphDataService.addBulk(version, payload, headers);
555 response = Response.status(Status.OK).entity(result).type(mediaType).build();
557 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
559 } catch (CrudException ce) {
560 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
561 } catch (Exception e) {
562 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
565 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
571 @Consumes({MediaType.APPLICATION_JSON})
572 @Produces({MediaType.APPLICATION_JSON})
573 public Response addVertex(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
574 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
576 LoggingUtil.initMdcContext(req, headers);
578 logger.debug("Incoming request..." + content);
579 Response response = null;
583 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
584 VertexPayload payload = VertexPayload.fromJson(content);
585 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
586 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
588 if (payload.getId() != null) {
589 throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST);
592 if (payload.getType() == null || payload.getType().isEmpty()) {
593 throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST);
596 payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true));
598 String result = graphDataService.addVertex(version, payload.getType(), payload);
599 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
601 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
603 } catch (CrudException ce) {
604 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
605 } catch (Exception e) {
606 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
609 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
614 @Path("/relationships/{version}/{type}/")
615 @Consumes({MediaType.APPLICATION_JSON})
616 @Produces({MediaType.APPLICATION_JSON})
617 public Response addEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
618 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
619 @Context HttpServletRequest req) {
621 LoggingUtil.initMdcContext(req, headers);
623 logger.debug("Incoming request..." + content);
624 Response response = null;
628 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
629 EdgePayload payload = EdgePayload.fromJson(content);
630 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
631 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
633 if (payload.getId() != null) {
634 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
637 if (payload.getType() != null && !payload.getType().equals(type)) {
638 throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
640 String result = graphDataService.addEdge(version, type, payload);
641 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
643 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
645 } catch (CrudException ce) {
646 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
647 } catch (Exception e) {
648 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
651 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
656 @Path("/relationships/{version}/")
657 @Consumes({MediaType.APPLICATION_JSON})
658 @Produces({MediaType.APPLICATION_JSON})
659 public Response addEdge(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
660 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
662 LoggingUtil.initMdcContext(req, headers);
664 logger.debug("Incoming request..." + content);
665 Response response = null;
669 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
670 EdgePayload payload = EdgePayload.fromJson(content);
671 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
672 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
674 if (payload.getId() != null) {
675 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
678 if (payload.getType() == null || payload.getType().isEmpty()) {
679 throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
681 String result = graphDataService.addEdge(version, payload.getType(), payload);
683 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
685 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
687 } catch (CrudException ce) {
688 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
689 } catch (Exception e) {
690 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
693 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
698 @Path("/{version}/{type}/{id}")
699 @Consumes({MediaType.APPLICATION_JSON})
700 @Produces({MediaType.APPLICATION_JSON})
701 public Response deleteVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
702 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
703 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
705 LoggingUtil.initMdcContext(req, headers);
707 logger.debug("Incoming request..." + content);
708 Response response = null;
712 if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
713 String result = graphDataService.deleteVertex(version, id, type);
714 response = Response.status(Status.OK).entity(result).type(mediaType).build();
716 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
718 } catch (CrudException ce) {
719 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
720 } catch (Exception e) {
721 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
724 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
729 @Path("/relationships/{version}/{type}/{id}")
730 @Consumes({MediaType.APPLICATION_JSON})
731 @Produces({MediaType.APPLICATION_JSON})
732 public Response deleteEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
733 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
734 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
736 LoggingUtil.initMdcContext(req, headers);
738 logger.debug("Incoming request..." + content);
739 Response response = null;
742 if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
743 String result = graphDataService.deleteEdge(version, id, type);
744 response = Response.status(Status.OK).entity(result).type(mediaType).build();
746 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
748 } catch (CrudException ce) {
749 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
750 } catch (Exception e) {
751 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
754 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
758 protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action,
759 String authPolicyFunctionName, HttpHeaders headers) throws CrudException {
760 boolean isValid = false;
762 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
763 String authUser = null;
764 if (cipherSuite != null) {
765 X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
766 X509Certificate clientCert = certChain[0];
767 X500Principal subjectDn = clientCert.getSubjectX500Principal();
768 authUser = subjectDn.toString();
770 isValid = this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
771 } catch (Exception e) {
772 logResult(action, uri, e);
776 String sourceOfTruth = null;
777 if (headers.getRequestHeaders().containsKey("X-FromAppId")) {
778 sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
781 if (sourceOfTruth == null || sourceOfTruth.trim() == "") {
782 throw new CrudException("Invalid request, Missing X-FromAppId header", Status.BAD_REQUEST);
788 void logResult(Action op, String uri, Exception e) {
790 logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, e.getStackTrace().toString());
792 // Clear the MDC context so that no other transaction inadvertently
793 // uses our transaction id.