View Javadoc
1   package org.apache.maven.wagon.shared.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.net.URI;
23  
24  import org.apache.http.HttpEntityEnclosingRequest;
25  import org.apache.http.HttpRequest;
26  import org.apache.http.HttpResponse;
27  import org.apache.http.HttpStatus;
28  import org.apache.http.ProtocolException;
29  import org.apache.http.client.methods.HttpGet;
30  import org.apache.http.client.methods.HttpHead;
31  import org.apache.http.client.methods.HttpPut;
32  import org.apache.http.client.methods.HttpUriRequest;
33  import org.apache.http.client.methods.RequestBuilder;
34  import org.apache.http.impl.client.DefaultRedirectStrategy;
35  import org.apache.http.protocol.HttpContext;
36  import org.apache.http.util.Args;
37  import org.apache.maven.wagon.events.TransferEvent;
38  import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon.WagonHttpEntity;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * A custom redirect strategy for Apache Maven Wagon HttpClient.
44   *
45   * @since 3.4.0
46   *
47   */
48  public class WagonRedirectStrategy extends DefaultRedirectStrategy
49  {
50  
51      private static final Logger LOGGER = LoggerFactory.getLogger( WagonRedirectStrategy.class );
52  
53      private static final int SC_PERMANENT_REDIRECT = 308;
54  
55      public WagonRedirectStrategy()
56      {
57          super( new String[] {
58                  HttpGet.METHOD_NAME,
59                  HttpHead.METHOD_NAME,
60                  HttpPut.METHOD_NAME,
61                  /**
62                   * This covers the most basic case where the redirection relocates to another
63                   * collection which has an existing parent collection.
64                   */
65                  "MKCOL" } );
66      }
67  
68      @Override
69      public boolean isRedirected( final HttpRequest request, final HttpResponse response,
70              final HttpContext context ) throws ProtocolException
71      {
72          Args.notNull( request, "HTTP request" );
73          Args.notNull( response, "HTTP response" );
74  
75          final int statusCode = response.getStatusLine().getStatusCode();
76          final String method = request.getRequestLine().getMethod();
77          switch ( statusCode )
78          {
79          case HttpStatus.SC_MOVED_TEMPORARILY:
80          case HttpStatus.SC_MOVED_PERMANENTLY:
81          case HttpStatus.SC_SEE_OTHER:
82          case HttpStatus.SC_TEMPORARY_REDIRECT:
83          case SC_PERMANENT_REDIRECT:
84              return isRedirectable( method );
85          default:
86              return false;
87          }
88      }
89  
90      @Override
91      public HttpUriRequest getRedirect( final HttpRequest request, final HttpResponse response,
92              final HttpContext context ) throws ProtocolException
93      {
94          final URI uri = getLocationURI( request, response, context );
95          if ( request instanceof HttpEntityEnclosingRequest )
96          {
97              HttpEntityEnclosingRequest encRequest = (HttpEntityEnclosingRequest) request;
98              if ( encRequest.getEntity() instanceof AbstractHttpClientWagon.WagonHttpEntity )
99              {
100                 AbstractHttpClientWagon.WagonHttpEntity whe = (WagonHttpEntity) encRequest.getEntity();
101                 if ( whe.getWagon() instanceof AbstractHttpClientWagon )
102                 {
103                     // Re-execute AbstractWagon#firePutStarted(Resource, File)
104                     AbstractHttpClientWagon httpWagon = (AbstractHttpClientWagon) whe.getWagon();
105                     TransferEvent transferEvent =
106                             new TransferEvent( httpWagon, whe.getResource(),
107                                                TransferEvent.TRANSFER_STARTED, TransferEvent.REQUEST_PUT );
108                     transferEvent.setTimestamp( System.currentTimeMillis() );
109                     transferEvent.setLocalFile( whe.getSource() );
110                     httpWagon.getTransferEventSupport().fireDebug(
111                             String.format( "Following redirect from '%s' to '%s'",
112                                           request.getRequestLine().getUri(), uri.toASCIIString() ) );
113                     httpWagon.getTransferEventSupport().fireTransferStarted( transferEvent );
114                 }
115                 else
116                 {
117                     LOGGER.warn( "Cannot properly handle redirect transfer event, wagon has unexpected class: {}",
118                                 whe.getWagon().getClass() );
119                 }
120             }
121         }
122 
123         return RequestBuilder.copy( request ).setUri( uri ).build();
124     }
125 
126 }