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  
58      private static final String COERCE_PATTERN = "%(\\w+),(.+)";
59  
60      public static void copyConfig( HttpMethodConfiguration config, RequestConfig.Builder builder )
61      {
62          if ( config.getConnectionTimeout() > 0 )
63          {
64              builder.setConnectTimeout( config.getConnectionTimeout() );
65          }
66          if ( config.getReadTimeout() > 0 )
67          {
68              builder.setSocketTimeout( config.getReadTimeout() );
69          }
70          Properties params = config.getParams();
71          if ( params != null )
72          {
73  
74              Pattern coercePattern = Pattern.compile( COERCE_PATTERN );
75              for ( Map.Entry entry : params.entrySet() )
76              {
77                  String key = (String) entry.getKey();
78                  String value = (String) entry.getValue();
79                  Matcher matcher = coercePattern.matcher( value );
80                  if ( matcher.matches() )
81                  {
82                      value = matcher.group( 2 );
83                  }
84  
85                  if ( key.equals( SO_TIMEOUT ) )
86                  {
87                      builder.setSocketTimeout( Integer.parseInt( value ) );
88                  }
89                  else if ( key.equals( STALE_CONNECTION_CHECK ) )
90                  {
91                      builder.setStaleConnectionCheckEnabled( Boolean.valueOf( value ) );
92                  }
93                  else if ( key.equals( CONNECTION_TIMEOUT ) )
94                  {
95                      builder.setConnectTimeout( Integer.parseInt( value ) );
96                  }
97                  else if ( key.equals( USE_EXPECT_CONTINUE ) )
98                  {
99                      builder.setExpectContinueEnabled( Boolean.valueOf( value ) );
100                 }
101                 else if ( key.equals( DEFAULT_PROXY ) )
102                 {
103                     builder.setProxy( new HttpHost( value ) );
104                 }
105                 else if ( key.equals( LOCAL_ADDRESS ) )
106                 {
107                     try
108                     {
109                         builder.setLocalAddress( InetAddress.getByName( value ) );
110                     }
111                     catch ( UnknownHostException ignore )
112                     {
113                         // ignore
114                     }
115                 }
116                 else if ( key.equals( PROXY_AUTH_PREF ) )
117                 {
118                     builder.setProxyPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) );
119                 }
120                 else if ( key.equals( TARGET_AUTH_PREF ) )
121                 {
122                     builder.setTargetPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) );
123                 }
124                 else if ( key.equals( HANDLE_AUTHENTICATION ) )
125                 {
126                     builder.setAuthenticationEnabled( Boolean.valueOf( value ) );
127                 }
128                 else if ( key.equals( ALLOW_CIRCULAR_REDIRECTS ) )
129                 {
130                     builder.setCircularRedirectsAllowed( Boolean.valueOf( value ) );
131                 }
132                 else if ( key.equals( CONN_MANAGER_TIMEOUT ) )
133                 {
134                     builder.setConnectionRequestTimeout( Integer.parseInt( value ) );
135                 }
136                 else if ( key.equals( COOKIE_POLICY ) )
137                 {
138                     builder.setCookieSpec( value );
139                 }
140                 else if ( key.equals( MAX_REDIRECTS ) )
141                 {
142                     builder.setMaxRedirects( Integer.parseInt( value ) );
143                 }
144                 else if ( key.equals( HANDLE_REDIRECTS ) )
145                 {
146                     builder.setRedirectsEnabled( Boolean.valueOf( value ) );
147                 }
148                 else if ( key.equals( REJECT_RELATIVE_REDIRECT ) )
149                 {
150                     builder.setRelativeRedirectsAllowed( !Boolean.valueOf( value ) );
151                 }
152             }
153         }
154     }
155 
156     public static Header[] asRequestHeaders( HttpMethodConfiguration config )
157     {
158         Properties headers = config.getHeaders();
159         if ( headers == null )
160         {
161             return new Header[0];
162         }
163 
164         Header[] result = new Header[headers.size()];
165 
166         int index = 0;
167         for ( Map.Entry entry : headers.entrySet() )
168         {
169             String key = (String) entry.getKey();
170             String value = (String) entry.getValue();
171 
172             Header header = new BasicHeader( key, value );
173             result[index++] = header;
174         }
175 
176         return result;
177     }
178 
179     public static HttpMethodConfiguration merge( HttpMethodConfiguration defaults, HttpMethodConfiguration base,
180                                             HttpMethodConfiguration local )
181     {
182         HttpMethodConfiguration result = merge( defaults, base );
183         return merge( result, local );
184     }
185 
186     public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration local )
187     {
188         if ( base == null && local == null )
189         {
190             return null;
191         }
192         else if ( base == null )
193         {
194             return local;
195         }
196         else if ( local == null )
197         {
198             return base;
199         }
200         else
201         {
202             HttpMethodConfiguration result = base.copy();
203 
204             if ( local.getConnectionTimeout() != Wagon.DEFAULT_CONNECTION_TIMEOUT )
205             {
206                 result.setConnectionTimeout( local.getConnectionTimeout() );
207             }
208 
209             if ( local.getReadTimeout() != Wagon.DEFAULT_READ_TIMEOUT )
210             {
211                 result.setReadTimeout( local.getReadTimeout() );
212             }
213 
214             if ( local.getHeaders() != null )
215             {
216                 result.getHeaders().putAll( local.getHeaders() );
217             }
218 
219             if ( local.getParams() != null )
220             {
221                 result.getParams().putAll( local.getParams() );
222             }
223 
224             if ( local.getUseDefaultHeaders() != null )
225             {
226                 result.setUseDefaultHeaders( local.isUseDefaultHeaders() );
227             }
228 
229             return result;
230         }
231     }
232 
233 }