View Javadoc
1   package org.apache.maven.wagon.shared.http;
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.HttpHost;
24  import org.apache.http.client.config.RequestConfig;
25  import org.apache.http.message.BasicHeader;
26  import org.apache.maven.wagon.Wagon;
27  
28  import java.net.InetAddress;
29  import java.net.UnknownHostException;
30  import java.util.Arrays;
31  import java.util.Map;
32  import java.util.Properties;
33  import java.util.regex.Matcher;
34  import java.util.regex.Pattern;
35  
36  /**
37   * Configuration helper class
38   */
39  public class ConfigurationUtils
40  {
41  
42      private static final String SO_TIMEOUT                  = "http.socket.timeout";
43      private static final String STALE_CONNECTION_CHECK      = "http.connection.stalecheck";
44      private static final String CONNECTION_TIMEOUT          = "http.connection.timeout";
45      private static final String USE_EXPECT_CONTINUE         = "http.protocol.expect-continue";
46      private static final String DEFAULT_PROXY               = "http.route.default-proxy";
47      private static final String LOCAL_ADDRESS               = "http.route.local-address";
48      private static final String PROXY_AUTH_PREF             = "http.auth.proxy-scheme-pref";
49      private static final String TARGET_AUTH_PREF            = "http.auth.target-scheme-pref";
50      private static final String HANDLE_AUTHENTICATION       = "http.protocol.handle-authentication";
51      private static final String ALLOW_CIRCULAR_REDIRECTS    = "http.protocol.allow-circular-redirects";
52      private static final String CONN_MANAGER_TIMEOUT        = "http.conn-manager.timeout";
53      private static final String COOKIE_POLICY               = "http.protocol.cookie-policy";
54      private static final String MAX_REDIRECTS               = "http.protocol.max-redirects";
55      private static final String HANDLE_REDIRECTS            = "http.protocol.handle-redirects";
56      private static final String REJECT_RELATIVE_REDIRECT    = "http.protocol.reject-relative-redirect";
57      private static final String HANDLE_CONTENT_COMPRESSION  = "http.protocol.handle-content-compression";
58      private static final String HANDLE_URI_NORMALIZATION    = "http.protocol.handle-uri-normalization";
59  
60      private static final String COERCE_PATTERN = "%(\\w+),(.+)";
61  
62      public static void copyConfig( HttpMethodConfiguration config, RequestConfig.Builder builder )
63      {
64          if ( config.getConnectionTimeout() > 0 )
65          {
66              builder.setConnectTimeout( config.getConnectionTimeout() );
67          }
68          if ( config.getReadTimeout() > 0 )
69          {
70              builder.setSocketTimeout( config.getReadTimeout() );
71          }
72          Properties params = config.getParams();
73          if ( params != null )
74          {
75  
76              Pattern coercePattern = Pattern.compile( COERCE_PATTERN );
77              for ( Map.Entry entry : params.entrySet() )
78              {
79                  String key = (String) entry.getKey();
80                  String value = (String) entry.getValue();
81                  Matcher matcher = coercePattern.matcher( value );
82                  if ( matcher.matches() )
83                  {
84                      value = matcher.group( 2 );
85                  }
86  
87                  if ( key.equals( SO_TIMEOUT ) )
88                  {
89                      builder.setSocketTimeout( Integer.parseInt( value ) );
90                  }
91                  else if ( key.equals( STALE_CONNECTION_CHECK ) )
92                  {
93                      builder.setStaleConnectionCheckEnabled( Boolean.valueOf( value ) );
94                  }
95                  else if ( key.equals( CONNECTION_TIMEOUT ) )
96                  {
97                      builder.setConnectTimeout( Integer.parseInt( value ) );
98                  }
99                  else if ( key.equals( USE_EXPECT_CONTINUE ) )
100                 {
101                     builder.setExpectContinueEnabled( Boolean.valueOf( value ) );
102                 }
103                 else if ( key.equals( DEFAULT_PROXY ) )
104                 {
105                     builder.setProxy( HttpHost.create( value ) );
106                 }
107                 else if ( key.equals( LOCAL_ADDRESS ) )
108                 {
109                     try
110                     {
111                         builder.setLocalAddress( InetAddress.getByName( value ) );
112                     }
113                     catch ( UnknownHostException ignore )
114                     {
115                         // ignore
116                     }
117                 }
118                 else if ( key.equals( PROXY_AUTH_PREF ) )
119                 {
120                     builder.setProxyPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) );
121                 }
122                 else if ( key.equals( TARGET_AUTH_PREF ) )
123                 {
124                     builder.setTargetPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) );
125                 }
126                 else if ( key.equals( HANDLE_AUTHENTICATION ) )
127                 {
128                     builder.setAuthenticationEnabled( Boolean.valueOf( value ) );
129                 }
130                 else if ( key.equals( ALLOW_CIRCULAR_REDIRECTS ) )
131                 {
132                     builder.setCircularRedirectsAllowed( Boolean.valueOf( value ) );
133                 }
134                 else if ( key.equals( CONN_MANAGER_TIMEOUT ) )
135                 {
136                     builder.setConnectionRequestTimeout( Integer.parseInt( value ) );
137                 }
138                 else if ( key.equals( COOKIE_POLICY ) )
139                 {
140                     builder.setCookieSpec( value );
141                 }
142                 else if ( key.equals( MAX_REDIRECTS ) )
143                 {
144                     builder.setMaxRedirects( Integer.parseInt( value ) );
145                 }
146                 else if ( key.equals( HANDLE_REDIRECTS ) )
147                 {
148                     builder.setRedirectsEnabled( Boolean.valueOf( value ) );
149                 }
150                 else if ( key.equals( REJECT_RELATIVE_REDIRECT ) )
151                 {
152                     builder.setRelativeRedirectsAllowed( !Boolean.valueOf( value ) );
153                 }
154                 else if ( key.equals ( HANDLE_CONTENT_COMPRESSION ) )
155                 {
156                     builder.setContentCompressionEnabled( Boolean.valueOf( value ) );
157                 }
158                 else if ( key.equals ( HANDLE_URI_NORMALIZATION ) )
159                 {
160                     builder.setNormalizeUri( Boolean.valueOf( value ) );
161                 }
162             }
163         }
164     }
165 
166     public static Header[] asRequestHeaders( HttpMethodConfiguration config )
167     {
168         Properties headers = config.getHeaders();
169         if ( headers == null )
170         {
171             return new Header[0];
172         }
173 
174         Header[] result = new Header[headers.size()];
175 
176         int index = 0;
177         for ( Map.Entry entry : headers.entrySet() )
178         {
179             String key = (String) entry.getKey();
180             String value = (String) entry.getValue();
181 
182             Header header = new BasicHeader( key, value );
183             result[index++] = header;
184         }
185 
186         return result;
187     }
188 
189     public static HttpMethodConfiguration merge( HttpMethodConfiguration defaults, HttpMethodConfiguration base,
190                                             HttpMethodConfiguration local )
191     {
192         HttpMethodConfiguration result = merge( defaults, base );
193         return merge( result, local );
194     }
195 
196     public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration local )
197     {
198         if ( base == null && local == null )
199         {
200             return null;
201         }
202         else if ( base == null )
203         {
204             return local;
205         }
206         else if ( local == null )
207         {
208             return base;
209         }
210         else
211         {
212             HttpMethodConfiguration result = base.copy();
213 
214             if ( local.getConnectionTimeout() != Wagon.DEFAULT_CONNECTION_TIMEOUT )
215             {
216                 result.setConnectionTimeout( local.getConnectionTimeout() );
217             }
218 
219             if ( local.getReadTimeout() != Wagon.DEFAULT_READ_TIMEOUT )
220             {
221                 result.setReadTimeout( local.getReadTimeout() );
222             }
223 
224             if ( local.getHeaders() != null )
225             {
226                 result.getHeaders().putAll( local.getHeaders() );
227             }
228 
229             if ( local.getParams() != null )
230             {
231                 result.getParams().putAll( local.getParams() );
232             }
233 
234             if ( local.getUseDefaultHeaders() != null )
235             {
236                 result.setUseDefaultHeaders( local.isUseDefaultHeaders() );
237             }
238 
239             return result;
240         }
241     }
242 
243 }