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 builder.setLocalAddress( InetAddress.getByName( value ) );
106 }
107 catch (UnknownHostException ignore) {
108 }
109 }
110 else if ( key.equals( PROXY_AUTH_PREF ) )
111 {
112 builder.setProxyPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) );
113 }
114 else if ( key.equals( TARGET_AUTH_PREF ) )
115 {
116 builder.setTargetPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) );
117 }
118 else if ( key.equals( HANDLE_AUTHENTICATION ) )
119 {
120 builder.setAuthenticationEnabled( Boolean.valueOf( value ) );
121 }
122 else if ( key.equals( ALLOW_CIRCULAR_REDIRECTS ) )
123 {
124 builder.setCircularRedirectsAllowed( Boolean.valueOf( value ) );
125 }
126 else if ( key.equals( CONN_MANAGER_TIMEOUT ) )
127 {
128 builder.setConnectionRequestTimeout( Integer.parseInt( value ) );
129 }
130 else if ( key.equals( COOKIE_POLICY ) )
131 {
132 builder.setCookieSpec( value );
133 }
134 else if ( key.equals( MAX_REDIRECTS ) )
135 {
136 builder.setMaxRedirects( Integer.parseInt( value ) );
137 }
138 else if ( key.equals( HANDLE_REDIRECTS ) )
139 {
140 builder.setRedirectsEnabled( Boolean.valueOf( value ) );
141 }
142 else if ( key.equals( REJECT_RELATIVE_REDIRECT ) )
143 {
144 builder.setRelativeRedirectsAllowed( !Boolean.valueOf( value ) );
145 }
146 }
147 }
148 }
149
150 public static Header[] asRequestHeaders( HttpMethodConfiguration config )
151 {
152 Properties headers = config.getHeaders();
153 if ( headers == null )
154 {
155 return new Header[0];
156 }
157
158 Header[] result = new Header[headers.size()];
159
160 int index = 0;
161 for ( Map.Entry entry : headers.entrySet() )
162 {
163 String key = (String) entry.getKey();
164 String value = (String) entry.getValue();
165
166 Header header = new BasicHeader( key, value );
167 result[index++] = header;
168 }
169
170 return result;
171 }
172
173 public static HttpMethodConfiguration merge( HttpMethodConfiguration defaults, HttpMethodConfiguration base,
174 HttpMethodConfiguration local )
175 {
176 HttpMethodConfiguration result = merge( defaults, base );
177 return merge( result, local );
178 }
179
180 public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration local )
181 {
182 if ( base == null && local == null )
183 {
184 return null;
185 }
186 else if ( base == null )
187 {
188 return local;
189 }
190 else if ( local == null )
191 {
192 return base;
193 }
194 else
195 {
196 HttpMethodConfiguration result = base.copy();
197
198 if ( local.getConnectionTimeout() != Wagon.DEFAULT_CONNECTION_TIMEOUT )
199 {
200 result.setConnectionTimeout( local.getConnectionTimeout() );
201 }
202
203 if ( local.getReadTimeout() != Wagon.DEFAULT_READ_TIMEOUT )
204 {
205 result.setReadTimeout( local.getReadTimeout() );
206 }
207
208 if ( local.getHeaders() != null )
209 {
210 result.getHeaders().putAll( local.getHeaders() );
211 }
212
213 if ( local.getParams() != null )
214 {
215 result.getParams().putAll( local.getParams() );
216 }
217
218 if ( local.getUseDefaultHeaders() != null )
219 {
220 result.setUseDefaultHeaders( local.isUseDefaultHeaders() );
221 }
222
223 return result;
224 }
225 }
226
227 }