From a214853e9473b432fca3ebd02f7214dd6d62e44d Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 5 Jul 2017 15:07:51 +0200 Subject: [PATCH] [Major] Inspector can now update StrolchElements from JSON/XML --- .../model/xml/StrolchElementToDomVisitor.java | 8 +- .../xml/StrolchElementToSaxWriterVisitor.java | 7 +- .../li/strolch/rest/endpoint/Inspector.java | 245 +++++++++++++++--- .../service/UpdateActivityService.java | 7 + .../strolch/service/UpdateOrderService.java | 7 + .../service/UpdateResourceService.java | 7 + 6 files changed, 235 insertions(+), 46 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java index b649d52cb..153e111ee 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java @@ -46,6 +46,7 @@ import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; import li.strolch.model.visitor.StrolchRootElementVisitor; import li.strolch.utils.helper.DomUtil; +import li.strolch.utils.helper.StringHelper; import li.strolch.utils.iso8601.ISO8601FormatFactory; /** @@ -143,8 +144,11 @@ public class StrolchElementToDomVisitor implements StrolchRootElementVisitor * Root path of the inspector @@ -128,7 +139,7 @@ public class Inspector { } } - return Response.ok().entity(agentOverview.toString()).build(); + return Response.ok().entity(toString(agentOverview)).build(); } catch (Exception e) { throw e; @@ -152,7 +163,7 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}") - public Response getRealm(@PathParam("realm") String realm, @Context HttpServletRequest request) { + public Response getRealm(@Context HttpServletRequest request, @PathParam("realm") String realm) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -199,7 +210,7 @@ public class Inspector { } } - return Response.ok().entity(realmDetailJ.toString()).build(); + return Response.ok().entity(toString(realmDetailJ)).build(); } /** @@ -218,7 +229,7 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/resources") - public Response getResourcesOverview(@PathParam("realm") String realm, @Context HttpServletRequest request) { + public Response getResourcesOverview(@Context HttpServletRequest request, @PathParam("realm") String realm) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -245,7 +256,7 @@ public class Inspector { }); } - return Response.ok().entity(mapOverview.toString()).build(); + return Response.ok().entity(toString(mapOverview)).build(); } /** @@ -264,7 +275,7 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/orders") - public Response getOrdersOverview(@PathParam("realm") String realm, @Context HttpServletRequest request) { + public Response getOrdersOverview(@Context HttpServletRequest request, @PathParam("realm") String realm) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -291,7 +302,7 @@ public class Inspector { }); } - return Response.ok().entity(mapOverview.toString()).build(); + return Response.ok().entity(toString(mapOverview)).build(); } /** @@ -312,7 +323,7 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/activities") - public Response getActivitiesOverview(@PathParam("realm") String realm, @Context HttpServletRequest request) { + public Response getActivitiesOverview(@Context HttpServletRequest request, @PathParam("realm") String realm) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -339,7 +350,7 @@ public class Inspector { }); } - return Response.ok().entity(mapOverview.toString()).build(); + return Response.ok().entity(toString(mapOverview)).build(); } // TODO for the get element type details, we should not simply query all objects, but rather find a solution to query only the id, name, type and date, state for the order @@ -363,8 +374,8 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/resources/{type}") - public Response queryResourcesByType(@BeanParam QueryData queryData, @PathParam("realm") String realm, - @PathParam("type") String type, @Context HttpServletRequest request) { + public Response queryResourcesByType(@Context HttpServletRequest request, @BeanParam QueryData queryData, + @PathParam("realm") String realm, @PathParam("type") String type) { queryData.initializeUnsetFields(); Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -393,9 +404,7 @@ public class Inspector { JsonObject root = RestfulHelper.toJson(queryData, dataSetSize, resources, toJsonVisitor); // marshall result - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String entity = gson.toJson(root); - return Response.ok(entity).build(); + return Response.ok(toString(root)).build(); } /** @@ -415,8 +424,8 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/orders/{type}") - public Response queryOrdersByType(@BeanParam QueryData queryData, @PathParam("realm") String realm, - @PathParam("type") String type, @Context HttpServletRequest request) { + public Response queryOrdersByType(@Context HttpServletRequest request, @BeanParam QueryData queryData, + @PathParam("realm") String realm, @PathParam("type") String type) { queryData.initializeUnsetFields(); Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -443,9 +452,7 @@ public class Inspector { JsonObject root = RestfulHelper.toJson(queryData, dataSetSize, orders, toJsonVisitor); // marshall result - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String entity = gson.toJson(root); - return Response.ok(entity).build(); + return Response.ok(toString(root)).build(); } @GET @@ -479,16 +486,14 @@ public class Inspector { JsonObject root = RestfulHelper.toJson(queryData, dataSetSize, activities, toJsonVisitor); // marshall result - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String entity = gson.toJson(root); - return Response.ok(entity).build(); + return Response.ok(toString(root)).build(); } @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/resources/{type}/{id}") - public Response getResourceAsJson(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, @Context HttpServletRequest request) { + public Response getResourceAsJson(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, @QueryParam("flat") String flat) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -501,12 +506,14 @@ public class Inspector { } StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor(); - return Response.ok().entity(resource.accept(visitor)).build(); + if (Boolean.parseBoolean(flat)) + visitor.flat(); + return Response.ok().entity(toString(resource.accept(visitor))).build(); } @GET @Produces(MediaType.APPLICATION_XML) - @Path("{realm}/Resource/{type}/{id}") + @Path("{realm}/resources/{type}/{id}") public Response getResourceAsXml(@PathParam("realm") String realm, @PathParam("type") String type, @PathParam("id") String id, @Context HttpServletRequest request) { @@ -528,8 +535,8 @@ public class Inspector { @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) @Path("{realm}/resources/{type}/{id}") - public Response updateResourceAsXml(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, String data, @Context HttpServletRequest request) { + public Response updateResourceAsXml(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, String data) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -567,11 +574,61 @@ public class Inspector { return Result.toResponse(result); } + @PUT + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("{realm}/resources/{type}/{id}") + public Response updateResourceAsJson(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, @QueryParam("flat") String flatS, String data) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + boolean flat = Boolean.parseBoolean(flatS); + + UpdateResourceService svc = new UpdateResourceService(); + UpdateResourceArg arg = new UpdateResourceArg(); + + // parse JSON string + JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); + Resource resource; + if (flat) { + + // parse from flat JSON + try (StrolchTransaction tx = openTx(cert, realm)) { + resource = tx.getResourceBy(type, id, true); + } + new FromFlatJsonVisitor().visit(resource, jsonObject); + + } else { + + // parse from complete JSON + ResourceFromJsonVisitor visitor = new ResourceFromJsonVisitor(); + resource = visitor.visit(jsonObject); + + // we are missing a version, so: + arg.refreshUnknownVersion = true; + } + + // prepare argument + arg.resource = resource; + arg.realm = realm; + + // do service + ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); + if (result.isOk()) { + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor(); + if (flat) + toJsonVisitor.flat(); + return Response.ok().entity(toString(resource.accept(toJsonVisitor))).build(); + } + + return Result.toResponse(result); + } + @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/orders/{type}/{id}") - public Response getOrderAsJson(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, @Context HttpServletRequest request) { + public Response getOrderAsJson(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, @QueryParam("flat") String flat) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -584,14 +641,16 @@ public class Inspector { } StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor(); - return Response.ok().entity(order.accept(visitor)).build(); + if (Boolean.parseBoolean(flat)) + visitor.flat(); + return Response.ok().entity(toString(order.accept(visitor))).build(); } @GET @Produces(MediaType.APPLICATION_XML) @Path("{realm}/orders/{type}/{id}") - public Response getOrderAsXml(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, @Context HttpServletRequest request) { + public Response getOrderAsXml(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -611,8 +670,8 @@ public class Inspector { @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) @Path("{realm}/orders/{type}/{id}") - public Response updateOrderAsXml(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, String data, @Context HttpServletRequest request) { + public Response updateOrderAsXml(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, String data) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -650,6 +709,56 @@ public class Inspector { return Result.toResponse(result); } + @PUT + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("{realm}/orders/{type}/{id}") + public Response updateOrderAsJson(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, @QueryParam("flat") String flatS, String data) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + boolean flat = Boolean.parseBoolean(flatS); + + UpdateOrderService svc = new UpdateOrderService(); + UpdateOrderArg arg = new UpdateOrderArg(); + + // parse JSON string + JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); + Order order; + if (flat) { + + // parse from flat JSON + try (StrolchTransaction tx = openTx(cert, realm)) { + order = tx.getOrderBy(type, id, true); + } + new FromFlatJsonVisitor().visit(order, jsonObject); + + } else { + + // parse from complete JSON + OrderFromJsonVisitor visitor = new OrderFromJsonVisitor(); + order = visitor.visit(jsonObject); + + // we are missing a version, so: + arg.refreshUnknownVersion = true; + } + + // prepare argument + arg.order = order; + arg.realm = realm; + + // do service + ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); + if (result.isOk()) { + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor(); + if (flat) + toJsonVisitor.flat(); + return Response.ok().entity(toString(order.accept(toJsonVisitor))).build(); + } + + return Result.toResponse(result); + } + /** *

* Activity inspector @@ -671,8 +780,8 @@ public class Inspector { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{realm}/activities/{type}/{id}") - public Response getActivityAsJson(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, @Context HttpServletRequest request) { + public Response getActivityAsJson(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, @QueryParam("flat") String flat) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -685,14 +794,16 @@ public class Inspector { } StrolchElementToJsonVisitor visitor = new StrolchElementToJsonVisitor(); - return Response.ok().entity(activity.accept(visitor)).build(); + if (Boolean.parseBoolean(flat)) + visitor.flat(); + return Response.ok().entity(toString(activity.accept(visitor))).build(); } @GET @Produces(MediaType.APPLICATION_XML) @Path("{realm}/activities/{type}/{id}") - public Response getActivityAsXml(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, @Context HttpServletRequest request) { + public Response getActivityAsXml(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -712,8 +823,8 @@ public class Inspector { @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) @Path("{realm}/activities/{type}/{id}") - public Response updateActivityAsXml(@PathParam("realm") String realm, @PathParam("type") String type, - @PathParam("id") String id, String data, @Context HttpServletRequest request) { + public Response updateActivityAsXml(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, String data) { Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); @@ -750,4 +861,54 @@ public class Inspector { return Result.toResponse(result); } + + @PUT + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("{realm}/activities/{type}/{id}") + public Response updateActivityAsJson(@Context HttpServletRequest request, @PathParam("realm") String realm, + @PathParam("type") String type, @PathParam("id") String id, @QueryParam("flat") String flatS, String data) { + + Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE); + boolean flat = Boolean.parseBoolean(flatS); + + UpdateActivityService svc = new UpdateActivityService(); + UpdateActivityArg arg = new UpdateActivityArg(); + + // parse JSON string + JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); + Activity activity; + if (flat) { + + // parse from flat JSON + try (StrolchTransaction tx = openTx(cert, realm)) { + activity = tx.getActivityBy(type, id, true); + } + new FromFlatJsonVisitor().visit(activity, jsonObject); + + } else { + + // parse from complete JSON + ActivityFromJsonVisitor visitor = new ActivityFromJsonVisitor(); + activity = visitor.visit(jsonObject); + + // we are missing a version, so: + arg.refreshUnknownVersion = true; + } + + // prepare argument + arg.activity = activity; + arg.realm = realm; + + // do service + ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg); + if (result.isOk()) { + StrolchElementToJsonVisitor toJsonVisitor = new StrolchElementToJsonVisitor(); + if (flat) + toJsonVisitor.flat(); + return Response.ok().entity(toString(activity.accept(toJsonVisitor))).build(); + } + + return Result.toResponse(result); + } } diff --git a/li.strolch.service/src/main/java/li/strolch/service/UpdateActivityService.java b/li.strolch.service/src/main/java/li/strolch/service/UpdateActivityService.java index 726acce43..71d04f436 100644 --- a/li.strolch.service/src/main/java/li/strolch/service/UpdateActivityService.java +++ b/li.strolch.service/src/main/java/li/strolch/service/UpdateActivityService.java @@ -45,6 +45,12 @@ public class UpdateActivityService extends AbstractService