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