← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17346: support jsonp in /api/me/user-account and /api/systemSettings

 

------------------------------------------------------------
revno: 17346
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-11-03 19:06:54 +0700
message:
  support jsonp in /api/me/user-account and /api/systemSettings
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/DefaultRenderService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/RenderService.java
  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/user/CurrentUserController.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/render/DefaultRenderService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/DefaultRenderService.java	2014-04-25 08:33:22 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/DefaultRenderService.java	2014-11-03 12:06:54 +0000
@@ -34,8 +34,10 @@
 import com.fasterxml.jackson.databind.MapperFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.util.JSONPObject;
 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
 import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
+import org.springframework.util.StringUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -62,18 +64,40 @@
     //--------------------------------------------------------------------------
 
     @Override
-    public <T> void toJson( OutputStream output, T value ) throws IOException
+    public void toJson( OutputStream output, Object value ) throws IOException
     {
         jsonMapper.writeValue( output, value );
     }
 
     @Override
-    public <T> void toJson( OutputStream output, T value, Class<?> klass ) throws IOException
+    public void toJson( OutputStream output, Object value, Class<?> klass ) throws IOException
     {
         jsonMapper.writerWithView( klass ).writeValue( output, value );
     }
 
     @Override
+    public void toJsonP( OutputStream output, Object value, String callback ) throws IOException
+    {
+        if ( StringUtils.isEmpty( callback ) )
+        {
+            callback = "callback";
+        }
+
+        jsonMapper.writeValue( output, new JSONPObject( callback, value ) );
+    }
+
+    @Override
+    public void toJsonP( OutputStream output, Object value, Class<?> klass, String callback ) throws IOException
+    {
+        if ( StringUtils.isEmpty( callback ) )
+        {
+            callback = "callback";
+        }
+
+        jsonMapper.writerWithView( klass ).writeValue( output, new JSONPObject( callback, value ) );
+    }
+
+    @Override
     public <T> T fromJson( InputStream input, Class<T> klass ) throws IOException
     {
         return jsonMapper.readValue( input, klass );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/RenderService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/RenderService.java	2014-10-10 16:57:54 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/render/RenderService.java	2014-11-03 12:06:54 +0000
@@ -37,9 +37,13 @@
  */
 public interface RenderService
 {
-    <T> void toJson( OutputStream output, T value ) throws IOException;
-
-    <T> void toJson( OutputStream output, T value, Class<?> klass ) throws IOException;
+    void toJson( OutputStream output, Object value ) throws IOException;
+
+    void toJson( OutputStream output, Object value, Class<?> klass ) throws IOException;
+
+    void toJsonP( OutputStream output, Object value, String callback ) throws IOException;
+
+    void toJsonP( OutputStream output, Object value, Class<?> klass, String callback ) throws IOException;
 
     <T> T fromJson( InputStream input, Class<T> klass ) throws IOException;
 

=== 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	2014-06-24 19:25:03 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SystemSettingController.java	2014-11-03 12:06:54 +0000
@@ -28,12 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.Serializable;
-import java.util.Map;
-import java.util.Set;
-
-import javax.servlet.http.HttpServletResponse;
-
+import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.setting.SystemSettingManager;
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +41,12 @@
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Map;
+import java.util.Set;
+
 /**
  * @author Lars Helge Overland
  */
@@ -56,6 +57,9 @@
     @Autowired
     private SystemSettingManager systemSettingManager;
 
+    @Autowired
+    private RenderService renderService;
+
     @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(
@@ -76,7 +80,7 @@
         }
 
         value = value != null ? value : valuePayload;
-        
+
         key = key != null ? key : keyParam;
 
         systemSettingManager.saveSystemSetting( key, value );
@@ -103,18 +107,36 @@
 
         return setting != null ? String.valueOf( setting ) : null;
     }
-    
-    @RequestMapping( method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON )
-    public @ResponseBody Map<String, Serializable> getSystemSettings( @RequestParam( value = "key", required = false ) Set<String> key )
-    {
+
+    @RequestMapping( method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_HTML } )
+    public void getSystemSettingsJson( @RequestParam( value = "key", required = false ) Set<String> key, HttpServletResponse response ) throws IOException
+    {
+        renderService.toJson( response.getOutputStream(), getSystemSettings( key ) );
+    }
+
+    @RequestMapping( method = RequestMethod.GET, produces = "application/javascript" )
+    public void getSystemSettingsJsonP(
+        @RequestParam( value = "key", required = false ) Set<String> key,
+        @RequestParam( defaultValue = "callback" ) String callback,
+        HttpServletResponse response ) throws IOException
+    {
+        renderService.toJsonP( response.getOutputStream(), getSystemSettings( key ), callback );
+    }
+
+    private Map<String, Serializable> getSystemSettings( Set<String> key )
+    {
+        Map<String, Serializable> value;
+
         if ( key != null && !key.isEmpty() )
         {
-            return systemSettingManager.getSystemSettings( key );
+            value = systemSettingManager.getSystemSettings( key );
         }
         else
         {
-            return systemSettingManager.getSystemSettingsAsMap();
+            value = systemSettingManager.getSystemSettingsAsMap();
         }
+
+        return value;
     }
 
     @RequestMapping( value = "/{key}", method = RequestMethod.DELETE )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java	2014-10-25 10:52:33 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java	2014-11-03 12:06:54 +0000
@@ -28,24 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.user.UserSettingService.KEY_DB_LOCALE;
-import static org.hisp.dhis.user.UserSettingService.KEY_MESSAGE_EMAIL_NOTIFICATION;
-import static org.hisp.dhis.user.UserSettingService.KEY_MESSAGE_SMS_NOTIFICATION;
-import static org.hisp.dhis.user.UserSettingService.KEY_UI_LOCALE;
-import static org.hisp.dhis.user.UserSettingService.KEY_ANALYSIS_DISPLAY_PROPERTY;
-
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
+import com.google.common.collect.Lists;
 import org.apache.commons.collections.CollectionUtils;
 import org.hisp.dhis.acl.AclService;
 import org.hisp.dhis.common.IdentifiableObjectManager;
@@ -56,6 +39,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.interpretation.Interpretation;
@@ -93,13 +77,24 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import com.google.common.collect.Lists;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hisp.dhis.user.UserSettingService.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping( value = { CurrentUserController.RESOURCE_PATH, "/me" }, method = RequestMethod.GET )
+@RequestMapping(value = { CurrentUserController.RESOURCE_PATH, "/me" }, method = RequestMethod.GET)
 public class CurrentUserController
 {
     public static final String RESOURCE_PATH = "/currentUser";
@@ -146,10 +141,13 @@
 
     @Autowired
     protected AclService aclService;
-    
+
     @Autowired
     private DataApprovalLevelService approvalLevelService;
 
+    @Autowired
+    private RenderService renderService;
+
     @RequestMapping( produces = { "application/json", "text/*" } )
     public void getCurrentUser( HttpServletResponse response ) throws Exception
     {
@@ -161,10 +159,10 @@
         }
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJsonWithView( response.getOutputStream(), currentUser, DetailedView.class );
+        renderService.toJson( response.getOutputStream(), currentUser, DetailedView.class );
     }
 
-    @RequestMapping( value = "/dashboards", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/dashboards", produces = { "application/json", "text/*" })
     public void getDashboards( HttpServletResponse response ) throws NotAuthenticatedException, IOException
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -187,10 +185,10 @@
         }
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJsonWithView( response.getOutputStream(), dashboards, DetailedView.class );
+        renderService.toJson( response.getOutputStream(), dashboards, DetailedView.class );
     }
 
-    @RequestMapping( value = "/inbox", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/inbox", produces = { "application/json", "text/*" })
     public void getInbox( HttpServletResponse response ) throws Exception
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -215,10 +213,10 @@
         }
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), inbox );
+        renderService.toJson( response.getOutputStream(), inbox );
     }
 
-    @RequestMapping( value = "/inbox/messageConversations", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/inbox/messageConversations", produces = { "application/json", "text/*" })
     public void getInboxMessageConversations( HttpServletResponse response ) throws Exception
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -237,10 +235,10 @@
             messageConversation.setAccess( aclService.getAccess( messageConversation ) );
         }
 
-        JacksonUtils.toJson( response.getOutputStream(), messageConversations );
+        renderService.toJson( response.getOutputStream(), messageConversations );
     }
 
-    @RequestMapping( value = "/inbox/interpretations", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/inbox/interpretations", produces = { "application/json", "text/*" })
     public void getInboxInterpretations( HttpServletResponse response ) throws Exception
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -258,10 +256,10 @@
             interpretation.setAccess( aclService.getAccess( interpretation ) );
         }
 
-        JacksonUtils.toJson( response.getOutputStream(), interpretations );
+        renderService.toJson( response.getOutputStream(), interpretations );
     }
 
-    @RequestMapping( value = "/dashboard", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/dashboard", produces = { "application/json", "text/*" })
     public void getDashboard( HttpServletResponse response ) throws Exception
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -276,11 +274,30 @@
         dashboard.setUnreadInterpretations( interpretationService.getNewInterpretationCount() );
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), dashboard );
-    }
-
-    @RequestMapping( value = { "/profile", "/user-account" }, produces = { "application/json", "text/*" } )
-    public void getUserAccount( HttpServletResponse response ) throws Exception
+        renderService.toJson( response.getOutputStream(), dashboard );
+    }
+
+    @RequestMapping( value = { "/profile", "/user-account" }, produces = { "application/json", "text/html" } )
+    public void getUserAccountJson( HttpServletResponse response ) throws Exception
+    {
+        UserAccount userAccount = getUserAccount();
+
+        response.setContentType( MediaType.APPLICATION_JSON_VALUE );
+        renderService.toJson( response.getOutputStream(), userAccount );
+    }
+
+    @RequestMapping( value = { "/profile", "/user-account" }, produces = { "application/javascript" } )
+    public void getUserAccountJsonP( @RequestParam( defaultValue = "callback" ) String callback, HttpServletResponse response, HttpServletRequest request ) throws Exception
+    {
+        UserAccount userAccount = getUserAccount();
+
+        System.err.println( "accept:" + request.getHeader( "Accept" ) );
+
+        response.setContentType( "application/javascript" );
+        renderService.toJsonP( response.getOutputStream(), userAccount, callback );
+    }
+
+    private UserAccount getUserAccount() throws NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
 
@@ -319,15 +336,13 @@
         userAccount.getSettings().put( KEY_MESSAGE_EMAIL_NOTIFICATION, TextUtils.toString( userSettingService.getUserSetting( KEY_MESSAGE_EMAIL_NOTIFICATION ) ) );
         userAccount.getSettings().put( KEY_MESSAGE_SMS_NOTIFICATION, TextUtils.toString( userSettingService.getUserSetting( KEY_MESSAGE_SMS_NOTIFICATION ) ) );
         userAccount.getSettings().put( KEY_ANALYSIS_DISPLAY_PROPERTY, TextUtils.toString( userSettingService.getUserSetting( KEY_ANALYSIS_DISPLAY_PROPERTY ) ) );
-
-        response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), userAccount );
+        return userAccount;
     }
 
-    @RequestMapping( value = { "/profile", "/user-account" }, method = RequestMethod.POST, consumes = "application/json" )
+    @RequestMapping(value = { "/profile", "/user-account" }, method = RequestMethod.POST, consumes = "application/json")
     public void postUserAccountJson( HttpServletResponse response, HttpServletRequest request ) throws Exception
     {
-        UserAccount userAccount = JacksonUtils.fromJson( request.getInputStream(), UserAccount.class );
+        UserAccount userAccount = renderService.fromJson( request.getInputStream(), UserAccount.class );
         User currentUser = currentUserService.getCurrentUser();
 
         if ( currentUser == null )
@@ -360,16 +375,16 @@
         userService.updateUser( currentUser );
     }
 
-    @RequestMapping( value = "/authorization", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/authorization", produces = { "application/json", "text/*" })
     public void getAuthorization( HttpServletResponse response ) throws IOException
     {
         User currentUser = currentUserService.getCurrentUser();
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), currentUser.getUserCredentials().getAllAuthorities() );
+        renderService.toJson( response.getOutputStream(), currentUser.getUserCredentials().getAllAuthorities() );
     }
 
-    @RequestMapping( value = "/authorization/{auth}", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/authorization/{auth}", produces = { "application/json", "text/*" })
     public void hasAuthorization( @PathVariable String auth, HttpServletResponse response ) throws IOException
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -377,12 +392,12 @@
         boolean hasAuth = currentUser != null && currentUser.getUserCredentials().isAuthorized( auth );
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), hasAuth );
+        renderService.toJson( response.getOutputStream(), hasAuth );
     }
 
-    @RequestMapping( value = "/recipients", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/recipients", produces = { "application/json", "text/*" })
     public void recipientsJson( HttpServletResponse response,
-        @RequestParam( value = "filter" ) String filter ) throws IOException, NotAuthenticatedException, FilterTooShortException
+        @RequestParam(value = "filter") String filter ) throws IOException, NotAuthenticatedException, FilterTooShortException
     {
         User currentUser = currentUserService.getCurrentUser();
 
@@ -405,10 +420,10 @@
         recipients.setUserGroups( new HashSet<>( userGroupService.getUserGroupsBetweenByName( filter, 0, MAX_OBJECTS ) ) );
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), recipients );
+        renderService.toJson( response.getOutputStream(), recipients );
     }
-    
-    @RequestMapping( value = { "/assignedOrganisationUnits", "/organisationUnits" }, produces = { "application/json", "text/*" } )
+
+    @RequestMapping(value = { "/assignedOrganisationUnits", "/organisationUnits" }, produces = { "application/json", "text/*" })
     public void getAssignedOrganisationUnits( HttpServletResponse response, @RequestParam Map<String, String> parameters ) throws IOException, NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -454,12 +469,12 @@
         Class<?> viewClass = JacksonUtils.getViewClass( viewName );
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJsonWithView( response.getOutputStream(), userOrganisationUnits, viewClass );
+        renderService.toJson( response.getOutputStream(), userOrganisationUnits, viewClass );
     }
 
-    @RequestMapping( value = { "/assignedPrograms", "/programs" }, produces = { "application/json", "text/*" } )
+    @RequestMapping(value = { "/assignedPrograms", "/programs" }, produces = { "application/json", "text/*" })
     public void getPrograms( HttpServletResponse response, @RequestParam Map<String, String> parameters,
-        @RequestParam( required = false ) Integer type )
+        @RequestParam(required = false) Integer type )
         throws IOException, NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -574,12 +589,12 @@
         }
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), forms );
+        renderService.toJson( response.getOutputStream(), forms );
     }
 
-    @SuppressWarnings( "unchecked" )
-    @RequestMapping( value = { "/assignedDataSets", "/dataSets" }, produces = { "application/json", "text/*" } )
-    public void getDataSets( @RequestParam( defaultValue = "false" ) boolean optionSets, @RequestParam( defaultValue = "50" ) int maxOptions,
+    @SuppressWarnings("unchecked")
+    @RequestMapping(value = { "/assignedDataSets", "/dataSets" }, produces = { "application/json", "text/*" })
+    public void getDataSets( @RequestParam(defaultValue = "false") boolean optionSets, @RequestParam(defaultValue = "50") int maxOptions,
         HttpServletResponse response, @RequestParam Map<String, String> parameters ) throws IOException, NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -696,14 +711,14 @@
         }
 
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), forms );
+        renderService.toJson( response.getOutputStream(), forms );
     }
 
-    @RequestMapping( value = "/dataApprovalLevels", produces = { "application/json", "text/*" } )
+    @RequestMapping(value = "/dataApprovalLevels", produces = { "application/json", "text/*" })
     public void getApprovalLevels( HttpServletResponse response ) throws IOException
     {
         List<DataApprovalLevel> approvalLevels = approvalLevelService.getUserDataApprovalLevels();
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        JacksonUtils.toJson( response.getOutputStream(), approvalLevels );        
+        renderService.toJson( response.getOutputStream(), approvalLevels );
     }
 }