1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
74
75
76
77
78
79
80
81
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
104
105
106
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
160
161
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
178
179
180
181
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
204
205
206
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
224
225
226
227
228
229
230
231
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
254
255
256
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
267
268
269
270
271
272
273
274
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
297
298
299
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
314
315
316
317
318
319
320
321
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
344
345
346
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
361
362
363
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 }