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.HtmlFileListParser;
31
32 import java.io.IOException;
33 import java.util.Collections;
34 import java.util.List;
35
36
37
38
39 public class HttpWagon
40 extends AbstractHttpClientWagon
41 {
42
43 public List<String> getFileList( String destinationDirectory )
44 throws AuthorizationException, ResourceDoesNotExistException, TransferFailedException
45 {
46 return getFileList( getInitialBackoffSeconds(), destinationDirectory );
47 }
48
49 private List<String> getFileList( int wait, String destinationDirectory )
50 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
51 {
52 if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
53 {
54 destinationDirectory += "/";
55 }
56
57 String url = getRepository().getUrl() + "/" + destinationDirectory;
58
59 HttpGet getMethod = new HttpGet( url );
60
61 try
62 {
63 CloseableHttpResponse response = execute( getMethod );
64 try
65 {
66 int statusCode = response.getStatusLine().getStatusCode();
67
68 fireTransferDebug( url + " - Status code: " + statusCode );
69
70 switch ( statusCode )
71 {
72 case HttpStatus.SC_OK:
73 break;
74
75 case HttpStatus.SC_FORBIDDEN:
76 throw new AuthorizationException( "Access denied to: " + url );
77
78 case HttpStatus.SC_UNAUTHORIZED:
79 throw new AuthorizationException( "Not authorized." );
80
81 case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
82 throw new AuthorizationException( "Not authorized by proxy." );
83
84 case HttpStatus.SC_NOT_FOUND:
85 throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
86
87 case SC_TOO_MANY_REQUESTS:
88 return getFileList( backoff( wait, url ), destinationDirectory );
89
90
91 default:
92 throw new TransferFailedException(
93 "Failed to transfer file: " + url + ". Return code is: " + statusCode );
94 }
95 HttpEntity entity = response.getEntity();
96 if ( entity != null )
97 {
98 return HtmlFileListParser.parseFileList( url, entity.getContent() );
99 }
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 }