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