View Javadoc
1   package fr.paris.lutece.plugins.rtu.service;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.IOException;
5   
6   import javax.xml.bind.JAXBContext;
7   import javax.xml.bind.JAXBException;
8   import javax.xml.bind.Unmarshaller;
9   
10  import com.fasterxml.jackson.databind.DeserializationFeature;
11  import com.fasterxml.jackson.databind.JsonNode;
12  import com.fasterxml.jackson.databind.ObjectMapper;
13  
14  import fr.paris.lutece.plugins.rtu.business.Entreprise;
15  import fr.paris.lutece.plugins.rtu.business.RetourApiAssociation;
16  import fr.paris.lutece.plugins.rtu.business.RetourApiEntreprise;
17  import fr.paris.lutece.plugins.rtu.business.RetourApiEtablissement;
18  import fr.paris.lutece.portal.service.util.AppException;
19  import fr.paris.lutece.portal.service.util.AppLogService;
20  import fr.paris.lutece.util.httpaccess.HttpAccess;
21  import fr.paris.lutece.util.httpaccess.HttpAccessException;
22  
23  // TODO: Auto-generated Javadoc
24  /**
25   * The Class AbstractRestClient.
26   *
27   * @param <T> the generic type
28   */
29  public abstract  class AbstractRestClient<T> {
30  
31  	
32  	/** The classe. */
33      private Class<T> classe;
34  
35      /**
36       * Instantiates a new abstract rest client.
37       *
38       * @param classe
39       *            the classe
40       */
41      public AbstractRestClient( Class<T> classe )
42      {
43          this.classe = classe;
44  
45      }
46  
47      /** The instance. */
48      private JAXBContext instance;
49  
50      /**
51       * Inits the context.
52       *
53       * @return the JAXB context
54       */
55      public synchronized JAXBContext initContext( )
56      {
57          try
58          {
59              if ( instance == null )
60                  instance = JAXBContext.newInstance( classe );
61          } catch ( JAXBException e )
62          {
63          }
64          return instance;
65      }
66  
67      /**
68       * Gets the endpoint.
69       *
70       * @return the endpoint
71       */
72      protected abstract String getEndpoint( );
73  
74      /**
75       * Checks if is proxy only.
76       *
77       * @return true, if is proxy only
78       */
79      protected abstract boolean isProxyOnly( );
80  
81      /**
82       * Gets the.
83       *
84       * @param apiName            the api name
85       * @param version the version
86       * @param siren the siren
87       * @param token the token
88       * @param context the context
89       * @param recipient the recipient
90       * @param object the object
91       * @return the t
92       * @throws ApiRtuUnavailableException the api rtu unavailable exception
93       */
94      protected RetourApiEntreprise get( String apiName,String version,  String siren, String token, String context, String recipient, String object ) throws ApiRtuUnavailableException
95      {
96          RetourApiEntreprise rent = null;
97  
98          HttpAccess httpAccess = new HttpAccess( );
99  
100         String response = null;
101         try
102         {
103             String strUrl = getEndpoint( ) + "/" + version + "/" + apiName + "/" + siren + "?token=" + token + "&context="+ context + "&recipient=" + recipient + "&object=" + object ;
104             AppLogService.info( "Appel de l'api entreprise: " + strUrl );
105             response = httpAccess.doGet( strUrl );
106             AppLogService.debug( "Trame à analyser en debug:" + response );
107         } catch ( HttpAccessException e )
108         {
109             AppLogService.error( "Une erreur est survenue lors de l'appel l'API ENTREPRISE", e );
110             AppLogService.error( "Trame en erreur:" + response );
111             throw new ApiRtuUnavailableException( e.getResponseCode() );
112         }
113 
114         
115         ObjectMapper objectMapper = new ObjectMapper();
116         objectMapper.configure(
117         	    DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118         
119         try {
120         	rent =  objectMapper.readValue(response, RetourApiEntreprise.class);
121         }
122         catch ( IOException  e )
123         {
124             AppLogService.error( "Une erreur est survenue lors de la récupération des données de l'API entreprise", e );
125             AppLogService.error( "Trame en erreur:" + response );
126             throw new ApiRtuUnavailableException( );
127         }
128 
129         return rent;
130     }
131     
132     /**
133      * Gets the.
134      *
135      * @param apiName            the api name
136      * @param version the version
137      * @param siren the siren
138      * @param token the token
139      * @param context the context
140      * @param recipient the recipient
141      * @param object the object
142      * @return the t
143      * @throws ApiRtuUnavailableException the api rtu unavailable exception
144      */
145     protected RetourApiEtablissement getEtablissement( String apiName,String version,  String siren, String token, String context, String recipient, String object ) throws ApiRtuUnavailableException
146     {
147         RetourApiEtablissement rent = null;
148 
149         HttpAccess httpAccess = new HttpAccess( );
150 
151         String response = null;
152         try
153         {
154             String strUrl = getEndpoint( ) + "/" + version + "/" + apiName + "/" + siren + "?token=" + token + "&context="+ context + "&recipient=" + recipient + "&object=" + object ;
155             AppLogService.info( "Appel de l'api entreprise: " + strUrl );
156             response = httpAccess.doGet( strUrl );
157             AppLogService.debug( "Trame à analyser en debug:" + response );
158         } catch ( HttpAccessException e )
159         {
160             AppLogService.error( "Une erreur est survenue lors de l'appel l'API ENTREPRISE", e );
161             AppLogService.error( "Trame en erreur:" + response );
162             throw new ApiRtuUnavailableException( e.getResponseCode() );
163         }
164 
165         
166         ObjectMapper objectMapper = new ObjectMapper();
167         objectMapper.configure(
168         	    DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169         
170         try {
171         	rent =  objectMapper.readValue(response, RetourApiEtablissement.class);
172         }
173         catch ( IOException  e )
174         {
175             AppLogService.error( "Une erreur est survenue lors de la récupération des données de l'API entreprise", e );
176             AppLogService.error( "Trame en erreur:" + response );
177             throw new ApiRtuUnavailableException( );
178         }
179 
180         return rent;
181     }
182 
183     /**
184      * Gets the.
185      *
186      * @param apiName            the api name
187      * @param version the version
188      * @param siren the siren
189      * @param token the token
190      * @param context the context
191      * @param recipient the recipient
192      * @param object the object
193      * @return the t
194      * @throws ApiRtuUnavailableException the api rtu unavailable exception
195      */
196     protected RetourApiAssociation getAssociation( String apiName,String version,  String siren, String token, String context, String recipient, String object ) throws ApiRtuUnavailableException
197     {
198     	RetourApiAssociation rent = null;
199 
200         HttpAccess httpAccess = new HttpAccess( );
201 
202         String response = null;
203         try
204         {
205             String strUrl = getEndpoint( ) + "/" + version + "/" + apiName + "/" + siren + "?token=" + token + "&context="+ context + "&recipient=" + recipient + "&object=" + object ;
206             AppLogService.info( "Appel de l'api entreprise: " + strUrl );
207             response = httpAccess.doGet( strUrl );
208             AppLogService.debug( "Trame à analyser en debug:" + response );
209         } catch ( HttpAccessException e )
210         {
211             AppLogService.error( "Une erreur est survenue lors de l'appel l'API ENTREPRISE", e );
212             AppLogService.error( "Trame en erreur:" + response );
213             throw new ApiRtuUnavailableException( e.getResponseCode() );
214         }
215 
216         
217         ObjectMapper objectMapper = new ObjectMapper();
218         objectMapper.configure(
219         	    DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
220         
221         try {
222         	rent =  objectMapper.readValue(response, RetourApiAssociation.class);
223         }
224         catch ( IOException  e )
225         {
226             AppLogService.error( "Une erreur est survenue lors de la récupération des données de l'API entreprise", e );
227             AppLogService.error( "Trame en erreur:" + response );
228             throw new ApiRtuUnavailableException( );
229         }
230 
231         return rent;
232     }
233 
234 }