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 java.io.IOException;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.http.HttpEntity;
027import org.apache.http.HttpException;
028import org.apache.http.HttpStatus;
029import org.apache.http.client.methods.CloseableHttpResponse;
030import org.apache.http.client.methods.HttpGet;
031import org.apache.maven.wagon.ResourceDoesNotExistException;
032import org.apache.maven.wagon.TransferFailedException;
033import org.apache.maven.wagon.authorization.AuthorizationException;
034import org.apache.maven.wagon.shared.http.HtmlFileListParser;
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            CloseableHttpResponse response = execute( getMethod );
058            try {
059                int statusCode = response.getStatusLine().getStatusCode();
060
061                fireTransferDebug( url + " - Status code: " + statusCode );
062
063                switch ( statusCode )
064                {
065                    case HttpStatus.SC_OK:
066                        break;
067
068                    case HttpStatus.SC_FORBIDDEN:
069                        throw new AuthorizationException( "Access denied to: " + url );
070
071                    case HttpStatus.SC_UNAUTHORIZED:
072                        throw new AuthorizationException( "Not authorized." );
073
074                    case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
075                        throw new AuthorizationException( "Not authorized by proxy." );
076
077                    case HttpStatus.SC_NOT_FOUND:
078                        throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
079
080                        //add more entries here
081                    default:
082                        throw new TransferFailedException(
083                            "Failed to transfer file: " + url + ". Return code is: " + statusCode );
084                }
085                HttpEntity entity = response.getEntity();
086                if ( entity != null )
087                {
088                    return HtmlFileListParser.parseFileList( url, entity.getContent() );
089                }
090                else
091                {
092                    return Collections.emptyList();
093                }
094
095            }
096            finally
097            {
098                response.close();
099            }
100        }
101        catch ( IOException e )
102        {
103            throw new TransferFailedException( "Could not read response body.", e );
104        }
105        catch ( HttpException e )
106        {
107            throw new TransferFailedException( "Could not read response body.", e );
108        }
109    }
110}