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