[New] Added rest GET of Order and Resource as XML

This commit is contained in:
Robert von Burg 2015-04-19 16:13:26 +02:00
parent 7871a33029
commit 69c842034a
7 changed files with 166 additions and 12 deletions

@ -1 +1 @@
Subproject commit ca8f5b2f6a3e737de85914359085be3761bc67c3
Subproject commit 556981777c129ea17cd4acc7d13e5e8162498ac6

@ -1 +1 @@
Subproject commit 2371cd78533b34484b050fd01fbfe2044bd0c94c
Subproject commit f72ff76b406f2675cf194174aa35872d0c969ab6

View File

@ -35,8 +35,8 @@ public class StrolchConstants {
public static final String PROP_REALM = "realm";
public static final String DEFAULT_REALM = "defaultRealm";
public static final String DEFAULT_XML_VERSION = "1.0"; //$NON-NLS-1$
public static final String DEFAULT_ENCODING = "utf-8"; //$NON-NLS-1$
public static final String DEFAULT_XML_VERSION = StrolchModelConstants.DEFAULT_XML_VERSION;
public static final String DEFAULT_ENCODING = StrolchModelConstants.DEFAULT_ENCODING;
/**
* @see StrolchModelConstants#TEMPLATE

View File

@ -4,6 +4,10 @@ import li.strolch.model.parameter.Parameter;
public class StrolchModelConstants {
public static final String DEFAULT_XML_VERSION = "1.0"; //$NON-NLS-1$
public static final String DEFAULT_ENCODING = "utf-8"; //$NON-NLS-1$
/**
* The type to set on {@link StrolchRootElement StrolchRootElements} when defining a template for a type of element
*/

View File

@ -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 javanet.staxutils.IndentingXMLStreamWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.StrolchModelConstants;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderToXmlStringVisitor implements OrderVisitor<String> {
@Override
public String visit(Order element) {
DBC.PRE.assertNotNull("Order 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 OrderToSaxWriterVisitor(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);
}
}
}

View File

@ -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 javanet.staxutils.IndentingXMLStreamWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.StrolchModelConstants;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ResourceToXmlStringVisitor implements ResourceVisitor<String> {
@Override
public String visit(Resource element) {
DBC.PRE.assertNotNull("Resource 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 ResourceToSaxWriterVisitor(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);
}
}
}

View File

@ -37,6 +37,8 @@ import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.xml.OrderToXmlStringVisitor;
import li.strolch.model.xml.ResourceToXmlStringVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.rest.RestfulStrolchComponent;
import li.strolch.rest.StrolchRestfulConstants;
@ -172,7 +174,7 @@ public class Inspector {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{realm}/resource")
@Path("{realm}/Resource")
public Response getResourcesOverview(@PathParam("realm") String realm, @Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -215,7 +217,7 @@ public class Inspector {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{realm}/order")
@Path("{realm}/Order")
public Response getOrdersOverview(@PathParam("realm") String realm, @Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -264,7 +266,7 @@ public class Inspector {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{realm}/resource/{type}")
@Path("{realm}/Resource/{type}")
public Response getResourceTypeDetails(@PathParam("realm") String realm, @PathParam("type") String type,
@Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -305,7 +307,7 @@ public class Inspector {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{realm}/order/{type}")
@Path("{realm}/Order/{type}")
public Response getOrderTypeDetails(@PathParam("realm") String realm, @PathParam("type") String type,
@Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -349,8 +351,8 @@ public class Inspector {
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{realm}/resource/{type}/{id}")
public Response getResource(@PathParam("realm") String realm, @PathParam("type") String type,
@Path("{realm}/Resource/{type}/{id}")
public Response getResourceAsJson(@PathParam("realm") String realm, @PathParam("type") String type,
@PathParam("id") String id, @Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -369,10 +371,29 @@ public class Inspector {
return Response.ok().entity(entity).build();
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{realm}/Resource/{type}/{id}")
public Response getResourceAsXml(@PathParam("realm") String realm, @PathParam("type") String type,
@PathParam("id") String id, @Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
Resource resource;
try (StrolchTransaction tx = openTx(cert, realm)) {
resource = tx.getResourceMap().getBy(tx, type, id);
}
if (resource == null) {
throw new StrolchException(MessageFormat.format("No Resource exists for {0}/{1}", type, id)); //$NON-NLS-1$
}
String asXml = new ResourceToXmlStringVisitor().visit(resource);
return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{realm}/order/{type}/{id}")
public Response getOrder(@PathParam("realm") String realm, @PathParam("type") String type,
@Path("{realm}/Order/{type}/{id}")
public Response getOrderAsJson(@PathParam("realm") String realm, @PathParam("type") String type,
@PathParam("id") String id, @Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
@ -390,4 +411,23 @@ public class Inspector {
};
return Response.ok().entity(entity).build();
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{realm}/Order/{type}/{id}")
public Response getOrderAsXml(@PathParam("realm") String realm, @PathParam("type") String type,
@PathParam("id") String id, @Context HttpServletRequest request) {
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
Order order;
try (StrolchTransaction tx = openTx(cert, realm)) {
order = tx.getOrderMap().getBy(tx, type, id);
}
if (order == null) {
throw new StrolchException(MessageFormat.format("No Order exists for {0}/{1}", type, id)); //$NON-NLS-1$
}
String asXml = new OrderToXmlStringVisitor().visit(order);
return Response.ok().type(MediaType.APPLICATION_XML).entity(asXml).build();
}
}