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.ObjectMapper;
38  import com.google.gson.reflect.TypeToken;
39  import fr.paris.lutece.plugins.referentielfo.v1.model.CycleScolaire;
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.ArrayList;
46  import java.util.List;
47  import java.util.Map;
48  import java.util.Optional;
49  
50  public class ReferentielCycleScolaireService extends AbstractWSReferentiel implements IReferentielCycleScolaireService
51  {
52      @Deprecated
53      public ReferentielCycleScolaireService( String referentielBaseUrl )
54      {
55          super( referentielBaseUrl );
56      }
57      public ReferentielCycleScolaireService( String referentielBaseUrl, String xGraviteeApiKey )
58      {
59          super( referentielBaseUrl, xGraviteeApiKey );
60      }
61  
62      @Override
63      public CycleScolaire recuperationCycleScolaireParValeur( Integer valeur ) throws ReferentielException
64      {
65          HttpAccess httpAccess = new HttpAccess( );
66          try
67          {
68              String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_CYCLESCOLAIRE_PAR_VALEUR + valeur;
69              Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
70              String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
71  
72              gererErreur( response );
73  
74              ObjectMapper mapper = new ObjectMapper();
75  
76              return mapper.readValue(response, CycleScolaire.class);
77          }
78          catch( HttpAccessException e )
79          {
80              AppLogService.error("error during mapping response", e);
81              throw new ReferentielException( e );
82          }
83          catch ( JsonProcessingException e )
84          {
85              throw new ReferentielException( e );
86          }
87      }
88  
89      @Override
90      public List<CycleScolaire> recuperationListeCycleScolaire( ) throws ReferentielException
91      {
92          HttpAccess httpAccess = new HttpAccess( );
93          try
94          {
95              String apiUrl = getReferentielBaseUrl( ) + Constants.RECUPERATION_LISTE_CYCLESCOLAIRE;
96              Map<String, String> mapHeadersRequest = genereHeaderGravitee( null );
97              String response = httpAccess.doGet( apiUrl, null, null, mapHeadersRequest );
98  
99              gererErreur( response );
100 
101             return ( List<CycleScolaire> ) Optional.ofNullable( getGson( ).fromJson( response, new TypeToken<ArrayList<CycleScolaire>>( ) {}.getType( ) ) ).orElse( new ArrayList<>(  ) );
102         }
103         catch( HttpAccessException e )
104         {
105             AppLogService.error("error during mapping response", e);
106             throw new ReferentielException( e );
107         }
108     }
109 }