View Javadoc

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