1 | |
package org.apache.maven.wagon.shared.http; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.http.Header; |
23 | |
import org.apache.http.message.BasicHeader; |
24 | |
import org.apache.http.params.BasicHttpParams; |
25 | |
import org.apache.http.params.CoreConnectionPNames; |
26 | |
import org.apache.http.params.DefaultedHttpParams; |
27 | |
import org.apache.http.params.HttpParams; |
28 | |
|
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.LinkedHashMap; |
32 | |
import java.util.List; |
33 | |
import java.util.Map; |
34 | |
import java.util.Properties; |
35 | |
import java.util.regex.Matcher; |
36 | |
import java.util.regex.Pattern; |
37 | |
|
38 | 5 | public class HttpMethodConfiguration |
39 | |
{ |
40 | |
|
41 | |
public static final int DEFAULT_CONNECTION_TIMEOUT = 60000; |
42 | |
|
43 | |
public static final int DEFAULT_READ_TIMEOUT = 60000; |
44 | |
|
45 | |
private static final String COERCE_PATTERN = "%(\\w+),(.+)"; |
46 | |
|
47 | |
private Boolean useDefaultHeaders; |
48 | |
|
49 | 5 | private Properties headers = new Properties(); |
50 | |
|
51 | 5 | private Properties params = new Properties(); |
52 | |
|
53 | 5 | private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
54 | |
|
55 | 5 | private int readTimeout = DEFAULT_READ_TIMEOUT; |
56 | |
|
57 | |
public boolean isUseDefaultHeaders() |
58 | |
{ |
59 | 2 | return useDefaultHeaders == null || useDefaultHeaders.booleanValue(); |
60 | |
} |
61 | |
|
62 | |
public HttpMethodConfiguration setUseDefaultHeaders( boolean useDefaultHeaders ) |
63 | |
{ |
64 | 1 | this.useDefaultHeaders = Boolean.valueOf( useDefaultHeaders ); |
65 | 1 | return this; |
66 | |
} |
67 | |
|
68 | |
public Boolean getUseDefaultHeaders() |
69 | |
{ |
70 | 0 | return useDefaultHeaders; |
71 | |
} |
72 | |
|
73 | |
public HttpMethodConfiguration addHeader( String header, String value ) |
74 | |
{ |
75 | 0 | headers.setProperty( header, value ); |
76 | 0 | return this; |
77 | |
} |
78 | |
|
79 | |
public Properties getHeaders() |
80 | |
{ |
81 | 0 | return headers; |
82 | |
} |
83 | |
|
84 | |
public HttpMethodConfiguration setHeaders( Properties headers ) |
85 | |
{ |
86 | 0 | this.headers = headers; |
87 | 0 | return this; |
88 | |
} |
89 | |
|
90 | |
public HttpMethodConfiguration addParam( String param, String value ) |
91 | |
{ |
92 | 2 | params.setProperty( param, value ); |
93 | 2 | return this; |
94 | |
} |
95 | |
|
96 | |
public Properties getParams() |
97 | |
{ |
98 | 0 | return params; |
99 | |
} |
100 | |
|
101 | |
public HttpMethodConfiguration setParams( Properties params ) |
102 | |
{ |
103 | 0 | this.params = params; |
104 | 0 | return this; |
105 | |
} |
106 | |
|
107 | |
public int getConnectionTimeout() |
108 | |
{ |
109 | 0 | return connectionTimeout; |
110 | |
} |
111 | |
|
112 | |
public HttpMethodConfiguration setConnectionTimeout( int connectionTimeout ) |
113 | |
{ |
114 | 0 | this.connectionTimeout = connectionTimeout; |
115 | 0 | return this; |
116 | |
} |
117 | |
|
118 | |
public int getReadTimeout() |
119 | |
{ |
120 | 2 | return readTimeout; |
121 | |
} |
122 | |
|
123 | |
public HttpMethodConfiguration setReadTimeout( int readTimeout ) |
124 | |
{ |
125 | 0 | this.readTimeout = readTimeout; |
126 | 0 | return this; |
127 | |
} |
128 | |
|
129 | |
public HttpParams asMethodParams( HttpParams defaults ) |
130 | |
{ |
131 | 2 | if ( !hasParams() ) |
132 | |
{ |
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | 2 | DefaultedHttpParams p = new DefaultedHttpParams( new BasicHttpParams(), defaults ); |
137 | |
|
138 | 2 | fillParams( p ); |
139 | |
|
140 | 2 | return p; |
141 | |
} |
142 | |
|
143 | |
private boolean hasParams() |
144 | |
{ |
145 | 4 | if ( connectionTimeout < 1 && params == null && readTimeout < 1 ) |
146 | |
{ |
147 | 0 | return false; |
148 | |
} |
149 | |
|
150 | 4 | return true; |
151 | |
} |
152 | |
|
153 | |
private void fillParams( HttpParams p ) |
154 | |
{ |
155 | 2 | if ( !hasParams() ) |
156 | |
{ |
157 | 0 | return; |
158 | |
} |
159 | |
|
160 | 2 | if ( connectionTimeout > 0 ) |
161 | |
{ |
162 | 2 | p.setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout ); |
163 | |
} |
164 | |
|
165 | 2 | if ( readTimeout > 0 ) |
166 | |
{ |
167 | 2 | p.setParameter( CoreConnectionPNames.SO_TIMEOUT, readTimeout ); |
168 | |
} |
169 | |
|
170 | 2 | if ( params != null ) |
171 | |
{ |
172 | 2 | Pattern coercePattern = Pattern.compile( COERCE_PATTERN ); |
173 | |
|
174 | 2 | for ( Iterator<?> it = params.entrySet().iterator(); it.hasNext(); ) |
175 | |
{ |
176 | 1 | Map.Entry<String, String> entry = (Map.Entry) it.next(); |
177 | |
|
178 | 1 | String key = entry.getKey(); |
179 | 1 | String value = entry.getValue(); |
180 | |
|
181 | 1 | Matcher matcher = coercePattern.matcher( value ); |
182 | 1 | if ( matcher.matches() ) |
183 | |
{ |
184 | 1 | char type = matcher.group( 1 ).charAt( 0 ); |
185 | 1 | value = matcher.group( 2 ); |
186 | |
|
187 | 1 | switch ( type ) |
188 | |
{ |
189 | |
case 'i': |
190 | |
{ |
191 | 1 | p.setIntParameter( key, Integer.parseInt( value ) ); |
192 | 1 | break; |
193 | |
} |
194 | |
case 'd': |
195 | |
{ |
196 | 0 | p.setDoubleParameter( key, Double.parseDouble( value ) ); |
197 | 0 | break; |
198 | |
} |
199 | |
case 'l': |
200 | |
{ |
201 | 0 | p.setLongParameter( key, Long.parseLong( value ) ); |
202 | 0 | break; |
203 | |
} |
204 | |
case 'b': |
205 | |
{ |
206 | 0 | p.setBooleanParameter( key, Boolean.valueOf( value ).booleanValue() ); |
207 | 0 | break; |
208 | |
} |
209 | |
case 'c': |
210 | |
{ |
211 | 0 | String[] entries = value.split( "," ); |
212 | 0 | List<String> collection = new ArrayList<String>(); |
213 | 0 | for ( int i = 0; i < entries.length; i++ ) |
214 | |
{ |
215 | 0 | collection.add( entries[i].trim() ); |
216 | |
} |
217 | |
|
218 | 0 | p.setParameter( key, collection ); |
219 | 0 | break; |
220 | |
} |
221 | |
case 'm': |
222 | |
{ |
223 | 0 | String[] entries = value.split( "," ); |
224 | |
|
225 | 0 | Map<String, String> map = new LinkedHashMap<String, String>(); |
226 | 0 | for ( int i = 0; i < entries.length; i++ ) |
227 | |
{ |
228 | 0 | int idx = entries[i].indexOf( "=>" ); |
229 | 0 | if ( idx < 1 ) |
230 | |
{ |
231 | 0 | break; |
232 | |
} |
233 | |
|
234 | 0 | String mapKey = entries[i].substring( 0, idx ); |
235 | 0 | String mapVal = entries[i].substring( idx + 1, entries[i].length() ); |
236 | 0 | map.put( mapKey.trim(), mapVal.trim() ); |
237 | |
} |
238 | |
|
239 | 0 | p.setParameter( key, map ); |
240 | 0 | break; |
241 | |
} |
242 | |
} |
243 | 1 | } |
244 | |
else |
245 | |
{ |
246 | 0 | p.setParameter( key, value ); |
247 | |
} |
248 | 1 | } |
249 | |
} |
250 | 2 | } |
251 | |
|
252 | |
public Header[] asRequestHeaders() |
253 | |
{ |
254 | 2 | if ( headers == null ) |
255 | |
{ |
256 | 0 | return new Header[0]; |
257 | |
} |
258 | |
|
259 | 2 | Header[] result = new Header[headers.size()]; |
260 | |
|
261 | 2 | int index = 0; |
262 | 2 | for ( Iterator<?> it = headers.entrySet().iterator(); it.hasNext(); ) |
263 | |
{ |
264 | 0 | Map.Entry<String, String> entry = (Map.Entry) it.next(); |
265 | |
|
266 | 0 | String key = entry.getKey(); |
267 | 0 | String value = entry.getValue(); |
268 | |
|
269 | 0 | Header header = new BasicHeader( key, value ); |
270 | 0 | result[index++] = header; |
271 | 0 | } |
272 | |
|
273 | 2 | return result; |
274 | |
} |
275 | |
|
276 | |
private HttpMethodConfiguration copy() |
277 | |
{ |
278 | 0 | HttpMethodConfiguration copy = new HttpMethodConfiguration(); |
279 | |
|
280 | 0 | copy.setConnectionTimeout( getConnectionTimeout() ); |
281 | 0 | copy.setReadTimeout( getReadTimeout() ); |
282 | 0 | if ( getHeaders() != null ) |
283 | |
{ |
284 | 0 | copy.setHeaders( getHeaders() ); |
285 | |
} |
286 | |
|
287 | 0 | if ( getParams() != null ) |
288 | |
{ |
289 | 0 | copy.setParams( getParams() ); |
290 | |
} |
291 | |
|
292 | 0 | copy.setUseDefaultHeaders( isUseDefaultHeaders() ); |
293 | |
|
294 | 0 | return copy; |
295 | |
} |
296 | |
|
297 | |
public static HttpMethodConfiguration merge( HttpMethodConfiguration defaults, HttpMethodConfiguration base, |
298 | |
HttpMethodConfiguration local ) |
299 | |
{ |
300 | 0 | HttpMethodConfiguration result = merge( defaults, base ); |
301 | 0 | return merge( result, local ); |
302 | |
} |
303 | |
|
304 | |
public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration local ) |
305 | |
{ |
306 | 4 | if ( base == null && local == null ) |
307 | |
{ |
308 | 0 | return null; |
309 | |
} |
310 | 4 | else if ( base == null ) |
311 | |
{ |
312 | 0 | return local; |
313 | |
} |
314 | 4 | else if ( local == null ) |
315 | |
{ |
316 | 4 | return base; |
317 | |
} |
318 | |
else |
319 | |
{ |
320 | 0 | HttpMethodConfiguration result = base.copy(); |
321 | |
|
322 | 0 | if ( local.getConnectionTimeout() != DEFAULT_CONNECTION_TIMEOUT ) |
323 | |
{ |
324 | 0 | result.setConnectionTimeout( local.getConnectionTimeout() ); |
325 | |
} |
326 | |
|
327 | 0 | if ( local.getReadTimeout() != DEFAULT_READ_TIMEOUT ) |
328 | |
{ |
329 | 0 | result.setReadTimeout( local.getReadTimeout() ); |
330 | |
} |
331 | |
|
332 | 0 | if ( local.getHeaders() != null ) |
333 | |
{ |
334 | 0 | result.getHeaders().putAll( local.getHeaders() ); |
335 | |
} |
336 | |
|
337 | 0 | if ( local.getParams() != null ) |
338 | |
{ |
339 | 0 | result.getParams().putAll( local.getParams() ); |
340 | |
} |
341 | |
|
342 | 0 | if ( local.getUseDefaultHeaders() != null ) |
343 | |
{ |
344 | 0 | result.setUseDefaultHeaders( local.isUseDefaultHeaders() ); |
345 | |
} |
346 | |
|
347 | 0 | return result; |
348 | |
} |
349 | |
} |
350 | |
|
351 | |
} |