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/16 20:21:12 UTC

[maven-wagon] branch WAGON-570 created (now 58a6467)

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a change to branch WAGON-570
in repository https://gitbox.apache.org/repos/asf/maven-wagon.git.


      at 58a6467  [WAGON-570] Use RedirectStrategy from HttpClient rather than our custom code

This branch includes the following new commits:

     new 58a6467  [WAGON-570] Use RedirectStrategy from HttpClient rather than our custom code

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-wagon] 01/01: [WAGON-570] Use RedirectStrategy from HttpClient rather than our custom code

Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch WAGON-570
in repository https://gitbox.apache.org/repos/asf/maven-wagon.git

commit 58a646776fa4811090063360e80280e21287e92b
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Feb 16 21:20:48 2020 +0100

    [WAGON-570] Use RedirectStrategy from HttpClient rather than our custom code
---
 .../wagon/shared/http/AbstractHttpClientWagon.java | 14 +--
 .../wagon/shared/http/WagonRedirectStrategy.java   | 99 ++++++++++++++++++++++
 .../wagon/providers/webdav/WebDavWagonTest.java    |  4 +-
 3 files changed, 102 insertions(+), 15 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..84f51a6 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
@@ -545,6 +545,7 @@ public abstract class AbstractHttpClientWagon
             .setRetryHandler( createRetryHandler() )
             .setServiceUnavailableRetryStrategy( createServiceUnavailableRetryStrategy() )
             .setDefaultAuthSchemeRegistry( createAuthSchemeRegistry() )
+            .setRedirectStrategy( new WagonRedirectStrategy() )
             .build();
     }
 
@@ -793,14 +794,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() );
@@ -962,11 +955,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..9ed7452
--- /dev/null
+++ b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/WagonRedirectStrategy.java
@@ -0,0 +1,99 @@
+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 );
+        final String method = request.getRequestLine().getMethod();
+        if ( method.equalsIgnoreCase( HttpHead.METHOD_NAME ) )
+        {
+            return new HttpHead( uri );
+        }
+        else if ( method.equalsIgnoreCase( HttpGet.METHOD_NAME ) )
+        {
+            return new HttpGet( uri );
+        }
+        else
+        {
+            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 );