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