Port async pipeline for gizmo
[aai/gizmo.git] / src / main / java / org / onap / crud / service / CrudAsyncRestService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.service;
25
26 import org.apache.cxf.jaxrs.ext.PATCH;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aaiauth.auth.Auth;
30 import org.onap.crud.exception.CrudException;
31 import org.onap.crud.logging.CrudServiceMsgs;
32 import org.onap.crud.logging.LoggingUtil;
33 import org.onap.crud.util.CrudServiceConstants;
34 import org.slf4j.MDC;
35
36 import java.security.cert.X509Certificate;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import javax.security.auth.x500.X500Principal;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.Consumes;
43 import javax.ws.rs.DELETE;
44 import javax.ws.rs.Encoded;
45 import javax.ws.rs.GET;
46 import javax.ws.rs.POST;
47 import javax.ws.rs.PUT;
48 import javax.ws.rs.Path;
49 import javax.ws.rs.PathParam;
50 import javax.ws.rs.Produces;
51 import javax.ws.rs.core.Context;
52 import javax.ws.rs.core.HttpHeaders;
53 import javax.ws.rs.core.MediaType;
54 import javax.ws.rs.core.Response;
55 import javax.ws.rs.core.Response.Status;
56 import javax.ws.rs.core.UriInfo;
57
58
59 public class CrudAsyncRestService {
60
61   private CrudAsyncGraphDataService crudAsyncGraphDataService;
62   Logger logger = LoggerFactory.getInstance().getLogger(CrudAsyncRestService.class.getName());
63   Logger auditLogger = LoggerFactory.getInstance()
64       .getAuditLogger(CrudAsyncRestService.class.getName());
65   private Auth auth;
66
67   private String mediaType = MediaType.APPLICATION_JSON;
68   public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
69
70   public CrudAsyncRestService(CrudAsyncGraphDataService crudAsyncGraphDataService)
71       throws Exception {
72     this.crudAsyncGraphDataService = crudAsyncGraphDataService;
73     this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
74   }
75
76   public enum Action {
77     POST, GET, PUT, DELETE, PATCH
78   }
79
80   public void startup() {
81
82   }
83
84   @GET
85   @Path("/{version}/{type}/{id}")
86   @Consumes({MediaType.APPLICATION_JSON})
87   @Produces({MediaType.APPLICATION_JSON})
88   public Response getVertex(String content, @PathParam("version") String version,
89                             @PathParam("type") String type,
90                             @PathParam("id") String id, @PathParam("uri") @Encoded String uri,
91                             @Context HttpHeaders headers,
92                             @Context UriInfo uriInfo, @Context HttpServletRequest req) {
93     LoggingUtil.initMdcContext(req, headers);
94
95     logger.debug("Incoming request..." + content);
96     Response response = null;
97
98     if (validateRequest(req, uri, content, Action.GET,
99         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
100
101       try {
102         String result = crudAsyncGraphDataService.getVertex(version, id, type);
103         response = Response.status(Status.OK).entity(result).type(mediaType).build();
104       } catch (CrudException ce) {
105         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
106       } catch (Exception e) {
107         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
108       }
109     } else {
110       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
111           .build();
112     }
113
114     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
115     return response;
116   }
117
118   @GET
119   @Path("/{version}/{type}/")
120   @Consumes({MediaType.APPLICATION_JSON})
121   @Produces({MediaType.APPLICATION_JSON})
122   public Response getVertices(String content, @PathParam("version") String version,
123                               @PathParam("type") String type,
124                               @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
125                               @Context UriInfo uriInfo,
126                               @Context HttpServletRequest req) {
127
128     LoggingUtil.initMdcContext(req, headers);
129
130     logger.debug("Incoming request..." + content);
131     Response response = null;
132     if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
133
134       Map<String, String> filter = new HashMap<String, String>();
135       for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
136         filter.put(e.getKey(), e.getValue().get(0));
137       }
138
139       try {
140         String result = crudAsyncGraphDataService.getVertices(version, type, filter);
141         response = Response.status(Status.OK).entity(result).type(mediaType).build();
142       } catch (CrudException ce) {
143         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
144       } catch (Exception e) {
145         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
146       }
147     } else {
148       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
149           .build();
150     }
151
152     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
153     return response;
154   }
155
156   @GET
157   @Path("/relationships/{version}/{type}/{id}")
158   @Consumes({MediaType.APPLICATION_JSON})
159   @Produces({MediaType.APPLICATION_JSON})
160   public Response getEdge(String content, @PathParam("version") String version,
161                           @PathParam("type") String type,
162                           @PathParam("id") String id, @PathParam("uri") @Encoded String uri,
163                           @Context HttpHeaders headers,
164                           @Context UriInfo uriInfo, @Context HttpServletRequest req) {
165     LoggingUtil.initMdcContext(req, headers);
166
167     logger.debug("Incoming request..." + content);
168     Response response = null;
169
170     if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
171
172       try {
173
174         String result = crudAsyncGraphDataService.getEdge(version, id, type);
175         response = Response.status(Status.OK).entity(result).type(mediaType).build();
176       } catch (CrudException ce) {
177         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
178       } catch (Exception e) {
179         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
180       }
181     } else {
182       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
183           .build();
184     }
185
186     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
187     return response;
188   }
189
190   @GET
191   @Path("/relationships/{version}/{type}/")
192   @Consumes({MediaType.APPLICATION_JSON})
193   @Produces({MediaType.APPLICATION_JSON})
194   public Response getEdges(String content, @PathParam("version") String version,
195                            @PathParam("type") String type, @PathParam("uri") @Encoded String uri,
196                            @Context HttpHeaders headers, @Context UriInfo uriInfo,
197                            @Context HttpServletRequest req) {
198
199     LoggingUtil.initMdcContext(req, headers);
200
201     logger.debug("Incoming request..." + content);
202     Response response = null;
203
204     if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
205
206       Map<String, String> filter = new HashMap<String, String>();
207       for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
208         filter.put(e.getKey(), e.getValue().get(0));
209       }
210
211       try {
212         String result = crudAsyncGraphDataService.getEdges(version, type, filter);
213         response = Response.status(Status.OK).entity(result).type(mediaType).build();
214       } catch (CrudException ce) {
215         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
216       } catch (Exception e) {
217         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
218       }
219     } else {
220       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
221           .build();
222
223     }
224
225     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
226     return response;
227   }
228
229   @PUT
230   @Path("/relationships/{version}/{type}/{id}")
231   @Consumes({MediaType.APPLICATION_JSON})
232   @Produces({MediaType.APPLICATION_JSON})
233   public Response updateEdge(String content, @PathParam("version") String version,
234                              @PathParam("type") String type, @PathParam("id") String id,
235                              @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
236                              @Context UriInfo uriInfo, @Context HttpServletRequest req) {
237
238     LoggingUtil.initMdcContext(req, headers);
239
240     logger.debug("Incoming request..." + content);
241     Response response = null;
242
243     if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
244
245       try {
246         EdgePayload payload = EdgePayload.fromJson(content);
247         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
248           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
249         }
250         if (payload.getId() != null && !payload.getId().equals(id)) {
251           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
252         }
253         String result;
254
255         if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
256             && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE)
257             .equalsIgnoreCase("PATCH")) {
258           result = crudAsyncGraphDataService.patchEdge(version, id, type, payload);
259         } else {
260
261           result = crudAsyncGraphDataService.updateEdge(version, id, type, payload);
262         }
263
264         response = Response.status(Status.OK).entity(result).type(mediaType).build();
265       } catch (CrudException ce) {
266         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
267       } catch (Exception e) {
268         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
269       }
270     } else {
271       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
272           .build();
273
274     }
275
276     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
277     return response;
278   }
279
280   @PATCH
281   @Path("/relationships/{version}/{type}/{id}")
282   @Consumes({"application/merge-patch+json"})
283   @Produces({MediaType.APPLICATION_JSON})
284   public Response patchEdge(String content, @PathParam("version") String version,
285                             @PathParam("type") String type, @PathParam("id") String id,
286                             @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
287                             @Context UriInfo uriInfo, @Context HttpServletRequest req) {
288
289     LoggingUtil.initMdcContext(req, headers);
290
291     logger.debug("Incoming request..." + content);
292     Response response = null;
293     if (validateRequest(req, uri, content, Action.PATCH,
294         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
295
296       try {
297         EdgePayload payload = EdgePayload.fromJson(content);
298         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
299           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
300         }
301         if (payload.getId() != null && !payload.getId().equals(id)) {
302           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
303         }
304
305         String result = crudAsyncGraphDataService.patchEdge(version, id, type, payload);
306         response = Response.status(Status.OK).entity(result).type(mediaType).build();
307       } catch (CrudException ce) {
308         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
309       } catch (Exception e) {
310         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
311       }
312     } else {
313       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
314           .build();
315     }
316
317     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
318     return response;
319   }
320
321   @PUT
322   @Path("/{version}/{type}/{id}")
323   @Consumes({MediaType.APPLICATION_JSON})
324   @Produces({MediaType.APPLICATION_JSON})
325   public Response updateVertex(String content, @PathParam("version") String version,
326                                @PathParam("type") String type, @PathParam("id") String id,
327                                @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
328                                @Context UriInfo uriInfo, @Context HttpServletRequest req) {
329
330     LoggingUtil.initMdcContext(req, headers);
331
332     logger.debug("Incoming request..." + content);
333     Response response = null;
334
335     if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
336
337       try {
338         VertexPayload payload = VertexPayload.fromJson(content);
339         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
340           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
341         }
342         if (payload.getId() != null && !payload.getId().equals(id)) {
343           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
344         }
345         String result;
346         if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
347             && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE)
348             .equalsIgnoreCase("PATCH")) {
349           result = crudAsyncGraphDataService.patchVertex(version, id, type, payload);
350         } else {
351
352           result = crudAsyncGraphDataService.updateVertex(version, id, type, payload);
353         }
354         response = Response.status(Status.OK).entity(result).type(mediaType).build();
355       } catch (CrudException ce) {
356         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
357       } catch (Exception e) {
358         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
359       }
360     } else {
361       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
362           .build();
363     }
364
365     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
366     return response;
367   }
368
369   @PATCH
370   @Path("/{version}/{type}/{id}")
371   @Consumes({"application/merge-patch+json"})
372   @Produces({MediaType.APPLICATION_JSON})
373   public Response patchVertex(String content, @PathParam("version") String version,
374                               @PathParam("type") String type, @PathParam("id") String id,
375                               @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
376                               @Context UriInfo uriInfo, @Context HttpServletRequest req) {
377
378     LoggingUtil.initMdcContext(req, headers);
379
380     logger.debug("Incoming request..." + content);
381     Response response = null;
382
383     if (validateRequest(req, uri, content, Action.PATCH,
384         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
385       try {
386         VertexPayload payload = VertexPayload.fromJson(content);
387         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
388           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
389         }
390         if (payload.getId() != null && !payload.getId().equals(id)) {
391           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
392         }
393
394         String result = crudAsyncGraphDataService.patchVertex(version, id, type, payload);
395         response = Response.status(Status.OK).entity(result).type(mediaType).build();
396       } catch (CrudException ce) {
397         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
398       } catch (Exception e) {
399         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
400       }
401     } else {
402       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
403           .build();
404     }
405
406     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
407     return response;
408   }
409
410   @POST
411   @Path("/{version}/{type}/")
412   @Consumes({MediaType.APPLICATION_JSON})
413   @Produces({MediaType.APPLICATION_JSON})
414   public Response addVertex(String content, @PathParam("version") String version,
415                             @PathParam("type") String type, @PathParam("uri") @Encoded String uri,
416                             @Context HttpHeaders headers, @Context UriInfo uriInfo,
417                             @Context HttpServletRequest req) {
418
419     LoggingUtil.initMdcContext(req, headers);
420
421     logger.debug("Incoming request..." + content);
422     Response response = null;
423
424     if (validateRequest(req, uri, content, Action.POST,
425         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
426
427       try {
428         VertexPayload payload = VertexPayload.fromJson(content);
429         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
430           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
431         }
432         if (payload.getId() != null) {
433           throw new CrudException("ID specified , use Http PUT to update Vertex",
434               Status.BAD_REQUEST);
435         }
436
437         if (payload.getType() != null && !payload.getType().equals(type)) {
438           throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
439         }
440
441         String result = crudAsyncGraphDataService.addVertex(version, type, payload);
442         response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
443       } catch (CrudException ce) {
444         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
445       } catch (Exception e) {
446         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
447       }
448     } else {
449       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
450           .build();
451     }
452
453     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
454     return response;
455   }
456
457   @POST
458   @Path("/{version}/")
459   @Consumes({MediaType.APPLICATION_JSON})
460   @Produces({MediaType.APPLICATION_JSON})
461   public Response addVertex(String content, @PathParam("version") String version,
462                             @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
463                             @Context UriInfo uriInfo,
464                             @Context HttpServletRequest req) {
465
466     LoggingUtil.initMdcContext(req, headers);
467
468     logger.debug("Incoming request..." + content);
469     Response response = null;
470
471     if (validateRequest(req, uri, content, Action.POST,
472         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
473       try {
474
475         VertexPayload payload = VertexPayload.fromJson(content);
476         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
477           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
478         }
479         if (payload.getId() != null) {
480           throw new CrudException("ID specified , use Http PUT to update Vertex",
481               Status.BAD_REQUEST);
482         }
483
484         if (payload.getType() == null || payload.getType().isEmpty()) {
485           throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST);
486         }
487         String result = crudAsyncGraphDataService.addVertex(version, payload.getType(), payload);
488         response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
489       } catch (CrudException ce) {
490         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
491       } catch (Exception e) {
492         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
493       }
494     } else {
495       response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON)
496           .build();
497     }
498
499     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
500     return response;
501   }
502
503   @POST
504   @Path("/relationships/{version}/{type}/")
505   @Consumes({MediaType.APPLICATION_JSON})
506   @Produces({MediaType.APPLICATION_JSON})
507   public Response addEdge(String content, @PathParam("version") String version,
508                           @PathParam("type") String type, @PathParam("uri") @Encoded String uri,
509                           @Context HttpHeaders headers, @Context UriInfo uriInfo,
510                           @Context HttpServletRequest req) {
511
512     LoggingUtil.initMdcContext(req, headers);
513
514     logger.debug("Incoming request..." + content);
515     Response response = null;
516
517     if (validateRequest(req, uri, content, Action.POST,
518         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
519
520
521       try {
522         EdgePayload payload = EdgePayload.fromJson(content);
523         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
524           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
525         }
526         if (payload.getId() != null) {
527           throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
528         }
529
530         if (payload.getType() != null && !payload.getType().equals(type)) {
531           throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
532         }
533         String result = crudAsyncGraphDataService.addEdge(version, type, payload);
534         response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
535       } catch (CrudException ce) {
536         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
537       } catch (Exception e) {
538         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
539       }
540     } else {
541       response = Response.status(Status.FORBIDDEN).entity(content)
542           .type(MediaType.APPLICATION_JSON).build();
543     }
544
545     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
546     return response;
547   }
548
549   @POST
550   @Path("/relationships/{version}/")
551   @Consumes({MediaType.APPLICATION_JSON})
552   @Produces({MediaType.APPLICATION_JSON})
553   public Response addEdge(String content, @PathParam("version") String version,
554                           @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
555                           @Context UriInfo uriInfo, @Context HttpServletRequest req) {
556
557     LoggingUtil.initMdcContext(req, headers);
558
559     logger.debug("Incoming request..." + content);
560     Response response = null;
561
562     if (validateRequest(req, uri, content, Action.POST,
563         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
564
565
566       try {
567         EdgePayload payload = EdgePayload.fromJson(content);
568         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
569           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
570         }
571         if (payload.getId() != null) {
572           throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
573         }
574
575         if (payload.getType() == null || payload.getType().isEmpty()) {
576           throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
577         }
578         String result = crudAsyncGraphDataService.addEdge(version, payload.getType(), payload);
579
580         response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
581       } catch (CrudException ce) {
582         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
583       } catch (Exception e) {
584         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
585       }
586     } else {
587       response = Response.status(Status.FORBIDDEN).entity(content)
588           .type(MediaType.APPLICATION_JSON).build();
589     }
590
591     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
592     return response;
593   }
594
595   @DELETE
596   @Path("/{version}/{type}/{id}")
597   @Consumes({MediaType.APPLICATION_JSON})
598   @Produces({MediaType.APPLICATION_JSON})
599   public Response deleteVertex(String content, @PathParam("version") String version,
600                                @PathParam("type") String type, @PathParam("id") String id,
601                                @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
602                                @Context UriInfo uriInfo, @Context HttpServletRequest req) {
603
604     LoggingUtil.initMdcContext(req, headers);
605
606     logger.debug("Incoming request..." + content);
607     Response response = null;
608
609     if (validateRequest(req, uri, content, Action.DELETE,
610         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
611
612
613       try {
614         String result = crudAsyncGraphDataService.deleteVertex(version, id, type);
615         response = Response.status(Status.OK).entity(result).type(mediaType).build();
616       } catch (CrudException ce) {
617         response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
618       } catch (Exception e) {
619         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
620       }
621     } else {
622       response = Response.status(Status.FORBIDDEN).entity(content)
623           .type(MediaType.APPLICATION_JSON).build();
624     }
625
626     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
627     return response;
628   }
629
630   @DELETE
631   @Path("/relationships/{version}/{type}/{id}")
632   @Consumes({MediaType.APPLICATION_JSON})
633   @Produces({MediaType.APPLICATION_JSON})
634   public Response deleteEdge(String content, @PathParam("version") String version,
635                              @PathParam("type") String type, @PathParam("id") String id,
636                              @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
637                              @Context UriInfo uriInfo, @Context HttpServletRequest req) {
638
639     LoggingUtil.initMdcContext(req, headers);
640
641     logger.debug("Incoming request..." + content);
642     Response response = null;
643     if (validateRequest(req, uri, content, Action.DELETE,
644         CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
645
646
647       try {
648         String result = crudAsyncGraphDataService.deleteEdge(version, id, type);
649         response = Response.status(Status.OK).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();
654       }
655     } else {
656       response = Response.status(Status.FORBIDDEN).entity(content)
657           .type(MediaType.APPLICATION_JSON).build();
658     }
659
660     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
661     return response;
662   }
663
664   protected boolean validateRequest(HttpServletRequest req, String uri, String content,
665                                     Action action, String authPolicyFunctionName) {
666     try {
667       String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
668       String authUser = null;
669       if (cipherSuite != null) {
670         X509Certificate[] certChain = (X509Certificate[]) req
671             .getAttribute("javax.servlet.request.X509Certificate");
672         X509Certificate clientCert = certChain[0];
673         X500Principal subjectDn = clientCert.getSubjectX500Principal();
674         authUser = subjectDn.toString();
675       }
676       return this.auth.validateRequest(authUser.toLowerCase(), action.toString()
677           + ":" + authPolicyFunctionName);
678     } catch (Exception e) {
679       logResult(action, uri, e);
680       return false;
681     }
682   }
683
684   void logResult(Action op, String uri, Exception e) {
685
686     logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(),
687         uri, e.getStackTrace().toString());
688
689     // Clear the MDC context so that no other transaction inadvertently
690     // uses our transaction id.
691     MDC.clear();
692   }
693 }