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