1 package org.apache.maven.wagon.providers.http;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.http.HttpEntity;
23 import org.apache.http.HttpException;
24 import org.apache.http.HttpStatus;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.maven.wagon.ResourceDoesNotExistException;
28 import org.apache.maven.wagon.TransferFailedException;
29 import org.apache.maven.wagon.authorization.AuthorizationException;
30 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
31 import org.apache.maven.wagon.shared.http.HtmlFileListParser;
32
33 import java.io.IOException;
34 import java.util.Collections;
35 import java.util.List;
36
37
38
39
40 public class HttpWagon
41 extends AbstractHttpClientWagon
42 {
43
44 public List<String> getFileList( String destinationDirectory )
45 throws AuthorizationException, ResourceDoesNotExistException, TransferFailedException
46 {
47 return getFileList( getInitialBackoffSeconds(), destinationDirectory );
48 }
49
50 private List<String> getFileList( int wait, String destinationDirectory )
51 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
52 {
53 if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
54 {
55 destinationDirectory += "/";
56 }
57
58 String url = getRepository().getUrl() + "/" + destinationDirectory;
59
60 HttpGet getMethod = new HttpGet( url );
61
62 try
63 {
64 CloseableHttpResponse response = execute( getMethod );
65 try
66 {
67 int statusCode = response.getStatusLine().getStatusCode();
68
69 fireTransferDebug( url + " - Status code: " + statusCode );
70
71 switch ( statusCode )
72 {
73 case HttpStatus.SC_OK:
74 break;
75
76 case HttpStatus.SC_FORBIDDEN:
77 throw new AuthorizationException( "Access denied to: " + url );
78
79 case HttpStatus.SC_UNAUTHORIZED:
80 throw new AuthorizationException( "Not authorized." );
81
82 case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
83 throw new AuthorizationException( "Not authorized by proxy." );
84
85 case HttpStatus.SC_NOT_FOUND:
86 throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
87
88 case SC_TOO_MANY_REQUESTS:
89 return getFileList( backoff( wait, url ), destinationDirectory );
90
91
92 default:
93 throw new TransferFailedException(
94 "Failed to transfer file: " + url + ". Return code is: " + statusCode );
95 }
96 HttpEntity entity = response.getEntity();
97 if ( entity != null )
98 {
99 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 }