001package org.apache.maven.wagon.providers.http;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.http.HttpException;
023import org.apache.http.HttpResponse;
024import org.apache.http.HttpStatus;
025import org.apache.http.client.methods.HttpGet;
026import org.apache.maven.wagon.ResourceDoesNotExistException;
027import org.apache.maven.wagon.TransferFailedException;
028import org.apache.maven.wagon.authorization.AuthorizationException;
029import org.apache.maven.wagon.shared.http4.AbstractHttpClientWagon;
030import org.apache.maven.wagon.shared.http4.HtmlFileListParser;
031
032import java.io.IOException;
033import java.io.InputStream;
034import java.util.List;
035
036/**
037 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
038 *
039 */
040public class HttpWagon
041    extends AbstractHttpClientWagon
042{
043    public List<String> getFileList( String destinationDirectory )
044        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
045    {
046        if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
047        {
048            destinationDirectory += "/";
049        }
050
051        String url = getRepository().getUrl() + "/" + destinationDirectory;
052
053        HttpGet getMethod = new HttpGet( url );
054
055        try
056        {
057
058            HttpResponse response = execute( getMethod );
059            int statusCode = response.getStatusLine().getStatusCode();
060
061            fireTransferDebug( url + " - Status code: " + statusCode );
062
063            // TODO [BP]: according to httpclient docs, really should swallow the output on error. verify if that is required
064            switch ( statusCode )
065            {
066                case HttpStatus.SC_OK:
067                    break;
068
069                case SC_NULL:
070                    throw new TransferFailedException( "Failed to transfer file: " );
071
072                case HttpStatus.SC_FORBIDDEN:
073                    throw new AuthorizationException( "Access denied to: " + url );
074
075                case HttpStatus.SC_UNAUTHORIZED:
076                    throw new AuthorizationException( "Not authorized." );
077
078                case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
079                    throw new AuthorizationException( "Not authorized by proxy." );
080
081                case HttpStatus.SC_NOT_FOUND:
082                    throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
083
084                    //add more entries here
085                default:
086                    throw new TransferFailedException(
087                        "Failed to transfer file: " + url + ". Return code is: " + statusCode );
088            }
089
090            InputStream is = response.getEntity().getContent();
091
092            return HtmlFileListParser.parseFileList( url, is );
093        }
094        catch ( IOException e )
095        {
096            throw new TransferFailedException( "Could not read response body.", e );
097        }
098        catch ( HttpException e )
099        {
100            throw new TransferFailedException( "Could not read response body.", e );
101        }
102        finally
103        {
104            getMethod.abort();
105        }
106    }
107}