View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.plugins.referentielfo.v1.rs;
35  
36  import com.fasterxml.jackson.core.JsonProcessingException;
37  import com.fasterxml.jackson.databind.JsonMappingException;
38  import com.fasterxml.jackson.databind.ObjectMapper;
39  import fr.paris.lutece.plugins.referentielfo.v1.model.ArrondissementDto;
40  import fr.paris.lutece.plugins.referentielfo.v1.model.exception.ReferentielException;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  import fr.paris.lutece.util.httpaccess.HttpAccess;
43  import fr.paris.lutece.util.httpaccess.HttpAccessException;
44  
45  import java.util.Collections;
46  import java.util.List;
47  import java.util.Map;
48  
49  public class ReferentielArrondissementService extends AbstractWSReferentiel implements IReferentielArrondissementService
50  {
51      @Deprecated
52      public ReferentielArrondissementService( String referentielBaseUrl )
53      {
54          super( referentielBaseUrl );
55      }
56      public ReferentielArrondissementService( String referentielBaseUrl, String xGraviteeApiKey )
57      {
58          super( referentielBaseUrl, xGraviteeApiKey );
59      }
60  
61      @Override
62      public List<ArrondissementDto> recuperationArrondissements( ) throws ReferentielException
63      {
64          HttpAccess httpAccess = new HttpAccess( );
65          try
66          {
67              String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_ARRONDISSEMENTS;
68              Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
69              String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
70  
71              gererErreur( response );
72  
73              return Collections.singletonList( getGson( ).fromJson( response, ArrondissementDto.class ) );
74          }
75          catch( HttpAccessException e )
76          {
77              AppLogService.error("error during mapping response", e);
78              throw new ReferentielException( e );
79          }
80      }
81  
82       @Override
83      public ArrondissementDto recupererArrondissementParId( String id ) throws ReferentielException
84      {
85          HttpAccess httpAccess = new HttpAccess( );
86          try
87          {
88              String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_ARRONDISSEMENT_PAR_ID + id;
89              Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
90              String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
91  
92              gererErreur( response );
93  
94              ObjectMapper mapper = new ObjectMapper();
95  
96              return mapper.readValue(response, ArrondissementDto.class);
97          }
98          catch( HttpAccessException e )
99          {
100             AppLogService.error("error during mapping response", e);
101             throw new ReferentielException( e );
102         }
103         catch ( JsonProcessingException e )
104         {
105             throw new ReferentielException( e );
106         }
107     }
108 
109     @Override
110     public ArrondissementDto recuperationArrondissementParSecteurId( String id ) throws ReferentielException
111     {
112         HttpAccess httpAccess = new HttpAccess( );
113         try
114         {
115             String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_ARRONDISSEMENT_PAR_SECTEUR_ID + id;
116             Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
117             String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
118 
119             gererErreur( response );
120 
121             ObjectMapper mapper = new ObjectMapper();
122 
123             return mapper.readValue(response, ArrondissementDto.class);
124         }
125         catch( HttpAccessException e )
126         {
127             AppLogService.error("error during mapping response", e);
128             throw new ReferentielException( e );
129         }
130         catch ( JsonProcessingException e )
131         {
132             throw new ReferentielException( e );
133         }
134     }
135 }