This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
ResourceExchange.java
342 lines (279 loc) · 9.01 KB
/
ResourceExchange.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package org.sonatype.nexus.index.updater.jetty;
import org.apache.maven.wagon.events.TransferEvent;
import org.codehaus.plexus.util.IOUtil;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.BufferUtil;
import org.eclipse.jetty.io.nio.NIOBuffer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import edu.emory.mathcs.backport.java.util.Arrays;
public class ResourceExchange
extends ContentExchange
{
private final File targetFile;
private final int maxRedirects;
private final TransferListenerSupport listenerSupport;
protected long lastModified;
private int redirectCount = 0;
private boolean redirectionRequested;
private String redirectionUrl;
private FileChannel targetChannel;
private File tmpFile;
private String originalUrl;
private TransferEvent transferEvent;
private String contentEncoding;
private int responseCode;
private int contentLength;
public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final int maxRedirects,
final TransferListenerSupport listenerSupport )
{
super( false );
this.targetFile = targetFile;
this.maxRedirects = maxRedirects;
this.listenerSupport = listenerSupport;
addRequestHeaders( httpHeaders );
}
private void addRequestHeaders( final HttpFields headers )
{
if ( headers != null )
{
for ( Enumeration<String> names = headers.getFieldNames(); names.hasMoreElements(); )
{
String name = names.nextElement();
addRequestHeader( name, headers.getStringField( name ) );
}
}
}
@SuppressWarnings( "deprecation" )
@Override
public void onResponseHeader( final Buffer name, final Buffer value )
throws IOException
{
super.onResponseHeader( name, value );
int header = HttpHeaders.CACHE.getOrdinal( name );
switch ( header )
{
case HttpHeaders.CONTENT_ENCODING_ORDINAL:
{
contentEncoding = value.toString();
break;
}
case HttpHeaders.CONTENT_LENGTH_ORDINAL:
{
contentLength = Integer.parseInt( value.toString() );
break;
}
case HttpHeaders.LAST_MODIFIED_ORDINAL:
{
String lastModifiedStr = BufferUtil.to8859_1_String( value );
lastModified =
( lastModifiedStr == null || lastModifiedStr.length() == 0 ? 0 : Date.parse( lastModifiedStr ) );
break;
}
case HttpHeaders.LOCATION_ORDINAL:
{
redirectionUrl = value.toString();
System.out.println( redirectionUrl );
redirectCount++;
break;
}
}
}
@Override
public void onResponseStatus( final Buffer version, final int status, final Buffer reason )
throws IOException
{
super.onResponseStatus( version, status, reason );
if ( status == ServerResponse.SC_MOVED_PERMANENTLY || status == ServerResponse.SC_MOVED_TEMPORARILY )
{
redirectionRequested = true;
}
}
public int getMaxRedirects()
{
return maxRedirects;
}
public int getRedirectCount()
{
return redirectCount;
}
public boolean prepareForRedirect()
{
if ( redirectionRequested && redirectCount < maxRedirects )
{
setURL( redirectionUrl );
reset();
return true;
}
return false;
}
@Override
public void reset()
{
super.reset();
redirectionRequested = false;
redirectionUrl = null;
}
@Override
protected synchronized void onResponseContent( final Buffer content )
throws IOException
{
if ( !redirectionRequested )
{
if ( targetChannel == null )
{
File destinationDirectory = targetFile.getParentFile();
if ( destinationDirectory != null && !destinationDirectory.exists() )
{
destinationDirectory.mkdirs();
if ( !destinationDirectory.exists() )
{
throw new IOException( "Specified destination directory cannot be created: "
+ destinationDirectory );
}
}
newTempFile();
targetChannel = new FileOutputStream( tmpFile ).getChannel();
listenerSupport.fireGetStarted( originalUrl, targetFile );
transferEvent =
new JettyTransferEvent( originalUrl, TransferEvent.TRANSFER_PROGRESS, TransferEvent.REQUEST_GET );
}
if ( content instanceof NIOBuffer )
{
targetChannel.write( ( (NIOBuffer) content ).getByteBuffer() );
}
else
{
targetChannel.write( ByteBuffer.wrap( content.asArray() ) );
}
transferEvent.setTimestamp( System.currentTimeMillis() );
listenerSupport.fireTransferProgress( transferEvent, content.array(), contentLength );
}
}
private void newTempFile()
{
tmpFile = new File( targetFile.getAbsolutePath() + ".tmp" );
}
@Override
protected void onResponseComplete()
throws IOException
{
if ( targetChannel != null && targetChannel.isOpen() )
{
targetChannel.close();
if ( "gzip".equals( contentEncoding ) )
{
InputStream in = null;
OutputStream out = null;
try
{
in = new GZIPInputStream( new FileInputStream( tmpFile ) );
out = new FileOutputStream( targetFile );
listenerSupport.fireDebug( "Unpacking GZIP content to destination: " + targetFile );
IOUtil.copy( in, out );
}
finally
{
IOUtil.close( in );
IOUtil.close( out );
}
}
else
{
if ( targetFile.exists() )
{
targetFile.delete();
}
tmpFile.renameTo( targetFile );
targetFile.setLastModified( lastModified );
}
tmpFile = null;
listenerSupport.fireGetCompleted( originalUrl, targetFile );
transferEvent = null;
}
}
@Override
public void setURL( final String url )
{
if ( originalUrl == null )
{
originalUrl = url;
}
super.setURL( url );
}
void setResponseContentStream( final InputStream in )
throws IOException
{
if ( targetChannel != null )
{
if ( targetChannel.isOpen() )
{
targetChannel.close();
}
targetChannel = null;
if ( tmpFile.exists() )
{
tmpFile.delete();
}
}
newTempFile();
OutputStream out = null;
try
{
out = new FileOutputStream( tmpFile );
transferEvent =
new JettyTransferEvent( originalUrl, TransferEvent.TRANSFER_PROGRESS, TransferEvent.REQUEST_GET );
byte[] buf = new byte[4096];
int read = -1;
while ( ( read = in.read( buf ) ) > -1 )
{
transferEvent.setTimestamp( System.currentTimeMillis() );
listenerSupport.fireTransferProgress( transferEvent, Arrays.copyOf( buf, read ), contentLength );
out.write( buf, 0, read );
}
IOUtil.copy( in, out );
out.flush();
}
finally
{
IOUtil.close( out );
}
}
void setLastModified( final long lastModified )
{
this.lastModified = lastModified;
}
void setResponseStatus( final int responseCode )
{
this.responseCode = responseCode;
}
@Override
public int getResponseStatus()
{
if ( responseCode > 0 )
{
return responseCode;
}
return super.getResponseStatus();
}
void setContentEncoding( final String contentEncoding )
{
this.contentEncoding = contentEncoding;
}
void setContentLength( final int contentLength )
{
this.contentLength = contentLength;
}
}