← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19622: Update WebMessage to use Spring HttpStatus instead of Servlet/Apache HttpStatus

 

------------------------------------------------------------
revno: 19622
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-07-13 10:59:57 +0700
message:
  Update WebMessage to use Spring HttpStatus instead of Servlet/Apache HttpStatus
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessage.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramIndicatorController.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
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessage.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessage.java	2015-06-08 08:45:54 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/webmessage/WebMessage.java	2015-07-13 03:59:57 +0000
@@ -62,12 +62,7 @@
     /**
      * HTTP status.
      */
-    protected String httpStatus = HttpStatus.OK.getReasonPhrase();
-
-    /**
-     * HTTP status code. Default value is 200.
-     */
-    protected Integer httpStatusCode = HttpStatus.OK.value();
+    protected HttpStatus httpStatus = HttpStatus.OK;
 
     /**
      * Non-technical message, should be simple and could possibly be used to display message
@@ -102,22 +97,27 @@
         this.status = status;
     }
 
-    public WebMessage( WebMessageStatus status, Integer httpStatusCode )
+    public WebMessage( WebMessageStatus status, HttpStatus httpStatus )
     {
         this.status = status;
-        setHttpStatusCode( httpStatusCode );
+        this.httpStatus = httpStatus;
     }
 
     // -------------------------------------------------------------------------
     // Logic
     // -------------------------------------------------------------------------     
 
-    public boolean okStatus()
+    public boolean isOk()
     {
         return WebMessageStatus.OK.equals( status );
     }
 
-    public boolean errorStatus()
+    public boolean isWarning()
+    {
+        return WebMessageStatus.WARNING.equals( status );
+    }
+
+    public boolean isError()
     {
         return WebMessageStatus.ERROR.equals( status );
     }
@@ -154,27 +154,19 @@
     @JacksonXmlProperty( isAttribute = true )
     public String getHttpStatus()
     {
-        return httpStatus;
+        return httpStatus.getReasonPhrase();
+    }
+
+    public void setHttpStatus( HttpStatus httpStatus )
+    {
+        this.httpStatus = httpStatus;
     }
 
     @JsonProperty
     @JacksonXmlProperty( isAttribute = true )
     public Integer getHttpStatusCode()
     {
-        return httpStatusCode;
-    }
-
-    public void setHttpStatusCode( Integer httpStatusCode )
-    {
-        try
-        {
-            this.httpStatus = HttpStatus.valueOf( httpStatusCode ).getReasonPhrase();
-        }
-        catch ( IllegalArgumentException ignored )
-        {
-        }
-
-        this.httpStatusCode = httpStatusCode;
+        return httpStatus.value();
     }
 
     @JsonProperty
@@ -219,7 +211,7 @@
         return MoreObjects.toStringHelper( this )
             .add( "status", status )
             .add( "code", code )
-            .add( "httpStatusCode", httpStatusCode )
+            .add( "httpStatus", httpStatus )
             .add( "message", message )
             .add( "devMessage", devMessage )
             .add( "response", response )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramIndicatorController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramIndicatorController.java	2015-06-05 11:19:27 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramIndicatorController.java	2015-07-13 03:59:57 +0000
@@ -28,10 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.IOException;
-
-import javax.servlet.http.HttpServletResponse;
-
 import org.hisp.dhis.dxf2.webmessage.DescriptiveWebMessage;
 import org.hisp.dhis.dxf2.webmessage.WebMessageStatus;
 import org.hisp.dhis.i18n.I18n;
@@ -47,6 +43,9 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
 @Controller
 @RequestMapping( value = ProgramIndicatorSchemaDescriptor.API_ENDPOINT )
 public class ProgramIndicatorController
@@ -63,18 +62,18 @@
         throws IOException
     {
         I18n i18n = i18nManager.getI18n();
-        
+
         String result = programIndicatorService.expressionIsValid( expression );
-        
+
         DescriptiveWebMessage message = new DescriptiveWebMessage();
         message.setStatus( ProgramIndicator.VALID.equals( result ) ? WebMessageStatus.OK : WebMessageStatus.ERROR );
         message.setMessage( i18n.getString( result ) );
-        
-        if ( message.okStatus() )
+
+        if ( message.isOk() )
         {
             message.setDescription( programIndicatorService.getExpressionDescription( expression ) );
         }
-        
+
         webMessageService.sendJson( message, response );
     }
 
@@ -83,18 +82,18 @@
         throws IOException
     {
         I18n i18n = i18nManager.getI18n();
-        
+
         String result = programIndicatorService.filterIsValid( expression );
-        
+
         DescriptiveWebMessage message = new DescriptiveWebMessage();
         message.setStatus( ProgramIndicator.VALID.equals( result ) ? WebMessageStatus.OK : WebMessageStatus.ERROR );
         message.setMessage( i18n.getString( result ) );
-        
-        if ( message.okStatus() )
+
+        if ( message.isOk() )
         {
             message.setDescription( programIndicatorService.getExpressionDescription( expression ) );
         }
-        
+
         webMessageService.sendJson( message, response );
     }
 }

=== 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-07-08 04:33:58 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java	2015-07-13 03:59:57 +0000
@@ -33,147 +33,135 @@
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
 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;
+import org.springframework.http.HttpStatus;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 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 );
+    public static WebMessage createWebMessage( String message, WebMessageStatus status, HttpStatus httpStatus )
+    {
+        WebMessage webMessage = new WebMessage( status, httpStatus );
+        webMessage.setMessage( message );
+
+        return webMessage;
+    }
+
+    public static WebMessage createWebMessage( String message, String devMessage, WebMessageStatus status, HttpStatus httpStatus )
+    {
+        WebMessage webMessage = new WebMessage( status, httpStatus );
+        webMessage.setMessage( message );
+        webMessage.setDevMessage( devMessage );
 
         return webMessage;
     }
 
     public static WebMessage ok( String message )
     {
-        return createWebMessage( message, WebMessageStatus.OK, HttpServletResponse.SC_OK );
+        return createWebMessage( message, WebMessageStatus.OK, HttpStatus.OK );
     }
 
     public static WebMessage ok( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.OK, HttpServletResponse.SC_OK );
+        return createWebMessage( message, devMessage, WebMessageStatus.OK, HttpStatus.OK );
     }
 
     public static WebMessage created( String message )
     {
-        return createWebMessage( message, WebMessageStatus.OK, HttpServletResponse.SC_CREATED );
+        return createWebMessage( message, WebMessageStatus.OK, HttpStatus.CREATED );
     }
 
     public static WebMessage created( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.OK, HttpServletResponse.SC_CREATED );
+        return createWebMessage( message, devMessage, WebMessageStatus.OK, HttpStatus.CREATED );
     }
 
     public static WebMessage notFound( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_NOT_FOUND );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.NOT_FOUND );
     }
 
     public static WebMessage notFound( Class<?> klass, String id )
     {
         String message = klass.getSimpleName() + " with id " + id + " could not be found.";
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_NOT_FOUND );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.NOT_FOUND );
     }
 
     public static WebMessage notFound( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_NOT_FOUND );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.NOT_FOUND );
     }
 
     public static WebMessage conflict( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_CONFLICT );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.CONFLICT );
     }
 
     public static WebMessage conflict( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_CONFLICT );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.CONFLICT );
     }
 
     public static WebMessage error( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.INTERNAL_SERVER_ERROR );
     }
 
     public static WebMessage error( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.INTERNAL_SERVER_ERROR );
     }
 
     public static WebMessage badRequest( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.BAD_REQUEST );
     }
 
     public static WebMessage badRequest( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.BAD_REQUEST );
     }
 
     public static WebMessage forbidden( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_FORBIDDEN );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.FORBIDDEN );
     }
 
     public static WebMessage forbidden( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_FORBIDDEN );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.FORBIDDEN );
     }
 
     public static WebMessage serviceUnavailable( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_SERVICE_UNAVAILABLE );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.SERVICE_UNAVAILABLE );
     }
 
     public static WebMessage serviceUnavailable( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_SERVICE_UNAVAILABLE );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.SERVICE_UNAVAILABLE );
     }
 
     public static WebMessage unprocessableEntity( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, 422 );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.UNPROCESSABLE_ENTITY );
     }
 
     public static WebMessage unprocessableEntity( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, 422 );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.UNPROCESSABLE_ENTITY );
     }
 
     public static WebMessage unathorized( String message )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_UNAUTHORIZED );
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpStatus.UNAUTHORIZED );
     }
 
     public static WebMessage unathorized( String message, String devMessage )
     {
-        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_UNAUTHORIZED );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpStatus.UNAUTHORIZED );
     }
 
     public static WebMessage importTypeSummary( ImportTypeSummary importTypeSummary )
@@ -184,19 +172,19 @@
         {
             webMessage.setMessage( "An error occurred, please check import summary." );
             webMessage.setStatus( WebMessageStatus.ERROR );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_CONFLICT );
+            webMessage.setHttpStatus( HttpStatus.CONFLICT );
         }
         else if ( !importTypeSummary.getConflicts().isEmpty() )
         {
             webMessage.setMessage( "One more conflicts encountered, please check import summary." );
             webMessage.setStatus( WebMessageStatus.WARNING );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_CONFLICT );
+            webMessage.setHttpStatus( HttpStatus.CONFLICT );
         }
         else
         {
             webMessage.setMessage( "Import was successful." );
             webMessage.setStatus( WebMessageStatus.OK );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_OK );
+            webMessage.setHttpStatus( HttpStatus.OK );
         }
 
         webMessage.setResponse( importTypeSummary );
@@ -212,19 +200,19 @@
         {
             webMessage.setMessage( "An error occurred, please check import summary." );
             webMessage.setStatus( WebMessageStatus.ERROR );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_CONFLICT );
+            webMessage.setHttpStatus( HttpStatus.CONFLICT );
         }
         else if ( !importSummary.getConflicts().isEmpty() )
         {
             webMessage.setMessage( "One more conflicts encountered, please check import summary." );
             webMessage.setStatus( WebMessageStatus.WARNING );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_CONFLICT );
+            webMessage.setHttpStatus( HttpStatus.CONFLICT );
         }
         else
         {
             webMessage.setMessage( "Import was successful." );
             webMessage.setStatus( WebMessageStatus.OK );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_OK );
+            webMessage.setHttpStatus( HttpStatus.OK );
         }
 
         webMessage.setResponse( importSummary );
@@ -240,13 +228,13 @@
         {
             webMessage.setMessage( "One more conflicts encountered, please check import summary." );
             webMessage.setStatus( WebMessageStatus.WARNING );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_CONFLICT );
+            webMessage.setHttpStatus( HttpStatus.CONFLICT );
         }
         else
         {
             webMessage.setMessage( "Import was successful." );
             webMessage.setStatus( WebMessageStatus.OK );
-            webMessage.setHttpStatusCode( HttpServletResponse.SC_OK );
+            webMessage.setHttpStatus( HttpStatus.OK );
         }
 
         webMessage.setResponse( importSummaries );