You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2020/02/22 12:11:21 UTC

[GitHub] [maven-wagon] ok2c commented on a change in pull request #62: [WAGON-570] Use RedirectStrategy from HttpClient rather than our cust…

ok2c commented on a change in pull request #62: [WAGON-570] Use RedirectStrategy from HttpClient rather than our cust…
URL: https://github.com/apache/maven-wagon/pull/62#discussion_r382908356
 
 

 ##########
 File path: 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();
 
 Review comment:
   @michael-o I suppose there is no need to treat `HEAD` and `GET` as special cases. `RequestBuilder.copy( request ).setUri( uri ).build();` should be OK for all methods. 
   Looks good to me.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services