dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #30551
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15512: add RenderServiceMessageConverter, uses renderService to render out objects (based on annotations...
------------------------------------------------------------
revno: 15512
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-06-02 09:45:44 +0200
message:
add RenderServiceMessageConverter, uses renderService to render out objects (based on annotations), don't support viewClasses, but useful for simplifying controllers where we just use JacksonUtils.toX or renderService.toX directly
added:
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/RenderServiceMessageConverter.java
modified:
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java
dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java 2014-06-02 07:17:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java 2014-06-02 07:45:44 +0000
@@ -28,19 +28,15 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import org.hisp.dhis.dxf2.utils.JacksonUtils;
import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.schema.Schemas;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
-
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -52,39 +48,15 @@
@Autowired
private SchemaService schemaService;
- @RequestMapping( value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
- public void getSchemasJson( HttpServletResponse response ) throws IOException
- {
- Schemas schemas = new Schemas( schemaService.getSchemas() );
-
- response.setContentType( MediaType.APPLICATION_JSON_VALUE );
- JacksonUtils.toJson( response.getOutputStream(), schemas );
- }
-
- @RequestMapping( value = "/{type}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
- public void getSchemaJson( @PathVariable String type, HttpServletResponse response ) throws IOException
- {
- Schema schema = schemaService.getSchemaBySingularName( type );
-
- response.setContentType( MediaType.APPLICATION_JSON_VALUE );
- JacksonUtils.toJson( response.getOutputStream(), schema );
- }
-
- @RequestMapping( value = "", method = RequestMethod.GET, produces = { MediaType.APPLICATION_XML_VALUE } )
- public void getSchemasXml( HttpServletResponse response ) throws IOException
- {
- Schemas schemas = new Schemas( schemaService.getSchemas() );
-
- response.setContentType( MediaType.APPLICATION_XML_VALUE );
- JacksonUtils.toXml( response.getOutputStream(), schemas );
- }
-
- @RequestMapping( value = "/{type}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_XML_VALUE } )
- public void getSchemaXml( @PathVariable String type, HttpServletResponse response ) throws IOException
- {
- Schema schema = schemaService.getSchemaBySingularName( type );
-
- response.setContentType( MediaType.APPLICATION_XML_VALUE );
- JacksonUtils.toXml( response.getOutputStream(), schema );
+ @RequestMapping
+ public @ResponseBody Schemas getSchemasJson()
+ {
+ return new Schemas( schemaService.getSchemas() );
+ }
+
+ @RequestMapping( value = "/{type}" )
+ public @ResponseBody Schema getSchemaJson( @PathVariable String type )
+ {
+ return schemaService.getSchemaBySingularName( type );
}
}
=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/RenderServiceMessageConverter.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/RenderServiceMessageConverter.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/RenderServiceMessageConverter.java 2014-06-02 07:45:44 +0000
@@ -0,0 +1,115 @@
+package org.hisp.dhis.webapi.utils;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
+ */
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
+ */
+
+import org.hisp.dhis.dxf2.render.RenderService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpInputMessage;
+import org.springframework.http.HttpOutputMessage;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.AbstractHttpMessageConverter;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.http.converter.HttpMessageNotWritableException;
+
+import java.io.IOException;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class RenderServiceMessageConverter extends AbstractHttpMessageConverter<Object>
+{
+ @Autowired
+ private RenderService renderService;
+
+ public RenderServiceMessageConverter( MediaType... supportedMediaTypes )
+ {
+ super( supportedMediaTypes );
+ }
+
+ @Override
+ protected boolean supports( Class<?> clazz )
+ {
+ return Object.class.isAssignableFrom( clazz );
+ }
+
+ @Override
+ protected boolean canRead( MediaType mediaType )
+ {
+ return false;
+ }
+
+ @Override
+ protected Object readInternal( Class<?> clazz, HttpInputMessage inputMessage ) throws IOException, HttpMessageNotReadableException
+ {
+ return null;
+ }
+
+ @Override
+ protected void writeInternal( Object object, HttpOutputMessage outputMessage ) throws IOException, HttpMessageNotWritableException
+ {
+ MediaType mediaType = outputMessage.getHeaders().getContentType();
+
+ if ( mediaType.getSubtype().equals( "json" ) )
+ {
+ renderService.toJson( outputMessage.getBody(), object );
+ }
+ else if ( mediaType.getSubtype().equals( "xml" ) )
+ {
+ renderService.toXml( outputMessage.getBody(), object );
+ }
+ }
+}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2014-06-02 07:17:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2014-06-02 07:45:44 +0000
@@ -22,9 +22,19 @@
</constructor-arg>
</bean>
+ <bean id="org.hisp.dhis.webapi.utils.RenderServiceMessageConverter" class="org.hisp.dhis.webapi.utils.RenderServiceMessageConverter">
+ <constructor-arg name="supportedMediaTypes">
+ <list>
+ <value>application/json</value>
+ <value>application/xml</value>
+ </list>
+ </constructor-arg>
+ </bean>
+
<mvc:annotation-driven>
- <mvc:message-converters>
+ <mvc:message-converters register-defaults="true">
<ref bean="org.hisp.dhis.webapi.utils.RootNodeMessageConverter" />
+ <ref bean="org.hisp.dhis.webapi.utils.RenderServiceMessageConverter" />
<bean class="org.hisp.dhis.api.mobile.support.DataStreamSerializableMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>