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