[New] Added inspector REST api for activities
This commit is contained in:
parent
90d9dead32
commit
753806f672
|
@ -78,6 +78,7 @@ public class StrolchElementToJsonVisitor {
|
|||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, Tags.Json.ACTIVITY);
|
||||
|
||||
rootJ.addProperty(Tags.Json.TIME_ORDERING, element.getTimeOrdering().getName());
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().getName());
|
||||
|
||||
toJson((AbstractStrolchElement) element, rootJ);
|
||||
|
||||
|
@ -113,13 +114,11 @@ public class StrolchElementToJsonVisitor {
|
|||
|
||||
rootJ.addProperty(Tags.Json.OBJECT_TYPE, Tags.Json.ACTION);
|
||||
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().getName());
|
||||
|
||||
// attributes
|
||||
toJson((AbstractStrolchElement) element, rootJ);
|
||||
rootJ.addProperty(Tags.Json.RESOURCE_ID, element.getResourceId());
|
||||
rootJ.addProperty(Tags.Json.RESOURCE_TYPE, element.getResourceType());
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().name());
|
||||
rootJ.addProperty(Tags.Json.STATE, element.getState().getName());
|
||||
|
||||
addParameterBags(element, rootJ);
|
||||
addPolicies(element, rootJ);
|
||||
|
@ -137,7 +136,8 @@ public class StrolchElementToJsonVisitor {
|
|||
JsonObject changeJ = new JsonObject();
|
||||
|
||||
changeJ.addProperty(Tags.Json.STATE_ID, valueChange.getStateId());
|
||||
changeJ.addProperty(Tags.Json.TIME, ISO8601FormatFactory.getInstance().formatDate(valueChange.getTime()));
|
||||
changeJ.addProperty(Tags.Json.TIME,
|
||||
ISO8601FormatFactory.getInstance().formatDate(valueChange.getTime()));
|
||||
changeJ.addProperty(Tags.Json.VALUE, valueChange.getValue().getValueAsString());
|
||||
changeJ.addProperty(Tags.Json.TYPE, valueChange.getValue().getType());
|
||||
|
||||
|
@ -262,7 +262,8 @@ public class StrolchElementToJsonVisitor {
|
|||
JsonObject versionJ = new JsonObject();
|
||||
versionJ.addProperty(Tags.Json.VERSION, version.getVersion());
|
||||
versionJ.addProperty(Tags.Json.CREATED_BY, version.getCreatedBy());
|
||||
versionJ.addProperty(Tags.Json.CREATED_AT, ISO8601FormatFactory.getInstance().formatDate(version.getCreatedAt()));
|
||||
versionJ.addProperty(Tags.Json.CREATED_AT,
|
||||
ISO8601FormatFactory.getInstance().formatDate(version.getCreatedAt()));
|
||||
versionJ.addProperty(Tags.Json.DELETED, version.isDeleted());
|
||||
rootJ.add(Tags.Json.VERSION, versionJ);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package li.strolch.model.xml;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
import javanet.staxutils.IndentingXMLStreamWriter;
|
||||
import li.strolch.model.StrolchModelConstants;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.visitor.ActivityVisitor;
|
||||
import li.strolch.utils.dbc.DBC;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class ActivityToXmlStringVisitor implements ActivityVisitor<String> {
|
||||
|
||||
@Override
|
||||
public String visit(Activity element) {
|
||||
DBC.PRE.assertNotNull("Activity my not be null!", element);
|
||||
try {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
XMLOutputFactory factory = XMLOutputFactory.newInstance();
|
||||
XMLStreamWriter writer = factory.createXMLStreamWriter(stringWriter);
|
||||
writer = new IndentingXMLStreamWriter(writer);
|
||||
|
||||
// start document
|
||||
writer.writeStartDocument(StrolchModelConstants.DEFAULT_ENCODING,
|
||||
StrolchModelConstants.DEFAULT_XML_VERSION);
|
||||
new ActivityToSaxWriterVisitor(writer).visit(element);
|
||||
writer.writeEndDocument();
|
||||
|
||||
return stringWriter.toString();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(
|
||||
"Failed to format Element " + element.getLocator() + " to xml string due to " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -50,8 +50,10 @@ import li.strolch.model.Order;
|
|||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.json.ActivityToJsonVisitor;
|
||||
import li.strolch.model.json.OrderToJsonVisitor;
|
||||
import li.strolch.model.json.ResourceToJsonVisitor;
|
||||
import li.strolch.model.xml.ActivityToXmlStringVisitor;
|
||||
import li.strolch.model.xml.OrderToXmlStringVisitor;
|
||||
import li.strolch.model.xml.ResourceToXmlStringVisitor;
|
||||
import li.strolch.model.xml.SimpleStrolchElementListener;
|
||||
|
@ -62,6 +64,8 @@ import li.strolch.privilege.model.Certificate;
|
|||
import li.strolch.rest.RestfulStrolchComponent;
|
||||
import li.strolch.rest.StrolchRestfulConstants;
|
||||
import li.strolch.rest.model.Result;
|
||||
import li.strolch.service.UpdateActivityService;
|
||||
import li.strolch.service.UpdateActivityService.UpdateActivityArg;
|
||||
import li.strolch.service.UpdateOrderService;
|
||||
import li.strolch.service.UpdateOrderService.UpdateOrderArg;
|
||||
import li.strolch.service.UpdateResourceService;
|
||||
|
@ -351,9 +355,6 @@ public class Inspector {
|
|||
*
|
||||
* @return an overview of the {@link Resource Resources} with the given type. This is a list of overviews of the
|
||||
* resources
|
||||
*
|
||||
* @see TypeDetail
|
||||
* @see StrolchElementOverview
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
|
@ -400,9 +401,6 @@ public class Inspector {
|
|||
* @param type
|
||||
*
|
||||
* @return an overview of the {@link Order Orders} with the given type. This is a list of overviews of the orders
|
||||
*
|
||||
* @see TypeDetail
|
||||
* @see StrolchElementOverview
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
|
@ -438,23 +436,6 @@ public class Inspector {
|
|||
return Response.ok().entity(typeDetailJ.toString()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Order type inspector
|
||||
* </p>
|
||||
* <p>
|
||||
* Returns an overview of the {@link Order Orders} with the given type. This is a list of overviews of the orders
|
||||
* </p>
|
||||
*
|
||||
* @param realm
|
||||
* the realm for which the order type overview is to be returned
|
||||
* @param type
|
||||
*
|
||||
* @return an overview of the {@link Order Orders} with the given type. This is a list of overviews of the orders
|
||||
*
|
||||
* @see TypeDetail
|
||||
* @see StrolchElementOverview
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("{realm}/activities/{type}")
|
||||
|
@ -489,26 +470,6 @@ public class Inspector {
|
|||
return Response.ok().entity(typeDetailJ.toString()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Resource inspector
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Returns the resource with the given id
|
||||
* </p>
|
||||
*
|
||||
* @param realm
|
||||
* the realm for which the resource is to be returned
|
||||
* @param type
|
||||
* the type of the resource
|
||||
* @param id
|
||||
* the id of the resource
|
||||
*
|
||||
* @return the resource with the given id
|
||||
*
|
||||
* @see ResourceDetail
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("{realm}/resources/{type}/{id}")
|
||||
|
@ -672,4 +633,104 @@ public class Inspector {
|
|||
|
||||
return Result.toResponse(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Activity inspector
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Returns the activity with the given id
|
||||
* </p>
|
||||
*
|
||||
* @param realm
|
||||
* the realm for which the activity is to be returned
|
||||
* @param type
|
||||
* the type of the activity
|
||||
* @param id
|
||||
* the id of the activity
|
||||
*
|
||||
* @return the activity with the given id
|
||||
*/
|
||||
@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) {
|
||||
|
||||
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
|
||||
|
||||
Activity activity;
|
||||
try (StrolchTransaction tx = openTx(cert, realm)) {
|
||||
activity = tx.getActivityMap().getBy(tx, type, id);
|
||||
}
|
||||
if (activity == null) {
|
||||
throw new StrolchException(MessageFormat.format("No Activity exists for {0}/{1}", type, id)); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
return Response.ok().entity(ActivityToJsonVisitor.toJsonString(activity)).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) {
|
||||
|
||||
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
|
||||
|
||||
Activity activity;
|
||||
try (StrolchTransaction tx = openTx(cert, realm)) {
|
||||
activity = tx.getActivityMap().getBy(tx, type, id);
|
||||
}
|
||||
if (activity == null) {
|
||||
throw new StrolchException(MessageFormat.format("No Activity exists for {0}/{1}", type, id)); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
String asXml = new ActivityToXmlStringVisitor().visit(activity);
|
||||
return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@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) {
|
||||
|
||||
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
|
||||
|
||||
Activity activity;
|
||||
try {
|
||||
SimpleStrolchElementListener listener = new SimpleStrolchElementListener();
|
||||
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
|
||||
parser.parse(new InputSource(new StringReader(data)), new XmlModelSaxReader(listener));
|
||||
|
||||
if (listener.getActivities().size() == 0)
|
||||
throw new StrolchPersistenceException(
|
||||
MessageFormat.format("No Activities parsed from xml value for {0} / {1}", id, type));
|
||||
if (listener.getActivities().size() > 1)
|
||||
throw new StrolchPersistenceException(
|
||||
MessageFormat.format("Multiple Activities parsed from xml value for {0} / {1}", id, type));
|
||||
|
||||
activity = listener.getActivities().get(0);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new StrolchPersistenceException(
|
||||
MessageFormat.format("Failed to extract Activities from xml value for {0} / {1}", id, type), e);
|
||||
}
|
||||
|
||||
UpdateActivityService svc = new UpdateActivityService();
|
||||
UpdateActivityArg arg = new UpdateActivityArg();
|
||||
arg.activity = activity;
|
||||
arg.realm = realm;
|
||||
|
||||
ServiceResult result = RestfulStrolchComponent.getInstance().getServiceHandler().doService(cert, svc, arg);
|
||||
if (result.isOk()) {
|
||||
String asXml = new ActivityToXmlStringVisitor().visit(activity);
|
||||
return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build();
|
||||
}
|
||||
|
||||
return Result.toResponse(result);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue