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  
35  package fr.paris.lutece.plugins.facturationbo.rs;
36  
37  import com.google.gson.Gson;
38  import com.google.gson.GsonBuilder;
39  import fr.paris.lutece.plugins.facturationbo.business.model.exception.FacturationException;
40  import fr.paris.lutece.util.json.ErrorJsonResponse;
41  import org.apache.commons.lang3.StringUtils;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  public class AbstractWSFacturation
47  {
48      private final Gson _gson;
49      private final String _referentielBaseUrl;
50      private final String _xGraviteeApiKey;
51  
52      @Deprecated
53      public AbstractWSFacturation( String referentielBaseUrl )
54      {
55          // Creates the json object which will manage the information received
56          GsonBuilder builder = new GsonBuilder();
57  
58          _gson = builder.create();
59          _referentielBaseUrl = referentielBaseUrl;
60          _xGraviteeApiKey = null;
61      }
62  
63      @Deprecated
64      public AbstractWSFacturation( Gson gson, String referentielBaseUrl )
65      {
66          _gson = gson;
67          _referentielBaseUrl = referentielBaseUrl;
68          _xGraviteeApiKey = null;
69      }
70  
71      public AbstractWSFacturation( String referentielBaseUrl, String xGraviteeApiKey )
72      {
73          // Creates the json object which will manage the information received
74          GsonBuilder builder = new GsonBuilder();
75  
76          _gson = builder.create();
77          _referentielBaseUrl = referentielBaseUrl;
78          _xGraviteeApiKey = xGraviteeApiKey;
79      }
80  
81      public AbstractWSFacturation( Gson gson, String referentielBaseUrl, String xGraviteeApiKey )
82      {
83          _gson = gson;
84          _referentielBaseUrl = referentielBaseUrl;
85          _xGraviteeApiKey = xGraviteeApiKey;
86      }
87  
88      protected void gererErreur( String response ) throws FacturationException
89      {
90          if ( StringUtils.isNotEmpty( response ) && response.contains( "errorCode" ) && response.contains( "message" ) )
91          {
92              //cas d'erreur
93              String errorCode = StringUtils.EMPTY;
94              String message = StringUtils.EMPTY;
95  
96              String responseClean = response.replaceAll( "\"", "" ).replaceAll( "}", "" ).replaceAll( "\\{", "" );
97              String[] split = responseClean.split( "," );
98              for ( String elmt : split )
99              {
100                 String[] split1 = elmt.split( ":" );
101                 switch ( split1[0] )
102                 {
103                     case "errorCode":
104                         errorCode = split1[1];
105                         break;
106                     case "message":
107                         message = split1[1];
108                         break;
109                 }
110             }
111             ErrorJsonResponse errorJsonResponse = new ErrorJsonResponse( errorCode, message );
112             throw new FacturationException( errorJsonResponse );
113         }
114     }
115 
116     public Gson getGson( )
117     {
118         return _gson;
119     }
120 
121     public String getReferentielBaseUrl( )
122     {
123         return _referentielBaseUrl;
124     }
125 
126     protected Map<String, String> genereHeaderGravitee( Map<String, String> mapHeadersRequest )
127     {
128         if ( mapHeadersRequest == null )
129         {
130             mapHeadersRequest = new HashMap<>( );
131         }
132 
133         mapHeadersRequest.put( Constants.CLE_X_GRAVITEE_API_KEY, _xGraviteeApiKey );
134 
135         return mapHeadersRequest;
136     }
137 
138     public String getxGraviteeApiKey( )
139     {
140         return _xGraviteeApiKey;
141     }
142 }