View Javadoc
1   package org.apache.maven.wagon.providers.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 org.apache.http.HttpEntity;
23  import org.apache.http.HttpException;
24  import org.apache.http.HttpStatus;
25  import org.apache.http.client.methods.CloseableHttpResponse;
26  import org.apache.http.client.methods.HttpGet;
27  import org.apache.maven.wagon.ResourceDoesNotExistException;
28  import org.apache.maven.wagon.TransferFailedException;
29  import org.apache.maven.wagon.authorization.AuthorizationException;
30  import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
31  import org.apache.maven.wagon.shared.http.HtmlFileListParser;
32  import org.apache.maven.wagon.shared.http.HttpMessageUtils;
33  
34  import java.io.IOException;
35  import java.util.Collections;
36  import java.util.List;
37  
38  import static org.apache.maven.wagon.shared.http.HttpMessageUtils.formatResourceDoesNotExistMessage;
39  import static org.apache.maven.wagon.shared.http.HttpMessageUtils.formatTransferDebugMessage;
40  import static org.apache.maven.wagon.shared.http.HttpMessageUtils.formatTransferFailedMessage;
41  
42  /**
43   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
44   */
45  public class HttpWagon
46      extends AbstractHttpClientWagon
47  {
48  
49      public List<String> getFileList( String destinationDirectory )
50          throws AuthorizationException, ResourceDoesNotExistException, TransferFailedException
51      {
52          return getFileList( getInitialBackoffSeconds(), destinationDirectory );
53      }
54  
55      private List<String> getFileList( int wait, String destinationDirectory )
56          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
57      {
58          if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
59          {
60              destinationDirectory += "/";
61          }
62  
63          String url = getRepository().getUrl() + "/" + destinationDirectory;
64  
65          HttpGet getMethod = new HttpGet( url );
66  
67          try
68          {
69              CloseableHttpResponse response = execute( getMethod );
70              try
71              {
72                  String reasonPhrase = response.getStatusLine().getReasonPhrase();
73                  int statusCode = response.getStatusLine().getStatusCode();
74  
75                  fireTransferDebug( formatTransferDebugMessage( url, statusCode, reasonPhrase, getProxyInfo() ) );
76  
77                  switch ( statusCode )
78                  {
79                      case HttpStatus.SC_OK:
80                          break;
81  
82                      case HttpStatus.SC_FORBIDDEN:
83                      case HttpStatus.SC_UNAUTHORIZED:
84                      case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
85                          throw new AuthorizationException( HttpMessageUtils.formatAuthorizationMessage( url, statusCode,
86                                  reasonPhrase, getProxyInfo() ) );
87  
88                      case HttpStatus.SC_NOT_FOUND:
89                          throw new ResourceDoesNotExistException( formatResourceDoesNotExistMessage( url, statusCode,
90                                  reasonPhrase, getProxyInfo() ) );
91  
92                      case SC_TOO_MANY_REQUESTS:
93                          return getFileList( backoff( wait, url ), destinationDirectory );
94  
95                      //add more entries here
96                      default:
97                          throw new TransferFailedException( formatTransferFailedMessage( url, statusCode, reasonPhrase,
98                                  getProxyInfo() ) );
99                  }
100                 HttpEntity entity = response.getEntity();
101                 if ( entity != null )
102                 {
103                     return HtmlFileListParser.parseFileList( url, entity.getContent() );
104                 }
105                 else
106                 {
107                     return Collections.emptyList();
108                 }
109 
110             }
111             finally
112             {
113                 response.close();
114             }
115         }
116         catch ( IOException e )
117         {
118             throw new TransferFailedException( "Could not read response body.", e );
119         }
120         catch ( HttpException e )
121         {
122             throw new TransferFailedException( "Could not read response body.", e );
123         }
124         catch ( InterruptedException e )
125         {
126             throw new TransferFailedException( "Unable to wait for resource.", e );
127         }
128     }
129 
130 }