1 | |
package org.apache.maven.wagon.shared.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.commons.httpclient.Credentials; |
23 | |
import org.apache.commons.httpclient.Header; |
24 | |
import org.apache.commons.httpclient.HostConfiguration; |
25 | |
import org.apache.commons.httpclient.HttpClient; |
26 | |
import org.apache.commons.httpclient.HttpConnectionManager; |
27 | |
import org.apache.commons.httpclient.HttpMethod; |
28 | |
import org.apache.commons.httpclient.HttpStatus; |
29 | |
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
30 | |
import org.apache.commons.httpclient.NTCredentials; |
31 | |
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
32 | |
import org.apache.commons.httpclient.auth.AuthScope; |
33 | |
import org.apache.commons.httpclient.cookie.CookiePolicy; |
34 | |
import org.apache.commons.httpclient.methods.GetMethod; |
35 | |
import org.apache.commons.httpclient.methods.HeadMethod; |
36 | |
import org.apache.commons.httpclient.methods.PutMethod; |
37 | |
import org.apache.commons.httpclient.methods.RequestEntity; |
38 | |
import org.apache.commons.httpclient.params.HttpMethodParams; |
39 | |
import org.apache.commons.httpclient.util.DateParseException; |
40 | |
import org.apache.commons.httpclient.util.DateUtil; |
41 | |
import org.apache.maven.wagon.InputData; |
42 | |
import org.apache.maven.wagon.OutputData; |
43 | |
import org.apache.maven.wagon.PathUtils; |
44 | |
import org.apache.maven.wagon.ResourceDoesNotExistException; |
45 | |
import org.apache.maven.wagon.StreamWagon; |
46 | |
import org.apache.maven.wagon.TransferFailedException; |
47 | |
import org.apache.maven.wagon.Wagon; |
48 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
49 | |
import org.apache.maven.wagon.events.TransferEvent; |
50 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
51 | |
import org.apache.maven.wagon.repository.Repository; |
52 | |
import org.apache.maven.wagon.resource.Resource; |
53 | |
import org.codehaus.plexus.util.IOUtil; |
54 | |
import org.codehaus.plexus.util.StringUtils; |
55 | |
|
56 | |
import java.io.File; |
57 | |
import java.io.FileInputStream; |
58 | |
import java.io.FileOutputStream; |
59 | |
import java.io.IOException; |
60 | |
import java.io.InputStream; |
61 | |
import java.io.OutputStream; |
62 | |
import java.net.URLEncoder; |
63 | |
import java.text.SimpleDateFormat; |
64 | |
import java.util.Date; |
65 | |
import java.util.Iterator; |
66 | |
import java.util.Locale; |
67 | |
import java.util.Properties; |
68 | |
import java.util.TimeZone; |
69 | |
import java.util.zip.GZIPInputStream; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 4 | public abstract class AbstractHttpClientWagon |
76 | |
extends StreamWagon |
77 | |
{ |
78 | 0 | private final class RequestEntityImplementation |
79 | |
implements RequestEntity |
80 | |
{ |
81 | |
private final Resource resource; |
82 | |
|
83 | |
private final Wagon wagon; |
84 | |
|
85 | |
private final File source; |
86 | |
|
87 | |
private RequestEntityImplementation( final InputStream stream, final Resource resource, final Wagon wagon, |
88 | |
final File source ) |
89 | |
throws TransferFailedException |
90 | 0 | { |
91 | 0 | if ( source != null ) |
92 | |
{ |
93 | 0 | this.source = source; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | 0 | FileOutputStream fos = null; |
98 | |
try |
99 | |
{ |
100 | 0 | this.source = File.createTempFile( "http-wagon.", ".tmp" ); |
101 | 0 | this.source.deleteOnExit(); |
102 | |
|
103 | 0 | fos = new FileOutputStream( this.source ); |
104 | 0 | IOUtil.copy( stream, fos ); |
105 | |
} |
106 | 0 | catch ( IOException e ) |
107 | |
{ |
108 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); |
109 | 0 | throw new TransferFailedException( "Failed to buffer stream contents to temp file for upload.", e ); |
110 | |
} |
111 | |
finally |
112 | |
{ |
113 | 0 | IOUtil.close( fos ); |
114 | 0 | } |
115 | |
} |
116 | |
|
117 | 0 | this.resource = resource; |
118 | 0 | this.wagon = wagon; |
119 | 0 | } |
120 | |
|
121 | |
public long getContentLength() |
122 | |
{ |
123 | 0 | return resource.getContentLength(); |
124 | |
} |
125 | |
|
126 | |
public String getContentType() |
127 | |
{ |
128 | 0 | return null; |
129 | |
} |
130 | |
|
131 | |
public boolean isRepeatable() |
132 | |
{ |
133 | 0 | return true; |
134 | |
} |
135 | |
|
136 | |
public void writeRequest( OutputStream output ) |
137 | |
throws IOException |
138 | |
{ |
139 | 0 | byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; |
140 | |
|
141 | 0 | TransferEvent transferEvent = |
142 | |
new TransferEvent( wagon, resource, TransferEvent.TRANSFER_PROGRESS, TransferEvent.REQUEST_PUT ); |
143 | 0 | transferEvent.setTimestamp( System.currentTimeMillis() ); |
144 | |
|
145 | 0 | FileInputStream fin = null; |
146 | |
try |
147 | |
{ |
148 | 0 | fin = new FileInputStream( source ); |
149 | 0 | int remaining = Integer.MAX_VALUE; |
150 | 0 | while ( remaining > 0 ) |
151 | |
{ |
152 | 0 | int n = fin.read( buffer, 0, Math.min( buffer.length, remaining ) ); |
153 | |
|
154 | 0 | if ( n == -1 ) |
155 | |
{ |
156 | 0 | break; |
157 | |
} |
158 | |
|
159 | 0 | fireTransferProgress( transferEvent, buffer, n ); |
160 | |
|
161 | 0 | output.write( buffer, 0, n ); |
162 | |
|
163 | 0 | remaining -= n; |
164 | 0 | } |
165 | |
} |
166 | |
finally |
167 | |
{ |
168 | 0 | IOUtil.close( fin ); |
169 | 0 | } |
170 | |
|
171 | 0 | output.flush(); |
172 | 0 | } |
173 | |
} |
174 | |
|
175 | |
protected static final int SC_NULL = -1; |
176 | |
|
177 | 1 | protected static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" ); |
178 | |
|
179 | |
private HttpClient client; |
180 | |
|
181 | 4 | protected HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
private Properties httpHeaders; |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
private HttpConfiguration httpConfiguration; |
192 | |
|
193 | |
private HttpMethod getMethod; |
194 | |
|
195 | |
public void openConnectionInternal() |
196 | |
{ |
197 | 0 | repository.setUrl( getURL( repository ) ); |
198 | 0 | client = new HttpClient( connectionManager ); |
199 | |
|
200 | |
|
201 | 0 | client.getParams().setCookiePolicy( CookiePolicy.BROWSER_COMPATIBILITY ); |
202 | |
|
203 | 0 | String username = null; |
204 | 0 | String password = null; |
205 | |
|
206 | 0 | if ( authenticationInfo != null ) |
207 | |
{ |
208 | 0 | username = authenticationInfo.getUserName(); |
209 | |
|
210 | 0 | password = authenticationInfo.getPassword(); |
211 | |
|
212 | 0 | client.getParams().setAuthenticationPreemptive( true ); |
213 | |
} |
214 | |
|
215 | 0 | String host = getRepository().getHost(); |
216 | |
|
217 | 0 | if ( StringUtils.isNotEmpty( username ) && StringUtils.isNotEmpty( password ) ) |
218 | |
{ |
219 | 0 | Credentials creds = new UsernamePasswordCredentials( username, password ); |
220 | |
|
221 | 0 | int port = getRepository().getPort() > -1 ? getRepository().getPort() : AuthScope.ANY_PORT; |
222 | |
|
223 | 0 | AuthScope scope = new AuthScope( host, port ); |
224 | 0 | client.getState().setCredentials( scope, creds ); |
225 | |
} |
226 | |
|
227 | 0 | HostConfiguration hc = new HostConfiguration(); |
228 | |
|
229 | 0 | ProxyInfo proxyInfo = getProxyInfo( getRepository().getProtocol(), getRepository().getHost() ); |
230 | 0 | if ( proxyInfo != null ) |
231 | |
{ |
232 | 0 | String proxyUsername = proxyInfo.getUserName(); |
233 | 0 | String proxyPassword = proxyInfo.getPassword(); |
234 | 0 | String proxyHost = proxyInfo.getHost(); |
235 | 0 | int proxyPort = proxyInfo.getPort(); |
236 | 0 | String proxyNtlmHost = proxyInfo.getNtlmHost(); |
237 | 0 | String proxyNtlmDomain = proxyInfo.getNtlmDomain(); |
238 | 0 | if ( proxyHost != null ) |
239 | |
{ |
240 | 0 | hc.setProxy( proxyHost, proxyPort ); |
241 | |
|
242 | 0 | if ( proxyUsername != null && proxyPassword != null ) |
243 | |
{ |
244 | |
Credentials creds; |
245 | 0 | if ( proxyNtlmHost != null || proxyNtlmDomain != null ) |
246 | |
{ |
247 | 0 | creds = new NTCredentials( proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain ); |
248 | |
} |
249 | |
else |
250 | |
{ |
251 | 0 | creds = new UsernamePasswordCredentials( proxyUsername, proxyPassword ); |
252 | |
} |
253 | |
|
254 | 0 | int port = proxyInfo.getPort() > -1 ? proxyInfo.getPort() : AuthScope.ANY_PORT; |
255 | |
|
256 | 0 | AuthScope scope = new AuthScope( proxyHost, port ); |
257 | 0 | client.getState().setProxyCredentials( scope, creds ); |
258 | |
} |
259 | |
} |
260 | |
} |
261 | |
|
262 | 0 | hc.setHost( host ); |
263 | |
|
264 | |
|
265 | 0 | client.setHostConfiguration( hc ); |
266 | 0 | } |
267 | |
|
268 | |
public void closeConnection() |
269 | |
{ |
270 | 0 | if ( connectionManager instanceof MultiThreadedHttpConnectionManager ) |
271 | |
{ |
272 | 0 | ( (MultiThreadedHttpConnectionManager) connectionManager ).shutdown(); |
273 | |
} |
274 | 0 | } |
275 | |
|
276 | |
public void put( File source, String resourceName ) |
277 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
278 | |
{ |
279 | 0 | Resource resource = new Resource( resourceName ); |
280 | |
|
281 | 0 | firePutInitiated( resource, source ); |
282 | |
|
283 | 0 | resource.setContentLength( source.length() ); |
284 | |
|
285 | 0 | resource.setLastModified( source.lastModified() ); |
286 | |
|
287 | 0 | put( null, resource, source ); |
288 | 0 | } |
289 | |
|
290 | |
public void putFromStream( final InputStream stream, String destination, long contentLength, long lastModified ) |
291 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
292 | |
{ |
293 | 0 | Resource resource = new Resource( destination ); |
294 | |
|
295 | 0 | firePutInitiated( resource, null ); |
296 | |
|
297 | 0 | resource.setContentLength( contentLength ); |
298 | |
|
299 | 0 | resource.setLastModified( lastModified ); |
300 | |
|
301 | 0 | put( stream, resource, null ); |
302 | 0 | } |
303 | |
|
304 | |
private void put( final InputStream stream, Resource resource, File source ) |
305 | |
throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException |
306 | |
{ |
307 | 0 | StringBuilder url = new StringBuilder( getRepository().getUrl() ); |
308 | 0 | String[] parts = StringUtils.split( resource.getName(), "/" ); |
309 | 0 | for ( String part : parts ) |
310 | |
{ |
311 | |
|
312 | |
|
313 | 0 | if ( !url.toString().endsWith( "/" ) ) |
314 | |
{ |
315 | 0 | url.append( '/' ); |
316 | |
} |
317 | 0 | url.append( URLEncoder.encode( part ) ); |
318 | |
} |
319 | |
|
320 | |
|
321 | |
try |
322 | |
{ |
323 | 0 | mkdirs( PathUtils.dirname( resource.getName() ) ); |
324 | |
} |
325 | 0 | catch ( IOException e ) |
326 | |
{ |
327 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
328 | 0 | } |
329 | |
|
330 | 0 | PutMethod putMethod = new PutMethod( url.toString() ); |
331 | |
|
332 | 0 | firePutStarted( resource, source ); |
333 | |
|
334 | |
try |
335 | |
{ |
336 | 0 | putMethod.setRequestEntity( new RequestEntityImplementation( stream, resource, this, source ) ); |
337 | |
|
338 | |
int statusCode; |
339 | |
try |
340 | |
{ |
341 | 0 | statusCode = execute( putMethod ); |
342 | |
} |
343 | 0 | catch ( IOException e ) |
344 | |
{ |
345 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); |
346 | |
|
347 | 0 | throw new TransferFailedException( e.getMessage(), e ); |
348 | 0 | } |
349 | |
|
350 | 0 | fireTransferDebug( url + " - Status code: " + statusCode ); |
351 | |
|
352 | |
|
353 | 0 | switch ( statusCode ) |
354 | |
{ |
355 | |
|
356 | |
case HttpStatus.SC_OK: |
357 | |
case HttpStatus.SC_CREATED: |
358 | |
case HttpStatus.SC_ACCEPTED: |
359 | |
case HttpStatus.SC_NO_CONTENT: |
360 | 0 | break; |
361 | |
|
362 | |
case SC_NULL: |
363 | |
{ |
364 | 0 | TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url ); |
365 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); |
366 | 0 | throw e; |
367 | |
} |
368 | |
|
369 | |
case HttpStatus.SC_FORBIDDEN: |
370 | 0 | fireSessionConnectionRefused(); |
371 | 0 | throw new AuthorizationException( "Access denied to: " + url ); |
372 | |
|
373 | |
case HttpStatus.SC_NOT_FOUND: |
374 | 0 | throw new ResourceDoesNotExistException( "File: " + url + " does not exist" ); |
375 | |
|
376 | |
|
377 | |
default: |
378 | |
{ |
379 | 0 | TransferFailedException e = new TransferFailedException( |
380 | |
"Failed to transfer file: " + url + ". Return code is: " + statusCode ); |
381 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); |
382 | 0 | throw e; |
383 | |
} |
384 | |
} |
385 | |
|
386 | 0 | firePutCompleted( resource, source ); |
387 | |
} |
388 | |
finally |
389 | |
{ |
390 | 0 | putMethod.releaseConnection(); |
391 | 0 | } |
392 | 0 | } |
393 | |
|
394 | |
protected void mkdirs( String dirname ) |
395 | |
throws IOException |
396 | |
{ |
397 | |
|
398 | 0 | } |
399 | |
|
400 | |
public boolean resourceExists( String resourceName ) |
401 | |
throws TransferFailedException, AuthorizationException |
402 | |
{ |
403 | 0 | StringBuilder url = new StringBuilder( getRepository().getUrl() ); |
404 | 0 | if ( !url.toString().endsWith( "/" ) ) |
405 | |
{ |
406 | 0 | url.append( '/' ); |
407 | |
} |
408 | 0 | url.append( resourceName ); |
409 | 0 | HeadMethod headMethod = new HeadMethod( url.toString() ); |
410 | |
int statusCode; |
411 | |
try |
412 | |
{ |
413 | 0 | statusCode = execute( headMethod ); |
414 | |
} |
415 | 0 | catch ( IOException e ) |
416 | |
{ |
417 | 0 | throw new TransferFailedException( e.getMessage(), e ); |
418 | 0 | } |
419 | |
try |
420 | |
{ |
421 | 0 | switch ( statusCode ) |
422 | |
{ |
423 | |
case HttpStatus.SC_OK: |
424 | 0 | return true; |
425 | |
|
426 | |
case HttpStatus.SC_NOT_MODIFIED: |
427 | 0 | return true; |
428 | |
|
429 | |
case SC_NULL: |
430 | 0 | throw new TransferFailedException( "Failed to transfer file: " + url ); |
431 | |
|
432 | |
case HttpStatus.SC_FORBIDDEN: |
433 | 0 | throw new AuthorizationException( "Access denied to: " + url ); |
434 | |
|
435 | |
case HttpStatus.SC_UNAUTHORIZED: |
436 | 0 | throw new AuthorizationException( "Not authorized." ); |
437 | |
|
438 | |
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: |
439 | 0 | throw new AuthorizationException( "Not authorized by proxy." ); |
440 | |
|
441 | |
case HttpStatus.SC_NOT_FOUND: |
442 | 0 | return false; |
443 | |
|
444 | |
|
445 | |
default: |
446 | 0 | throw new TransferFailedException( |
447 | |
"Failed to transfer file: " + url + ". Return code is: " + statusCode ); |
448 | |
} |
449 | |
} |
450 | |
finally |
451 | |
{ |
452 | 0 | headMethod.releaseConnection(); |
453 | |
} |
454 | |
} |
455 | |
|
456 | |
protected int execute( HttpMethod httpMethod ) |
457 | |
throws IOException |
458 | |
{ |
459 | |
int statusCode; |
460 | |
|
461 | 0 | setParameters( httpMethod ); |
462 | 0 | setHeaders( httpMethod ); |
463 | |
|
464 | 0 | statusCode = client.executeMethod( httpMethod ); |
465 | 0 | return statusCode; |
466 | |
} |
467 | |
|
468 | |
protected void setParameters( HttpMethod method ) |
469 | |
{ |
470 | 2 | HttpMethodConfiguration config = |
471 | |
httpConfiguration == null ? null : httpConfiguration.getMethodConfiguration( method ); |
472 | 2 | if ( config != null ) |
473 | |
{ |
474 | 2 | HttpMethodParams params = config.asMethodParams( method.getParams() ); |
475 | 2 | if ( params != null ) |
476 | |
{ |
477 | 2 | method.setParams( params ); |
478 | |
} |
479 | |
} |
480 | |
|
481 | 2 | if ( config == null || config.getConnectionTimeout() == HttpMethodConfiguration.DEFAULT_CONNECTION_TIMEOUT ) |
482 | |
{ |
483 | 2 | method.getParams().setSoTimeout( getTimeout() ); |
484 | |
} |
485 | 2 | } |
486 | |
|
487 | |
protected void setHeaders( HttpMethod method ) |
488 | |
{ |
489 | 2 | HttpMethodConfiguration config = |
490 | |
httpConfiguration == null ? null : httpConfiguration.getMethodConfiguration( method ); |
491 | 2 | if ( config == null || config.isUseDefaultHeaders() ) |
492 | |
{ |
493 | |
|
494 | 1 | method.addRequestHeader( "Cache-control", "no-cache" ); |
495 | 1 | method.addRequestHeader( "Cache-store", "no-store" ); |
496 | 1 | method.addRequestHeader( "Pragma", "no-cache" ); |
497 | 1 | method.addRequestHeader( "Expires", "0" ); |
498 | 1 | method.addRequestHeader( "Accept-Encoding", "gzip" ); |
499 | |
} |
500 | |
|
501 | 2 | if ( httpHeaders != null ) |
502 | |
{ |
503 | 0 | for ( Iterator i = httpHeaders.keySet().iterator(); i.hasNext(); ) |
504 | |
{ |
505 | 0 | String header = (String) i.next(); |
506 | 0 | method.addRequestHeader( header, httpHeaders.getProperty( header ) ); |
507 | 0 | } |
508 | |
} |
509 | |
|
510 | 2 | Header[] headers = config == null ? null : config.asRequestHeaders(); |
511 | 2 | if ( headers != null ) |
512 | |
{ |
513 | 2 | for ( int i = 0; i < headers.length; i++ ) |
514 | |
{ |
515 | 0 | method.addRequestHeader( headers[i] ); |
516 | |
} |
517 | |
} |
518 | 2 | } |
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
protected String getURL( Repository repository ) |
528 | |
{ |
529 | 0 | return repository.getUrl(); |
530 | |
} |
531 | |
|
532 | |
protected HttpClient getClient() |
533 | |
{ |
534 | 0 | return client; |
535 | |
} |
536 | |
|
537 | |
public void setConnectionManager( HttpConnectionManager connectionManager ) |
538 | |
{ |
539 | 0 | this.connectionManager = connectionManager; |
540 | 0 | } |
541 | |
|
542 | |
public Properties getHttpHeaders() |
543 | |
{ |
544 | 0 | return httpHeaders; |
545 | |
} |
546 | |
|
547 | |
public void setHttpHeaders( Properties httpHeaders ) |
548 | |
{ |
549 | 0 | this.httpHeaders = httpHeaders; |
550 | 0 | } |
551 | |
|
552 | |
public HttpConfiguration getHttpConfiguration() |
553 | |
{ |
554 | 0 | return httpConfiguration; |
555 | |
} |
556 | |
|
557 | |
public void setHttpConfiguration( HttpConfiguration httpConfiguration ) |
558 | |
{ |
559 | 4 | this.httpConfiguration = httpConfiguration; |
560 | 4 | } |
561 | |
|
562 | |
public void fillInputData( InputData inputData ) |
563 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
564 | |
{ |
565 | 0 | Resource resource = inputData.getResource(); |
566 | |
|
567 | 0 | StringBuilder url = new StringBuilder( getRepository().getUrl() ); |
568 | 0 | if ( !url.toString().endsWith( "/" ) ) |
569 | |
{ |
570 | 0 | url.append( '/' ); |
571 | |
} |
572 | 0 | url.append( resource.getName() ); |
573 | |
|
574 | 0 | getMethod = new GetMethod( url.toString() ); |
575 | 0 | long timestamp = resource.getLastModified(); |
576 | 0 | if ( timestamp > 0 ) |
577 | |
{ |
578 | 0 | SimpleDateFormat fmt = new SimpleDateFormat( "EEE, dd-MMM-yy HH:mm:ss zzz", Locale.US ); |
579 | 0 | fmt.setTimeZone( GMT_TIME_ZONE ); |
580 | 0 | Header hdr = new Header( "If-Modified-Since", fmt.format( new Date( timestamp ) ) ); |
581 | 0 | fireTransferDebug( "sending ==> " + hdr + "(" + timestamp + ")" ); |
582 | 0 | getMethod.addRequestHeader( hdr ); |
583 | |
} |
584 | |
|
585 | |
int statusCode; |
586 | |
try |
587 | |
{ |
588 | 0 | statusCode = execute( getMethod ); |
589 | |
} |
590 | 0 | catch ( IOException e ) |
591 | |
{ |
592 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
593 | |
|
594 | 0 | throw new TransferFailedException( e.getMessage(), e ); |
595 | 0 | } |
596 | |
|
597 | 0 | fireTransferDebug( url + " - Status code: " + statusCode ); |
598 | |
|
599 | |
|
600 | |
|
601 | 0 | switch ( statusCode ) |
602 | |
{ |
603 | |
case HttpStatus.SC_OK: |
604 | 0 | break; |
605 | |
|
606 | |
case HttpStatus.SC_NOT_MODIFIED: |
607 | |
|
608 | 0 | return; |
609 | |
|
610 | |
case SC_NULL: |
611 | |
{ |
612 | 0 | TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url ); |
613 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
614 | 0 | throw e; |
615 | |
} |
616 | |
|
617 | |
case HttpStatus.SC_FORBIDDEN: |
618 | 0 | fireSessionConnectionRefused(); |
619 | 0 | throw new AuthorizationException( "Access denied to: " + url ); |
620 | |
|
621 | |
case HttpStatus.SC_UNAUTHORIZED: |
622 | 0 | fireSessionConnectionRefused(); |
623 | 0 | throw new AuthorizationException( "Not authorized." ); |
624 | |
|
625 | |
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: |
626 | 0 | fireSessionConnectionRefused(); |
627 | 0 | throw new AuthorizationException( "Not authorized by proxy." ); |
628 | |
|
629 | |
case HttpStatus.SC_NOT_FOUND: |
630 | 0 | throw new ResourceDoesNotExistException( "File: " + url + " does not exist" ); |
631 | |
|
632 | |
|
633 | |
default: |
634 | |
{ |
635 | 0 | cleanupGetTransfer( resource ); |
636 | 0 | TransferFailedException e = new TransferFailedException( |
637 | |
"Failed to transfer file: " + url + ". Return code is: " + statusCode ); |
638 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
639 | 0 | throw e; |
640 | |
} |
641 | |
} |
642 | |
|
643 | 0 | InputStream is = null; |
644 | |
|
645 | 0 | Header contentLengthHeader = getMethod.getResponseHeader( "Content-Length" ); |
646 | |
|
647 | 0 | if ( contentLengthHeader != null ) |
648 | |
{ |
649 | |
try |
650 | |
{ |
651 | 0 | long contentLength = Integer.valueOf( contentLengthHeader.getValue() ).intValue(); |
652 | |
|
653 | 0 | resource.setContentLength( contentLength ); |
654 | |
} |
655 | 0 | catch ( NumberFormatException e ) |
656 | |
{ |
657 | 0 | fireTransferDebug( |
658 | |
"error parsing content length header '" + contentLengthHeader.getValue() + "' " + e ); |
659 | 0 | } |
660 | |
} |
661 | |
|
662 | 0 | Header lastModifiedHeader = getMethod.getResponseHeader( "Last-Modified" ); |
663 | |
|
664 | 0 | long lastModified = 0; |
665 | |
|
666 | 0 | if ( lastModifiedHeader != null ) |
667 | |
{ |
668 | |
try |
669 | |
{ |
670 | 0 | lastModified = DateUtil.parseDate( lastModifiedHeader.getValue() ).getTime(); |
671 | |
|
672 | 0 | resource.setLastModified( lastModified ); |
673 | |
} |
674 | 0 | catch ( DateParseException e ) |
675 | |
{ |
676 | 0 | fireTransferDebug( "Unable to parse last modified header" ); |
677 | 0 | } |
678 | |
|
679 | 0 | fireTransferDebug( "last-modified = " + lastModifiedHeader.getValue() + " (" + lastModified + ")" ); |
680 | |
} |
681 | |
|
682 | 0 | Header contentEncoding = getMethod.getResponseHeader( "Content-Encoding" ); |
683 | 0 | boolean isGZipped = contentEncoding != null && "gzip".equalsIgnoreCase( contentEncoding.getValue() ); |
684 | |
|
685 | |
try |
686 | |
{ |
687 | 0 | is = getMethod.getResponseBodyAsStream(); |
688 | 0 | if ( isGZipped ) |
689 | |
{ |
690 | 0 | is = new GZIPInputStream( is ); |
691 | |
} |
692 | |
} |
693 | 0 | catch ( IOException e ) |
694 | |
{ |
695 | 0 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
696 | |
|
697 | 0 | String msg = |
698 | |
"Error occurred while retrieving from remote repository:" + getRepository() + ": " + e.getMessage(); |
699 | |
|
700 | 0 | throw new TransferFailedException( msg, e ); |
701 | 0 | } |
702 | |
|
703 | 0 | inputData.setInputStream( is ); |
704 | 0 | } |
705 | |
|
706 | |
protected void cleanupGetTransfer( Resource resource ) |
707 | |
{ |
708 | 0 | if ( getMethod != null ) |
709 | |
{ |
710 | 0 | getMethod.releaseConnection(); |
711 | |
} |
712 | 0 | } |
713 | |
|
714 | |
@Override |
715 | |
public void putFromStream( InputStream stream, String destination ) |
716 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
717 | |
{ |
718 | 0 | putFromStream( stream, destination, -1, -1 ); |
719 | 0 | } |
720 | |
|
721 | |
@Override |
722 | |
protected void putFromStream( InputStream stream, Resource resource ) |
723 | |
throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException |
724 | |
{ |
725 | 0 | putFromStream( stream, resource.getName( ), -1, -1 ); |
726 | 0 | } |
727 | |
|
728 | |
@Override |
729 | |
public void fillOutputData( OutputData outputData ) |
730 | |
throws TransferFailedException |
731 | |
{ |
732 | |
|
733 | 0 | throw new IllegalStateException( "this wagon http client must not use fillOutputData" ); |
734 | |
} |
735 | |
} |