View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * The Entreprise API Class.
56   */
57  public class ApiEntrepriseService
58  {
59  
60      /** The Constant PARAMETER_OBJECT. */
61      private static final String PARAMETER_OBJECT = "object";
62  
63      /** The Constant PARAMETER_RECIPIENT. */
64      private static final String PARAMETER_RECIPIENT = "recipient";
65  
66      /** The Constant API_ENTREPRISE_SIREN_NAME. */
67      private static final String API_ENTREPRISE_SIREN_NAME = "insee/sirene/unites_legales";
68  
69      /** The Constant API_ENTREPRISE_SIREN_VERSION. */
70      private static final String API_ENTREPRISE_SIREN_VERSION = "v3";
71  
72      /** The Constant API_TVA_SIREN_PART1. */
73      private static final String API_TVA_SIREN_PART1 = "european_commission/unites_legales";
74  
75      /** The Constant API_TVA_SIREN_PART2. */
76      private static final String API_TVA_SIREN_PART2 = "numero_tva";
77  
78      /** The Constant API_TVA_SIREN_VERSION. */
79      private static final String API_TVA_SIREN_VERSION = "v3";
80  
81      /** The Constant API_ETABLISSEMENT_SIRET_NAME. */
82      private static final String API_ETABLISSEMENT_SIRET_NAME = "insee/sirene/etablissements";
83  
84      /** The Constant API_ETABLISSEMENT_SIRET_VERSION. */
85      private static final String API_ETABLISSEMENT_SIRET_VERSION = "v3";
86  
87      /** The Constant API_ASSOCIATION_RNA_NAME. */
88      private static final String API_ASSOCIATION_RNA_NAME = "ministere_interieur/api-asso/associations";
89  
90      /** The Constant API_ASSOCIATION_RNA_VERSION. */
91      private static final String API_ASSOCIATION_RNA_VERSION = "v4";
92  
93      /** The Context Prameter name. */
94      private static final String PARAMETER_CONTEXT = "context";
95  
96      /** The instance. */
97      private static ApiEntrepriseService _instance;
98  
99      /** The str endpoint. */
100     private static String _strEndpoint;
101 
102     /**
103      * Gets the single instance of ApiRTUService.
104      *
105      * @param endpoint
106      *            the endpoint
107      * @return single instance of ApiRTUService
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      * Gets the structure.
121      *
122      * @param siren
123      *            the siren
124      * @param token
125      *            the token
126      * @param context
127      *            the context
128      * @param recipient
129      *            the recipient
130      * @param object
131      *            the object
132      * @return the structure
133      * @throws ApiRtuUnavailableException
134      *             the api Entreprise unavailable exception
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      * Gets the structure.
150      *
151      * @param siren
152      *            the siren
153      * @param token
154      *            the token
155      * @param context
156      *            the context
157      * @param recipient
158      *            the recipient
159      * @param object
160      *            the object
161      * @return the structure
162      * @throws ApiRtuUnavailableException
163      *             the api Entreprise unavailable exception
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      * Gets the structure.
179      *
180      * @param siret
181      *            the siret
182      * @param token
183      *            the token
184      * @param context
185      *            the context
186      * @param recipient
187      *            the recipient
188      * @param object
189      *            the object
190      * @return the structure
191      * @throws ApiRtuUnavailableException
192      *             the api Entreprise unavailable exception
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      * Gets the structure.
208      *
209      * @param rna
210      *            the rna
211      * @param token
212      *            the token
213      * @param context
214      *            the context
215      * @param recipient
216      *            the recipient
217      * @param object
218      *            the object
219      * @return the structure
220      * @throws ApiRtuUnavailableException
221      *             the api Entreprise unavailable exception
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      * Return the endpoint.
236      *
237      * @return the endpoint
238      */
239     public String getEndpoint( )
240     {
241         return _strEndpoint;
242     }
243 
244     /**
245      * Gets the.
246      *
247      * @param apiName
248      *            the api name
249      * @param version
250      *            the version
251      * @param siren
252      *            the siren
253      * @param token
254      *            the token
255      * @param context
256      *            the context
257      * @param recipient
258      *            the recipient
259      * @param object
260      *            the object
261      * @return the t
262      * @throws ApiRtuUnavailableException
263      *             the api rtu unavailable exception
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      * Gets the retour api.
276      *
277      * @param <T>
278      *            the generic type
279      * @param strUrl
280      *            the str url
281      * @param typeRetour
282      *            the type retour
283      * @param token
284      *            token
285      * @return the retour api
286      * @throws ApiRtuUnavailableException
287      *             the api rtu unavailable exception
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      * Gets the api entreprise url.
329      *
330      * @param apiName
331      *            the api name
332      * @param version
333      *            the version
334      * @param siren
335      *            the siren
336      * @param context
337      *            the context
338      * @param recipient
339      *            the recipient
340      * @param object
341      *            the object
342      * @return the api entreprise url
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      * Gets the api tva url.
353      *
354      * @param siren
355      *            the siren
356      * @param context
357      *            the context
358      * @param recipient
359      *            the recipient
360      * @param object
361      *            the object
362      * @return the api entreprise url
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      * Add the parameters to the entreprise url.
373      *
374      * @param context
375      *            the context
376      * @param recipient
377      *            the recipient
378      * @param object
379      *            the object
380      * @return the api entreprise url
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      * Gets the.
391      *
392      * @param siren
393      *            the siren
394      * @param token
395      *            the token
396      * @param context
397      *            the context
398      * @param recipient
399      *            the recipient
400      * @param object
401      *            the object
402      * @return the t
403      * @throws ApiRtuUnavailableException
404      *             the api rtu unavailable exception
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      * Gets the.
431      *
432      * @param apiName
433      *            the api name
434      * @param version
435      *            the version
436      * @param siren
437      *            the siren
438      * @param token
439      *            the token
440      * @param context
441      *            the context
442      * @param recipient
443      *            the recipient
444      * @param object
445      *            the object
446      * @return the t
447      * @throws ApiRtuUnavailableException
448      *             the api rtu unavailable exception
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      * Gets the.
459      *
460      * @param apiName
461      *            the api name
462      * @param version
463      *            the version
464      * @param siren
465      *            the siren
466      * @param token
467      *            the token
468      * @param context
469      *            the context
470      * @param recipient
471      *            the recipient
472      * @param object
473      *            the object
474      * @return the t
475      * @throws ApiRtuUnavailableException
476      *             the api rtu unavailable exception
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 }