1 | |
package org.apache.maven.index.updater; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.wagon.ConnectionException; |
23 | |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
24 | |
import org.apache.maven.wagon.Wagon; |
25 | |
import org.apache.maven.wagon.WagonException; |
26 | |
import org.apache.maven.wagon.authentication.AuthenticationException; |
27 | |
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
28 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
29 | |
import org.apache.maven.wagon.events.TransferListener; |
30 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
31 | |
import org.apache.maven.wagon.repository.Repository; |
32 | |
import org.codehaus.plexus.PlexusContainer; |
33 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
34 | |
|
35 | |
import java.io.File; |
36 | |
import java.io.FileInputStream; |
37 | |
import java.io.FileNotFoundException; |
38 | |
import java.io.IOException; |
39 | |
import java.io.InputStream; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class WagonHelper |
51 | |
{ |
52 | |
private final PlexusContainer plexusContainer; |
53 | |
|
54 | |
public WagonHelper( final PlexusContainer plexusContainer ) |
55 | 3 | { |
56 | 3 | this.plexusContainer = plexusContainer; |
57 | 3 | } |
58 | |
|
59 | |
public WagonFetcher getWagonResourceFetcher( final TransferListener listener ) |
60 | |
throws ComponentLookupException |
61 | |
{ |
62 | 3 | return getWagonResourceFetcher( listener, null, null ); |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public WagonFetcher getWagonResourceFetcher( final TransferListener listener, |
74 | |
final AuthenticationInfo authenticationInfo, |
75 | |
final ProxyInfo proxyInfo ) |
76 | |
throws ComponentLookupException |
77 | |
{ |
78 | |
|
79 | 3 | return new WagonFetcher( plexusContainer.lookup( Wagon.class, "http" ), listener, authenticationInfo, |
80 | |
proxyInfo ); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public WagonFetcher getWagonResourceFetcher( final TransferListener listener, |
93 | |
final AuthenticationInfo authenticationInfo, final ProxyInfo proxyInfo, |
94 | |
String protocol ) |
95 | |
throws ComponentLookupException |
96 | |
{ |
97 | 0 | return new WagonFetcher( plexusContainer.lookup( Wagon.class, protocol ), listener, authenticationInfo, |
98 | |
proxyInfo ); |
99 | |
} |
100 | |
|
101 | |
public static class WagonFetcher |
102 | |
implements ResourceFetcher |
103 | |
{ |
104 | |
private final TransferListener listener; |
105 | |
|
106 | |
private final AuthenticationInfo authenticationInfo; |
107 | |
|
108 | |
private final ProxyInfo proxyInfo; |
109 | |
|
110 | |
private final Wagon wagon; |
111 | |
|
112 | |
public WagonFetcher( final Wagon wagon, final TransferListener listener, |
113 | |
final AuthenticationInfo authenticationInfo, final ProxyInfo proxyInfo ) |
114 | 3 | { |
115 | 3 | this.wagon = wagon; |
116 | 3 | this.listener = listener; |
117 | 3 | this.authenticationInfo = authenticationInfo; |
118 | 3 | this.proxyInfo = proxyInfo; |
119 | 3 | } |
120 | |
|
121 | |
public void connect( final String id, final String url ) |
122 | |
throws IOException |
123 | |
{ |
124 | 3 | Repository repository = new Repository( id, url ); |
125 | |
|
126 | |
try |
127 | |
{ |
128 | |
|
129 | |
|
130 | 3 | if ( listener != null ) |
131 | |
{ |
132 | 0 | wagon.addTransferListener( listener ); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 3 | if ( authenticationInfo != null ) |
139 | |
{ |
140 | 0 | if ( proxyInfo != null ) |
141 | |
{ |
142 | 0 | wagon.connect( repository, authenticationInfo, proxyInfo ); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | wagon.connect( repository, authenticationInfo ); |
147 | |
} |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 3 | if ( proxyInfo != null ) |
152 | |
{ |
153 | 0 | wagon.connect( repository, proxyInfo ); |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | 3 | wagon.connect( repository ); |
158 | |
} |
159 | |
} |
160 | |
} |
161 | 0 | catch ( AuthenticationException ex ) |
162 | |
{ |
163 | 0 | String msg = "Authentication exception connecting to " + repository; |
164 | 0 | logError( msg, ex ); |
165 | 0 | IOException ioException = new IOException( msg ); |
166 | 0 | ioException.initCause( ex ); |
167 | 0 | throw ioException; |
168 | |
} |
169 | 0 | catch ( WagonException ex ) |
170 | |
{ |
171 | 0 | String msg = "Wagon exception connecting to " + repository; |
172 | 0 | logError( msg, ex ); |
173 | 0 | IOException ioException = new IOException( msg ); |
174 | 0 | ioException.initCause( ex ); |
175 | 0 | throw ioException; |
176 | 3 | } |
177 | 3 | } |
178 | |
|
179 | |
public void disconnect() |
180 | |
throws IOException |
181 | |
{ |
182 | 3 | if ( wagon != null ) |
183 | |
{ |
184 | |
try |
185 | |
{ |
186 | 3 | wagon.disconnect(); |
187 | |
} |
188 | 0 | catch ( ConnectionException ex ) |
189 | |
{ |
190 | 0 | IOException ioe = new IOException( ex.toString() ); |
191 | 0 | ioe.initCause( ex ); |
192 | 0 | throw ioe; |
193 | 3 | } |
194 | |
} |
195 | 3 | } |
196 | |
|
197 | |
public InputStream retrieve( String name ) |
198 | |
throws IOException, FileNotFoundException |
199 | |
{ |
200 | 6 | final File target = File.createTempFile( name, "" ); |
201 | 6 | retrieve( name, target ); |
202 | 6 | return new FileInputStream( target ) |
203 | 6 | { |
204 | |
@Override |
205 | |
public void close() |
206 | |
throws IOException |
207 | |
{ |
208 | 12 | super.close(); |
209 | 12 | target.delete(); |
210 | 12 | } |
211 | |
}; |
212 | |
} |
213 | |
|
214 | |
public void retrieve( final String name, final File targetFile ) |
215 | |
throws IOException, FileNotFoundException |
216 | |
{ |
217 | |
try |
218 | |
{ |
219 | 6 | wagon.get( name, targetFile ); |
220 | |
} |
221 | 0 | catch ( AuthorizationException e ) |
222 | |
{ |
223 | 0 | String msg = "Authorization exception retrieving " + name; |
224 | 0 | logError( msg, e ); |
225 | 0 | IOException ioException = new IOException( msg ); |
226 | 0 | ioException.initCause( e ); |
227 | 0 | throw ioException; |
228 | |
} |
229 | 0 | catch ( ResourceDoesNotExistException e ) |
230 | |
{ |
231 | 0 | String msg = "Resource " + name + " does not exist"; |
232 | 0 | logError( msg, e ); |
233 | 0 | FileNotFoundException fileNotFoundException = new FileNotFoundException( msg ); |
234 | 0 | fileNotFoundException.initCause( e ); |
235 | 0 | throw fileNotFoundException; |
236 | |
} |
237 | 0 | catch ( WagonException e ) |
238 | |
{ |
239 | 0 | String msg = "Transfer for " + name + " failed"; |
240 | 0 | logError( msg, e ); |
241 | 0 | IOException ioException = new IOException( msg + "; " + e.getMessage() ); |
242 | 0 | ioException.initCause( e ); |
243 | 0 | throw ioException; |
244 | 6 | } |
245 | 6 | } |
246 | |
|
247 | |
private void logError( final String msg, final Exception ex ) |
248 | |
{ |
249 | 0 | if ( listener != null ) |
250 | |
{ |
251 | 0 | listener.debug( msg + "; " + ex.getMessage() ); |
252 | |
} |
253 | 0 | } |
254 | |
} |
255 | |
} |