diff options
Diffstat (limited to 'src/main/java/org/openecomp/crud/service')
3 files changed, 452 insertions, 511 deletions
diff --git a/src/main/java/org/openecomp/crud/service/BulkPayload.java b/src/main/java/org/openecomp/crud/service/BulkPayload.java index 28cf420..619c53b 100644 --- a/src/main/java/org/openecomp/crud/service/BulkPayload.java +++ b/src/main/java/org/openecomp/crud/service/BulkPayload.java @@ -39,90 +39,86 @@ import java.util.Set; import javax.ws.rs.core.Response.Status; public class BulkPayload { - public enum OperationType { - CREATE, UPDATE, DELETE - } - - - - private List<JsonElement> objects = new ArrayList<JsonElement>(); - private List<JsonElement> relationships = new ArrayList<JsonElement>(); - - private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); - - public String toJson() { - return gson.toJson(this); - } - - public static BulkPayload fromJson(String payload) throws CrudException { - try { - if (payload == null || payload.isEmpty()) { - throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST); - } - return gson.fromJson(payload, BulkPayload.class); - } catch (Exception ex) { - throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST); - } - } - - public List<JsonElement> getObjects() { - return objects; - } - - public void setObjects(List<JsonElement> objects) { - this.objects = objects; - } - - public List<JsonElement> getRelationships() { - return relationships; - } - - public void setRelationships(List<JsonElement> relationships) { - this.relationships = relationships; - } - - @Override - public String toString() { - return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]"; - } - - public static void main(String[] args) throws Exception { - BulkPayload p = new BulkPayload(); - JsonObject root = new JsonObject(); - JsonArray vertices = new JsonArray(); - JsonObject v1 = new JsonObject(); - JsonObject v2 = new JsonObject(); - JsonObject prop = new JsonObject(); - - prop.addProperty("p1","value1"); - prop.addProperty("p2","value2"); - v1.add("v1", prop); - v2.add("v2",prop); - - vertices.add(v1); - vertices.add(v2); - - - root.add("objects", vertices); - - String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}"; - - p = BulkPayload.fromJson(s); - - List<JsonElement> po = p.getObjects(); - List<String> ids = new ArrayList<String>(); - for (JsonElement e : po){ - Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet(); - - for (Map.Entry<String, JsonElement> entry : entries) { - ids.add(entry.getKey()); - } - } - - - System.out.println("root: " + root.toString()); - System.out.println("payload ids: " + ids.toString()); - - } + public enum OperationType { + CREATE, UPDATE, DELETE + } + + private List<JsonElement> objects = new ArrayList<JsonElement>(); + private List<JsonElement> relationships = new ArrayList<JsonElement>(); + + private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create(); + + public String toJson() { + return gson.toJson(this); + } + + public static BulkPayload fromJson(String payload) throws CrudException { + try { + if (payload == null || payload.isEmpty()) { + throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST); + } + return gson.fromJson(payload, BulkPayload.class); + } catch (Exception ex) { + throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST); + } + } + + public List<JsonElement> getObjects() { + return objects; + } + + public void setObjects(List<JsonElement> objects) { + this.objects = objects; + } + + public List<JsonElement> getRelationships() { + return relationships; + } + + public void setRelationships(List<JsonElement> relationships) { + this.relationships = relationships; + } + + @Override + public String toString() { + return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]"; + } + + public static void main(String[] args) throws Exception { + BulkPayload p = new BulkPayload(); + JsonObject root = new JsonObject(); + JsonArray vertices = new JsonArray(); + JsonObject v1 = new JsonObject(); + JsonObject v2 = new JsonObject(); + JsonObject prop = new JsonObject(); + + prop.addProperty("p1", "value1"); + prop.addProperty("p2", "value2"); + v1.add("v1", prop); + v2.add("v2", prop); + + vertices.add(v1); + vertices.add(v2); + + root.add("objects", vertices); + + String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}"; + + p = BulkPayload.fromJson(s); + + List<JsonElement> po = p.getObjects(); + List<String> ids = new ArrayList<String>(); + for (JsonElement e : po) { + Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet(); + + for (Map.Entry<String, JsonElement> entry : entries) { + ids.add(entry.getKey()); + } + } + + System.out.println("root: " + root.toString()); + System.out.println("payload ids: " + ids.toString()); + + } }
\ No newline at end of file diff --git a/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java b/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java index c6b6a48..340fafa 100644 --- a/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java +++ b/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java @@ -50,148 +50,155 @@ public class CrudGraphDataService { private GraphDao dao; public CrudGraphDataService(ChampGraph graphImpl) throws CrudException { - this.dao = new ChampDao(graphImpl); - - loadModels(); - } + this.dao = new ChampDao(graphImpl); + loadModels(); + } - public CrudGraphDataService(GraphDao dao) throws CrudException { - this.dao = dao; + public CrudGraphDataService(GraphDao dao) throws CrudException { + this.dao = dao; - loadModels(); - } + loadModels(); + } - private void loadModels() throws CrudException { - //load the schemas - try { + private void loadModels() throws CrudException { + // load the schemas + try { OxmModelLoader.loadModels(); } catch (Exception e) { throw new CrudException(e); } - RelationshipSchemaLoader.loadModels(); - } + RelationshipSchemaLoader.loadModels(); + } public String addVertex(String version, String type, VertexPayload payload) throws CrudException { - Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type, - payload.getProperties()); + Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type, payload.getProperties()); return addVertex(version, vertex); } - - public String addBulk(String version, BulkPayload payload) throws CrudException { - HashMap<String, Vertex> vertices = new HashMap<String, Vertex>(); - HashMap<String, Edge> edges = new HashMap<String, Edge>(); - String txId = dao.openTransaction(); - try { - // Handle vertices - 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); - - VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString()); - - if (opr.getValue().getAsString().equalsIgnoreCase("add") || opr.getValue().getAsString().equalsIgnoreCase("modify")) { - Vertex validatedVertex; - Vertex persistedVertex; - if (opr.getValue().getAsString().equalsIgnoreCase("add")) { - validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(), - vertexPayload.getProperties()); - // Call champDAO to add the vertex - persistedVertex = dao.addVertex(validatedVertex.getType(), validatedVertex.getProperties(), txId); - } else { - validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version, vertexPayload.getType(), - vertexPayload.getProperties()); - // Call champDAO to update the vertex - persistedVertex = dao.updateVertex(vertexPayload.getId(), validatedVertex.getType(), validatedVertex.getProperties(), txId); - } - - Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex); - - vertices.put(item.getKey(), outgoingVertex); - - } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) { - dao.deleteVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), txId); - } - - } - // Handle Edges - 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); - - EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString()); - - if (opr.getValue().getAsString().equalsIgnoreCase("add") || opr.getValue().getAsString().equalsIgnoreCase("modify")) { - Edge validatedEdge; - Edge persistedEdge; - if (opr.getValue().getAsString().equalsIgnoreCase("add")) { - // Fix the source/detination - if (edgePayload.getSource().startsWith("$")) { - Vertex source = vertices.get(edgePayload.getSource().substring(1)); - if (source == null) { - throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1), Status.INTERNAL_SERVER_ERROR); - } - edgePayload.setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get()); - } - if (edgePayload.getTarget().startsWith("$")) { - Vertex target = vertices.get(edgePayload.getTarget().substring(1)); - if (target == null) { - throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1), Status.INTERNAL_SERVER_ERROR); - } - edgePayload.setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get()); - } - validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), edgePayload); - persistedEdge = dao.addEdge(validatedEdge.getType(), validatedEdge.getSource(), validatedEdge.getTarget(), - validatedEdge.getProperties(), txId); - } else { - Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId); - validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload); - persistedEdge = dao.updateEdge(edge, txId); - } - - Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge); - - edges.put(item.getKey(), outgoingEdge); - - } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) { - RelationshipSchemaValidator.validateType(version, edgePayload.getType()); - dao.deleteEdge(edgePayload.getId(), edgePayload.getType(), txId); - } - - } - // close champ TX - dao.commitTransaction(txId); - } catch (CrudException ex) { - dao.rollbackTransaction(txId); - throw ex; - } catch (Exception ex) { - dao.rollbackTransaction(txId); - throw ex; - } finally { - if (dao.transactionExists(txId)) { - dao.rollbackTransaction(txId); - } - } - - - return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload); - } + + public String addBulk(String version, BulkPayload payload) throws CrudException { + HashMap<String, Vertex> vertices = new HashMap<String, Vertex>(); + HashMap<String, Edge> edges = new HashMap<String, Edge>(); + String txId = dao.openTransaction(); + try { + // Handle vertices + 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); + + VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString()); + + if (opr.getValue().getAsString().equalsIgnoreCase("add") + || opr.getValue().getAsString().equalsIgnoreCase("modify")) { + Vertex validatedVertex; + Vertex persistedVertex; + if (opr.getValue().getAsString().equalsIgnoreCase("add")) { + validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(), + vertexPayload.getProperties()); + // Call champDAO to add the vertex + persistedVertex = dao.addVertex(validatedVertex.getType(), validatedVertex.getProperties(), txId); + } else { + validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version, + vertexPayload.getType(), vertexPayload.getProperties()); + // Call champDAO to update the vertex + persistedVertex = dao.updateVertex(vertexPayload.getId(), validatedVertex.getType(), + validatedVertex.getProperties(), txId); + } + + Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex); + + vertices.put(item.getKey(), outgoingVertex); + + } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) { + dao.deleteVertex(vertexPayload.getId(), + OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), txId); + } + + } + // Handle Edges + 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); + + EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString()); + + if (opr.getValue().getAsString().equalsIgnoreCase("add") + || opr.getValue().getAsString().equalsIgnoreCase("modify")) { + Edge validatedEdge; + Edge persistedEdge; + if (opr.getValue().getAsString().equalsIgnoreCase("add")) { + // Fix the source/detination + if (edgePayload.getSource().startsWith("$")) { + Vertex source = vertices.get(edgePayload.getSource().substring(1)); + if (source == null) { + throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1), + Status.INTERNAL_SERVER_ERROR); + } + edgePayload + .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get()); + } + if (edgePayload.getTarget().startsWith("$")) { + Vertex target = vertices.get(edgePayload.getTarget().substring(1)); + if (target == null) { + throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1), + Status.INTERNAL_SERVER_ERROR); + } + edgePayload + .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get()); + } + validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), + edgePayload); + persistedEdge = dao.addEdge(validatedEdge.getType(), validatedEdge.getSource(), validatedEdge.getTarget(), + validatedEdge.getProperties(), txId); + } else { + Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId); + validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload); + persistedEdge = dao.updateEdge(edge, txId); + } + + Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge); + + edges.put(item.getKey(), outgoingEdge); + + } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) { + RelationshipSchemaValidator.validateType(version, edgePayload.getType()); + dao.deleteEdge(edgePayload.getId(), edgePayload.getType(), txId); + } + + } + // close champ TX + dao.commitTransaction(txId); + } catch (CrudException ex) { + dao.rollbackTransaction(txId); + throw ex; + } catch (Exception ex) { + dao.rollbackTransaction(txId); + throw ex; + } finally { + if (dao.transactionExists(txId)) { + dao.rollbackTransaction(txId); + } + } + + return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload); + } private String addVertex(String version, Vertex vertex) throws CrudException { Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties()); return CrudResponseBuilder - .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex), - version); + .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex), version); } public String addEdge(String version, String type, EdgePayload payload) throws CrudException { @@ -200,52 +207,41 @@ public class CrudGraphDataService { } private String addEdge(String version, Edge edge) throws CrudException { - Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), - edge.getProperties()); - return CrudResponseBuilder.buildUpsertEdgeResponse( - RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version); + Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties()); + return CrudResponseBuilder + .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version); } public String getEdge(String version, String id, String type) throws CrudException { RelationshipSchemaValidator.validateType(version, type); Edge edge = dao.getEdge(id, type); - return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator - .validateOutgoingPayload(version, edge), + return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, edge), version); } - public String getEdges(String version, String type, Map<String, String> filter) - throws CrudException { + public String getEdges(String version, String type, Map<String, String> filter) throws CrudException { RelationshipSchemaValidator.validateType(version, type); - List<Edge> items = dao.getEdges(type, RelationshipSchemaValidator - .resolveCollectionfilter(version, type, filter)); + List<Edge> items = dao.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter)); return CrudResponseBuilder.buildGetEdgesResponse(items, version); } - - public String updateVertex(String version, String id, String type, VertexPayload payload) - throws CrudException { - Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type, - payload.getProperties()); + public String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException { + Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type, payload.getProperties()); return updateVertex(version, vertex); } private String updateVertex(String version, Vertex vertex) throws CrudException { - Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(), - vertex.getProperties()); + Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(), vertex.getProperties()); return CrudResponseBuilder - .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, - updatedVertex), version); + .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, updatedVertex), version); } - public String patchVertex(String version, String id, String type, VertexPayload payload) - throws CrudException { - Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, - type)); - Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, - payload.getProperties(), existingVertex); + public String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException { + Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type)); + Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, payload.getProperties(), + existingVertex); return updateVertex(version, vertex); } @@ -264,26 +260,22 @@ public class CrudGraphDataService { } - public String updateEdge(String version, String id, String type, EdgePayload payload) - throws CrudException { + public String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException { Edge edge = dao.getEdge(id, type); - Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, - version, payload); + Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload); return updateEdge(version, validatedEdge); } private String updateEdge(String version, Edge edge) throws CrudException { Edge updatedEdge = dao.updateEdge(edge); - return CrudResponseBuilder.buildUpsertEdgeResponse( - RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version); + return CrudResponseBuilder + .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version); } - public String patchEdge(String version, String id, String type, EdgePayload payload) - throws CrudException { + public String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException { Edge edge = dao.getEdge(id, type); - Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, - version, payload); + Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload); return updateEdge(version, patchedEdge); } @@ -291,23 +283,19 @@ public class CrudGraphDataService { public Vertex getVertex(String id) throws CrudException { return dao.getVertex(id); } - + public String getVertex(String version, String id, String type) throws CrudException { type = OxmModelValidator.resolveCollectionType(version, type); Vertex vertex = dao.getVertex(id, type); List<Edge> edges = dao.getVertexEdges(id); - return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator - .validateOutgoingPayload(version, vertex), edges, version); + return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges, + version); } - public String getVertices(String version, String type, Map<String, String> filter) - throws CrudException { + public String getVertices(String version, String type, Map<String, String> filter) throws CrudException { type = OxmModelValidator.resolveCollectionType(version, type); - List<Vertex> items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, - type, filter)); + List<Vertex> items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter)); return CrudResponseBuilder.buildGetVerticesResponse(items, version); } - - } diff --git a/src/main/java/org/openecomp/crud/service/CrudRestService.java b/src/main/java/org/openecomp/crud/service/CrudRestService.java index 8a9f1c0..79b5624 100644 --- a/src/main/java/org/openecomp/crud/service/CrudRestService.java +++ b/src/main/java/org/openecomp/crud/service/CrudRestService.java @@ -87,12 +87,11 @@ public class CrudRestService { @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) { + @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); @@ -109,8 +108,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -119,12 +117,11 @@ public class CrudRestService { @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) { + @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); @@ -146,8 +143,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -156,12 +152,11 @@ public class CrudRestService { @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) { + @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); @@ -179,8 +174,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -189,12 +183,11 @@ public class CrudRestService { @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) { + @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); @@ -217,8 +210,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -228,12 +220,11 @@ public class CrudRestService { @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) { + @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); @@ -253,8 +244,7 @@ public class CrudRestService { String result; if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null - && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) - .equalsIgnoreCase("PATCH")) { + && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { result = crudGraphDataService.patchEdge(version, id, type, payload); } else { @@ -268,8 +258,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -279,19 +268,17 @@ public class CrudRestService { @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.PATCH, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { + if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { EdgePayload payload = EdgePayload.fromJson(content); @@ -310,8 +297,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -320,12 +306,11 @@ public class CrudRestService { @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) { + @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); @@ -344,8 +329,7 @@ public class CrudRestService { } String result; if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null - && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) - .equalsIgnoreCase("PATCH")) { + && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { result = crudGraphDataService.patchVertex(version, id, type, payload); } else { @@ -358,8 +342,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -368,20 +351,18 @@ public class CrudRestService { @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.PATCH, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { + if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { VertexPayload payload = VertexPayload.fromJson(content); if (payload.getProperties() == null || payload.getProperties().isJsonNull()) { @@ -399,8 +380,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -409,20 +389,18 @@ public class CrudRestService { @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.POST, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { VertexPayload payload = VertexPayload.fromJson(content); @@ -430,8 +408,7 @@ public class CrudRestService { 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); + throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST); } if (payload.getType() != null && !payload.getType().equals(type)) { @@ -446,135 +423,138 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } 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("delete")) { - throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); - } - // check if ID is populate for modify/delete operation - if ((opr.getValue().getAsString().equalsIgnoreCase("modify") || opr.getValue().getAsString().equalsIgnoreCase("delete")) - && (vertexPayload.getId() == null)) { - - throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); - - } - - vertices.add(item.getKey()); - } - - 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 (edgePayload.getType() == null) { - throw new CrudException("Edge 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("delete")) { - throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); - } - // check if ID is populate for modify/delete operation - if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify") - || 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()); - - } - - } - + + 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("delete")) { + throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); + } + // check if ID is populate for modify/delete operation + if ((opr.getValue().getAsString().equalsIgnoreCase("modify") + || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) { + + throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST); + + } + + vertices.add(item.getKey()); + } + + 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 (edgePayload.getType() == null) { + throw new CrudException("Edge 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("delete")) { + throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST); + } + // check if ID is populate for modify/delete operation + if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify") + || 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()); + + } + + } + @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.POST, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { BulkPayload payload = BulkPayload.fromJson(content); - if ((payload.getObjects() == null && payload.getRelationships() == null) || (payload.getObjects() != null && payload.getObjects().isEmpty() - && payload.getRelationships() != null && payload.getRelationships().isEmpty())) { + 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 = crudGraphDataService.addBulk(version, payload); + + validateBulkPayload(payload); + String result = crudGraphDataService.addBulk(version, payload); response = Response.status(Status.OK).entity(result).type(mediaType).build(); } catch (CrudException ce) { response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build(); @@ -582,8 +562,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -592,19 +571,17 @@ public class CrudRestService { @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.POST, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { VertexPayload payload = VertexPayload.fromJson(content); @@ -612,8 +589,7 @@ public class CrudRestService { 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); + throw new CrudException("ID specified , use Http PUT to update Vertex", Status.BAD_REQUEST); } if (payload.getType() == null || payload.getType().isEmpty()) { @@ -627,8 +603,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -637,21 +612,18 @@ public class CrudRestService { @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.POST, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { - + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { EdgePayload payload = EdgePayload.fromJson(content); @@ -673,8 +645,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -683,20 +654,17 @@ public class CrudRestService { @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) { + @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); Response response = null; - if (validateRequest(req, uri, content, Action.POST, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { - + if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { EdgePayload payload = EdgePayload.fromJson(content); @@ -719,8 +687,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -729,21 +696,18 @@ public class CrudRestService { @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) { + @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; - if (validateRequest(req, uri, content, Action.DELETE, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { - + if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { String result = crudGraphDataService.deleteVertex(version, id, type); @@ -754,8 +718,7 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); @@ -764,19 +727,17 @@ public class CrudRestService { @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) { + @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; - if (validateRequest(req, uri, content, Action.DELETE, - CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { + if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) { try { String result = crudGraphDataService.deleteEdge(version, id, type); @@ -787,28 +748,25 @@ public class CrudRestService { response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } else { - response = Response.status(Status.FORBIDDEN).entity(content) - .type(MediaType.APPLICATION_JSON).build(); + response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } LoggingUtil.logRestRequest(logger, auditLogger, req, response); return response; } - protected boolean validateRequest(HttpServletRequest req, String uri, String content, - Action action, String authPolicyFunctionName) { + protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action, + String authPolicyFunctionName) { 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[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate"); X509Certificate clientCert = certChain[0]; X500Principal subjectDn = clientCert.getSubjectX500Principal(); authUser = subjectDn.toString(); } - return this.auth.validateRequest(authUser.toLowerCase(), action.toString() - + ":" + authPolicyFunctionName); + return this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName); } catch (Exception e) { logResult(action, uri, e); return false; @@ -817,8 +775,7 @@ public class CrudRestService { void logResult(Action op, String uri, Exception e) { - logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, - e.getStackTrace().toString()); + logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, e.getStackTrace().toString()); // Clear the MDC context so that no other transaction inadvertently // uses our transaction id. |