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.openecomp.crud.service;
26 import java.security.cert.X509Certificate;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
33 import javax.security.auth.x500.X500Principal;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.DELETE;
37 import javax.ws.rs.Encoded;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.POST;
40 import javax.ws.rs.PUT;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.Context;
45 import javax.ws.rs.core.HttpHeaders;
46 import javax.ws.rs.core.MediaType;
47 import javax.ws.rs.core.Response;
48 import javax.ws.rs.core.Response.Status;
49 import javax.ws.rs.core.UriInfo;
51 import org.apache.cxf.jaxrs.ext.PATCH;
52 import org.openecomp.auth.Auth;
53 import org.openecomp.cl.api.Logger;
54 import org.openecomp.cl.eelf.LoggerFactory;
55 import org.openecomp.crud.exception.CrudException;
56 import org.openecomp.crud.logging.CrudServiceMsgs;
57 import org.openecomp.crud.logging.LoggingUtil;
58 import org.openecomp.crud.util.CrudServiceConstants;
61 import com.google.gson.JsonElement;
63 public class CrudRestService {
65 private CrudGraphDataService crudGraphDataService;
66 Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName());
67 Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName());
70 private String mediaType = MediaType.APPLICATION_JSON;
71 public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
73 public CrudRestService(CrudGraphDataService crudGraphDataService) throws Exception {
74 this.crudGraphDataService = crudGraphDataService;
75 this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
79 POST, GET, PUT, DELETE, PATCH
84 public void startup() {
89 @Path("/{version}/{type}/{id}")
90 @Consumes({MediaType.APPLICATION_JSON})
91 @Produces({MediaType.APPLICATION_JSON})
92 public Response getVertex(String content, @PathParam("version") String version,
93 @PathParam("type") String type, @PathParam("id") String id,
94 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
95 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
96 LoggingUtil.initMdcContext(req, headers);
98 logger.debug("Incoming request..." + content);
99 Response response = null;
101 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
104 String result = crudGraphDataService.getVertex(version, id, type);
105 response = Response.status(Status.OK).entity(result).type(mediaType).build();
106 } catch (CrudException ce) {
107 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
108 } catch (Exception e) {
109 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
112 response = Response.status(Status.FORBIDDEN).entity(content)
113 .type(MediaType.APPLICATION_JSON).build();
116 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
121 @Path("/{version}/{type}/")
122 @Consumes({MediaType.APPLICATION_JSON})
123 @Produces({MediaType.APPLICATION_JSON})
124 public Response getVertices(String content, @PathParam("version") String version,
125 @PathParam("type") String type, @PathParam("uri") @Encoded String uri,
126 @Context HttpHeaders headers, @Context UriInfo uriInfo,
127 @Context HttpServletRequest req) {
129 LoggingUtil.initMdcContext(req, headers);
131 logger.debug("Incoming request..." + content);
132 Response response = null;
133 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
135 Map<String, String> filter = new HashMap<String, String>();
136 for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
137 filter.put(e.getKey(), e.getValue().get(0));
141 String result = crudGraphDataService.getVertices(version, type, filter);
142 response = Response.status(Status.OK).entity(result).type(mediaType).build();
143 } catch (CrudException ce) {
144 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
145 } catch (Exception e) {
146 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
149 response = Response.status(Status.FORBIDDEN).entity(content)
150 .type(MediaType.APPLICATION_JSON).build();
153 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
158 @Path("/relationships/{version}/{type}/{id}")
159 @Consumes({MediaType.APPLICATION_JSON})
160 @Produces({MediaType.APPLICATION_JSON})
161 public Response getEdge(String content, @PathParam("version") String version,
162 @PathParam("type") String type, @PathParam("id") String id,
163 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
164 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
165 LoggingUtil.initMdcContext(req, headers);
167 logger.debug("Incoming request..." + content);
168 Response response = null;
170 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
174 String result = crudGraphDataService.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();
182 response = Response.status(Status.FORBIDDEN).entity(content)
183 .type(MediaType.APPLICATION_JSON).build();
186 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
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) {
199 LoggingUtil.initMdcContext(req, headers);
201 logger.debug("Incoming request..." + content);
202 Response response = null;
204 if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
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));
212 String result = crudGraphDataService.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();
220 response = Response.status(Status.FORBIDDEN).entity(content)
221 .type(MediaType.APPLICATION_JSON).build();
225 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
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) {
238 LoggingUtil.initMdcContext(req, headers);
240 logger.debug("Incoming request..." + content);
241 Response response = null;
243 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
246 EdgePayload payload = EdgePayload.fromJson(content);
247 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
248 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
250 if (payload.getId() != null && !payload.getId().equals(id)) {
251 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
255 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
256 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE)
257 .equalsIgnoreCase("PATCH")) {
258 result = crudGraphDataService.patchEdge(version, id, type, payload);
261 result = crudGraphDataService.updateEdge(version, id, type, payload);
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();
271 response = Response.status(Status.FORBIDDEN).entity(content)
272 .type(MediaType.APPLICATION_JSON).build();
276 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
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) {
289 LoggingUtil.initMdcContext(req, headers);
291 logger.debug("Incoming request..." + content);
292 Response response = null;
293 if (validateRequest(req, uri, content, Action.PATCH,
294 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
297 EdgePayload payload = EdgePayload.fromJson(content);
298 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
299 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
301 if (payload.getId() != null && !payload.getId().equals(id)) {
302 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
305 String result = crudGraphDataService.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();
313 response = Response.status(Status.FORBIDDEN).entity(content)
314 .type(MediaType.APPLICATION_JSON).build();
317 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
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) {
330 LoggingUtil.initMdcContext(req, headers);
332 logger.debug("Incoming request..." + content);
333 Response response = null;
335 if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
338 VertexPayload payload = VertexPayload.fromJson(content);
339 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
340 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
342 if (payload.getId() != null && !payload.getId().equals(id)) {
343 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
346 if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
347 && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE)
348 .equalsIgnoreCase("PATCH")) {
349 result = crudGraphDataService.patchVertex(version, id, type, payload);
352 result = crudGraphDataService.updateVertex(version, id, type, payload);
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();
361 response = Response.status(Status.FORBIDDEN).entity(content)
362 .type(MediaType.APPLICATION_JSON).build();
365 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
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) {
378 LoggingUtil.initMdcContext(req, headers);
380 logger.debug("Incoming request..." + content);
381 Response response = null;
383 if (validateRequest(req, uri, content, Action.PATCH,
384 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
386 VertexPayload payload = VertexPayload.fromJson(content);
387 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
388 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
390 if (payload.getId() != null && !payload.getId().equals(id)) {
391 throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
394 String result = crudGraphDataService.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();
402 response = Response.status(Status.FORBIDDEN).entity(content)
403 .type(MediaType.APPLICATION_JSON).build();
406 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
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) {
419 LoggingUtil.initMdcContext(req, headers);
421 logger.debug("Incoming request..." + content);
422 Response response = null;
424 if (validateRequest(req, uri, content, Action.POST,
425 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
428 VertexPayload payload = VertexPayload.fromJson(content);
429 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
430 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
432 if (payload.getId() != null) {
433 throw new CrudException("ID specified , use Http PUT to update Vertex",
437 if (payload.getType() != null && !payload.getType().equals(type)) {
438 throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
441 String result = crudGraphDataService.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();
449 response = Response.status(Status.FORBIDDEN).entity(content)
450 .type(MediaType.APPLICATION_JSON).build();
453 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
457 private void validateBulkPayload(BulkPayload payload) throws CrudException {
458 List<String> vertices = new ArrayList<String>();
459 List<String> edges = new ArrayList<String>();
461 for (JsonElement v : payload.getObjects()) {
462 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(v.getAsJsonObject().entrySet());
464 if (entries.size() != 2) {
465 throw new CrudException("", Status.BAD_REQUEST);
467 Map.Entry<String, JsonElement> opr = entries.get(0);
468 Map.Entry<String, JsonElement> item = entries.get(1);
470 if (vertices.contains(item.getKey())) {
471 throw new CrudException("duplicate vertex in payload: " + item.getKey(), Status.BAD_REQUEST);
473 VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
474 if (vertexPayload.getType() == null) {
475 throw new CrudException("Vertex Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
478 if (!opr.getKey().equalsIgnoreCase("operation")) {
479 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
482 if (!opr.getValue().getAsString().equalsIgnoreCase("add") && !opr.getValue().getAsString().equalsIgnoreCase("modify")
483 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
484 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
486 // check if ID is populate for modify/delete operation
487 if ((opr.getValue().getAsString().equalsIgnoreCase("modify") || opr.getValue().getAsString().equalsIgnoreCase("delete"))
488 && (vertexPayload.getId() == null)) {
490 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
494 vertices.add(item.getKey());
497 for (JsonElement v : payload.getRelationships()) {
498 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(v.getAsJsonObject().entrySet());
500 if (entries.size() != 2) {
501 throw new CrudException("", Status.BAD_REQUEST);
503 Map.Entry<String, JsonElement> opr = entries.get(0);
504 Map.Entry<String, JsonElement> item = entries.get(1);
506 if (edges.contains(item.getKey())) {
507 throw new CrudException("duplicate Edge in payload: " + item.getKey(), Status.BAD_REQUEST);
510 EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
512 if (edgePayload.getType() == null) {
513 throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
516 if (!opr.getKey().equalsIgnoreCase("operation")) {
517 throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST);
520 if (!opr.getValue().getAsString().equalsIgnoreCase("add") && !opr.getValue().getAsString().equalsIgnoreCase("modify")
521 && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
522 throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
524 // check if ID is populate for modify/delete operation
525 if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify")
526 || opr.getValue().getAsString().equalsIgnoreCase("delete"))) {
528 throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
531 if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
532 if(edgePayload.getSource()==null || edgePayload.getTarget()==null){
533 throw new CrudException("Source/Target cannot be null for edge: " + item.getKey(),
536 if (edgePayload.getSource().startsWith("$") && !vertices.contains(edgePayload.getSource().substring(1))) {
537 throw new CrudException("Source Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(),
541 if (edgePayload.getTarget().startsWith("$") && !vertices.contains(edgePayload.getTarget().substring(1))) {
542 throw new CrudException("Target Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(),
546 edges.add(item.getKey());
553 @Path("/{version}/bulk/")
554 @Consumes({MediaType.APPLICATION_JSON})
555 @Produces({MediaType.APPLICATION_JSON})
556 public Response addBulk(String content, @PathParam("version") String version,
557 @PathParam("type") String type, @PathParam("uri") @Encoded String uri,
558 @Context HttpHeaders headers, @Context UriInfo uriInfo,
559 @Context HttpServletRequest req) {
561 LoggingUtil.initMdcContext(req, headers);
563 logger.debug("Incoming request..." + content);
564 Response response = null;
566 if (validateRequest(req, uri, content, Action.POST,
567 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
570 BulkPayload payload = BulkPayload.fromJson(content);
571 if ((payload.getObjects() == null && payload.getRelationships() == null) || (payload.getObjects() != null && payload.getObjects().isEmpty()
572 && payload.getRelationships() != null && payload.getRelationships().isEmpty())) {
573 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
576 validateBulkPayload(payload);
577 String result = crudGraphDataService.addBulk(version, payload);
578 response = Response.status(Status.OK).entity(result).type(mediaType).build();
579 } catch (CrudException ce) {
580 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
581 } catch (Exception e) {
582 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
585 response = Response.status(Status.FORBIDDEN).entity(content)
586 .type(MediaType.APPLICATION_JSON).build();
589 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
595 @Consumes({MediaType.APPLICATION_JSON})
596 @Produces({MediaType.APPLICATION_JSON})
597 public Response addVertex(String content, @PathParam("version") String version,
598 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
599 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
601 LoggingUtil.initMdcContext(req, headers);
603 logger.debug("Incoming request..." + content);
604 Response response = null;
606 if (validateRequest(req, uri, content, Action.POST,
607 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
610 VertexPayload payload = VertexPayload.fromJson(content);
611 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
612 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
614 if (payload.getId() != null) {
615 throw new CrudException("ID specified , use Http PUT to update Vertex",
619 if (payload.getType() == null || payload.getType().isEmpty()) {
620 throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST);
622 String result = crudGraphDataService.addVertex(version, payload.getType(), payload);
623 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
624 } catch (CrudException ce) {
625 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
626 } catch (Exception e) {
627 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
630 response = Response.status(Status.FORBIDDEN).entity(content)
631 .type(MediaType.APPLICATION_JSON).build();
634 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
639 @Path("/relationships/{version}/{type}/")
640 @Consumes({MediaType.APPLICATION_JSON})
641 @Produces({MediaType.APPLICATION_JSON})
642 public Response addEdge(String content, @PathParam("version") String version,
643 @PathParam("type") String type, @PathParam("uri") @Encoded String uri,
644 @Context HttpHeaders headers, @Context UriInfo uriInfo,
645 @Context HttpServletRequest req) {
647 LoggingUtil.initMdcContext(req, headers);
649 logger.debug("Incoming request..." + content);
650 Response response = null;
652 if (validateRequest(req, uri, content, Action.POST,
653 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
657 EdgePayload payload = EdgePayload.fromJson(content);
658 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
659 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
661 if (payload.getId() != null) {
662 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
665 if (payload.getType() != null && !payload.getType().equals(type)) {
666 throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
668 String result = crudGraphDataService.addEdge(version, type, payload);
669 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
670 } catch (CrudException ce) {
671 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
672 } catch (Exception e) {
673 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
676 response = Response.status(Status.FORBIDDEN).entity(content)
677 .type(MediaType.APPLICATION_JSON).build();
680 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
685 @Path("/relationships/{version}/")
686 @Consumes({MediaType.APPLICATION_JSON})
687 @Produces({MediaType.APPLICATION_JSON})
688 public Response addEdge(String content, @PathParam("version") String version,
689 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
690 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
692 LoggingUtil.initMdcContext(req, headers);
694 logger.debug("Incoming request..." + content);
695 Response response = null;
697 if (validateRequest(req, uri, content, Action.POST,
698 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
702 EdgePayload payload = EdgePayload.fromJson(content);
703 if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
704 throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
706 if (payload.getId() != null) {
707 throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST);
710 if (payload.getType() == null || payload.getType().isEmpty()) {
711 throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
713 String result = crudGraphDataService.addEdge(version, payload.getType(), payload);
715 response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
716 } catch (CrudException ce) {
717 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
718 } catch (Exception e) {
719 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
722 response = Response.status(Status.FORBIDDEN).entity(content)
723 .type(MediaType.APPLICATION_JSON).build();
726 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
731 @Path("/{version}/{type}/{id}")
732 @Consumes({MediaType.APPLICATION_JSON})
733 @Produces({MediaType.APPLICATION_JSON})
734 public Response deleteVertex(String content, @PathParam("version") String version,
735 @PathParam("type") String type, @PathParam("id") String id,
736 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
737 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
739 LoggingUtil.initMdcContext(req, headers);
741 logger.debug("Incoming request..." + content);
742 Response response = null;
744 if (validateRequest(req, uri, content, Action.DELETE,
745 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
749 String result = crudGraphDataService.deleteVertex(version, id, type);
750 response = Response.status(Status.OK).entity(result).type(mediaType).build();
751 } catch (CrudException ce) {
752 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
753 } catch (Exception e) {
754 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
757 response = Response.status(Status.FORBIDDEN).entity(content)
758 .type(MediaType.APPLICATION_JSON).build();
761 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
766 @Path("/relationships/{version}/{type}/{id}")
767 @Consumes({MediaType.APPLICATION_JSON})
768 @Produces({MediaType.APPLICATION_JSON})
769 public Response deleteEdge(String content, @PathParam("version") String version,
770 @PathParam("type") String type, @PathParam("id") String id,
771 @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
772 @Context UriInfo uriInfo, @Context HttpServletRequest req) {
774 LoggingUtil.initMdcContext(req, headers);
776 logger.debug("Incoming request..." + content);
777 Response response = null;
778 if (validateRequest(req, uri, content, Action.DELETE,
779 CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
782 String result = crudGraphDataService.deleteEdge(version, id, type);
783 response = Response.status(Status.OK).entity(result).type(mediaType).build();
784 } catch (CrudException ce) {
785 response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
786 } catch (Exception e) {
787 response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
790 response = Response.status(Status.FORBIDDEN).entity(content)
791 .type(MediaType.APPLICATION_JSON).build();
794 LoggingUtil.logRestRequest(logger, auditLogger, req, response);
798 protected boolean validateRequest(HttpServletRequest req, String uri, String content,
799 Action action, String authPolicyFunctionName) {
801 String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
802 String authUser = null;
803 if (cipherSuite != null) {
804 X509Certificate[] certChain = (X509Certificate[]) req
805 .getAttribute("javax.servlet.request.X509Certificate");
806 X509Certificate clientCert = certChain[0];
807 X500Principal subjectDn = clientCert.getSubjectX500Principal();
808 authUser = subjectDn.toString();
810 return this.auth.validateRequest(authUser.toLowerCase(), action.toString()
811 + ":" + authPolicyFunctionName);
812 } catch (Exception e) {
813 logResult(action, uri, e);
818 void logResult(Action op, String uri, Exception e) {
820 logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri,
821 e.getStackTrace().toString());
823 // Clear the MDC context so that no other transaction inadvertently
824 // uses our transaction id.