View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.myportal.modules.myapps.web.rs;
35  
36  import java.util.Base64;
37  import java.util.List;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.ws.rs.GET;
41  import javax.ws.rs.Path;
42  import javax.ws.rs.PathParam;
43  import javax.ws.rs.core.Context;
44  import javax.ws.rs.core.MediaType;
45  import javax.ws.rs.core.Response;
46  
47  import org.apache.commons.lang.StringUtils;
48  
49  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabase;
50  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseHome;
51  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseUser;
52  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseUserHome;
53  import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabasePlugin;
54  import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabaseService;
55  import fr.paris.lutece.plugins.rest.service.RestConstants;
56  import fr.paris.lutece.portal.service.image.ImageResource;
57  import fr.paris.lutece.portal.service.plugin.Plugin;
58  import fr.paris.lutece.portal.service.plugin.PluginService;
59  import fr.paris.lutece.portal.service.security.LuteceUser;
60  import fr.paris.lutece.portal.service.security.SecurityService;
61  import net.sf.json.JSONArray;
62  import net.sf.json.JSONObject;
63  
64  /**
65   * REST service for MyPortalMyApps
66   */
67  @Path( RestConstants.BASE_PATH + MyPortalMyAppsRest.PLUGIN_PATH )
68  public class MyPortalMyAppsRest
69  {
70      // Constants
71      protected static final String USER_GUID = "user_guid";
72      protected static final String PLUGIN_PATH = "myapps/";
73      protected static final String PATH_APPS = "public/apps/";
74      protected static final String PATH_APPS_BY_USERGUID = "private/apps/{" + MyPortalMyAppsRest.USER_GUID + "}";
75  
76      // Format constants
77      private static final String FORMAT_MYAPPS_STATUS_RESPONSE = "status";
78      private static final String FORMAT_MYAPPS_RESPONSE_RESULT = "result";
79      private static final String FORMAT_MYAPPS_KEY = "apps";
80      private static final String FORMAT_MYAPPS_ID = "id";
81      private static final String FORMAT_MYAPPS_NAME = "name";
82      private static final String FORMAT_MYAPPS_ICON = "icon";
83      private static final String FORMAT_MYAPPS_ORDER = "order";
84  
85      // Status constants
86      private static final String STATUS_OK = "OK";
87      private static final String STATUS_KO = "KO";
88  
89      /**
90       * Return the list of all MyApps of a user
91       * 
92       *
93       * @param request
94       *            httpServletRequest
95       * @return the json list corresponding to the list of all user MyApps
96       */
97      @GET
98      @Path( MyPortalMyAppsRest.PATH_APPS )
99      public Response getUserMyAppsList( @Context HttpServletRequest request )
100     {
101         String strStatus = STATUS_OK;
102         String strFavoritesList = StringUtils.EMPTY;
103 
104         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
105         // the user must be authenticated
106         if ( user != null )
107         {
108 
109             try
110             {
111                 // Retrieve the list of the applications of the user
112                 Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
113                 List<MyAppsDatabaseUser> listMyAppsDatabaseUser = MyAppsDatabaseUserHome.getUserListApplications( user.getName( ), pluginMyAppsDatabase );
114 
115                 // Format the list of applications
116                 if ( listMyAppsDatabaseUser != null && !listMyAppsDatabaseUser.isEmpty( ) )
117                 {
118                     strFavoritesList = formatUserMyAppsList( listMyAppsDatabaseUser );
119                 }
120             }
121             catch( Exception exception )
122             {
123                 // We set the status at KO if an error occurred during the processing
124                 strStatus = STATUS_KO;
125             }
126         }
127         else
128         {
129             strStatus = STATUS_KO;
130 
131         }
132 
133         // Format the response with the given status and the list of favorites
134         String strResponse = formatResponse( strStatus, strFavoritesList );
135 
136         // Return the response
137         return Response.ok( strResponse, MediaType.APPLICATION_JSON ).build( );
138     }
139 
140     /**
141      * Return the list of all MyApps of a user by user guid the rest service is protected by signed request
142      *
143      * @param request
144      *            httpServletRequest
145      * @param strGuid
146      *            the user Guid
147      * @return the json list corresponding to the list of all user MyApps
148      */
149     @GET
150     @Path( MyPortalMyAppsRest.PATH_APPS_BY_USERGUID )
151     public Response getUserMyAppsListByGuid( @Context HttpServletRequest request, @PathParam( MyPortalMyAppsRest.USER_GUID ) String strGuid )
152     {
153         String strStatus = STATUS_OK;
154         String strFavoritesList = StringUtils.EMPTY;
155 
156         // the user must be authenticated
157         if ( !StringUtils.isEmpty( strGuid ) )
158         {
159 
160             try
161             {
162                 // Retrieve the list of the applications of the user
163                 Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
164                 List<MyAppsDatabaseUser> listMyAppsDatabaseUser = MyAppsDatabaseUserHome.getUserListApplications( strGuid, pluginMyAppsDatabase );
165 
166                 // Format the list of applications
167                 if ( listMyAppsDatabaseUser != null && !listMyAppsDatabaseUser.isEmpty( ) )
168                 {
169                     strFavoritesList = formatUserMyAppsList( listMyAppsDatabaseUser );
170                 }
171             }
172             catch( Exception exception )
173             {
174                 // We set the status at KO if an error occurred during the processing
175                 strStatus = STATUS_KO;
176             }
177         }
178         else
179         {
180             strStatus = STATUS_KO;
181 
182         }
183 
184         // Format the response with the given status and the list of favorites
185         String strResponse = formatResponse( strStatus, strFavoritesList );
186 
187         // Return the response
188         return Response.ok( strResponse, MediaType.APPLICATION_JSON ).build( );
189     }
190 
191     /**
192      * Return the Json response with the given status
193      * 
194      * @param strStatus
195      *            The status of the treatment "OK" by default "KO" if an error occurred during the processing
196      * @param strResponse
197      *            The response to send
198      * @return the Json response with the given status
199      */
200     private String formatResponse( String strStatus, String strResponse )
201     {
202         JSONObject jsonResponse = new JSONObject( );
203         jsonResponse.accumulate( FORMAT_MYAPPS_STATUS_RESPONSE, strStatus );
204         jsonResponse.accumulate( FORMAT_MYAPPS_RESPONSE_RESULT, strResponse );
205 
206         return jsonResponse.toString( );
207     }
208 
209     /**
210      * Return the Json of a list of MyApps object
211      * 
212      * @param listMyAppsDatabaseUser
213      *            the list of the MyApps to format
214      * @return the Json of a list of MyApps object
215      */
216     private String formatUserMyAppsList( List<MyAppsDatabaseUser> listMyAppsDatabaseUser )
217     {
218         JSONObject jsonResponse = new JSONObject( );
219         JSONArray jsonAllMyApps = new JSONArray( );
220 
221         for ( MyAppsDatabaseUser myAppsDatabaseUser : listMyAppsDatabaseUser )
222         {
223             JSONObject jsonMyApps = new JSONObject( );
224             add( jsonMyApps, myAppsDatabaseUser );
225             jsonAllMyApps.add( jsonMyApps );
226         }
227 
228         jsonResponse.accumulate( FORMAT_MYAPPS_KEY, jsonAllMyApps );
229 
230         return jsonResponse.toString( );
231     }
232 
233     /**
234      * Add the data from a MyApps object to a JsonObject
235      * 
236      * @param jsonMyApps
237      *            the Json to include the data
238      * @param myAppsDatabaseUser
239      *            the MyAppsDatabaseUser to retrieve the data from
240      */
241     private void add( JSONObject jsonMyApps, MyAppsDatabaseUser myAppsDatabaseUser )
242     {
243         if ( jsonMyApps != null && myAppsDatabaseUser != null )
244         {
245             // Retrieve the application associate to the current MyAppsDatabaseUser
246             int nIdApplication = myAppsDatabaseUser.getIdApplication( );
247             Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
248             MyAppsDatabase myAppsDatabase = (MyAppsDatabase) MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nIdApplication, pluginMyAppsDatabase );
249 
250             // Collect the data from the MyAppsDatabase
251             if ( myAppsDatabase != null )
252             {
253                 int nIdAppsApplication = myAppsDatabase.getIdApplication( );
254                 jsonMyApps.accumulate( FORMAT_MYAPPS_ID, nIdApplication );
255                 jsonMyApps.accumulate( FORMAT_MYAPPS_NAME, myAppsDatabase.getName( ) );
256                 jsonMyApps.accumulate( FORMAT_MYAPPS_ICON, getApplicationIcon( nIdAppsApplication ) );
257             }
258 
259             // Collect the data from the MyAppsDatabaseUser
260             jsonMyApps.accumulate( FORMAT_MYAPPS_ORDER, myAppsDatabaseUser.getApplicationOrder( ) );
261         }
262     }
263 
264     /**
265      * Return the icon of the application as string encoded in base 64
266      * 
267      * @param nIdApplication
268      *            The id of the application
269      * @return the string representation of the icon of the application
270      */
271     private String getApplicationIcon( int nIdApplication )
272     {
273         String strIconEncoded = StringUtils.EMPTY;
274 
275         Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
276         ImageResource imageResource = MyAppsDatabaseHome.getImageResource( nIdApplication, pluginMyAppsDatabase );
277 
278         if ( imageResource != null && imageResource.getImage( ) != null )
279         {
280             strIconEncoded = Base64.getEncoder( ).encodeToString( imageResource.getImage( ) );
281         }
282 
283         return strIconEncoded;
284     }
285 }