View Javadoc
1   /*
2   * Copyright (c) 2002-2018, 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.bandeaugra.rs;
35  
36  import java.net.URI;
37  import java.net.URISyntaxException;
38  import java.util.function.Function;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.ws.rs.GET;
43  import javax.ws.rs.Path;
44  import javax.ws.rs.PathParam;
45  import javax.ws.rs.Produces;
46  import javax.ws.rs.WebApplicationException;
47  import javax.ws.rs.core.Context;
48  import javax.ws.rs.core.MediaType;
49  import javax.ws.rs.core.Response;
50  
51  import org.apache.commons.lang3.StringUtils;
52  
53  import fr.paris.lutece.plugins.bandeaugra.business.BannerInformations;
54  import fr.paris.lutece.plugins.bandeaugra.service.RemoteSiteBandeauClientService;
55  import fr.paris.lutece.plugins.rest.service.RestConstants;
56  import fr.paris.lutece.portal.service.security.LuteceUser;
57  import fr.paris.lutece.portal.service.security.SecurityService;
58  import fr.paris.lutece.portal.service.util.AppLogService;
59  import fr.paris.lutece.portal.service.util.AppPathService;
60  import fr.paris.lutece.util.json.ErrorJsonResponse;
61  import fr.paris.lutece.util.json.JsonResponse;
62  import fr.paris.lutece.util.json.JsonUtil;
63  
64  /**
65   * BannerInformationsRest
66   */
67  @Path( RestConstants.BASE_PATH + Constants.PATH_BANNER_API )
68  public class BannerInformationsRest
69  {
70      private static final int VERSION_1 = 1;
71      
72      /**
73       * Get User Informations
74       * 
75       * @param nVersion
76       *            the Api version
77       * @param request
78       *            the http servlet request
79       * @param response
80       *            the http servlet response
81       * @return
82       */
83      @GET
84      @Path( Constants.PATH_USER_INFORMATIONS )
85      @Produces( MediaType.APPLICATION_JSON )
86      public Response getUserInformations( @PathParam( Constants.API_VERSION ) Integer nVersion, @Context HttpServletRequest request,
87              @Context HttpServletResponse response )
88      {
89          switch( nVersion )
90          {
91              case VERSION_1:
92                  return getUserInformationsV1( request, response );
93  
94              default:
95                  break;
96          }
97  
98          throw new WebApplicationException( Response.Status.NOT_FOUND );
99      }
100 
101   
102     /**
103      *  Get User Informations V1
104      * @param request the request 
105      * @param response the http servlet response
106      * @return user informations
107      */
108     private Response getUserInformationsV1( HttpServletRequest request, HttpServletResponse response )
109     {
110 
111         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
112 
113         String strHeaderOrigin = request.getHeader( Constants.HEADER_ORIGIN );
114         String strAccessControlAllowOrigin = strHeaderOrigin;
115         if ( user != null )
116         {
117 
118             return Response.status( Response.Status.OK ).entity( BannerInformationsRest.getJsonBannerInformations( user ) )
119                     .header( Constants.ACCESS_CONTROL_ALLOW_ORIGIN, strAccessControlAllowOrigin ).header( Constants.ACCESS_CONTROL_ALLOW_METHODS, Constants.METHODS_LIST )
120                     .header( Constants.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString( ) ).build( );
121         }
122         else
123         {
124 
125             try
126             {
127                 
128                 String strBaseUrl=AppPathService.getBaseUrl( request );
129                 if(strBaseUrl.endsWith( "/" ))
130                 {
131                     strBaseUrl=strBaseUrl.substring( 0,strBaseUrl.length( )-1);
132                     
133                 }
134                 String strUrl = strBaseUrl+ "/servlet/plugins/oauth2/callback?data_client=bannerInfoDataClient";
135                 URI urlRedirect = null;
136                 urlRedirect = new URI( strUrl );
137                 return Response.seeOther( urlRedirect ).header( Constants.ACCESS_CONTROL_ALLOW_ORIGIN, strAccessControlAllowOrigin ).header( Constants.ACCESS_CONTROL_ALLOW_METHODS, Constants.METHODS_LIST )
138                         .header( Constants.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString( ) ).build( );
139 
140             }
141             catch( URISyntaxException e )
142             {
143                 AppLogService.error( e.getMessage( ), e );
144             }
145 
146         }
147 
148         return Response.status( Response.Status.UNAUTHORIZED )
149                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Constants.ERROR_USER_NOT_AUTHENTICATED, Constants.ERROR_USER_NOT_AUTHENTICATED ) ) )
150                 .header( Constants.ACCESS_CONTROL_ALLOW_ORIGIN, strAccessControlAllowOrigin ).header( Constants.ACCESS_CONTROL_ALLOW_METHODS, Constants.METHODS_LIST )
151                 .header( Constants.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString( ) ).build( );
152 
153     }
154     
155     
156     
157     
158   /**
159    * Get Json banner informatiobns
160    * @param user the LuteceUser
161    * @return Banner informations
162    */
163     public static String getJsonBannerInformations( LuteceUser user )
164     {
165 
166         BannerInformationserInformations.html#BannerInformations">BannerInformations headbandInformations = new BannerInformations( );
167         headbandInformations.setFirstName( user.getUserInfo( LuteceUser.NAME_GIVEN ) );
168         headbandInformations.setLastName( user.getUserInfo( LuteceUser.NAME_FAMILY ) );
169         return JsonUtil.buildJsonResponse( new JsonResponse( headbandInformations ) );
170     }
171     
172     
173     
174     
175     
176     /**
177      * Return JSON containing true if the user is authenticated
178      * @param nVersion the api version
179      * @param request the http request
180      * @param response the http response
181      * @return JSON containing true if the user is authenticated
182      */
183     @GET
184     @Path( Constants.PATH_IS_USER_AUTHENTICATED )
185     @Produces( MediaType.APPLICATION_JSON )
186     public Response isUserAuthenticated( @PathParam( Constants.API_VERSION ) Integer nVersion, @Context HttpServletRequest request,
187             @Context HttpServletResponse response )
188     {
189         switch( nVersion )
190         {
191             case VERSION_1:
192                 return isUserAuthenticatedV1( request, response );
193 
194             default:
195                 break;
196         }
197 
198         throw new WebApplicationException( Response.Status.NOT_FOUND  );
199     }
200 
201     
202     /**
203      * Return JSON containing true if the user is authenticated
204      * @param request the http request
205      * @param response the http response
206      * @return JSON containing true if the user is authenticated
207      */
208     private Response isUserAuthenticatedV1( HttpServletRequest request, HttpServletResponse response )
209     {
210 
211         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
212 
213         String strHeaderOrigin = request.getHeader( Constants.HEADER_ORIGIN );
214         String strAccessControlAllowOrigin = strHeaderOrigin;
215         return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( user != null )) )
216                     .header( Constants.ACCESS_CONTROL_ALLOW_ORIGIN, strAccessControlAllowOrigin ).header( Constants.ACCESS_CONTROL_ALLOW_METHODS, Constants.METHODS_LIST )
217                     .header( Constants.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString( ) ).build( );
218        
219     }
220     
221     
222     /**
223      * Get User Informations
224      * 
225      * @param nVersion
226      *            the Api version
227      * @param request
228      *            the http servlet request
229      * @param response
230      *            the http servlet response
231      * @return
232      */
233     @GET
234     @Path( Constants.PATH_MYAPPS )
235     @Produces( MediaType.APPLICATION_JSON )
236     public Response getMyApps( @PathParam( Constants.API_VERSION ) Integer nVersion, @Context HttpServletRequest request,
237             @Context HttpServletResponse response )
238     {
239         switch( nVersion )
240         {
241             case VERSION_1:
242                 return getMyAppsV1( request, response );
243 
244             default:
245                 break;
246         }
247 
248         throw new WebApplicationException( Response.Status.NOT_FOUND  );
249     }
250 
251   
252     /**
253      *  Get User Informations V1
254      * @param request the request 
255      * @param response the http servlet response
256      * @return user informations
257      */
258     private Response getMyAppsV1( HttpServletRequest request, HttpServletResponse response )
259     {
260 
261         return callBandeauSite( request, response, x -> RemoteSiteBandeauClientService.getInstance( ).getMyApps( x.getName( )));
262 
263     }
264     
265     /**
266      * Get User Informations
267      * 
268      * @param nVersion
269      *            the Api version
270      * @param request
271      *            the http servlet request
272      * @param response
273      *            the http servlet response
274      * @return
275      */
276     @GET
277     @Path( Constants.PATH_NOTIFICATIONS )
278     @Produces( MediaType.APPLICATION_JSON )
279     public Response getNotifications( @PathParam( Constants.API_VERSION ) Integer nVersion, @Context HttpServletRequest request,
280             @Context HttpServletResponse response )
281     {
282         switch( nVersion )
283         {
284             case VERSION_1:
285                 return getNotificationsV1( request, response );
286 
287             default:
288                 break;
289         }
290 
291         throw new WebApplicationException( Response.Status.NOT_FOUND  );
292     }
293 
294   
295     /**
296      *  Get User Informations V1
297      * @param request the request 
298      * @param response the http servlet response
299      * @return user informations
300      */
301     private Response getNotificationsV1( HttpServletRequest request, HttpServletResponse response )
302     {
303 
304         return callBandeauSite( request, response, x -> RemoteSiteBandeauClientService.getInstance( ).getNotifications( x.getName( )));
305 
306     }
307     
308     
309     
310     
311     
312     /**
313      * Get User Informations
314      * 
315      * @param nVersion
316      *            the Api version
317      * @param request
318      *            the http servlet request
319      * @param response
320      *            the http servlet response
321      * @return
322      */
323     @GET
324     @Path( Constants.PATH_MY_FAVORITES )
325     @Produces( MediaType.APPLICATION_JSON )
326     public Response getMyFavorites( @PathParam( Constants.API_VERSION ) Integer nVersion, @Context HttpServletRequest request,
327             @Context HttpServletResponse response )
328     {
329         switch( nVersion )
330         {
331             case VERSION_1:
332                 return getMyFavoritesV1( request, response );
333 
334             default:
335                 break;
336         }
337 
338         throw new WebApplicationException( Response.Status.NOT_FOUND  );
339     }
340 
341   
342     /**
343      *  Get User Informations V1
344      * @param request the request 
345      * @param response the http servlet response
346      * @return user informations
347      */
348     private Response getMyFavoritesV1( HttpServletRequest request, HttpServletResponse response )
349     {
350 
351         return callBandeauSite( request, response, x -> RemoteSiteBandeauClientService.getInstance( ).getMyFavorites( x.getName( )));
352 
353     }
354     
355     
356     
357     
358     
359     /**
360      *  Get User Informations V1
361      * @param request the request 
362      * @param response the http servlet response
363      * @return user informations
364      */
365     private Response callBandeauSite( HttpServletRequest request, HttpServletResponse response ,Function<LuteceUser,String> functGetBandeauResponse )
366     {
367 
368         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
369 
370         String strHeaderOrigin = request.getHeader( Constants.HEADER_ORIGIN );
371         String strAccessControlAllowOrigin = strHeaderOrigin;
372         if ( user != null )
373         {
374 
375             String strBandeauSiteResponse=functGetBandeauResponse.apply( user);
376             
377             if(!StringUtils.isEmpty(strBandeauSiteResponse))
378             {
379                 return Response.status( Response.Status.OK ).entity(strBandeauSiteResponse )
380                     .header( Constants.ACCESS_CONTROL_ALLOW_ORIGIN, strAccessControlAllowOrigin ).header( Constants.ACCESS_CONTROL_ALLOW_METHODS, Constants.METHODS_LIST )
381                     .header( Constants.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString( ) ).build( );
382             }     
383            }
384     
385 
386         return Response.status( Response.Status.UNAUTHORIZED )
387                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Constants.ERROR_USER_NOT_AUTHENTICATED, Constants.ERROR_USER_NOT_AUTHENTICATED ) ) )
388                 .header( Constants.ACCESS_CONTROL_ALLOW_ORIGIN, strAccessControlAllowOrigin ).header( Constants.ACCESS_CONTROL_ALLOW_METHODS, Constants.METHODS_LIST )
389                 .header( Constants.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString( ) ).build( );
390 
391     }
392     
393     
394 }