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