- 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());
- }
-
- 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("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);
+ @ApiOperation(value = "Create Edge" , notes = "# Payload \n"
+ + "{ \r\n" +
+ " \"source\":\"source vertex like : services/inventory/v11/vserver/0\",\r\n" +
+ " \"target\":\"target vertex like : services/inventory/v11/pserver/7\",\r\n" +
+ " \"properties\":{ \r\n" +
+ " \"prop1\" : \"value\",\r\n" +
+ " \"prop2\" :\"value\",\r\n" +
+ " ..}\r\n" +
+ " }")
+ @ApiResponses({
+ @ApiResponse(code = 201, message = "Created"),
+ @ApiResponse(code = 403, message = "Forbidden"),
+ @ApiResponse(code = 400, message = "Bad Request"),
+ @ApiResponse(code = 404, message = "Not Found"),
+ @ApiResponse(code = 500, message = "Internal Server Error") })
+
+ @ApiImplicitParams({
+ @ApiImplicitParam(name = "X-FromAppId", required = true, dataType = "string", paramType = "header"),
+ @ApiImplicitParam(name = "X-TransactionId", required = true, dataType = "string", paramType = "header"),
+ })
+ @POST
+ @Path("/relationships/{version}/{type}/")
+ @Consumes({MediaType.APPLICATION_JSON})
+ @Produces({MediaType.APPLICATION_JSON})
+ public Response addEdge(String content, @PathParam("version") @ApiParam(value="oxm model version",defaultValue="v13") String version, @PathParam("type") String type,
+ @PathParam("uri") @Encoded @ApiParam(hidden=true) 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());