Skip to content

Commit db8e48c

Browse files
committed
Listener to hear when file stalls on upload.
1 parent 179d690 commit db8e48c

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

src/main/java/com/ning/http/multipart/FilePart.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ protected void sendData(OutputStream out) throws IOException {
202202
}
203203
}
204204

205+
public void setStalledTime(long ms) {
206+
_stalledTime = ms;
207+
}
208+
209+
public long getStalledTime() {
210+
return _stalledTime;
211+
}
212+
205213
/**
206214
* Returns the source of the file part.
207215
*
@@ -221,4 +229,6 @@ protected long lengthOfData() throws IOException {
221229
return source.getLength();
222230
}
223231

232+
private long _stalledTime = -1;
233+
224234
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU Lesser General Public License as published by the Free
6+
* Software Foundation; either version 2.1 of the License, or (at your option)
7+
* any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12+
* details.
13+
*/
14+
package com.ning.http.multipart;
15+
16+
import java.util.Timer;
17+
import java.util.TimerTask;
18+
19+
/**
20+
* @author Gail Hernandez
21+
*/
22+
public class FilePartStallHandler extends TimerTask {
23+
public FilePartStallHandler(long waitTime, FilePart filePart) {
24+
_waitTime = waitTime;
25+
_failed = false;
26+
_written = false;
27+
}
28+
29+
public void completed() {
30+
if(_waitTime > 0) {
31+
_timer.cancel();
32+
}
33+
}
34+
35+
public boolean isFailed() {
36+
return _failed;
37+
}
38+
39+
public void run() {
40+
if(!_written) {
41+
_failed = true;
42+
_timer.cancel();
43+
}
44+
_written = false;
45+
}
46+
47+
public void start() {
48+
if(_waitTime > 0) {
49+
_timer = new Timer();
50+
_timer.scheduleAtFixedRate(this, _waitTime, _waitTime);
51+
}
52+
}
53+
54+
public void writeHappened() {
55+
_written = true;
56+
}
57+
58+
private long _waitTime;
59+
private Timer _timer;
60+
private boolean _failed;
61+
private boolean _written;
62+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU Lesser General Public License as published by the Free
6+
* Software Foundation; either version 2.1 of the License, or (at your option)
7+
* any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12+
* details.
13+
*/
14+
package com.ning.http.multipart;
15+
16+
import java.io.IOException;
17+
18+
/**
19+
* @author Gail Hernandez
20+
*/
21+
public class FileUploadStalledException extends IOException {
22+
23+
}

src/main/java/com/ning/http/multipart/MultipartBody.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package com.ning.http.multipart;
1414

1515
import com.ning.http.client.RandomAccessBody;
16+
1617
import org.slf4j.Logger;
1718
import org.slf4j.LoggerFactory;
1819

@@ -434,7 +435,11 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
434435
}
435436

436437
private long handleFilePart(WritableByteChannel target, FilePart filePart) throws IOException {
437-
438+
FilePartStallHandler handler = new FilePartStallHandler(
439+
filePart.getStalledTime(), filePart);
440+
441+
handler.start();
442+
438443
if (FilePartSource.class.isAssignableFrom(filePart.getSource().getClass())) {
439444
int length = 0;
440445

@@ -453,8 +458,13 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
453458
long nWrite = 0;
454459
synchronized (fc) {
455460
while (fileLength != l) {
461+
if(handler.isFailed()) {
462+
logger.debug("Stalled error");
463+
throw new FileUploadStalledException();
464+
}
456465
try {
457466
nWrite = fc.transferTo(fileLength, l, target);
467+
458468
if (nWrite == 0) {
459469
logger.info("Waiting for writing...");
460470
try {
@@ -463,6 +473,9 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
463473
logger.trace(e.getMessage(), e);
464474
}
465475
}
476+
else {
477+
handler.writeHappened();
478+
}
466479
} catch (IOException ex) {
467480
String message = ex.getMessage();
468481

@@ -482,6 +495,8 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
482495
fileLength += nWrite;
483496
}
484497
}
498+
handler.completed();
499+
485500
fc.close();
486501

487502
length += handleFileEnd(target, filePart);
@@ -541,7 +556,7 @@ private long handleMultiPart(WritableByteChannel target, Part currentPart) throw
541556
return handleStringPart(target, (StringPart) currentPart);
542557
} else if (currentPart.getClass().equals(FilePart.class)) {
543558
FilePart filePart = (FilePart) currentPart;
544-
559+
545560
return handleFilePart(target, filePart);
546561
}
547562
return 0;

0 commit comments

Comments
 (0)