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 java.io.IOException;
23  import java.io.InputStream;
24  import java.util.List;
25  
26  import org.apache.commons.httpclient.HttpStatus;
27  import org.apache.commons.httpclient.methods.GetMethod;
28  import org.apache.maven.wagon.ResourceDoesNotExistException;
29  import org.apache.maven.wagon.TransferFailedException;
30  import org.apache.maven.wagon.authorization.AuthorizationException;
31  import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
32  import org.apache.maven.wagon.shared.http.HtmlFileListParser;
33  
34  /**
35   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
36   * @version $Id: HttpWagon.java 745730 2009-02-19 05:15:51Z brett $
37   */
38  public class HttpWagon
39      extends AbstractHttpClientWagon
40  { 
41      public List getFileList( String destinationDirectory )
42          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 
43      {
44          if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
45          {
46              destinationDirectory += "/";
47          }
48  
49          String url = getRepository().getUrl() + "/" + destinationDirectory;
50  
51          GetMethod getMethod = new GetMethod( url );
52  
53          try
54          {
55              int statusCode = execute( getMethod );
56  
57              fireTransferDebug( url + " - Status code: " + statusCode );
58  
59              // TODO [BP]: according to httpclient docs, really should swallow the output on error. verify if that is required
60              switch ( statusCode )
61              {
62                  case HttpStatus.SC_OK:
63                      break;
64  
65                  case SC_NULL:
66                      throw new TransferFailedException( "Failed to transfer file: " );
67  
68                  case HttpStatus.SC_FORBIDDEN:
69                      throw new AuthorizationException( "Access denied to: " + url );
70  
71                  case HttpStatus.SC_UNAUTHORIZED:
72                      throw new AuthorizationException( "Not authorized." );
73  
74                  case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
75                      throw new AuthorizationException( "Not authorized by proxy." );
76  
77                  case HttpStatus.SC_NOT_FOUND:
78                      throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
79  
80                      //add more entries here
81                  default :
82                      throw new TransferFailedException(
83                          "Failed to transfer file: " + url + ". Return code is: " + statusCode );
84              }
85  
86              InputStream is = null;
87              
88              is = getMethod.getResponseBodyAsStream();
89  
90              return HtmlFileListParser.parseFileList( url, is );
91          }
92          catch ( IOException e )
93          {
94              throw new TransferFailedException( "Could not read response body.", e );
95          }
96          finally
97          {
98              getMethod.releaseConnection();
99          }
100     }
101 }