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;
60 import com.google.gson.JsonElement;
62 public class CrudRestService {
64 private AbstractGraphDataService graphDataService;
65 Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName());
66 Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName());
69 private String mediaType = MediaType.APPLICATION_JSON;
70 public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
72 public CrudRestService(AbstractGraphDataService graphDataService) throws Exception {
73 this.graphDataService = graphDataService;
74 this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
78 POST, GET, PUT, DELETE, PATCH
81 public void startup() {
86 @Path("/{version}/{type}/{id}")
87 @Consumes({ MediaType.APPLICATION_JSON })
88 @Produces({ MediaType.APPLICATION_JSON })
89 public Response getVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
90 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
91 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
92 LoggingUtil.initMdcContext(req, headers);
94 logger.debug("Incoming request..." + content);
95 Response response = null;
97 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
100 String result = graphDataService.getVertex(version, id, type);
101 response = Response.status(Status.OK).entity(result).type(mediaType).build();
102 } catch (CrudException ce) {
103 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
104 } catch (Exception e) {
105 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
108 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
111 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
116 @Path("/{version}/{type}/")
117 @Consumes({ MediaType.APPLICATION_JSON })
118 @Produces({ MediaType.APPLICATION_JSON })
119 public Response getVertices(String content, @PathParam("version") String version, @PathParam("type") String type,
120 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
121 @Context HttpServletRequest req) {
123 LoggingUtil.initMdcContext(req, headers);
125 logger.debug("Incoming request..." + content);
126 Response response = null;
127 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
129 Map<String, String> filter = new HashMap<String, String>();
130 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
131 filter.put(e.getKey(), e.getValue().get(0));
135 String result = graphDataService.getVertices(version, type, filter);
136 response = Response.status(Status.OK).entity(result).type(mediaType).build();
137 } catch (CrudException ce) {
138 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
139 } catch (Exception e) {
140 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
143 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
146 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
151 @Path("/relationships/{version}/{type}/{id}")
152 @Consumes({ MediaType.APPLICATION_JSON })
153 @Produces({ MediaType.APPLICATION_JSON })
154 public Response getEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
155 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
156 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
157 LoggingUtil.initMdcContext(req, headers);
159 logger.debug("Incoming request..." + content);
160 Response response = null;
162 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
166 String result = graphDataService.getEdge(version, id, type);
167 response = Response.status(Status.OK).entity(result).type(mediaType).build();
168 } catch (CrudException ce) {
169 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
170 } catch (Exception e) {
171 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
174 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
177 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
182 @Path("/relationships/{version}/{type}/")
183 @Consumes({ MediaType.APPLICATION_JSON })
184 @Produces({ MediaType.APPLICATION_JSON })
185 public Response getEdges(String content, @PathParam("version") String version, @PathParam("type") String type,
186 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
187 @Context HttpServletRequest req) {
189 LoggingUtil.initMdcContext(req, headers);
191 logger.debug("Incoming request..." + content);
192 Response response = null;
194 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
196 Map<String, String> filter = new HashMap<String, String>();
197 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
198 filter.put(e.getKey(), e.getValue().get(0));
202 String result = graphDataService.getEdges(version, type, filter);
203 response = Response.status(Status.OK).entity(result).type(mediaType).build();
204 } catch (CrudException ce) {
205 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
206 } catch (Exception e) {
207 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
210 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
214 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
219 @Path("/relationships/{version}/{type}/{id}")
220 @Consumes({ MediaType.APPLICATION_JSON })
221 @Produces({ MediaType.APPLICATION_JSON })
222 public Response updateEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
223 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
224 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
226 LoggingUtil.initMdcContext(req, headers);
228 logger.debug("Incoming request..." + content);
229 Response response = null;
231 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
234 EdgePayload payload = EdgePayload.fromJson(content);
235 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
236 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
238 if (payload.getId() != null && !payload.getId().equals(id)) {
239 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
243 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
244 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
245 result = graphDataService.patchEdge(version, id, type, payload);
248 result = graphDataService.updateEdge(version, id, type, payload);
251 response = Response.status(Status.OK).entity(result).type(mediaType).build();
252 } catch (CrudException ce) {
253 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
254 } catch (Exception e) {
255 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
258 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
262 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
267 @Path("/relationships/{version}/{type}/{id}")
268 @Consumes({ "application/merge-patch+json" })
269 @Produces({ MediaType.APPLICATION_JSON })
270 public Response patchEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
271 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
272 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
274 LoggingUtil.initMdcContext(req, headers);
276 logger.debug("Incoming request..." + content);
277 Response response = null;
278 if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
281 EdgePayload payload = EdgePayload.fromJson(content);
282 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
283 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
285 if (payload.getId() != null && !payload.getId().equals(id)) {
286 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
289 String result = graphDataService.patchEdge(version, id, type, payload);
290 response = Response.status(Status.OK).entity(result).type(mediaType).build();
291 } catch (CrudException ce) {
292 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
293 } catch (Exception e) {
294 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
297 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
300 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
305 @Path("/{version}/{type}/{id}")
306 @Consumes({ MediaType.APPLICATION_JSON })
307 @Produces({ MediaType.APPLICATION_JSON })
308 public Response updateVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
309 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
310 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
312 LoggingUtil.initMdcContext(req, headers);
314 logger.debug("Incoming request..." + content);
315 Response response = null;
317 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
320 VertexPayload payload = VertexPayload.fromJson(content);
321 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
322 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
324 if (payload.getId() != null && !payload.getId().equals(id)) {
325 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
328 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
329 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
330 result = graphDataService.patchVertex(version, id, type, payload);
333 result = graphDataService.updateVertex(version, id, type, payload);
335 response = Response.status(Status.OK).entity(result).type(mediaType).build();
336 } catch (CrudException ce) {
337 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
338 } catch (Exception e) {
339 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
342 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
345 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
350 @Path("/{version}/{type}/{id}")
351 @Consumes({ "application/merge-patch+json" })
352 @Produces({ MediaType.APPLICATION_JSON })
353 public Response patchVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
354 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
355 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
357 LoggingUtil.initMdcContext(req, headers);
359 logger.debug("Incoming request..." + content);
360 Response response = null;
362 if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
364 VertexPayload payload = VertexPayload.fromJson(content);
365 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
366 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
368 if (payload.getId() != null && !payload.getId().equals(id)) {
369 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
372 String result = graphDataService.patchVertex(version, id, type, payload);
373 response = Response.status(Status.OK).entity(result).type(mediaType).build();
374 } catch (CrudException ce) {
375 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
376 } catch (Exception e) {
377 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
380 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
383 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
388 @Path("/{version}/{type}/")
389 @Consumes({ MediaType.APPLICATION_JSON })
390 @Produces({ MediaType.APPLICATION_JSON })
391 public Response addVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
392 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
393 @Context HttpServletRequest req) {
395 LoggingUtil.initMdcContext(req, headers);
397 logger.debug("Incoming request..." + content);
398 Response response = null;
400 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
403 VertexPayload payload = VertexPayload.fromJson(content);
404 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
405 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
407 if (payload.getId() != null) {
408 throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST);
411 if (payload.getType() != null && !payload.getType().equals(type)) {
412 throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
415 String result = graphDataService.addVertex(version, type, payload);
416 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
417 } catch (CrudException ce) {
418 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
419 } catch (Exception e) {
420 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
423 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
426 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
430 private void validateBulkPayload(BulkPayload payload) throws CrudException {
431 List<String> vertices = new ArrayList<String>();
432 List<String> edges = new ArrayList<String>();
434 for (JsonElement v : payload.getObjects()) {
435 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
436 v.getAsJsonObject().entrySet());
438 if (entries.size() != 2) {
439 throw new CrudException("", Status.BAD_REQUEST);
441 Map.Entry<String, JsonElement> opr = entries.get(0);
442 Map.Entry<String, JsonElement> item = entries.get(1);
444 if (vertices.contains(item.getKey())) {
445 throw new CrudException("duplicate vertex in payload: " + item.getKey(), Status.BAD_REQUEST);
447 VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
448 if (vertexPayload.getType() == null) {
449 throw new CrudException("Vertex Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
452 if (!opr.getKey().equalsIgnoreCase("operation")) {
453 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
456 if (!opr.getValue().getAsString().equalsIgnoreCase("add")
457 && !opr.getValue().getAsString().equalsIgnoreCase("modify")
458 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
459 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
461 // check if ID is populate for modify/delete operation
462 if ((opr.getValue().getAsString().equalsIgnoreCase("modify")
463 || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) {
465 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
469 vertices.add(item.getKey());
472 for (JsonElement v : payload.getRelationships()) {
473 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
474 v.getAsJsonObject().entrySet());
476 if (entries.size() != 2) {
477 throw new CrudException("", Status.BAD_REQUEST);
479 Map.Entry<String, JsonElement> opr = entries.get(0);
480 Map.Entry<String, JsonElement> item = entries.get(1);
482 if (edges.contains(item.getKey())) {
483 throw new CrudException("duplicate Edge in payload: " + item.getKey(), Status.BAD_REQUEST);
486 EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
488 if (edgePayload.getType() == null) {
489 throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
492 if (!opr.getKey().equalsIgnoreCase("operation")) {
493 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
496 if (!opr.getValue().getAsString().equalsIgnoreCase("add")
497 && !opr.getValue().getAsString().equalsIgnoreCase("modify")
498 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
499 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
501 // check if ID is populate for modify/delete operation
502 if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify")
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;
543 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
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);
555 response = Response.status(Status.OK).entity(result).type(mediaType).build();
556 } catch (CrudException ce) {
557 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
558 } catch (Exception e) {
559 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
562 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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;
581 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
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);
595 String result = graphDataService.addVertex(version, payload.getType(), payload);
596 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
597 } catch (CrudException ce) {
598 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
599 } catch (Exception e) {
600 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
603 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
606 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
611 @Path("/relationships/{version}/{type}/")
612 @Consumes({ MediaType.APPLICATION_JSON })
613 @Produces({ MediaType.APPLICATION_JSON })
614 public Response addEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
615 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
616 @Context HttpServletRequest req) {
618 LoggingUtil.initMdcContext(req, headers);
620 logger.debug("Incoming request..." + content);
621 Response response = null;
623 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
626 EdgePayload payload = EdgePayload.fromJson(content);
627 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
628 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
630 if (payload.getId() != null) {
631 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
634 if (payload.getType() != null && !payload.getType().equals(type)) {
635 throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
637 String result = graphDataService.addEdge(version, type, payload);
638 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
639 } catch (CrudException ce) {
640 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
641 } catch (Exception e) {
642 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
645 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
648 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
653 @Path("/relationships/{version}/")
654 @Consumes({ MediaType.APPLICATION_JSON })
655 @Produces({ MediaType.APPLICATION_JSON })
656 public Response addEdge(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
657 @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
659 LoggingUtil.initMdcContext(req, headers);
661 logger.debug("Incoming request..." + content);
662 Response response = null;
664 if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
667 EdgePayload payload = EdgePayload.fromJson(content);
668 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
669 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
671 if (payload.getId() != null) {
672 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
675 if (payload.getType() == null || payload.getType().isEmpty()) {
676 throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
678 String result = graphDataService.addEdge(version, payload.getType(), payload);
680 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
681 } catch (CrudException ce) {
682 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
683 } catch (Exception e) {
684 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
687 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
690 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
695 @Path("/{version}/{type}/{id}")
696 @Consumes({ MediaType.APPLICATION_JSON })
697 @Produces({ MediaType.APPLICATION_JSON })
698 public Response deleteVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
699 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
700 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
702 LoggingUtil.initMdcContext(req, headers);
704 logger.debug("Incoming request..." + content);
705 Response response = null;
707 if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
710 String result = graphDataService.deleteVertex(version, id, type);
711 response = Response.status(Status.OK).entity(result).type(mediaType).build();
712 } catch (CrudException ce) {
713 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
714 } catch (Exception e) {
715 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
718 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
721 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
726 @Path("/relationships/{version}/{type}/{id}")
727 @Consumes({ MediaType.APPLICATION_JSON })
728 @Produces({ MediaType.APPLICATION_JSON })
729 public Response deleteEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
730 @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
731 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
733 LoggingUtil.initMdcContext(req, headers);
735 logger.debug("Incoming request..." + content);
736 Response response = null;
737 if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
740 String result = graphDataService.deleteEdge(version, id, type);
741 response = Response.status(Status.OK).entity(result).type(mediaType).build();
742 } catch (CrudException ce) {
743 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
744 } catch (Exception e) {
745 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
748 response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build();
751 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
755 protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action,
756 String authPolicyFunctionName) {
758 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
759 String authUser = null;
760 if (cipherSuite != null) {
761 X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
762 X509Certificate clientCert = certChain[0];
763 X500Principal subjectDn = clientCert.getSubjectX500Principal();
764 authUser = subjectDn.toString();
766 return this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
767 } catch (Exception e) {
768 logResult(action, uri, e);
773 void logResult(Action op, String uri, Exception e) {
775 logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, e.getStackTrace().toString());
777 // Clear the MDC context so that no other transaction inadvertently
778 // uses our transaction id.