=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessageException.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessageException.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessageException.java 2015-06-05 10:33:56 +0000 @@ -0,0 +1,47 @@ +package org.hisp.dhis.dxf2.webmessage; + +/* + * 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. + */ + +/** + * @author Morten Olav Hansen + */ +public class WebMessageException extends Exception +{ + private WebMessage webMessage; + + public WebMessageException( WebMessage webMessage ) + { + this.webMessage = webMessage; + } + + public WebMessage getWebMessage() + { + return webMessage; + } +} === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CrudControllerAdvice.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CrudControllerAdvice.java 2015-04-28 06:52:28 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CrudControllerAdvice.java 2015-06-05 10:33:56 +0000 @@ -32,10 +32,13 @@ import org.hisp.dhis.common.IllegalQueryException; import org.hisp.dhis.common.MaintenanceModeException; import org.hisp.dhis.dataapproval.exceptions.DataApprovalException; +import org.hisp.dhis.dxf2.webmessage.WebMessageException; import org.hisp.dhis.system.util.DateUtils; import org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException; import org.hisp.dhis.webapi.controller.exception.NotFoundException; +import org.hisp.dhis.webapi.service.WebMessageService; import org.hisp.dhis.webapi.utils.ContextUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -48,6 +51,7 @@ import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.HttpStatusCodeException; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.ConstraintViolationException; import java.beans.PropertyEditorSupport; @@ -59,6 +63,9 @@ @ControllerAdvice public class CrudControllerAdvice { + @Autowired + private WebMessageService webMessageService; + @InitBinder protected void initBinder( WebDataBinder binder ) { @@ -126,6 +133,12 @@ ContextUtils.conflictResponse( response, ex.getClass().getName() ); //TODO fix message } + @ExceptionHandler( WebMessageException.class ) + public void webMessageExceptionHandler( WebMessageException ex, HttpServletResponse response, HttpServletRequest request ) + { + webMessageService.send( ex.getWebMessage(), response, request ); + } + private HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); @@ -134,4 +147,3 @@ return headers; } } - === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/WebMessageService.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/WebMessageService.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/service/WebMessageService.java 2015-06-05 10:33:56 +0000 @@ -0,0 +1,115 @@ +package org.hisp.dhis.webapi.service; + +/* + * 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 org.hisp.dhis.dxf2.render.RenderService; +import org.hisp.dhis.dxf2.webmessage.WebMessage; +import org.hisp.dhis.dxf2.webmessage.WebMessageResponse; +import org.hisp.dhis.dxf2.webmessage.WebMessageStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * WebMessage service methods. + * + * @author Morten Olav Hansen + */ +@Service +public class WebMessageService +{ + @Autowired + private RenderService renderService; + + public void sendJson( WebMessage message, HttpServletResponse response ) + { + response.setContentType( MediaType.APPLICATION_JSON_VALUE ); + response.setStatus( message.getHttpStatusCode() ); + + try + { + renderService.toJson( response.getOutputStream(), message ); + } + catch ( IOException ex ) + { + ex.printStackTrace(); + } + } + + public void sendXml( WebMessage message, HttpServletResponse response ) + { + response.setContentType( MediaType.APPLICATION_XML_VALUE ); + response.setStatus( message.getHttpStatusCode() ); + + try + { + renderService.toXml( response.getOutputStream(), message ); + } + catch ( IOException ex ) + { + ex.printStackTrace(); + } + } + + public void send( WebMessage webMessage, HttpServletResponse response, HttpServletRequest request ) + { + String type = request.getHeader( "Accept" ); + type = !StringUtils.isEmpty( type ) ? type : request.getContentType(); + type = !StringUtils.isEmpty( type ) ? type : MediaType.APPLICATION_JSON_VALUE; + + // allow type to be overridden by path extension + if ( request.getPathInfo().endsWith( ".json" ) ) + { + type = MediaType.APPLICATION_JSON_VALUE; + } + else if ( request.getPathInfo().endsWith( ".xml" ) ) + { + type = MediaType.APPLICATION_XML_VALUE; + } + + if ( isCompatibleWith( type, MediaType.APPLICATION_JSON ) ) + { + sendJson( webMessage, response ); + } + else if ( isCompatibleWith( type, MediaType.APPLICATION_XML ) ) + { + sendXml( webMessage, response ); + } + } + + private boolean isCompatibleWith( String type, MediaType mediaType ) + { + return !StringUtils.isEmpty( type ) && MediaType.parseMediaType( type ).isCompatibleWith( mediaType ); + } +} === added 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 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java 2015-06-05 10:33:56 +0000 @@ -0,0 +1,132 @@ +package org.hisp.dhis.webapi.utils; + +/* + * 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 org.hisp.dhis.dxf2.webmessage.WebMessage; +import org.hisp.dhis.dxf2.webmessage.WebMessageResponse; +import org.hisp.dhis.dxf2.webmessage.WebMessageStatus; + +import javax.servlet.http.HttpServletResponse; + +/** + * @author Morten Olav Hansen + */ +public final class WebMessageUtils +{ + public static WebMessage createWebMessage( String message, WebMessageStatus status, int httpStatus ) + { + WebMessage webMessage = new WebMessage( status, httpStatus ); + webMessage.setMessage( message ); + + return webMessage; + } + + public static WebMessage createWebMessage( String message, String devMessage, WebMessageStatus status, int httpStatus ) + { + WebMessage webMessage = new WebMessage( status, httpStatus ); + webMessage.setMessage( message ); + webMessage.setDevMessage( devMessage ); + + return webMessage; + } + + public static WebMessage createWebMessage( String message, String devMessage, WebMessageStatus status, int httpStatus, WebMessageResponse webMessageResponse ) + { + WebMessage webMessage = new WebMessage( status, httpStatus ); + webMessage.setMessage( message ); + webMessage.setDevMessage( devMessage ); + webMessage.setResponse( webMessageResponse ); + + return webMessage; + } + + public static WebMessage ok( String message ) + { + return createWebMessage( message, WebMessageStatus.OK, HttpServletResponse.SC_OK ); + } + + public static WebMessage ok( String message, String devMessage ) + { + return createWebMessage( message, devMessage, WebMessageStatus.OK, HttpServletResponse.SC_OK ); + } + + public static WebMessage created( String message ) + { + return createWebMessage( message, WebMessageStatus.OK, HttpServletResponse.SC_CREATED ); + } + + public static WebMessage created( String message, String devMessage ) + { + return createWebMessage( message, devMessage, WebMessageStatus.OK, HttpServletResponse.SC_CREATED ); + } + + public static WebMessage notFound( String message ) + { + return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_NOT_FOUND ); + } + + public static WebMessage notFound( String message, String devMessage ) + { + return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_NOT_FOUND ); + } + + public static WebMessage conflict( String message ) + { + return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_CONFLICT ); + } + + public static WebMessage conflict( String message, String devMessage ) + { + return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_CONFLICT ); + } + + public static WebMessage error( String message ) + { + return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); + } + + public static WebMessage error( String message, String devMessage ) + { + return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR ); + } + + public static WebMessage badRequest( String message ) + { + return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST ); + } + + public static WebMessage badRequest( String message, String devMessage ) + { + return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST ); + } + + private WebMessageUtils() + { + } +}