dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #40671
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20665: use webmessage for validation violations
------------------------------------------------------------
revno: 20665
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-10-13 15:24:58 +0700
message:
use webmessage for validation violations
added:
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/responses/ValidationViolationsWebMessageResponse.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/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java
--
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
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/responses/ValidationViolationsWebMessageResponse.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/responses/ValidationViolationsWebMessageResponse.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/responses/ValidationViolationsWebMessageResponse.java 2015-10-13 08:24:58 +0000
@@ -0,0 +1,66 @@
+package org.hisp.dhis.dxf2.webmessage.responses;
+
+/*
+ * Copyright (c) 2004-2015, 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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.dxf2.schema.ValidationViolation;
+import org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class ValidationViolationsWebMessageResponse
+ extends AbstractWebMessageResponse
+{
+ private List<ValidationViolation> validationViolations = new ArrayList<>();
+
+ public ValidationViolationsWebMessageResponse( List<ValidationViolation> validationViolations )
+ {
+ this.validationViolations = validationViolations;
+ }
+
+ @JsonProperty
+ @JacksonXmlElementWrapper( localName = "validationViolations", namespace = DxfNamespaces.DXF_2_0, useWrapping = false )
+ @JacksonXmlProperty( localName = "validationViolation", namespace = DxfNamespaces.DXF_2_0 )
+ public List<ValidationViolation> getValidationViolations()
+ {
+ return validationViolations;
+ }
+
+ public void setValidationViolations( List<ValidationViolation> validationViolations )
+ {
+ this.validationViolations = validationViolations;
+ }
+}
=== 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 2015-09-14 09:13:10 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java 2015-10-13 08:24:58 +0000
@@ -29,11 +29,11 @@
*/
import com.google.common.collect.Lists;
-import org.hisp.dhis.fieldfilter.FieldFilterService;
import org.hisp.dhis.dxf2.render.RenderService;
import org.hisp.dhis.dxf2.schema.SchemaValidator;
import org.hisp.dhis.dxf2.schema.ValidationViolation;
-import org.hisp.dhis.dxf2.schema.ValidationViolations;
+import org.hisp.dhis.dxf2.webmessage.WebMessage;
+import org.hisp.dhis.fieldfilter.FieldFilterService;
import org.hisp.dhis.node.NodeUtils;
import org.hisp.dhis.node.types.CollectionNode;
import org.hisp.dhis.node.types.RootNode;
@@ -43,9 +43,10 @@
import org.hisp.dhis.schema.Schemas;
import org.hisp.dhis.webapi.service.ContextService;
import org.hisp.dhis.webapi.service.LinkService;
+import org.hisp.dhis.webapi.service.WebMessageService;
+import org.hisp.dhis.webapi.utils.WebMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -82,7 +83,10 @@
private FieldFilterService fieldFilterService;
@Autowired
- protected ContextService contextService;
+ private ContextService contextService;
+
+ @Autowired
+ private WebMessageService webMessageService;
@RequestMapping
public @ResponseBody RootNode getSchemas()
@@ -128,7 +132,7 @@
throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );
}
- @RequestMapping( value = "/{type}", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_JSON_VALUE )
+ @RequestMapping( value = "/{type}", method = { RequestMethod.POST, RequestMethod.PUT } )
public void validateSchemaJson( @PathVariable String type, HttpServletRequest request, HttpServletResponse response ) throws IOException
{
Schema schema = getSchemaFromType( type );
@@ -141,23 +145,8 @@
Object object = renderService.fromJson( request.getInputStream(), schema.getKlass() );
List<ValidationViolation> validationViolations = schemaValidator.validate( object );
- renderService.toJson( response.getOutputStream(), validationViolations );
- }
-
- @RequestMapping( value = "/{type}", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_XML_VALUE )
- public void validateSchemaXml( @PathVariable String type, HttpServletRequest request, HttpServletResponse response ) throws IOException
- {
- Schema schema = getSchemaFromType( type );
-
- if ( schema == null )
- {
- throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );
- }
-
- Object object = renderService.fromXml( request.getInputStream(), schema.getKlass() );
- List<ValidationViolation> validationViolations = schemaValidator.validate( object );
-
- renderService.toXml( response.getOutputStream(), new ValidationViolations( validationViolations ) );
+ WebMessage webMessage = WebMessageUtils.validationViolations( validationViolations );
+ webMessageService.send( webMessage, response, request );
}
@RequestMapping( value = "/{type}/{property}", method = RequestMethod.GET )
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java 2015-09-02 15:45:47 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java 2015-10-13 08:24:58 +0000
@@ -32,10 +32,14 @@
import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
import org.hisp.dhis.dxf2.importsummary.ImportSummary;
import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
+import org.hisp.dhis.dxf2.schema.ValidationViolation;
import org.hisp.dhis.dxf2.webmessage.WebMessage;
import org.hisp.dhis.dxf2.webmessage.WebMessageStatus;
+import org.hisp.dhis.dxf2.webmessage.responses.ValidationViolationsWebMessageResponse;
import org.springframework.http.HttpStatus;
+import java.util.List;
+
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
*/
@@ -242,6 +246,20 @@
return webMessage;
}
+ public static WebMessage validationViolations( List<ValidationViolation> validationViolations )
+ {
+ WebMessage webMessage = new WebMessage();
+ webMessage.setResponse( new ValidationViolationsWebMessageResponse( validationViolations ) );
+
+ if ( !validationViolations.isEmpty() )
+ {
+ webMessage.setStatus( WebMessageStatus.ERROR );
+ webMessage.setHttpStatus( HttpStatus.BAD_REQUEST );
+ }
+
+ return webMessage;
+ }
+
private WebMessageUtils()
{
}