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.http.util.EntityUtils;
28 import org.apache.maven.wagon.ResourceDoesNotExistException;
29 import org.apache.maven.wagon.TransferFailedException;
30 import org.apache.maven.wagon.authorization.AuthorizationException;
31 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
32 import org.apache.maven.wagon.shared.http.HtmlFileListParser;
33 import org.apache.maven.wagon.shared.http.HttpMessageUtils;
34
35 import java.io.IOException;
36 import java.util.Collections;
37 import java.util.List;
38
39 import static org.apache.maven.wagon.shared.http.HttpMessageUtils.formatResourceDoesNotExistMessage;
40 import static org.apache.maven.wagon.shared.http.HttpMessageUtils.formatTransferDebugMessage;
41 import static org.apache.maven.wagon.shared.http.HttpMessageUtils.formatTransferFailedMessage;
42
43
44
45
46 public class HttpWagon
47 extends AbstractHttpClientWagon
48 {
49
50 public List<String> getFileList( String destinationDirectory )
51 throws AuthorizationException, ResourceDoesNotExistException, TransferFailedException
52 {
53 return getFileList( getInitialBackoffSeconds(), destinationDirectory );
54 }
55
56 private List<String> getFileList( int wait, String destinationDirectory )
57 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
58 {
59 if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
60 {
61 destinationDirectory += "/";
62 }
63
64 String url = getRepository().getUrl() + "/" + destinationDirectory;
65
66 HttpGet getMethod = new HttpGet( url );
67
68 try
69 {
70 CloseableHttpResponse response = execute( getMethod );
71 try
72 {
73 String reasonPhrase = response.getStatusLine().getReasonPhrase();
74 int statusCode = response.getStatusLine().getStatusCode();
75
76 fireTransferDebug( formatTransferDebugMessage( url, statusCode, reasonPhrase, getProxyInfo() ) );
77
78 switch ( statusCode )
79 {
80 case HttpStatus.SC_OK:
81 break;
82
83
84 case HttpStatus.SC_FORBIDDEN:
85 case HttpStatus.SC_UNAUTHORIZED:
86 case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
87 EntityUtils.consumeQuietly( response.getEntity() );
88 throw new AuthorizationException( HttpMessageUtils.formatAuthorizationMessage( url, statusCode,
89 reasonPhrase, getProxyInfo() ) );
90
91 case HttpStatus.SC_NOT_FOUND:
92 case HttpStatus.SC_GONE:
93 EntityUtils.consumeQuietly( response.getEntity() );
94 throw new ResourceDoesNotExistException( formatResourceDoesNotExistMessage( url, statusCode,
95 reasonPhrase, getProxyInfo() ) );
96
97 case SC_TOO_MANY_REQUESTS:
98 EntityUtils.consumeQuietly( response.getEntity() );
99 return getFileList( backoff( wait, url ), destinationDirectory );
100
101
102 default:
103 EntityUtils.consumeQuietly( response.getEntity() );
104 throw new TransferFailedException( formatTransferFailedMessage( url, statusCode, reasonPhrase,
105 getProxyInfo() ) );
106 }
107 HttpEntity entity = response.getEntity();
108 if ( entity != null )
109 {
110 return HtmlFileListParser.parseFileList( url, entity.getContent() );
111 }
112 else
113 {
114 return Collections.emptyList();
115 }
116
117 }
118 finally
119 {
120 response.close();
121 }
122 }
123 catch ( IOException e )
124 {
125 throw new TransferFailedException( "Could not read response body.", e );
126 }
127 catch ( HttpException e )
128 {
129 throw new TransferFailedException( "Could not read response body.", e );
130 }
131 catch ( InterruptedException e )
132 {
133 throw new TransferFailedException( "Unable to wait for resource.", e );
134 }
135 }
136
137 }