diff --git a/ch.eitchnet.utils b/ch.eitchnet.utils index ca8f5b2f6..556981777 160000 --- a/ch.eitchnet.utils +++ b/ch.eitchnet.utils @@ -1 +1 @@ -Subproject commit ca8f5b2f6a3e737de85914359085be3761bc67c3 +Subproject commit 556981777c129ea17cd4acc7d13e5e8162498ac6 diff --git a/ch.eitchnet.xmlpers b/ch.eitchnet.xmlpers index 2371cd785..f72ff76b4 160000 --- a/ch.eitchnet.xmlpers +++ b/ch.eitchnet.xmlpers @@ -1 +1 @@ -Subproject commit 2371cd78533b34484b050fd01fbfe2044bd0c94c +Subproject commit f72ff76b406f2675cf194174aa35872d0c969ab6 diff --git a/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java b/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java index f6564cbe0..d6ad8a2be 100644 --- a/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java +++ b/li.strolch.agent/src/main/java/li/strolch/runtime/StrolchConstants.java @@ -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 diff --git a/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java b/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java index 5b9caedf2..ea67d4870 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java +++ b/li.strolch.model/src/main/java/li/strolch/model/StrolchModelConstants.java @@ -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 */ diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToXmlStringVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToXmlStringVisitor.java new file mode 100644 index 000000000..bd4c5d1f7 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/OrderToXmlStringVisitor.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class OrderToXmlStringVisitor implements OrderVisitor { + + @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); + } + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToXmlStringVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToXmlStringVisitor.java new file mode 100644 index 000000000..c176cd34b --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/ResourceToXmlStringVisitor.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class ResourceToXmlStringVisitor implements ResourceVisitor { + + @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); + } + } +} diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java index 99a56c64b..dcc9d1024 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/endpoint/Inspector.java @@ -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(); + } }