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