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.address.rs;
35
36 import fr.paris.lutece.plugins.address.service.AddressServiceProvider;
37 import fr.paris.lutece.plugins.rest.service.RestConstants;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.util.ReferenceList;
40 import fr.paris.lutece.util.json.ErrorJsonResponse;
41 import fr.paris.lutece.util.json.JsonResponse;
42 import fr.paris.lutece.util.json.JsonUtil;
43 import java.rmi.RemoteException;
44
45 import org.apache.log4j.Logger;
46 import javax.ws.rs.GET;
47 import javax.ws.rs.Path;
48 import javax.ws.rs.PathParam;
49 import javax.ws.rs.Produces;
50 import javax.ws.rs.core.MediaType;
51 import javax.ws.rs.core.Response;
52
53
54
55
56 @Path( RestConstants.BASE_PATH + Constants.API_PATH + Constants.VERSION_PATH )
57 public class AddressRest
58 {
59 private static final int VERSION_1 = 1;
60 private final Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
61
62
63 private static final String MSG_ERROR_GET_ADDRESSES = "address.message.getAdress.error";
64
65
66
67
68
69
70
71
72
73
74 @GET
75 @Path( Constants.ID_SEARCH_ADR )
76 @Produces( MediaType.APPLICATION_JSON )
77 public Response getAdressList( @PathParam( Constants.VERSION ) Integer nVersion, @PathParam( Constants.TERM ) String term )
78 {
79 switch( nVersion )
80 {
81 case VERSION_1:
82 return getAddressList( term );
83 default:
84 break;
85 }
86 _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
87 return Response.status( Response.Status.NOT_FOUND )
88 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
89 }
90
91
92
93
94
95
96
97 private Response getAddressList( String term )
98 {
99
100 ReferenceList list = null;
101 try
102 {
103 if ( "RestAddressService".equals( AddressServiceProvider.getInstanceClass( ) ) )
104 {
105 list = AddressServiceProvider.searchAddress( null, term );
106 }
107
108 }
109 catch( RemoteException e )
110 {
111 AppLogService.error( e );
112 }
113
114 if ( list == null )
115 {
116 _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
117 return Response.status( Response.Status.NOT_FOUND )
118 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), MSG_ERROR_GET_ADDRESSES ) ) ).build( );
119 }
120
121 return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( list ) ) ).build( );
122 }
123
124 }