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.myfavorites.web.rs;
35  
36  import java.util.List;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.ws.rs.GET;
40  import javax.ws.rs.Path;
41  import javax.ws.rs.PathParam;
42  import javax.ws.rs.core.Context;
43  import javax.ws.rs.core.MediaType;
44  import javax.ws.rs.core.Response;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import fr.paris.lutece.plugins.myportal.modules.myfavorites.business.MyFavorites;
49  import fr.paris.lutece.plugins.myportal.modules.myfavorites.business.MyFavoritesHome;
50  import fr.paris.lutece.plugins.rest.service.RestConstants;
51  import fr.paris.lutece.portal.service.security.LuteceUser;
52  import fr.paris.lutece.portal.service.security.SecurityService;
53  import net.sf.json.JSONArray;
54  import net.sf.json.JSONObject;
55  
56  /**
57   * REST service for MyFavorites resource
58   */
59  @Path( RestConstants.BASE_PATH + MyFavoritesRest.PLUGIN_PATH )
60  public class MyFavoritesRest
61  {
62      // Path constants
63      protected static final String USER_GUID = "user_guid";
64      protected static final String PLUGIN_PATH = "myfavorites/";
65      protected static final String PATH_FAVORITES = "public/favorites/";
66      protected static final String PATH_FAVORITES_BY_USERGUID = "private/favorites/{" + MyFavoritesRest.USER_GUID + "}";
67  
68      // Format constants
69      private static final String FORMAT_FAVORITES_STATUS_RESPONSE = "status";
70      private static final String FORMAT_FAVORITES_RESPONSE_RESULT = "result";
71      private static final String FORMAT_FAVORITES_KEY = "favorites";
72      private static final String FORMAT_FAVORITES_ID = "id";
73      private static final String FORMAT_FAVORITES_URL = "url";
74      private static final String FORMAT_FAVORITES_LABEL = "label";
75      private static final String FORMAT_FAVORITES_USER_ID = "strIdUser";
76      private static final String FORMAT_FAVORITES_ORDER = "order";
77  
78      // Status constants
79      private static final String STATUS_OK = "OK";
80      private static final String STATUS_KO = "KO";
81  
82      /**
83       * Return the list of all favorites of a user
84       * 
85       * @param request
86       *            httpServletRequest
87       * @return the list of all favorites of a user
88       */
89      @GET
90      @Path( MyFavoritesRest.PATH_FAVORITES )
91      public Response getUserListFavorites( @Context HttpServletRequest request )
92      {
93          String strStatus = STATUS_OK;
94  
95          String strFavoritesList = StringUtils.EMPTY;
96  
97          LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
98          // the user must be authenticated
99          if ( user != null )
100         {
101             try
102             {
103                 List<MyFavorites> listUserFavorites = MyFavoritesHome.getMyFavoritessList( user.getName( ) );
104                 if ( listUserFavorites != null && !listUserFavorites.isEmpty( ) )
105                 {
106                     strFavoritesList = formatMyFavoritesList( listUserFavorites );
107                 }
108             }
109             catch( Exception exception )
110             {
111                 // We set the status at KO if an error occurred during the processing
112                 strStatus = STATUS_KO;
113             }
114         }
115         else
116         {
117             strStatus = STATUS_KO;
118         }
119 
120         // Format the response with the given status and the list of favorites
121         String strResponse = formatResponse( strStatus, strFavoritesList );
122 
123         return Response.ok( strResponse, MediaType.APPLICATION_JSON ).build( );
124     }
125 
126     /**
127      * Return the list of all favorites of a user by guid the rest module is protected by signed request filter
128      * 
129      * @param request
130      *            httpServletRequest
131      * @return the list of all favorites of a user
132      */
133     @Path( MyFavoritesRest.PATH_FAVORITES_BY_USERGUID )
134     @GET
135     public Response getUserListFavoritesByGuid( @Context HttpServletRequest request, @PathParam( MyFavoritesRest.USER_GUID ) String strGuid )
136     {
137         String strStatus = STATUS_OK;
138 
139         String strFavoritesList = StringUtils.EMPTY;
140 
141         // the user must be authenticated
142         if ( !StringUtils.isEmpty( strGuid ) )
143         {
144             try
145             {
146                 List<MyFavorites> listUserFavorites = MyFavoritesHome.getMyFavoritessList( strGuid );
147                 if ( listUserFavorites != null && !listUserFavorites.isEmpty( ) )
148                 {
149                     strFavoritesList = formatMyFavoritesList( listUserFavorites );
150                 }
151             }
152             catch( Exception exception )
153             {
154                 // We set the status at KO if an error occurred during the processing
155                 strStatus = STATUS_KO;
156             }
157         }
158         else
159         {
160             strStatus = STATUS_KO;
161         }
162 
163         // Format the response with the given status and the list of favorites
164         String strResponse = formatResponse( strStatus, strFavoritesList );
165 
166         return Response.ok( strResponse, MediaType.APPLICATION_JSON ).build( );
167     }
168 
169     /**
170      * Return the Json response with the given status
171      * 
172      * @param strStatus
173      *            The status of the treatment "OK" by default "KO" if an error occurred during the processing
174      * @param strResponse
175      *            The response to send
176      * @return the Json response with the given status
177      */
178     private String formatResponse( String strStatus, String strResponse )
179     {
180         JSONObject jsonResponse = new JSONObject( );
181         jsonResponse.accumulate( FORMAT_FAVORITES_STATUS_RESPONSE, strStatus );
182         jsonResponse.accumulate( FORMAT_FAVORITES_RESPONSE_RESULT, strResponse );
183 
184         return jsonResponse.toString( );
185     }
186 
187     /**
188      * Return the Json of a list of MyFavorites object
189      * 
190      * @param listUserFavorites
191      *            the list of the favorites to format
192      * @return the Json of a list of MyFavorites object
193      */
194     private String formatMyFavoritesList( List<MyFavorites> listUserFavorites )
195     {
196         JSONObject jsonResponse = new JSONObject( );
197         JSONArray jsonAllMyFavorites = new JSONArray( );
198 
199         for ( MyFavorites myFavorites : listUserFavorites )
200         {
201             JSONObject jsonMyFavorites = new JSONObject( );
202             add( jsonMyFavorites, myFavorites );
203             jsonAllMyFavorites.add( jsonMyFavorites );
204         }
205 
206         jsonResponse.accumulate( FORMAT_FAVORITES_KEY, jsonAllMyFavorites );
207 
208         return jsonResponse.toString( );
209     }
210 
211     /**
212      * Add the data from a MyFavorites object to a JsonObject
213      * 
214      * @param jsonMyFavorites
215      *            the Json to include the data
216      * @param myFavorites
217      *            the MyFavorites to retrieve the data from
218      */
219     private void add( JSONObject jsonMyFavorites, MyFavorites myFavorites )
220     {
221         if ( jsonMyFavorites != null && myFavorites != null )
222         {
223             jsonMyFavorites.accumulate( FORMAT_FAVORITES_ID, myFavorites.getId( ) );
224             jsonMyFavorites.accumulate( FORMAT_FAVORITES_URL, myFavorites.getUrl( ) );
225             jsonMyFavorites.accumulate( FORMAT_FAVORITES_LABEL, myFavorites.getLabel( ) );
226             jsonMyFavorites.accumulate( FORMAT_FAVORITES_USER_ID, myFavorites.getIdUser( ) );
227             jsonMyFavorites.accumulate( FORMAT_FAVORITES_ORDER, myFavorites.getOrder( ) );
228         }
229     }
230 }