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.mylutece.modules.users.rs;
35
36 import fr.paris.lutece.plugins.mylutece.modules.users.service.search.LocalUserSearchEngine;
37 import fr.paris.lutece.plugins.mylutece.modules.users.web.LocalUserJspBean;
38 import fr.paris.lutece.plugins.rest.service.RestConstants;
39 import fr.paris.lutece.portal.business.user.AdminUser;
40 import fr.paris.lutece.portal.service.admin.AdminUserService;
41 import fr.paris.lutece.util.json.ErrorJsonResponse;
42 import fr.paris.lutece.util.json.JsonResponse;
43 import fr.paris.lutece.util.json.JsonUtil;
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.log4j.Logger;
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.ws.rs.GET;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.ws.rs.Path;
52 import javax.ws.rs.PathParam;
53 import javax.ws.rs.Produces;
54 import javax.ws.rs.core.Context;
55 import javax.ws.rs.core.MediaType;
56 import javax.ws.rs.core.Response;
57
58
59
60
61 @Path( RestConstants.BASE_PATH + Constants.API_PATH )
62 public class LocalUserSearchRestService
63 {
64 private final Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
65 private static final String BEAN_SEARCH_ENGINE = "mylutece-users.localUserSearchEngine";
66 private static final String PARAMETER_KEYWORDS = "keywords";
67
68
69
70
71
72
73
74
75 @GET
76 @Path( StringUtils.EMPTY )
77 @Produces( MediaType.APPLICATION_JSON )
78 public Response getSearchResultList( @PathParam( Constants.VERSION ) Integer nVersion, @Context HttpServletRequest request )
79 {
80 AdminUser adminUser = AdminUserService.getAdminUser( request );
81 String strKeywords = request.getParameter( PARAMETER_KEYWORDS );
82 if ( adminUser != null && adminUser.checkRight( LocalUserJspBean.RIGHT_MYLUTECEUSERSMANAGEMENT ) && !strKeywords.isEmpty( ) )
83 {
84 return getSearchResultList( strKeywords, request );
85 }
86 else
87 {
88 _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
89 return Response.status( Response.Status.NOT_FOUND )
90 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) )
91 .build( );
92 }
93 }
94
95
96
97
98
99
100 private Response getSearchResultList( String strQuery, HttpServletRequest request )
101 {
102 List<Map<String, String>> listResults = LocalUserSearchEngine.getSearchResults( strQuery, request );
103 if ( listResults == null || listResults.isEmpty( ) )
104 {
105 return Response.status( Response.Status.NO_CONTENT ).entity( JsonUtil.buildJsonResponse( new JsonResponse( Constants.EMPTY_OBJECT ) ) ).build( );
106 }
107 return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( listResults ) ) ).build( );
108 }
109 }