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.geocode.v1.web.rs.service;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.JavaType;
38 import com.fasterxml.jackson.databind.JsonNode;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 import fr.paris.lutece.plugins.geocode.v1.web.rs.util.Constants;
42 import fr.paris.lutece.plugins.geocode.v1.web.service.CustomResponseStatusValidator;
43 import fr.paris.lutece.plugins.geocode.v1.web.service.IHttpTransportProvider;
44 import fr.paris.lutece.util.httpaccess.HttpAccess;
45 import fr.paris.lutece.util.httpaccess.InvalidResponseStatus;
46 import org.apache.hc.core5.http.HttpStatus;
47 import org.apache.hc.core5.net.URIBuilder;
48 import org.apache.log4j.Logger;
49
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.Map;
53
54
55
56
57 public class HttpAccessTransport implements IHttpTransportProvider
58 {
59 private static Logger _logger = Logger.getLogger( HttpAccessTransport.class );
60
61 protected HttpAccess _httpClient;
62 protected String _strEndPoint;
63
64 public HttpAccessTransport( )
65 {
66 this._httpClient = new HttpAccess( CustomResponseStatusValidator.getInstance( ) );
67 }
68
69 @Override
70 public <T> T doGet(String strEndPointUrl, Map<String, String> mapParams, Map<String, String> mapHeadersRequest,
71 Class<T> responseJsonClass, ObjectMapper mapper) throws Exception
72 {
73 String strResponseJSON = "";
74 T oResponse = null;
75
76 try
77 {
78 URIBuilder uriBuilder = new URIBuilder( strEndPointUrl );
79
80 if ( ( mapParams != null ) && !mapParams.isEmpty( ) )
81 {
82 for ( String strParamKey : mapParams.keySet( ) )
83 {
84 uriBuilder.addParameter( strParamKey, mapParams.get( strParamKey ) );
85 }
86 }
87
88 addAuthentication( mapHeadersRequest );
89 strResponseJSON = this._httpClient.doGet( uriBuilder.toString( ), null, null, mapHeadersRequest );
90 oResponse = mapJson( mapper, strResponseJSON, responseJsonClass );
91 }
92 catch( Exception e )
93 {
94 handleExceptionGeocode( e );
95 }
96
97 return oResponse;
98 }
99
100 @Override
101 public <T> List<T> doGetList(String strEndPointUrl, Map<String, String> mapParams, Map<String, String> mapHeadersRequest,
102 Class<T> responseJsonClass, ObjectMapper mapper) throws Exception
103 {
104 String strResponseJSON = "";
105 List<T> oResponse = null;
106
107 try
108 {
109 URIBuilder uriBuilder = new URIBuilder( strEndPointUrl );
110
111 if ( ( mapParams != null ) && !mapParams.isEmpty( ) )
112 {
113 for ( String strParamKey : mapParams.keySet( ) )
114 {
115 uriBuilder.addParameter( strParamKey, mapParams.get( strParamKey ) );
116 }
117 }
118
119 addAuthentication( mapHeadersRequest );
120 strResponseJSON = this._httpClient.doGet( uriBuilder.toString( ), null, null, mapHeadersRequest );
121 oResponse = mapJsonList( mapper, strResponseJSON, responseJsonClass );
122 }
123 catch( Exception e )
124 {
125 handleExceptionGeocode( e );
126 }
127
128 return oResponse;
129 }
130
131
132
133
134
135
136
137
138
139
140
141 protected void handleExceptionGeocode( Exception e ) throws Exception
142 {
143 String strError = "LibraryIdentityStore - Error HttpAccessTransport :";
144 _logger.error( strError + e.getMessage( ), e );
145
146 if ( e instanceof InvalidResponseStatus && HttpStatus.SC_NOT_FOUND == ( (InvalidResponseStatus) e ).getResponseStatus( )
147 || e instanceof Exception )
148 {
149
150 throw new Exception( strError, e );
151 }
152 else
153 {
154 throw new Exception( strError, e );
155 }
156 }
157
158
159
160
161
162
163
164
165 protected void addAuthentication( Map<String, String> mapHeadersRequest ) throws Exception
166 {
167
168 }
169
170
171
172
173
174
175 public void setApiEndPointUrl( String strApiEndPointUrl )
176 {
177
178 _strEndPoint = strApiEndPointUrl;
179 }
180
181
182
183
184
185
186 public String getApiEndPointUrl( )
187 {
188
189 return _strEndPoint;
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203 private <T> T mapJson( final ObjectMapper mapper, final String jsonStr, final Class<T> responseClass )
204 throws JsonProcessingException, InstantiationException, IllegalAccessException
205 {
206 T response = null;
207 try
208 {
209 JsonNode root = mapper.readTree( jsonStr );
210 JsonNode rootResult = root.get( Constants.PARAM_RESULT );
211 String strResult = rootResult.toPrettyString( );
212 response = mapper.readValue( strResult, responseClass );
213 }
214 catch( final Exception e )
215 {
216 String strError = "API GEOCODE - Error converting to Object from JSON '" + jsonStr + "' : ";
217 _logger.error( strError + e.getMessage( ), e );
218 }
219 return response;
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233 private <T> List<T> mapJsonList( final ObjectMapper mapper, final String jsonStr, final Class<T> responseClass )
234 throws JsonProcessingException, InstantiationException, IllegalAccessException
235 {
236 List<T> responseList = new ArrayList<>( );
237 JavaType responseListClassType = mapper.getTypeFactory( ).constructCollectionType( List.class, responseClass );
238 try
239 {
240 JsonNode root = mapper.readTree( jsonStr );
241 JsonNode rootResult = root.get( Constants.PARAM_RESULT );
242 String strResult = rootResult.toPrettyString( );
243 responseList = mapper.readValue( strResult, responseListClassType );
244 }
245 catch( final Exception e1 )
246 {
247 try
248 {
249
250
251 String strError = "API GEOCODE - Error converting to Object from JSON '" + jsonStr + "' : ";
252 _logger.error( strError + e1.getMessage( ), e1 );
253 }
254 catch( final Exception e2 )
255 {
256 String strError = "API GEOCODE - Error converting to Object from JSON '" + jsonStr + "' : ";
257 _logger.error( strError + e2.getMessage( ), e2 );
258 }
259 }
260 return responseList;
261 }
262
263 }