Made HttpMethodConfiguration more pure bean like by moving utility methods into a separate class
Project: http://git-wip-us.apache.org/repos/asf/maven-wagon/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-wagon/commit/d4a6064b Tree: http://git-wip-us.apache.org/repos/asf/maven-wagon/tree/d4a6064b Diff: http://git-wip-us.apache.org/repos/asf/maven-wagon/diff/d4a6064b Branch: refs/heads/master Commit: d4a6064b1c81213a3add3dd4a06892317d77c46c Parents: e198f01 Author: Oleg Kalnichevski <ol...@apache.org> Authored: Wed Sep 25 09:57:15 2013 +0200 Committer: Oleg Kalnichevski <ol...@apache.org> Committed: Wed Sep 25 09:57:15 2013 +0200 ---------------------------------------------------------------------- .../providers/http/AbstractHttpClientWagon.java | 2 +- .../providers/http/ConfigurationUtils.java | 231 +++++++++++++++++++ .../wagon/providers/http/HttpConfiguration.java | 6 +- .../providers/http/HttpMethodConfiguration.java | 194 +--------------- .../providers/http/HttpClientWagonTest.java | 2 +- 5 files changed, 237 insertions(+), 198 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/d4a6064b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java ---------------------------------------------------------------------- diff --git a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java index 0a7e59d..fea72b4 100755 --- a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java +++ b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java @@ -702,7 +702,7 @@ public abstract class AbstractHttpClientWagon if ( config != null ) { - config.applyConfig(requestConfigBuilder); + ConfigurationUtils.copyConfig(config, requestConfigBuilder ); if ( config.isUsePreemptive() && authenticationInfo != null ) { http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/d4a6064b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/ConfigurationUtils.java ---------------------------------------------------------------------- diff --git a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/ConfigurationUtils.java b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/ConfigurationUtils.java new file mode 100755 index 0000000..cfa1b13 --- /dev/null +++ b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/ConfigurationUtils.java @@ -0,0 +1,231 @@ +package org.apache.maven.wagon.providers.http; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.message.BasicHeader; +import org.apache.maven.wagon.Wagon; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +class ConfigurationUtils +{ + + private static final String SO_TIMEOUT = "http.socket.timeout"; + private static final String STALE_CONNECTION_CHECK = "http.connection.stalecheck"; + private static final String CONNECTION_TIMEOUT = "http.connection.timeout"; + private static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue"; + private static final String DEFAULT_PROXY = "http.route.default-proxy"; + private static final String LOCAL_ADDRESS = "http.route.local-address"; + private static final String PROXY_AUTH_PREF = "http.auth.proxy-scheme-pref"; + private static final String TARGET_AUTH_PREF = "http.auth.target-scheme-pref"; + private static final String HANDLE_AUTHENTICATION = "http.protocol.handle-authentication"; + private static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects"; + private static final String CONN_MANAGER_TIMEOUT = "http.conn-manager.timeout"; + private static final String COOKIE_POLICY = "http.protocol.cookie-policy"; + private static final String MAX_REDIRECTS = "http.protocol.max-redirects"; + private static final String HANDLE_REDIRECTS = "http.protocol.handle-redirects"; + private static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect"; + + private static final String COERCE_PATTERN = "%(\\w+),(.+)"; + + public static void copyConfig( HttpMethodConfiguration config, RequestConfig.Builder builder ) + { + if ( config.getConnectionTimeout() > 0 ) + { + builder.setConnectTimeout( config.getConnectionTimeout() ); + } + if ( config.getReadTimeout() > 0 ) + { + builder.setSocketTimeout( config.getReadTimeout() ); + } + Properties params = config.getParams(); + if ( params != null ) + { + + Pattern coercePattern = Pattern.compile( COERCE_PATTERN ); + for ( Iterator<?> it = params.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry<String, String> entry = (Map.Entry) it.next(); + String key = entry.getKey(); + String value = entry.getValue(); + Matcher matcher = coercePattern.matcher(value); + if ( matcher.matches() ) + { + value = matcher.group( 2 ); + } + + if ( key.equals( SO_TIMEOUT ) ) + { + builder.setSocketTimeout( Integer.parseInt( value ) ); + } + else if ( key.equals( STALE_CONNECTION_CHECK ) ) + { + builder.setStaleConnectionCheckEnabled( Boolean.valueOf( value ) ); + } + else if ( key.equals( CONNECTION_TIMEOUT ) ) + { + builder.setConnectTimeout( Integer.parseInt( value ) ); + } + else if ( key.equals( USE_EXPECT_CONTINUE ) ) + { + builder.setExpectContinueEnabled( Boolean.valueOf( value ) ); + } + else if ( key.equals( DEFAULT_PROXY ) ) + { + builder.setProxy( new HttpHost( value )); + } + else if ( key.equals( LOCAL_ADDRESS ) ) + { + try { + builder.setLocalAddress( InetAddress.getByName( value ) ); + } + catch (UnknownHostException ignore) { + } + } + else if ( key.equals( PROXY_AUTH_PREF ) ) + { + builder.setProxyPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) ); + } + else if ( key.equals( TARGET_AUTH_PREF ) ) + { + builder.setTargetPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) ); + } + else if ( key.equals( HANDLE_AUTHENTICATION ) ) + { + builder.setAuthenticationEnabled( Boolean.valueOf( value ) ); + } + else if ( key.equals( ALLOW_CIRCULAR_REDIRECTS ) ) + { + builder.setCircularRedirectsAllowed( Boolean.valueOf( value ) ); + } + else if ( key.equals( CONN_MANAGER_TIMEOUT ) ) + { + builder.setConnectionRequestTimeout( Integer.parseInt( value ) ); + } + else if ( key.equals( COOKIE_POLICY ) ) + { + builder.setCookieSpec( value ); + } + else if ( key.equals( MAX_REDIRECTS ) ) + { + builder.setMaxRedirects( Integer.parseInt( value ) ); + } + else if ( key.equals( HANDLE_REDIRECTS ) ) + { + builder.setRedirectsEnabled( Boolean.valueOf( value ) ); + } + else if ( key.equals( REJECT_RELATIVE_REDIRECT ) ) + { + builder.setRelativeRedirectsAllowed( !Boolean.valueOf( value ) ); + } + } + } + } + + public static Header[] asRequestHeaders( HttpMethodConfiguration config ) + { + Properties headers = config.getHeaders(); + if ( headers == null ) + { + return new Header[0]; + } + + Header[] result = new Header[headers.size()]; + + int index = 0; + for ( Iterator<?> it = headers.entrySet().iterator(); it.hasNext(); ) + { + Map.Entry<String, String> entry = (Map.Entry) it.next(); + + String key = entry.getKey(); + String value = entry.getValue(); + + Header header = new BasicHeader( key, value ); + result[index++] = header; + } + + return result; + } + + public static HttpMethodConfiguration merge( HttpMethodConfiguration defaults, HttpMethodConfiguration base, + HttpMethodConfiguration local ) + { + HttpMethodConfiguration result = merge( defaults, base ); + return merge( result, local ); + } + + public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration local ) + { + if ( base == null && local == null ) + { + return null; + } + else if ( base == null ) + { + return local; + } + else if ( local == null ) + { + return base; + } + else + { + HttpMethodConfiguration result = base.copy(); + + if ( local.getConnectionTimeout() != Wagon.DEFAULT_CONNECTION_TIMEOUT ) + { + result.setConnectionTimeout( local.getConnectionTimeout() ); + } + + if ( local.getReadTimeout() != Wagon.DEFAULT_READ_TIMEOUT ) + { + result.setReadTimeout( local.getReadTimeout() ); + } + + if ( local.getHeaders() != null ) + { + result.getHeaders().putAll( local.getHeaders() ); + } + + if ( local.getParams() != null ) + { + result.getParams().putAll( local.getParams() ); + } + + if ( local.getUseDefaultHeaders() != null ) + { + result.setUseDefaultHeaders( local.isUseDefaultHeaders() ); + } + + return result; + } + } + +} http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/d4a6064b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpConfiguration.java ---------------------------------------------------------------------- diff --git a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpConfiguration.java b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpConfiguration.java index cacad63..94c0141 100644 --- a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpConfiguration.java +++ b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpConfiguration.java @@ -87,15 +87,15 @@ public class HttpConfiguration { if ( method instanceof HttpGet ) { - return HttpMethodConfiguration.merge( all, get ); + return ConfigurationUtils.merge( all, get ); } else if ( method instanceof HttpPut ) { - return HttpMethodConfiguration.merge( DEFAULT_PUT, all, put ); + return ConfigurationUtils.merge( DEFAULT_PUT, all, put ); } else if ( method instanceof HttpHead ) { - return HttpMethodConfiguration.merge( all, head ); + return ConfigurationUtils.merge( all, head ); } return all; http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/d4a6064b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpMethodConfiguration.java ---------------------------------------------------------------------- diff --git a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpMethodConfiguration.java b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpMethodConfiguration.java index a1341eb..d8d26b4 100755 --- a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpMethodConfiguration.java +++ b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/HttpMethodConfiguration.java @@ -19,25 +19,12 @@ package org.apache.maven.wagon.providers.http; * under the License. */ -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.apache.http.Header; -import org.apache.http.HttpHost; -import org.apache.http.client.config.CookieSpecs; -import org.apache.http.client.config.RequestConfig; import org.apache.http.message.BasicHeader; -import org.apache.http.params.CoreConnectionPNames; -import org.apache.http.params.HttpParams; import org.apache.maven.wagon.Wagon; public class HttpMethodConfiguration @@ -128,131 +115,6 @@ public class HttpMethodConfiguration return this; } - private static final String SO_TIMEOUT = "http.socket.timeout"; - private static final String STALE_CONNECTION_CHECK = "http.connection.stalecheck"; - private static final String CONNECTION_TIMEOUT = "http.connection.timeout"; - private static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue"; - private static final String DEFAULT_PROXY = "http.route.default-proxy"; - private static final String LOCAL_ADDRESS = "http.route.local-address"; - private static final String PROXY_AUTH_PREF = "http.auth.proxy-scheme-pref"; - private static final String TARGET_AUTH_PREF = "http.auth.target-scheme-pref"; - private static final String HANDLE_AUTHENTICATION = "http.protocol.handle-authentication"; - private static final String ALLOW_CIRCULAR_REDIRECTS = "http.protocol.allow-circular-redirects"; - private static final String CONN_MANAGER_TIMEOUT = "http.conn-manager.timeout"; - private static final String COOKIE_POLICY = "http.protocol.cookie-policy"; - private static final String MAX_REDIRECTS = "http.protocol.max-redirects"; - private static final String HANDLE_REDIRECTS = "http.protocol.handle-redirects"; - private static final String REJECT_RELATIVE_REDIRECT = "http.protocol.reject-relative-redirect"; - - private static final String COERCE_PATTERN = "%(\\w+),(.+)"; - - public void applyConfig(RequestConfig.Builder builder) - { - if ( !hasParams() ) - { - return; - } - if ( connectionTimeout > 0 ) - { - builder.setConnectTimeout(connectionTimeout); - } - if ( readTimeout > 0 ) - { - builder.setSocketTimeout(readTimeout); - } - if ( params != null ) - { - - Pattern coercePattern = Pattern.compile( COERCE_PATTERN ); - for ( Iterator<?> it = params.entrySet().iterator(); it.hasNext(); ) - { - Map.Entry<String, String> entry = (Map.Entry) it.next(); - String key = entry.getKey(); - String value = entry.getValue(); - Matcher matcher = coercePattern.matcher(value); - if ( matcher.matches() ) - { - value = matcher.group( 2 ); - } - - if ( key.equals( SO_TIMEOUT ) ) - { - builder.setSocketTimeout( Integer.parseInt( value ) ); - } - else if ( key.equals( STALE_CONNECTION_CHECK ) ) - { - builder.setStaleConnectionCheckEnabled( Boolean.valueOf( value ) ); - } - else if ( key.equals( CONNECTION_TIMEOUT ) ) - { - builder.setConnectTimeout( Integer.parseInt( value ) ); - } - else if ( key.equals( USE_EXPECT_CONTINUE ) ) - { - builder.setExpectContinueEnabled( Boolean.valueOf( value ) ); - } - else if ( key.equals( DEFAULT_PROXY ) ) - { - builder.setProxy( new HttpHost( value )); - } - else if ( key.equals( LOCAL_ADDRESS ) ) - { - try { - builder.setLocalAddress( InetAddress.getByName( value ) ); - } - catch (UnknownHostException ignore) { - } - } - else if ( key.equals( PROXY_AUTH_PREF ) ) - { - builder.setProxyPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) ); - } - else if ( key.equals( TARGET_AUTH_PREF ) ) - { - builder.setTargetPreferredAuthSchemes( Arrays.asList( value.split( "," ) ) ); - } - else if ( key.equals( HANDLE_AUTHENTICATION ) ) - { - builder.setAuthenticationEnabled( Boolean.valueOf( value ) ); - } - else if ( key.equals( ALLOW_CIRCULAR_REDIRECTS ) ) - { - builder.setCircularRedirectsAllowed( Boolean.valueOf( value ) ); - } - else if ( key.equals( CONN_MANAGER_TIMEOUT ) ) - { - builder.setConnectionRequestTimeout( Integer.parseInt( value ) ); - } - else if ( key.equals( COOKIE_POLICY ) ) - { - builder.setCookieSpec( value ); - } - else if ( key.equals( MAX_REDIRECTS ) ) - { - builder.setMaxRedirects( Integer.parseInt( value ) ); - } - else if ( key.equals( HANDLE_REDIRECTS ) ) - { - builder.setRedirectsEnabled( Boolean.valueOf( value ) ); - } - else if ( key.equals( REJECT_RELATIVE_REDIRECT ) ) - { - builder.setRelativeRedirectsAllowed( !Boolean.valueOf( value ) ); - } - } - } - } - - private boolean hasParams() - { - if ( connectionTimeout < 1 && params == null && readTimeout < 1 ) - { - return false; - } - - return true; - } - public boolean isUsePreemptive() { return usePreemptive; @@ -288,7 +150,7 @@ public class HttpMethodConfiguration return result; } - private HttpMethodConfiguration copy() + HttpMethodConfiguration copy() { HttpMethodConfiguration copy = new HttpMethodConfiguration(); @@ -309,58 +171,4 @@ public class HttpMethodConfiguration return copy; } - public static HttpMethodConfiguration merge( HttpMethodConfiguration defaults, HttpMethodConfiguration base, - HttpMethodConfiguration local ) - { - HttpMethodConfiguration result = merge( defaults, base ); - return merge( result, local ); - } - - public static HttpMethodConfiguration merge( HttpMethodConfiguration base, HttpMethodConfiguration local ) - { - if ( base == null && local == null ) - { - return null; - } - else if ( base == null ) - { - return local; - } - else if ( local == null ) - { - return base; - } - else - { - HttpMethodConfiguration result = base.copy(); - - if ( local.getConnectionTimeout() != Wagon.DEFAULT_CONNECTION_TIMEOUT ) - { - result.setConnectionTimeout( local.getConnectionTimeout() ); - } - - if ( local.getReadTimeout() != Wagon.DEFAULT_READ_TIMEOUT ) - { - result.setReadTimeout( local.getReadTimeout() ); - } - - if ( local.getHeaders() != null ) - { - result.getHeaders().putAll( local.getHeaders() ); - } - - if ( local.getParams() != null ) - { - result.getParams().putAll( local.getParams() ); - } - - if ( local.getUseDefaultHeaders() != null ) - { - result.setUseDefaultHeaders( local.isUseDefaultHeaders() ); - } - - return result; - } - } - } http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/d4a6064b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpClientWagonTest.java ---------------------------------------------------------------------- diff --git a/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpClientWagonTest.java b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpClientWagonTest.java index 01a130c..4634481 100755 --- a/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpClientWagonTest.java +++ b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpClientWagonTest.java @@ -43,7 +43,7 @@ public class HttpClientWagonTest HttpHead method = new HttpHead(); RequestConfig.Builder builder = RequestConfig.custom(); - config.getMethodConfiguration(method).applyConfig( builder ); + ConfigurationUtils.copyConfig( config.getMethodConfiguration( method ), builder ); RequestConfig requestConfig = builder.build(); assertEquals(2, requestConfig.getMaxRedirects());