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