← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8680: Web api, always returning a descriptive message, location header and 201 status code when creatin...

 

------------------------------------------------------------
revno: 8680
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-10-24 10:31:40 +0200
message:
  Web api, always returning a descriptive message, location header and 201 status code when creating new objects
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/InterpretationController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SqlViewController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.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/api/controller/InterpretationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/InterpretationController.java	2012-10-17 17:04:50 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/InterpretationController.java	2012-10-24 08:31:40 +0000
@@ -131,8 +131,7 @@
         
         interpretationService.saveInterpretation( interpretation );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
-        response.setHeader( "Location", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
+        ContextUtils.createdResponse( response, "Interpretation created", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
     }
 
     @RequestMapping( value = "/map/{uid}", method = RequestMethod.POST, consumes = { "text/html", "text/plain" } )
@@ -152,8 +151,7 @@
         
         interpretationService.saveInterpretation( interpretation );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
-        response.setHeader( "Location", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
+        ContextUtils.createdResponse( response, "Interpretation created", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
     }
 
     @RequestMapping( value = "/reportTable/{uid}", method = RequestMethod.POST, consumes = { "text/html", "text/plain" } )
@@ -187,8 +185,7 @@
         
         interpretationService.saveInterpretation( interpretation );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
-        response.setHeader( "Location", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
+        ContextUtils.createdResponse( response, "Interpretation created", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
     }
 
     @RequestMapping( value = "/dataSetReport/{uid}", method = RequestMethod.POST, consumes = { "text/html", "text/plain" } )
@@ -226,8 +223,7 @@
         
         interpretationService.saveInterpretation( interpretation );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
-        response.setHeader( "Location", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
+        ContextUtils.createdResponse( response, "Interpretation created", InterpretationController.RESOURCE_PATH + "/" + interpretation.getUid() );
     }
 
     @RequestMapping( value = "/{uid}/comment", method = RequestMethod.POST, consumes = { "text/html", "text/plain" } )
@@ -237,7 +233,6 @@
     {
         interpretationService.addInterpretationComment( uid, text );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
-        response.setHeader( "Location", InterpretationController.RESOURCE_PATH + "/" + uid );
+        ContextUtils.createdResponse( response, "Commented created", InterpretationController.RESOURCE_PATH + "/" + uid );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java	2012-10-23 12:15:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java	2012-10-24 08:31:40 +0000
@@ -39,7 +39,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
@@ -188,10 +187,10 @@
         String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
 
         int id = messageService.sendMessage( message.getSubject(), message.getText(), metaData, message.getUsers() );
-        MessageConversation m = messageService.getMessageConversation( id );
+        
+        MessageConversation conversation = messageService.getMessageConversation( id );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
-        response.setHeader( "Location", MessageConversationController.RESOURCE_PATH + "/" + m.getUid() );
+        ContextUtils.createdResponse( response, "Message conversation created", MessageConversationController.RESOURCE_PATH + "/" + conversation.getUid() );
     }
 
     //--------------------------------------------------------------------------
@@ -204,17 +203,17 @@
     {
         String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
 
-        MessageConversation messageConversation = messageService.getMessageConversation( uid );
+        MessageConversation conversation = messageService.getMessageConversation( uid );
 
-        if ( messageConversation == null )
+        if ( conversation == null )
         {
             ContextUtils.conflictResponse( response, "Message conversation does not exist: " + uid );
             return;
         }
 
-        messageService.sendReply( messageConversation, body, metaData );
+        messageService.sendReply( conversation, body, metaData );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
+        ContextUtils.createdResponse( response, "Message conversation created", MessageConversationController.RESOURCE_PATH + "/" + conversation.getUid() );
     }
 
     //--------------------------------------------------------------------------
@@ -229,6 +228,6 @@
 
         messageService.sendFeedback( subject, body, metaData );
 
-        response.setStatus( HttpServletResponse.SC_CREATED );
+        ContextUtils.createdResponse( response, "Feedback created", null );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SqlViewController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SqlViewController.java	2012-08-29 16:24:08 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SqlViewController.java	2012-10-24 08:31:40 +0000
@@ -100,7 +100,9 @@
         }
         else
         {
-            ContextUtils.createdResponse( response, "SQL view created" );
+            String location = RESOURCE_PATH + "/" + sqlView.getUid();
+            
+            ContextUtils.createdResponse( response, "SQL view created", location );
         }
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java	2012-05-28 14:25:12 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapLegendSetController.java	2012-10-24 08:31:40 +0000
@@ -27,10 +27,21 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.io.InputStream;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 import org.hisp.dhis.api.controller.AbstractCrudController;
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.mapping.MapLegendSet;
+import org.hisp.dhis.mapping.MappingService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -41,4 +52,19 @@
     extends AbstractCrudController<MapLegendSet>
 {
     public static final String RESOURCE_PATH = "/mapLegendSets";
+
+    @Autowired
+    private MappingService mappingService;
+    
+    @Override
+    @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
+    @PreAuthorize( "hasRole('F_GIS_ADMIN')" )
+    public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
+    {
+        MapLegendSet legendSet = JacksonUtils.fromJson( input, MapLegendSet.class );
+        
+        mappingService.addMapLegendSet( legendSet );
+        
+        ContextUtils.createdResponse( response, "Mal legend set created", RESOURCE_PATH + "/" + legendSet.getUid() );
+    }    
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java	2012-10-18 21:27:32 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/utils/ContextUtils.java	2012-10-24 08:31:40 +0000
@@ -70,7 +70,8 @@
     public static final String HEADER_EXPIRES = "Expires";
     public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
     public static final String HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
-
+    public static final String HEADER_LOCATION = "Location";
+    
     @Autowired
     private SystemSettingManager systemSettingManager;
 
@@ -147,12 +148,17 @@
     {
         setResponse( response, HttpServletResponse.SC_OK, message );
     }
-
-    public static void createdResponse( HttpServletResponse response, String message )
+    
+    public static void createdResponse( HttpServletResponse response, String message, String location )
     {
+        if ( location != null )
+        {
+            response.addHeader( HEADER_LOCATION, location );
+        }
+        
         setResponse( response, HttpServletResponse.SC_CREATED, message );
     }
-
+    
     public static void notFoundResponse( HttpServletResponse response, String message )
     {
         setResponse( response, HttpServletResponse.SC_NOT_FOUND, message );