← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19599: more webMessage fixes

 

------------------------------------------------------------
revno: 19599
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-07-09 14:12:16 +0700
message:
  more webMessage fixes
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.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/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2015-07-08 05:42:56 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2015-07-09 07:12:16 +0000
@@ -165,14 +165,4 @@
         response.setContentType( CONTENT_TYPE_XML );
         JacksonUtils.toXml( response.getOutputStream(), summary );
     }
-
-    // -------------------------------------------------------------------------
-    // Supportive
-    // -------------------------------------------------------------------------
-
-    @ExceptionHandler( IllegalArgumentException.class )
-    public void handleError( IllegalArgumentException ex, HttpServletResponse response )
-    {
-        ContextUtils.conflictResponse( response, ex.getMessage() );
-    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java	2015-07-08 05:42:56 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java	2015-07-09 07:12:16 +0000
@@ -33,6 +33,7 @@
 import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.common.JacksonUtils;
 import org.hisp.dhis.dxf2.common.TranslateOptions;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.message.MessageService;
@@ -49,6 +50,7 @@
 import org.hisp.dhis.user.UserGroupService;
 import org.hisp.dhis.user.UserService;
 import org.hisp.dhis.webapi.utils.ContextUtils;
+import org.hisp.dhis.webapi.utils.WebMessageUtils;
 import org.hisp.dhis.webapi.webdomain.MessageConversation;
 import org.hisp.dhis.webapi.webdomain.WebMetaData;
 import org.hisp.dhis.webapi.webdomain.WebOptions;
@@ -172,7 +174,7 @@
         postObject( response, request, messageConversation );
     }
 
-    public void postObject( HttpServletResponse response, HttpServletRequest request, MessageConversation messageConversation )
+    public void postObject( HttpServletResponse response, HttpServletRequest request, MessageConversation messageConversation ) throws WebMessageException
     {
         List<User> users = new ArrayList<>( messageConversation.getUsers() );
         messageConversation.getUsers().clear();
@@ -183,8 +185,7 @@
 
             if ( organisationUnit == null )
             {
-                ContextUtils.conflictResponse( response, "Organisation Unit does not exist: " + ou.getUid() );
-                return;
+                throw new WebMessageException( WebMessageUtils.conflict( "Organisation Unit does not exist: " + ou.getUid() ) );
             }
 
             messageConversation.getUsers().addAll( organisationUnit.getUsers() );
@@ -196,8 +197,7 @@
 
             if ( user == null )
             {
-                ContextUtils.conflictResponse( response, "User does not exist: " + u.getUid() );
-                return;
+                throw new WebMessageException( WebMessageUtils.conflict( "User does not exist: " + u.getUid() ) );
             }
 
             messageConversation.getUsers().add( user );
@@ -209,8 +209,7 @@
 
             if ( userGroup == null )
             {
-                ContextUtils.conflictResponse( response, "User Group does not exist: " + ug.getUid() );
-                return;
+                throw new WebMessageException( WebMessageUtils.notFound( "User Group does not exist: " + ug.getUid() ) );
             }
 
             messageConversation.getUsers().addAll( userGroup.getMembers() );
@@ -218,8 +217,7 @@
 
         if ( messageConversation.getUsers().isEmpty() )
         {
-            ContextUtils.conflictResponse( response, "No recipients selected." );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "No recipients selected." ) );
         }
 
         String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
@@ -228,7 +226,8 @@
 
         org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation( id );
 
-        ContextUtils.createdResponse( response, "Message conversation created", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid() );
+        response.addHeader( "Location", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid() );
+        webMessageService.send( WebMessageUtils.created( "Message conversation created" ), response, request );
     }
 
     //--------------------------------------------------------------------------
@@ -245,13 +244,13 @@
 
         if ( conversation == null )
         {
-            ContextUtils.conflictResponse( response, "Message conversation does not exist: " + uid );
-            return;
+            throw new WebMessageException( WebMessageUtils.notFound( "Message conversation does not exist: " + uid ) );
         }
 
         messageService.sendReply( conversation, body, metaData );
 
-        ContextUtils.createdResponse( response, "Message conversation created", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid() );
+        response.addHeader( "Location", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid() );
+        webMessageService.send( WebMessageUtils.created( "Message conversation created" ), response, request );
     }
 
     //--------------------------------------------------------------------------
@@ -266,7 +265,7 @@
 
         messageService.sendFeedback( subject, body, metaData );
 
-        ContextUtils.createdResponse( response, "Feedback created", null );
+        webMessageService.send( WebMessageUtils.created( "Feedback created" ), response, request );
     }
 
     //--------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java	2015-07-08 04:02:09 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java	2015-07-09 07:12:16 +0000
@@ -46,6 +46,7 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.Set;
@@ -206,7 +207,7 @@
 
     @RequestMapping( value = "/{uid}/execute", method = RequestMethod.POST )
     public void executeView( @PathVariable( "uid" ) String uid, @RequestParam( required = false ) Set<String> var,
-        HttpServletResponse response ) throws WebMessageException
+        HttpServletResponse response, HttpServletRequest request ) throws WebMessageException
     {
         SqlView sqlView = sqlViewService.getSqlViewByUid( uid );
 
@@ -219,13 +220,12 @@
 
         if ( result != null )
         {
-            ContextUtils.conflictResponse( response, result );
+            throw new WebMessageException( WebMessageUtils.conflict( result ) );
         }
         else
         {
-            String location = SqlViewSchemaDescriptor.API_ENDPOINT + "/" + sqlView.getUid();
-
-            ContextUtils.createdResponse( response, "SQL view created", location );
+            response.addHeader( "Location", SqlViewSchemaDescriptor.API_ENDPOINT + "/" + sqlView.getUid() );
+            webMessageService.send( WebMessageUtils.created( "SQL view created" ), response, request );
         }
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java	2015-03-27 18:24:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java	2015-07-09 07:12:16 +0000
@@ -29,8 +29,11 @@
  */
 
 import org.hisp.dhis.dxf2.render.RenderService;
+import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.setting.SystemSettingManager;
+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.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -42,6 +45,7 @@
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.Serializable;
@@ -61,23 +65,26 @@
     @Autowired
     private RenderService renderService;
 
+    @Autowired
+    private WebMessageService webMessageService;
+
     @RequestMapping( value = "/{key}", method = RequestMethod.POST, consumes = { ContextUtils.CONTENT_TYPE_TEXT, ContextUtils.CONTENT_TYPE_HTML } )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_SYSTEM_SETTING')" )
     public void setSystemSetting(
         @PathVariable( value = "key" ) String key,
         @RequestParam( required = false, value = "key" ) String keyParam,
         @RequestParam( required = false ) String value,
-        @RequestBody( required = false ) String valuePayload, HttpServletResponse response )
+        @RequestBody( required = false ) String valuePayload,
+        HttpServletResponse response, HttpServletRequest request ) throws WebMessageException
     {
         if ( key == null && keyParam == null )
         {
-            ContextUtils.conflictResponse( response, "Key must be specified" );
-            return;
+            throw new WebMessageException( WebMessageUtils.conflict( "Key must be specified" ) );
         }
 
         if ( value == null && valuePayload == null )
         {
-            ContextUtils.conflictResponse( response, "Value must be specified as query param or as payload" );
+            throw new WebMessageException( WebMessageUtils.conflict( "Value must be specified as query param or as payload" ) );
         }
 
         value = value != null ? value : valuePayload;
@@ -86,19 +93,19 @@
 
         systemSettingManager.saveSystemSetting( key, value );
 
-        ContextUtils.okResponse( response, "System setting " + key + " set as value '" + value + "'." );
+        webMessageService.send( WebMessageUtils.ok( "System setting " + key + " set as value '" + value + "'." ), response, request );
     }
 
     @RequestMapping( method = RequestMethod.POST, consumes = { ContextUtils.CONTENT_TYPE_JSON } )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_SYSTEM_SETTING')" )
-    public void setSystemSetting( @RequestBody Map<String, Object> settings, HttpServletResponse response )
+    public void setSystemSetting( @RequestBody Map<String, Object> settings, HttpServletResponse response, HttpServletRequest request )
     {
         for ( String key : settings.keySet() )
         {
             systemSettingManager.saveSystemSetting( key, (Serializable) settings.get( key ) );
         }
 
-        ContextUtils.okResponse( response, "System settings imported" );
+        webMessageService.send( WebMessageUtils.ok( "System settings imported" ), response, request );
     }
 
     @RequestMapping( value = "/{key}", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_TEXT )