You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2020/02/22 13:11:58 UTC
[maven-wagon] branch master updated: [WAGON-570] Use
RedirectStrategy from HttpClient rather than a custom approach
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-wagon.git
The following commit(s) were added to refs/heads/master by this push:
new e9dd774 [WAGON-570] Use RedirectStrategy from HttpClient rather than a custom approach
e9dd774 is described below
commit e9dd77479834bfe24162be1fc983505000c3fe26
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Feb 16 21:20:48 2020 +0100
[WAGON-570] Use RedirectStrategy from HttpClient rather than a custom approach
This closes #62
---
.../wagon/shared/http/AbstractHttpClientWagon.java | 23 +-----
.../wagon/shared/http/WagonRedirectStrategy.java | 87 ++++++++++++++++++++++
.../wagon/providers/webdav/WebDavWagonTest.java | 4 +-
3 files changed, 90 insertions(+), 24 deletions(-)
diff --git a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java
index dd1c690..4927525 100755
--- a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java
+++ b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/AbstractHttpClientWagon.java
@@ -23,7 +23,6 @@ import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
@@ -545,6 +544,7 @@ public abstract class AbstractHttpClientWagon
.setRetryHandler( createRetryHandler() )
.setServiceUnavailableRetryStrategy( createServiceUnavailableRetryStrategy() )
.setDefaultAuthSchemeRegistry( createAuthSchemeRegistry() )
+ .setRedirectStrategy( new WagonRedirectStrategy() )
.build();
}
@@ -793,14 +793,6 @@ public abstract class AbstractHttpClientWagon
case HttpStatus.SC_ACCEPTED: // 202
case HttpStatus.SC_NO_CONTENT: // 204
break;
- // handle all redirect even if http specs says " the user agent MUST NOT automatically redirect
- // the request unless it can be confirmed by the user"
- case HttpStatus.SC_MOVED_PERMANENTLY: // 301
- case HttpStatus.SC_MOVED_TEMPORARILY: // 302
- case HttpStatus.SC_SEE_OTHER: // 303
- EntityUtils.consumeQuietly( response.getEntity() );
- put( resource, source, httpEntity, calculateRelocatedUrl( response ) );
- return;
//case HttpStatus.SC_UNAUTHORIZED:
case HttpStatus.SC_FORBIDDEN:
EntityUtils.consumeQuietly( response.getEntity() );
@@ -847,14 +839,6 @@ public abstract class AbstractHttpClientWagon
}
- protected String calculateRelocatedUrl( HttpResponse response )
- {
- Header locationHeader = response.getFirstHeader( "Location" );
- String locationField = locationHeader.getValue();
- // is it a relative Location or a full ?
- return locationField.startsWith( "http" ) ? locationField : getURL( getRepository() ) + '/' + locationField;
- }
-
protected void mkdirs( String dirname )
throws HttpException, IOException
{
@@ -962,11 +946,6 @@ public abstract class AbstractHttpClientWagon
}
}
- if ( httpMethod instanceof HttpPut )
- {
- requestConfigBuilder.setRedirectsEnabled( false );
- }
-
HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider( credentialsProvider );
localContext.setAuthCache( authCache );
diff --git a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/WagonRedirectStrategy.java b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/WagonRedirectStrategy.java
new file mode 100644
index 0000000..efd324f
--- /dev/null
+++ b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/WagonRedirectStrategy.java
@@ -0,0 +1,87 @@
+package org.apache.maven.wagon.shared.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 java.net.URI;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.ProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.methods.RequestBuilder;
+import org.apache.http.impl.client.DefaultRedirectStrategy;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.Args;
+
+/**
+ * A custom redirect strategy for Apache Maven Wagon HttpClient.
+ *
+ * @since 3.4.0
+ *
+ */
+public class WagonRedirectStrategy extends DefaultRedirectStrategy
+{
+
+ private static final int SC_PERMANENT_REDIRECT = 308;
+
+ public WagonRedirectStrategy()
+ {
+ super( new String[] {
+ HttpGet.METHOD_NAME,
+ HttpHead.METHOD_NAME,
+ HttpPut.METHOD_NAME,
+ "MKCOL" } );
+ }
+
+ @Override
+ public boolean isRedirected( final HttpRequest request, final HttpResponse response,
+ final HttpContext context ) throws ProtocolException
+ {
+ Args.notNull( request, "HTTP request" );
+ Args.notNull( response, "HTTP response" );
+
+ final int statusCode = response.getStatusLine().getStatusCode();
+ final String method = request.getRequestLine().getMethod();
+ switch ( statusCode )
+ {
+ case HttpStatus.SC_MOVED_TEMPORARILY:
+ case HttpStatus.SC_MOVED_PERMANENTLY:
+ case HttpStatus.SC_SEE_OTHER:
+ case HttpStatus.SC_TEMPORARY_REDIRECT:
+ case SC_PERMANENT_REDIRECT:
+ return isRedirectable( method );
+ default:
+ return false;
+ }
+ }
+
+ @Override
+ public HttpUriRequest getRedirect( final HttpRequest request, final HttpResponse response,
+ final HttpContext context ) throws ProtocolException
+ {
+ final URI uri = getLocationURI( request, response, context );
+ return RequestBuilder.copy( request ).setUri( uri ).build();
+ }
+
+}
diff --git a/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java b/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
index 5c07d87..e1150e3 100644
--- a/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
+++ b/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
@@ -500,7 +500,7 @@ public class WebDavWagonTest
assertEquals( "found:" + putHandler.handlerRequestResponses, 1, putHandler.handlerRequestResponses.size() );
assertEquals( "found:" + putHandler.handlerRequestResponses, HttpServletResponse.SC_CREATED,
putHandler.handlerRequestResponses.get( 0 ).responseCode );
- assertEquals( "found:" + redirectHandler.handlerRequestResponses, 3,
+ assertEquals( "found:" + redirectHandler.handlerRequestResponses, 2,
redirectHandler.handlerRequestResponses.size() );
assertEquals( "found:" + redirectHandler.handlerRequestResponses, HttpServletResponse.SC_SEE_OTHER,
redirectHandler.handlerRequestResponses.get( 0 ).responseCode );
@@ -512,7 +512,7 @@ public class WebDavWagonTest
{
assertEquals( "found:" + putHandler.handlerRequestResponses, 0, putHandler.handlerRequestResponses.size() );
- assertEquals( "found:" + redirectHandler.handlerRequestResponses, 6,
+ assertEquals( "found:" + redirectHandler.handlerRequestResponses, 4,
redirectHandler.handlerRequestResponses.size() );
assertEquals( "found:" + redirectHandler.handlerRequestResponses, HttpServletResponse.SC_SEE_OTHER,
redirectHandler.handlerRequestResponses.get( 0 ).responseCode );