1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
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 }