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.api.entreprise.service;
35
36 import java.io.IOException;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 import fr.paris.lutece.api.entreprise.business.RetourApiNumeroTva;
41 import org.springframework.http.HttpHeaders;
42
43 import com.fasterxml.jackson.databind.DeserializationFeature;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 import fr.paris.lutece.api.entreprise.business.RetourApiAssociation;
47 import fr.paris.lutece.api.entreprise.business.RetourApiEntreprise;
48 import fr.paris.lutece.api.entreprise.business.RetourApiEtablissement;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.util.httpaccess.HttpAccess;
51 import fr.paris.lutece.util.httpaccess.HttpAccessException;
52 import fr.paris.lutece.util.url.UrlItem;
53
54
55
56
57 public class ApiEntrepriseService
58 {
59
60
61 private static final String PARAMETER_OBJECT = "object";
62
63
64 private static final String PARAMETER_RECIPIENT = "recipient";
65
66
67 private static final String API_ENTREPRISE_SIREN_NAME = "insee/sirene/unites_legales";
68
69
70 private static final String API_ENTREPRISE_SIREN_VERSION = "v3";
71
72
73 private static final String API_TVA_SIREN_PART1 = "european_commission/unites_legales";
74
75
76 private static final String API_TVA_SIREN_PART2 = "numero_tva";
77
78
79 private static final String API_TVA_SIREN_VERSION = "v3";
80
81
82 private static final String API_ETABLISSEMENT_SIRET_NAME = "insee/sirene/etablissements";
83
84
85 private static final String API_ETABLISSEMENT_SIRET_VERSION = "v3";
86
87
88 private static final String API_ASSOCIATION_RNA_NAME = "ministere_interieur/api-asso/associations";
89
90
91 private static final String API_ASSOCIATION_RNA_VERSION = "v4";
92
93
94 private static final String PARAMETER_CONTEXT = "context";
95
96
97 private static ApiEntrepriseService _instance;
98
99
100 private static String _strEndpoint;
101
102
103
104
105
106
107
108
109 public static ApiEntrepriseService getInstance( String endpoint )
110 {
111 if ( _instance == null )
112 {
113 _instance = new ApiEntrepriseService( );
114 }
115 _strEndpoint = endpoint;
116 return _instance;
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public RetourApiEntreprise getEntrepriseSiren( String siren, String token, String context, String recipient, String object )
137 throws ApiRtuUnavailableException
138 {
139 if ( siren == null || "".equals( siren ) )
140 {
141 return new RetourApiEntreprise( );
142 }
143
144 return get( API_ENTREPRISE_SIREN_NAME, API_ENTREPRISE_SIREN_VERSION, siren, token, context, recipient, object );
145
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public RetourApiNumeroTva getNumeroTvaSiren( String siren, String token, String context, String recipient, String object )
166 throws ApiRtuUnavailableException
167 {
168 if ( siren == null || "".equals( siren ) )
169 {
170 return new RetourApiNumeroTva( );
171 }
172
173 return getNumeroTva( siren, token, context, recipient, object );
174
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public RetourApiEtablissement getEtablissementSiret( String siret, String token, String context, String recipient, String object )
195 throws ApiRtuUnavailableException
196 {
197 if ( siret == null || "".equals( siret ) )
198 {
199 return new RetourApiEtablissement( );
200 }
201
202 return getEtablissement( API_ETABLISSEMENT_SIRET_NAME, API_ETABLISSEMENT_SIRET_VERSION, siret, token, context, recipient, object );
203
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public RetourApiAssociation getAssociationRNA( String rna, String token, String context, String recipient, String object )
224 throws ApiRtuUnavailableException
225 {
226 if ( rna == null || "".equals( rna ) )
227 {
228 return new RetourApiAssociation( );
229 }
230 return getAssociation( API_ASSOCIATION_RNA_NAME, API_ASSOCIATION_RNA_VERSION, rna, token, context, recipient, object );
231
232 }
233
234
235
236
237
238
239 public String getEndpoint( )
240 {
241 return _strEndpoint;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 protected RetourApiEntreprise get( String apiName, String version, String siren, String token, String context, String recipient, String object )
266 throws ApiRtuUnavailableException
267 {
268 String strUrl = getApiEntrepriseUrl( apiName, version, siren, context, recipient, object );
269 RetourApiEntreprise retourApiEntreprise = getRetourApi( strUrl, RetourApiEntreprise.class, token );
270 retourApiEntreprise.setEtablissement_siege( getEtablissementSiret( retourApiEntreprise.getEntreprise().getSiret_siege_social( ), token, context, recipient, object ).getEtablissement() );
271 return retourApiEntreprise;
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 private <T> T getRetourApi( String strUrl, Class<T> typeRetour, String token ) throws ApiRtuUnavailableException
290 {
291
292 T rent = null;
293 HttpAccess httpAccess = new HttpAccess( );
294
295 String response = null;
296 try
297 {
298 AppLogService.info( "Appel de l'api entreprise: " + strUrl );
299 Map<String, String> headersRequest = new HashMap<>( );
300 headersRequest.put( HttpHeaders.AUTHORIZATION, "Bearer " + token );
301 response = httpAccess.doGet( strUrl, null, null, headersRequest );
302 AppLogService.debug( "Trame à analyser en debug:" + response );
303 }
304 catch( HttpAccessException e )
305 {
306 AppLogService.error( "Une erreur est survenue lors de l'appel l'API ENTREPRISE", e );
307 AppLogService.error( "Trame en erreur:" + response );
308 throw new ApiRtuUnavailableException( e.getResponseCode( ), e.getMessage( ) );
309 }
310
311 ObjectMapper objectMapper = new ObjectMapper( );
312 objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
313
314 try
315 {
316 rent = objectMapper.readValue( response, typeRetour );
317 }
318 catch( IOException e )
319 {
320 AppLogService.error( "Une erreur est survenue lors de la récupération des données de l'API entreprise", e );
321 AppLogService.error( "Trame en erreur:" + response );
322 throw new ApiRtuUnavailableException( );
323 }
324 return rent;
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 private String getApiEntrepriseUrl( String apiName, String version, String siren, String context, String recipient, String object )
345 {
346 UrlItem urlItem = new UrlItem( getEndpoint( ) + "/" + version + "/" + apiName + "/" + siren );
347 addUrlParameters( urlItem, context, recipient, object );
348 return urlItem.getUrl( );
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 private String getApiTvaUrl( String siren, String context, String recipient, String object )
365 {
366 UrlItem urlItem = new UrlItem( getEndpoint( ) + "/" + API_TVA_SIREN_VERSION + "/" + API_TVA_SIREN_PART1 + "/" + siren + "/" + API_TVA_SIREN_PART2 );
367 addUrlParameters( urlItem, context, recipient, object );
368 return urlItem.getUrl( );
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382 private void addUrlParameters( UrlItem urlItem, String context, String recipient, String object )
383 {
384 urlItem.addParameter( PARAMETER_CONTEXT, context );
385 urlItem.addParameter( PARAMETER_RECIPIENT, recipient );
386 urlItem.addParameter( PARAMETER_OBJECT, object );
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 private RetourApiNumeroTva getNumeroTva( String siren, String token, String context, String recipient, String object )
407 throws ApiRtuUnavailableException
408 {
409 RetourApiNumeroTva rtva = null;
410 try
411 {
412 String strUrl = getApiTvaUrl( siren, context, recipient, object );
413 rtva = getRetourApi( strUrl, RetourApiNumeroTva.class, token );
414 }
415 catch ( ApiRtuUnavailableException e )
416 {
417 if (e.getMessage().contains( "\"code\":\"28003\"" ))
418 {
419 AppLogService.debug( "Pas de numero de TVA intercommunautaire trouvé pour le Siren: " + siren );
420 }
421 else
422 {
423 throw e;
424 }
425 }
426 return rtva;
427 }
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450 private RetourApiEtablissement getEtablissement( String apiName, String version, String siren, String token, String context, String recipient,
451 String object ) throws ApiRtuUnavailableException
452 {
453 String strUrl = getApiEntrepriseUrl( apiName, version, siren, context, recipient, object );
454 return getRetourApi( strUrl, RetourApiEtablissement.class, token );
455 }
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478 private RetourApiAssociation getAssociation( String apiName, String version, String siren, String token, String context, String recipient, String object )
479 throws ApiRtuUnavailableException
480 {
481 String strUrl = getApiEntrepriseUrl( apiName, version, siren, context, recipient, object );
482 return getRetourApi( strUrl, RetourApiAssociation.class, token );
483 }
484
485 }