diff options
author | 2018-10-12 08:52:39 +0000 | |
---|---|---|
committer | 2018-10-12 08:52:39 +0000 | |
commit | 70f45f64cd252663b33793ab073a9d6764a16a20 (patch) | |
tree | 018956cfab76cfba9b8214137854d365b6a6ff2a /src/main/java | |
parent | 2626b3f0c9cac0e8f6e21995a1098c2241b99d5b (diff) | |
parent | fbcc34c30ba2eacaaced5629c730a3bd4b9515c6 (diff) |
Merge "Return X-TransactionId header in REST response"1.3.1
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/onap/crud/service/CrudRestService.java | 1449 | ||||
-rw-r--r-- | src/main/java/org/onap/crud/service/JaxrsEchoService.java | 47 |
2 files changed, 787 insertions, 709 deletions
diff --git a/src/main/java/org/onap/crud/service/CrudRestService.java b/src/main/java/org/onap/crud/service/CrudRestService.java index 3cfc657..7bf4ca4 100644 --- a/src/main/java/org/onap/crud/service/CrudRestService.java +++ b/src/main/java/org/onap/crud/service/CrudRestService.java @@ -20,6 +20,7 @@ */ package org.onap.crud.service; +import com.google.gson.JsonElement; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Arrays; @@ -43,6 +44,7 @@ import javax.ws.rs.core.EntityTag; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -60,788 +62,857 @@ import org.onap.crud.util.CrudProperties; import org.onap.crud.util.CrudServiceConstants; import org.onap.crud.util.CrudServiceUtil; import org.slf4j.MDC; -import com.google.gson.JsonElement; @Path("/services/inventory") public class CrudRestService { - private AbstractGraphDataService graphDataService; - Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName()); - Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName()); - private Auth auth; - - private String mediaType = MediaType.APPLICATION_JSON; - public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override"; - - public CrudRestService(AbstractGraphDataService graphDataService) throws Exception { - this.graphDataService = graphDataService; - this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE); - } - - // For unit testing - public CrudRestService(AbstractGraphDataService graphDataService, Auth auth) throws Exception { - this.graphDataService = graphDataService; - this.auth = auth; - } - - public enum Action { - POST, GET, PUT, DELETE, PATCH - } - - public void startup() { - - } - - @GET - @Path("/{version}/{type}/{id}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response getVertex(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { - LoggingUtil.initMdcContext(req, headers); - - logger.debug("Incoming request..." + content); - Response response = null; - - Map<String, String> params = addParams ( uriInfo, false, type, version ); - - try { - if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - ImmutablePair<EntityTag, String> result = graphDataService.getVertex(version, id, type, params); - response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - - } - - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @GET - @Path("/{version}/{type}/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response getVertices(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, - @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); - - logger.debug("Incoming request..." + content); - Response response = null; - try { - if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - String propertiesKey = CrudProperties.get(CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY); - Map<String, String> filter = addParams ( uriInfo, true, type, version ); - - HashSet<String> properties; - if (uriInfo.getQueryParameters().containsKey(propertiesKey)) { - properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey)); - } else { - properties = new HashSet<>(); - } + private AbstractGraphDataService graphDataService; + Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName()); + Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName()); + private Auth auth; - ImmutablePair<EntityTag, String> result = graphDataService.getVertices(version, type, filter, properties); - response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + private String mediaType = MediaType.APPLICATION_JSON; + public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override"; + public static final String TRANSACTIONID_HEADER = "X-TransactionId"; + public CrudRestService(AbstractGraphDataService graphDataService) throws Exception { + this.graphDataService = graphDataService; + this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE); } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @GET - @Path("/relationships/{version}/{type}/{id}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response getEdge(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { - LoggingUtil.initMdcContext(req, headers); - - logger.debug("Incoming request..." + content); - Response response = null; - - Map<String, String> params = addParams ( uriInfo, false, type, version ); - - try { - if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - - ImmutablePair<EntityTag, String> result = graphDataService.getEdge(version, id, type, params); - response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + // For unit testing + public CrudRestService(AbstractGraphDataService graphDataService, Auth auth) throws Exception { + this.graphDataService = graphDataService; + this.auth = auth; } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @GET - @Path("/relationships/{version}/{type}/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response getEdges(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, - @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); - - logger.debug("Incoming request..." + content); - Response response = null; - Map<String, String> filter = addParams ( uriInfo, true, type, version ); - - try { - if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - ImmutablePair<EntityTag, String> result = graphDataService.getEdges(version, type, filter); - response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + public enum Action { + POST, GET, PUT, DELETE, PATCH } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @PUT - @Path("/relationships/{version}/{type}/{id}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response updateEdge(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); + public void startup() { - logger.debug("Incoming request..." + content); - Response response = null; + } + @GET + @Path("/{version}/{type}/{id}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response getVertex(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + Map<String, String> params = addParams(uriInfo, false, type, version); + + try { + if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + ImmutablePair<EntityTag, String> result = graphDataService.getVertex(version, id, type, params); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); - try { - if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - EdgePayload payload = EdgePayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null && !payload.getId().equals(id)) { - throw new CrudException("ID Mismatch", Status.BAD_REQUEST); - } - ImmutablePair<EntityTag, String> result; - if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null - && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { - result = graphDataService.patchEdge(version, id, type, payload); - response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); - } else { - result = graphDataService.updateEdge(version, id, type, payload); - response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } - - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - } - - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - @PATCH - @Path("/relationships/{version}/{type}/{id}") - @Consumes({"application/merge-patch+json"}) - @Produces({MediaType.APPLICATION_JSON}) - public Response patchEdge(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { + setTxIdOnResponseHeader(headers, responseBuilder); - LoggingUtil.initMdcContext(req, headers); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - logger.debug("Incoming request..." + content); - Response response = null; + @GET + @Path("/{version}/{type}/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response getVertices(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + try { + if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + String propertiesKey = CrudProperties.get(CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY); + Map<String, String> filter = addParams(uriInfo, true, type, version); + + HashSet<String> properties; + if (uriInfo.getQueryParameters().containsKey(propertiesKey)) { + properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey)); + } else { + properties = new HashSet<>(); + } + + ImmutablePair<EntityTag, String> result = + graphDataService.getVertices(version, type, filter, properties); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); - try { - if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - EdgePayload payload = EdgePayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null && !payload.getId().equals(id)) { - throw new CrudException("ID Mismatch", Status.BAD_REQUEST); } - ImmutablePair<EntityTag, String> result = graphDataService.patchEdge(version, id, type, payload); - response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - } + setTxIdOnResponseHeader(headers, responseBuilder); - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - @PUT - @Path("/{version}/{type}/{id}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response updateVertex(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { + @GET + @Path("/relationships/{version}/{type}/{id}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response getEdge(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + Map<String, String> params = addParams(uriInfo, false, type, version); + + try { + if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + + ImmutablePair<EntityTag, String> result = graphDataService.getEdge(version, id, type, params); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } - LoggingUtil.initMdcContext(req, headers); + setTxIdOnResponseHeader(headers, responseBuilder); - logger.debug("Incoming request..." + content); - Response response = null; + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - try { - if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - VertexPayload payload = VertexPayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null && !payload.getId().equals(id)) { - throw new CrudException("ID Mismatch", Status.BAD_REQUEST); + @GET + @Path("/relationships/{version}/{type}/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response getEdges(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + Map<String, String> filter = addParams(uriInfo, true, type, version); + + try { + if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + ImmutablePair<EntityTag, String> result = graphDataService.getEdges(version, type, filter); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); } - payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); + setTxIdOnResponseHeader(headers, responseBuilder); - ImmutablePair<EntityTag, String> result; - if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null - && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { - result = graphDataService.patchVertex(version, id, type, payload); - response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); - } else { - result = graphDataService.updateVertex(version, id, type, payload); - response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); - } - - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @PATCH - @Path("/{version}/{type}/{id}") - @Consumes({"application/merge-patch+json"}) - @Produces({MediaType.APPLICATION_JSON}) - public Response patchVertex(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { + @PUT + @Path("/relationships/{version}/{type}/{id}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response updateEdge(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + EdgePayload payload = EdgePayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null && !payload.getId().equals(id)) { + throw new CrudException("ID Mismatch", Status.BAD_REQUEST); + } + ImmutablePair<EntityTag, String> result; + if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null + && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { + result = graphDataService.patchEdge(version, id, type, payload); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()); + } else { + result = graphDataService.updateEdge(version, id, type, payload); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()); + } + + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } - LoggingUtil.initMdcContext(req, headers); + setTxIdOnResponseHeader(headers, responseBuilder); - logger.debug("Incoming request..." + content); - Response response = null; + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - try { - if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - VertexPayload payload = VertexPayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null && !payload.getId().equals(id)) { - throw new CrudException("ID Mismatch", Status.BAD_REQUEST); + @PATCH + @Path("/relationships/{version}/{type}/{id}") + @Consumes({"application/merge-patch+json"}) + @Produces({MediaType.APPLICATION_JSON}) + public Response patchEdge(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + EdgePayload payload = EdgePayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null && !payload.getId().equals(id)) { + throw new CrudException("ID Mismatch", Status.BAD_REQUEST); + } + + ImmutablePair<EntityTag, String> result = graphDataService.patchEdge(version, id, type, payload); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); } - payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); - - ImmutablePair<EntityTag, String> result = graphDataService.patchVertex(version, id, type, payload); - response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + setTxIdOnResponseHeader(headers, responseBuilder); + + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } + @PUT + @Path("/{version}/{type}/{id}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response updateVertex(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + VertexPayload payload = VertexPayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null && !payload.getId().equals(id)) { + throw new CrudException("ID Mismatch", Status.BAD_REQUEST); + } + + payload.setProperties( + CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); + + ImmutablePair<EntityTag, String> result; + if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null + && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { + result = graphDataService.patchVertex(version, id, type, payload); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()); + } else { + result = graphDataService.updateVertex(version, id, type, payload); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()); + } + + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } - @POST - @Path("/{version}/{type}/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response addVertex(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, - @Context HttpServletRequest req) { + setTxIdOnResponseHeader(headers, responseBuilder); - LoggingUtil.initMdcContext(req, headers); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - logger.debug("Incoming request..." + content); - Response response = null; + @PATCH + @Path("/{version}/{type}/{id}") + @Consumes({"application/merge-patch+json"}) + @Produces({MediaType.APPLICATION_JSON}) + public Response patchVertex(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + VertexPayload payload = VertexPayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null && !payload.getId().equals(id)) { + throw new CrudException("ID Mismatch", Status.BAD_REQUEST); + } + + payload.setProperties( + CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); + + ImmutablePair<EntityTag, String> result = graphDataService.patchVertex(version, id, type, payload); + responseBuilder = + Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } + setTxIdOnResponseHeader(headers, responseBuilder); - try { - if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - VertexPayload payload = VertexPayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null) { - throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST); - } + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - if (payload.getType() != null && !payload.getType().equals(type)) { - throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST); + @POST + @Path("/{version}/{type}/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response addVertex(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + VertexPayload payload = VertexPayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null) { + throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST); + } + + if (payload.getType() != null && !payload.getType().equals(type)) { + throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST); + } + + payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); + + ImmutablePair<EntityTag, String> result = graphDataService.addVertex(version, type, payload); + responseBuilder = + Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); } - payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); - - ImmutablePair<EntityTag, String> result = graphDataService.addVertex(version, type, payload); - response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - } + setTxIdOnResponseHeader(headers, responseBuilder); - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - private void validateBulkPayload(BulkPayload payload) throws CrudException { - List<String> vertices = new ArrayList<String>(); - List<String> edges = new ArrayList<String>(); - - for (JsonElement v : payload.getObjects()) { - List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>( - v.getAsJsonObject().entrySet()); - - if (entries.size() != 2) { - throw new CrudException("", Status.BAD_REQUEST); - } - Map.Entry<String, JsonElement> opr = entries.get(0); - Map.Entry<String, JsonElement> item = entries.get(1); - - if (vertices.contains(item.getKey())) { - throw new CrudException("duplicate vertex in payload: " + item.getKey(), Status.BAD_REQUEST); - } - VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString()); - if (vertexPayload.getType() == null) { - throw new CrudException("Vertex Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST); - } - - if (!opr.getKey().equalsIgnoreCase("operation")) { - throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST); - } - - if (!opr.getValue().getAsString().equalsIgnoreCase("add") - && !opr.getValue().getAsString().equalsIgnoreCase("modify") - && !opr.getValue().getAsString().equalsIgnoreCase("patch") - && !opr.getValue().getAsString().equalsIgnoreCase("delete")) { - throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); - } - // check if ID is populate for modify/patch/delete operation - if ((opr.getValue().getAsString().equalsIgnoreCase("modify") - || opr.getValue().getAsString().equalsIgnoreCase("patch") - || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) { - - throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); - - } - - vertices.add(item.getKey()); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; } - for (JsonElement v : payload.getRelationships()) { - List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>( - v.getAsJsonObject().entrySet()); - - if (entries.size() != 2) { - throw new CrudException("", Status.BAD_REQUEST); - } - Map.Entry<String, JsonElement> opr = entries.get(0); - Map.Entry<String, JsonElement> item = entries.get(1); - - if (edges.contains(item.getKey())) { - throw new CrudException("duplicate Edge in payload: " + item.getKey(), Status.BAD_REQUEST); - } - - EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString()); - - if (!opr.getKey().equalsIgnoreCase("operation")) { - throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST); - } - - if (!opr.getValue().getAsString().equalsIgnoreCase("add") - && !opr.getValue().getAsString().equalsIgnoreCase("modify") - && !opr.getValue().getAsString().equalsIgnoreCase("patch") - && !opr.getValue().getAsString().equalsIgnoreCase("delete")) { - throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); - } - // check if ID is populate for modify/patch/delete operation - if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify") - || opr.getValue().getAsString().equalsIgnoreCase("patch") - || opr.getValue().getAsString().equalsIgnoreCase("delete"))) { - - throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); - - } - if (opr.getValue().getAsString().equalsIgnoreCase("add")) { - if (edgePayload.getSource() == null || edgePayload.getTarget() == null) { - throw new CrudException("Source/Target cannot be null for edge: " + item.getKey(), Status.BAD_REQUEST); - } - if (edgePayload.getSource().startsWith("$") && !vertices.contains(edgePayload.getSource().substring(1))) { - throw new CrudException( - "Source Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(), - Status.BAD_REQUEST); + private void validateBulkPayload(BulkPayload payload) throws CrudException { + List<String> vertices = new ArrayList<String>(); + List<String> edges = new ArrayList<String>(); + + for (JsonElement v : payload.getObjects()) { + List<Map.Entry<String, JsonElement>> entries = + new ArrayList<Map.Entry<String, JsonElement>>(v.getAsJsonObject().entrySet()); + + if (entries.size() != 2) { + throw new CrudException("", Status.BAD_REQUEST); + } + Map.Entry<String, JsonElement> opr = entries.get(0); + Map.Entry<String, JsonElement> item = entries.get(1); + + if (vertices.contains(item.getKey())) { + throw new CrudException("duplicate vertex in payload: " + item.getKey(), Status.BAD_REQUEST); + } + VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString()); + if (vertexPayload.getType() == null) { + throw new CrudException("Vertex Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST); + } + + if (!opr.getKey().equalsIgnoreCase("operation")) { + throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST); + } + + if (!opr.getValue().getAsString().equalsIgnoreCase("add") + && !opr.getValue().getAsString().equalsIgnoreCase("modify") + && !opr.getValue().getAsString().equalsIgnoreCase("patch") + && !opr.getValue().getAsString().equalsIgnoreCase("delete")) { + throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); + } + // check if ID is populate for modify/patch/delete operation + if ((opr.getValue().getAsString().equalsIgnoreCase("modify") + || opr.getValue().getAsString().equalsIgnoreCase("patch") + || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) { + + throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); + + } + + vertices.add(item.getKey()); } - if (edgePayload.getTarget().startsWith("$") && !vertices.contains(edgePayload.getTarget().substring(1))) { - throw new CrudException( - "Target Vertex " + edgePayload.getSource().substring(1) + " not found for Edge: " + item.getKey(), - Status.BAD_REQUEST); + for (JsonElement v : payload.getRelationships()) { + List<Map.Entry<String, JsonElement>> entries = + new ArrayList<Map.Entry<String, JsonElement>>(v.getAsJsonObject().entrySet()); + + if (entries.size() != 2) { + throw new CrudException("", Status.BAD_REQUEST); + } + Map.Entry<String, JsonElement> opr = entries.get(0); + Map.Entry<String, JsonElement> item = entries.get(1); + + if (edges.contains(item.getKey())) { + throw new CrudException("duplicate Edge in payload: " + item.getKey(), Status.BAD_REQUEST); + } + + EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString()); + + if (!opr.getKey().equalsIgnoreCase("operation")) { + throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST); + } + + if (!opr.getValue().getAsString().equalsIgnoreCase("add") + && !opr.getValue().getAsString().equalsIgnoreCase("modify") + && !opr.getValue().getAsString().equalsIgnoreCase("patch") + && !opr.getValue().getAsString().equalsIgnoreCase("delete")) { + throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); + } + // check if ID is populate for modify/patch/delete operation + if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify") + || opr.getValue().getAsString().equalsIgnoreCase("patch") + || opr.getValue().getAsString().equalsIgnoreCase("delete"))) { + + throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); + + } + if (opr.getValue().getAsString().equalsIgnoreCase("add")) { + if (edgePayload.getSource() == null || edgePayload.getTarget() == null) { + throw new CrudException("Source/Target cannot be null for edge: " + item.getKey(), + Status.BAD_REQUEST); + } + if (edgePayload.getSource().startsWith("$") + && !vertices.contains(edgePayload.getSource().substring(1))) { + throw new CrudException("Source Vertex " + edgePayload.getSource().substring(1) + + " not found for Edge: " + item.getKey(), Status.BAD_REQUEST); + } + + if (edgePayload.getTarget().startsWith("$") + && !vertices.contains(edgePayload.getTarget().substring(1))) { + throw new CrudException("Target Vertex " + edgePayload.getSource().substring(1) + + " not found for Edge: " + item.getKey(), Status.BAD_REQUEST); + } + } + edges.add(item.getKey()); + } - } - edges.add(item.getKey()); } - } - - @POST - @Path("/{version}/bulk/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response addBulk(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, - @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); + @POST + @Path("/{version}/bulk/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response addBulk(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + BulkPayload payload = BulkPayload.fromJson(content); + if ((payload.getObjects() == null && payload.getRelationships() == null) + || (payload.getObjects() != null && payload.getObjects().isEmpty() + && payload.getRelationships() != null && payload.getRelationships().isEmpty())) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + + validateBulkPayload(payload); + String result = graphDataService.addBulk(version, payload, headers); + responseBuilder = Response.status(Status.OK).entity(result).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } - logger.debug("Incoming request..." + content); - Response response = null; + setTxIdOnResponseHeader(headers, responseBuilder); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - try { - if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - BulkPayload payload = BulkPayload.fromJson(content); - if ((payload.getObjects() == null && payload.getRelationships() == null) - || (payload.getObjects() != null && payload.getObjects().isEmpty() && payload.getRelationships() != null - && payload.getRelationships().isEmpty())) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + @POST + @Path("/{version}/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response addVertex(String content, @PathParam("version") String version, + @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + VertexPayload payload = VertexPayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null) { + throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST); + } + + if (payload.getType() == null || payload.getType().isEmpty()) { + throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST); + } + + payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); + + ImmutablePair<EntityTag, String> result = + graphDataService.addVertex(version, payload.getType(), payload); + responseBuilder = + Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); } - validateBulkPayload(payload); - String result = graphDataService.addBulk(version, payload, headers); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - } + setTxIdOnResponseHeader(headers, responseBuilder); - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @POST - @Path("/{version}/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response addVertex(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri, - @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) { + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - LoggingUtil.initMdcContext(req, headers); + @POST + @Path("/relationships/{version}/{type}/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response addEdge(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + EdgePayload payload = EdgePayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null) { + throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST); + } + + if (payload.getType() != null && !payload.getType().equals(type)) { + throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST); + } + ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, type, payload); + responseBuilder = + Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } - logger.debug("Incoming request..." + content); - Response response = null; + setTxIdOnResponseHeader(headers, responseBuilder); - try { + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - VertexPayload payload = VertexPayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null) { - throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST); + @POST + @Path("/relationships/{version}/") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response addEdge(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri, + @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + EdgePayload payload = EdgePayload.fromJson(content); + if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { + throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + } + if (payload.getId() != null) { + throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST); + } + + if (payload.getType() == null || payload.getType().isEmpty()) { + payload.setType(CrudServiceUtil.determineEdgeType(payload, version)); + } + + ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, payload.getType(), payload); + responseBuilder = + Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); } - if (payload.getType() == null || payload.getType().isEmpty()) { - throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST); - } + setTxIdOnResponseHeader(headers, responseBuilder); - payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); - - ImmutablePair<EntityTag, String> result = graphDataService.addVertex(version, payload.getType(), payload); - response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @POST - @Path("/relationships/{version}/{type}/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response addEdge(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo, - @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); + @DELETE + @Path("/{version}/{type}/{id}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response deleteVertex(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + String result = graphDataService.deleteVertex(version, id, type); + responseBuilder = Response.status(Status.OK).entity(result).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); + } - logger.debug("Incoming request..." + content); - Response response = null; + setTxIdOnResponseHeader(headers, responseBuilder); + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - try { - if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - EdgePayload payload = EdgePayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); - } - if (payload.getId() != null) { - throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST); + @DELETE + @Path("/relationships/{version}/{type}/{id}") + @Consumes({MediaType.APPLICATION_JSON}) + @Produces({MediaType.APPLICATION_JSON}) + public Response deleteEdge(String content, @PathParam("version") String version, @PathParam("type") String type, + @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, + @Context UriInfo uriInfo, @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + logger.debug("Incoming request..." + content); + + ResponseBuilder responseBuilder; + + try { + if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { + String result = graphDataService.deleteEdge(version, id, type); + responseBuilder = Response.status(Status.OK).entity(result).type(mediaType); + } else { + responseBuilder = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON); + } + } catch (CrudException ce) { + responseBuilder = Response.status(ce.getHttpStatus()).entity(ce.getMessage()); + } catch (Exception e) { + responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()); } - if (payload.getType() != null && !payload.getType().equals(type)) { - throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST); - } - ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, type, payload); - response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - } + setTxIdOnResponseHeader(headers, responseBuilder); - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + return response; + } - @POST - @Path("/relationships/{version}/") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response addEdge(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri, - @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) { + protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action, + String authPolicyFunctionName, HttpHeaders headers) throws CrudException { + boolean isValid = false; + try { + String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite"); + String authUser = null; + if (cipherSuite != null) { + X509Certificate[] certChain = + (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate"); + X509Certificate clientCert = certChain[0]; + X500Principal subjectDn = clientCert.getSubjectX500Principal(); + authUser = subjectDn.toString(); + } + if (null != authUser) { + isValid = this.auth.validateRequest(authUser.toLowerCase(), + action.toString() + ":" + authPolicyFunctionName); + } + } catch (Exception e) { + logResult(action, uri, e); + return false; + } - LoggingUtil.initMdcContext(req, headers); + validateRequestHeader(headers); - logger.debug("Incoming request..." + content); - Response response = null; + return isValid; + } + public void validateRequestHeader(HttpHeaders headers) throws CrudException { + String sourceOfTruth = null; + if (headers.getRequestHeaders().containsKey("X-FromAppId")) { + sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); + } - try { - if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - EdgePayload payload = EdgePayload.fromJson(content); - if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { - throw new CrudException("Invalid request Payload", Status.BAD_REQUEST); + if (sourceOfTruth == null || sourceOfTruth.trim() == "") { + throw new CrudException("Invalid request, Missing X-FromAppId header", Status.BAD_REQUEST); } - if (payload.getId() != null) { - throw new CrudException("ID specified , use Http PUT to update Edge", Status.BAD_REQUEST); + + String transId = null; + if (headers.getRequestHeaders().containsKey("X-TransactionId")) { + transId = headers.getRequestHeaders().getFirst("X-TransactionId"); } - if (payload.getType() == null || payload.getType().isEmpty()) { - payload.setType(CrudServiceUtil.determineEdgeType(payload, version)); + if (transId == null || transId.trim() == "") { + throw new CrudException("Invalid request, Missing X-TransactionId header", Status.BAD_REQUEST); } - - ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, payload.getType(), payload); - response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @DELETE - @Path("/{version}/{type}/{id}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response deleteVertex(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); - - logger.debug("Incoming request..." + content); - Response response = null; - - - try { - if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - String result = graphDataService.deleteVertex(version, id, type); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); - } + void logResult(Action op, String uri, Exception e) { - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - @DELETE - @Path("/relationships/{version}/{type}/{id}") - @Consumes({MediaType.APPLICATION_JSON}) - @Produces({MediaType.APPLICATION_JSON}) - public Response deleteEdge(String content, @PathParam("version") String version, @PathParam("type") String type, - @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, - @Context UriInfo uriInfo, @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); - - logger.debug("Incoming request..." + content); - Response response = null; - - try { - if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - String result = graphDataService.deleteEdge(version, id, type); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); - } else { - response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); - } - } catch (CrudException ce) { - response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); - } catch (Exception e) { - response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, + Arrays.toString(e.getStackTrace())); + + // Clear the MDC context so that no other transaction inadvertently + // uses our transaction id. + MDC.clear(); } - LoggingUtil.logRestRequest(logger, auditLogger, req, response); - return response; - } - - protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action, - String authPolicyFunctionName, HttpHeaders headers) throws CrudException { - boolean isValid = false; - try { - String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite"); - String authUser = null; - if (cipherSuite != null) { - X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate"); - X509Certificate clientCert = certChain[0]; - X500Principal subjectDn = clientCert.getSubjectX500Principal(); - authUser = subjectDn.toString(); - } - if(null != authUser) { - isValid = this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName); - } - } catch (Exception e) { - logResult(action, uri, e); - return false; + private Map<String, String> addParams(UriInfo info, boolean filter, String type, String version) { + String propertiesKey = CrudProperties.get(CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY); + Map<String, String> params = new HashMap<String, String>(); + params.put(CrudServiceConstants.CRD_RESERVED_VERSION, version); + params.put(CrudServiceConstants.CRD_RESERVED_NODE_TYPE, type); + if (filter) { + for (Map.Entry<String, List<String>> e : info.getQueryParameters().entrySet()) { + if (!e.getKey().equals(propertiesKey)) { + params.put(e.getKey(), e.getValue().get(0)); + } + } + } else { + for (Map.Entry<String, List<String>> e : info.getQueryParameters().entrySet()) { + params.put(e.getKey(), e.getValue().get(0)); + } + } + return params; } - validateRequestHeader(headers); - - return isValid; - } - - public void validateRequestHeader(HttpHeaders headers) throws CrudException { - String sourceOfTruth = null; - if (headers.getRequestHeaders().containsKey("X-FromAppId")) { - sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); - } - - if (sourceOfTruth == null || sourceOfTruth.trim() == "") { - throw new CrudException("Invalid request, Missing X-FromAppId header", Status.BAD_REQUEST); - } - - String transId = null; - if (headers.getRequestHeaders().containsKey("X-TransactionId")) { - transId = headers.getRequestHeaders().getFirst("X-TransactionId"); - } - - if (transId == null || transId.trim() == "") { - throw new CrudException("Invalid request, Missing X-TransactionId header", Status.BAD_REQUEST); - } - } - - void logResult(Action op, String uri, Exception e) { - - logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, Arrays.toString(e.getStackTrace())); - - // Clear the MDC context so that no other transaction inadvertently - // uses our transaction id. - MDC.clear(); - } - - private Map<String, String> addParams ( UriInfo info, boolean filter, String type, String version ) { - String propertiesKey = CrudProperties.get ( CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY ); - Map<String, String> params = new HashMap<String, String> (); - params.put ( CrudServiceConstants.CRD_RESERVED_VERSION, version ); - params.put ( CrudServiceConstants.CRD_RESERVED_NODE_TYPE, type ); - if (filter) { - for (Map.Entry<String, List<String>> e : info.getQueryParameters ().entrySet ()) { - if (!e.getKey ().equals ( propertiesKey )) { - params.put ( e.getKey (), e.getValue ().get ( 0 ) ); + private void setTxIdOnResponseHeader(HttpHeaders headers, ResponseBuilder responseBuilder) { + String txId = headers.getHeaderString(TRANSACTIONID_HEADER); + if (txId != null) { + responseBuilder.header(TRANSACTIONID_HEADER, txId); } - } - } else { - for (Map.Entry<String, List<String>> e : info.getQueryParameters ().entrySet ()) { - params.put ( e.getKey (), e.getValue ().get ( 0 ) ); - } } - return params; - } } diff --git a/src/main/java/org/onap/crud/service/JaxrsEchoService.java b/src/main/java/org/onap/crud/service/JaxrsEchoService.java index dc01435..671b2fd 100644 --- a/src/main/java/org/onap/crud/service/JaxrsEchoService.java +++ b/src/main/java/org/onap/crud/service/JaxrsEchoService.java @@ -28,6 +28,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import org.onap.aai.cl.api.Logger; @@ -39,23 +40,29 @@ import org.springframework.stereotype.Component; @Path("/services/gizmo/v1/echo-service/") public class JaxrsEchoService { - private static Logger logger = LoggerFactory.getInstance() - .getLogger(JaxrsEchoService.class.getName()); - private static Logger auditLogger = LoggerFactory.getInstance() - .getAuditLogger(JaxrsEchoService.class.getName()); - - @GET - @Path("echo/{input}") - @Produces("text/plain") - public String ping(@PathParam("input") String input, - @Context HttpHeaders headers, - @Context UriInfo info, - @Context HttpServletRequest req) { - - LoggingUtil.initMdcContext(req, headers); - LoggingUtil.logRestRequest(logger, auditLogger, req, Response.status(Status.OK) - .entity("OK").build()); - - return "Hello, " + input + "."; - } -}
\ No newline at end of file + private static Logger logger = LoggerFactory.getInstance().getLogger(JaxrsEchoService.class.getName()); + private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(JaxrsEchoService.class.getName()); + + public static final String TRANSACTIONID_HEADER = "X-TransactionId"; + + @GET + @Path("echo/{input}") + @Produces("text/plain") + public Response ping(@PathParam("input") String input, @Context HttpHeaders headers, @Context UriInfo info, + @Context HttpServletRequest req) { + + LoggingUtil.initMdcContext(req, headers); + + ResponseBuilder responseBuilder = Response.status(Status.OK).entity("Hello, " + input + "."); + + String txId = headers.getHeaderString(TRANSACTIONID_HEADER); + if (txId != null) { + responseBuilder.header(TRANSACTIONID_HEADER, txId); + } + + Response response = responseBuilder.build(); + LoggingUtil.logRestRequest(logger, auditLogger, req, response); + + return response; + } +} |