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.HttpEntity;
023import org.apache.http.HttpException;
024import org.apache.http.HttpStatus;
025import org.apache.http.client.methods.CloseableHttpResponse;
026import org.apache.http.client.methods.HttpGet;
027import org.apache.maven.wagon.ResourceDoesNotExistException;
028import org.apache.maven.wagon.TransferFailedException;
029import org.apache.maven.wagon.authorization.AuthorizationException;
030import org.apache.maven.wagon.shared.http.HtmlFileListParser;
031
032import java.io.IOException;
033import java.util.Collections;
034import java.util.List;
035
036/**
037 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
038 */
039public class HttpWagon
040    extends AbstractHttpClientWagon
041{
042
043    public List<String> getFileList( String destinationDirectory )
044        throws AuthorizationException, ResourceDoesNotExistException, TransferFailedException
045    {
046        return getFileList( getInitialBackoffSeconds(), destinationDirectory );
047    }
048
049    private List<String> getFileList( int wait, String destinationDirectory )
050        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
051    {
052        if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
053        {
054            destinationDirectory += "/";
055        }
056
057        String url = getRepository().getUrl() + "/" + destinationDirectory;
058
059        HttpGet getMethod = new HttpGet( url );
060
061        try
062        {
063            CloseableHttpResponse response = execute( getMethod );
064            try
065            {
066                int statusCode = response.getStatusLine().getStatusCode();
067
068                fireTransferDebug( url + " - Status code: " + statusCode );
069
070                switch ( statusCode )
071                {
072                    case HttpStatus.SC_OK:
073                        break;
074
075                    case HttpStatus.SC_FORBIDDEN:
076                        throw new AuthorizationException( "Access denied to: " + url );
077
078                    case HttpStatus.SC_UNAUTHORIZED:
079                        throw new AuthorizationException( "Not authorized." );
080
081                    case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
082                        throw new AuthorizationException( "Not authorized by proxy." );
083
084                    case HttpStatus.SC_NOT_FOUND:
085                        throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
086
087                    case SC_TOO_MANY_REQUESTS:
088                        return getFileList( backoff( wait, url ), destinationDirectory );
089
090                    //add more entries here
091                    default:
092                        throw new TransferFailedException(
093                            "Failed to transfer file: " + url + ". Return code is: " + statusCode );
094                }
095                HttpEntity entity = response.getEntity();
096                if ( entity != null )
097                {
098                    return HtmlFileListParser.parseFileList( url, entity.getContent() );
099                }
100                else
101                {
102                    return Collections.emptyList();
103                }
104
105            }
106            finally
107            {
108                response.close();
109            }
110        }
111        catch ( IOException e )
112        {
113            throw new TransferFailedException( "Could not read response body.", e );
114        }
115        catch ( HttpException e )
116        {
117            throw new TransferFailedException( "Could not read response body.", e );
118        }
119        catch ( InterruptedException e )
120        {
121            throw new TransferFailedException( "Unable to wait for resource.", e );
122        }
123    }
124
125}