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