← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19570: WebMessage additions, handles IllegalQueryException etc

 

------------------------------------------------------------
revno: 19570
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-07-08 10:52:20 +0700
message:
  WebMessage additions, handles IllegalQueryException etc
modified:
  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/event/TrackedEntityInstanceController.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-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-06-11 02:55:55 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CrudControllerAdvice.java	2015-07-08 03:52:20 +0000
@@ -37,21 +37,15 @@
 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.hisp.dhis.webapi.utils.WebMessageUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
 import org.springframework.security.access.AccessDeniedException;
 import org.springframework.web.bind.WebDataBinder;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.InitBinder;
-import org.springframework.web.client.HttpClientErrorException;
-import org.springframework.web.client.HttpServerErrorException;
-import org.springframework.web.client.HttpStatusCodeException;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -81,58 +75,52 @@
         } );
     }
 
-    @ExceptionHandler
-    public ResponseEntity<String> notAuthenticatedExceptionHandler( NotAuthenticatedException ex )
+    @ExceptionHandler( { NotAuthenticatedException.class } )
+    public void notAuthenticatedExceptionHandler( NotAuthenticatedException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.UNAUTHORIZED );
+        webMessageService.send( WebMessageUtils.unathorized( ex.getClass().getName() ), response, request );
     }
 
     @ExceptionHandler( { NotFoundException.class } )
-    public ResponseEntity<String> notFoundExceptionHandler( Exception ex )
-    {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.NOT_FOUND );
-    }
-
-    @ExceptionHandler( { HttpClientErrorException.class, HttpServerErrorException.class } )
-    public ResponseEntity<String> httpClient( HttpStatusCodeException ex )
-    {
-        return new ResponseEntity<>( ex.getStatusText(), getHeaders(), ex.getStatusCode() );
+    public void notFoundExceptionHandler( NotFoundException ex, HttpServletResponse response, HttpServletRequest request )
+    {
+        webMessageService.send( WebMessageUtils.notFound( ex.getClass().getName() ), response, request );
     }
 
     @ExceptionHandler( ConstraintViolationException.class )
-    public ResponseEntity<String> constraintViolationExceptionHandler( ConstraintViolationException ex )
+    public void constraintViolationExceptionHandler( ConstraintViolationException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.UNPROCESSABLE_ENTITY );
+        webMessageService.send( WebMessageUtils.unprocessableEntity( ex.getClass().getName() ), response, request );
     }
 
     @ExceptionHandler( DeleteNotAllowedException.class )
-    public ResponseEntity<String> deleteNotAllowedExceptionHandler( DeleteNotAllowedException ex )
+    public void deleteNotAllowedExceptionHandler( DeleteNotAllowedException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.CONFLICT );
+        webMessageService.send( WebMessageUtils.conflict( ex.getMessage() ), response, request );
     }
 
     @ExceptionHandler( IllegalQueryException.class )
-    public ResponseEntity<String> illegalQueryExceptionHandler( IllegalQueryException ex )
+    public void illegalQueryExceptionHandler( IllegalQueryException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.CONFLICT );
+        webMessageService.send( WebMessageUtils.conflict( ex.getMessage() ), response, request );
     }
 
     @ExceptionHandler( IllegalArgumentException.class )
-    public ResponseEntity<String> illegalArgumentExceptionHandler( IllegalArgumentException ex )
+    public void illegalArgumentExceptionHandler( IllegalArgumentException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.CONFLICT );
+        webMessageService.send( WebMessageUtils.conflict( ex.getMessage() ), response, request );
     }
 
     @ExceptionHandler( MaintenanceModeException.class )
-    public ResponseEntity<String> maintenanceModeExceptionHandler( MaintenanceModeException ex )
+    public void maintenanceModeExceptionHandler( MaintenanceModeException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        return new ResponseEntity<>( ex.getMessage(), getHeaders(), HttpStatus.SERVICE_UNAVAILABLE );
+        webMessageService.send( WebMessageUtils.serviceUnavailable( ex.getClass().getName() ), response, request );
     }
 
     @ExceptionHandler( DataApprovalException.class )
-    public void dataApprovalExceptionHandler( DataApprovalException ex, HttpServletResponse response )
+    public void dataApprovalExceptionHandler( DataApprovalException ex, HttpServletResponse response, HttpServletRequest request )
     {
-        ContextUtils.conflictResponse( response, ex.getClass().getName() ); //TODO fix message
+        webMessageService.send( WebMessageUtils.conflict( ex.getClass().getName() ), response, request ); //TODO fix message
     }
 
     @ExceptionHandler( AccessDeniedException.class )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java	2015-07-07 04:23:37 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/TrackedEntityInstanceController.java	2015-07-08 03:52:20 +0000
@@ -442,14 +442,4 @@
     {
         return ContextUtils.getContextPath( request ) + "/api/" + "trackedEntityInstances" + "/" + importSummary.getReference();
     }
-
-    // -------------------------------------------------------------------------
-    // Exception handling
-    // -------------------------------------------------------------------------
-
-    @ExceptionHandler( IllegalQueryException.class )
-    public void handleError( IllegalQueryException ex, HttpServletResponse response )
-    {
-        ContextUtils.conflictResponse( response, ex.getMessage() );
-    }
 }

=== 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-06-11 02:55:55 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/WebMessageUtils.java	2015-07-08 03:52:20 +0000
@@ -139,7 +139,37 @@
 
     public static WebMessage forbidden( String message, String devMessage )
     {
-        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_FORBIDDEN );
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_FORBIDDEN );
+    }
+
+    public static WebMessage serviceUnavailable( String message )
+    {
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_SERVICE_UNAVAILABLE );
+    }
+
+    public static WebMessage serviceUnavailable( String message, String devMessage )
+    {
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_SERVICE_UNAVAILABLE );
+    }
+
+    public static WebMessage unprocessableEntity( String message )
+    {
+        return createWebMessage( message, WebMessageStatus.ERROR, 422 );
+    }
+
+    public static WebMessage unprocessableEntity( String message, String devMessage )
+    {
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, 422 );
+    }
+
+    public static WebMessage unathorized( String message )
+    {
+        return createWebMessage( message, WebMessageStatus.ERROR, HttpServletResponse.SC_UNAUTHORIZED );
+    }
+
+    public static WebMessage unathorized( String message, String devMessage )
+    {
+        return createWebMessage( message, devMessage, WebMessageStatus.ERROR, HttpServletResponse.SC_UNAUTHORIZED );
     }
 
     private WebMessageUtils()