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 java.io.IOException;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.http.HttpEntity;
27 import org.apache.http.HttpException;
28 import org.apache.http.HttpStatus;
29 import org.apache.http.client.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.maven.wagon.ResourceDoesNotExistException;
32 import org.apache.maven.wagon.TransferFailedException;
33 import org.apache.maven.wagon.authorization.AuthorizationException;
34 import org.apache.maven.wagon.shared.http.HtmlFileListParser;
35
36
37
38
39
40 public class HttpWagon
41 extends AbstractHttpClientWagon
42 {
43 public List<String> getFileList( String destinationDirectory )
44 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
45 {
46 if ( destinationDirectory.length() > 0 && !destinationDirectory.endsWith( "/" ) )
47 {
48 destinationDirectory += "/";
49 }
50
51 String url = getRepository().getUrl() + "/" + destinationDirectory;
52
53 HttpGet getMethod = new HttpGet( url );
54
55 try
56 {
57 CloseableHttpResponse response = execute( getMethod );
58 try {
59 int statusCode = response.getStatusLine().getStatusCode();
60
61 fireTransferDebug( url + " - Status code: " + statusCode );
62
63 switch ( statusCode )
64 {
65 case HttpStatus.SC_OK:
66 break;
67
68 case HttpStatus.SC_FORBIDDEN:
69 throw new AuthorizationException( "Access denied to: " + url );
70
71 case HttpStatus.SC_UNAUTHORIZED:
72 throw new AuthorizationException( "Not authorized." );
73
74 case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
75 throw new AuthorizationException( "Not authorized by proxy." );
76
77 case HttpStatus.SC_NOT_FOUND:
78 throw new ResourceDoesNotExistException( "File: " + url + " does not exist" );
79
80
81 default:
82 throw new TransferFailedException(
83 "Failed to transfer file: " + url + ". Return code is: " + statusCode );
84 }
85 HttpEntity entity = response.getEntity();
86 if ( entity != null )
87 {
88 return HtmlFileListParser.parseFileList( url, entity.getContent() );
89 }
90 else
91 {
92 return Collections.emptyList();
93 }
94
95 }
96 finally
97 {
98 response.close();
99 }
100 }
101 catch ( IOException e )
102 {
103 throw new TransferFailedException( "Could not read response body.", e );
104 }
105 catch ( HttpException e )
106 {
107 throw new TransferFailedException( "Could not read response body.", e );
108 }
109 }
110 }