Skip to content

Listener to hear when file stalls on upload. #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/ning/http/multipart/FilePart.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ protected void sendData(OutputStream out) throws IOException {
}
}

public void setStalledTime(long ms) {
_stalledTime = ms;
}

public long getStalledTime() {
return _stalledTime;
}

/**
* Returns the source of the file part.
*
Expand All @@ -221,4 +229,6 @@ protected long lengthOfData() throws IOException {
return source.getLength();
}

private long _stalledTime = -1;

}
62 changes: 62 additions & 0 deletions src/main/java/com/ning/http/multipart/FilePartStallHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.ning.http.multipart;

import java.util.Timer;
import java.util.TimerTask;

/**
* @author Gail Hernandez
*/
public class FilePartStallHandler extends TimerTask {
public FilePartStallHandler(long waitTime, FilePart filePart) {
_waitTime = waitTime;
_failed = false;
_written = false;
}

public void completed() {
if(_waitTime > 0) {
_timer.cancel();
}
}

public boolean isFailed() {
return _failed;
}

public void run() {
if(!_written) {
_failed = true;
_timer.cancel();
}
_written = false;
}

public void start() {
if(_waitTime > 0) {
_timer = new Timer();
_timer.scheduleAtFixedRate(this, _waitTime, _waitTime);
}
}

public void writeHappened() {
_written = true;
}

private long _waitTime;
private Timer _timer;
private boolean _failed;
private boolean _written;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.ning.http.multipart;

import java.io.IOException;

/**
* @author Gail Hernandez
*/
public class FileUploadStalledException extends IOException {

}
19 changes: 17 additions & 2 deletions src/main/java/com/ning/http/multipart/MultipartBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.ning.http.multipart;

import com.ning.http.client.RandomAccessBody;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -434,7 +435,11 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
}

private long handleFilePart(WritableByteChannel target, FilePart filePart) throws IOException {

FilePartStallHandler handler = new FilePartStallHandler(
filePart.getStalledTime(), filePart);

handler.start();

if (FilePartSource.class.isAssignableFrom(filePart.getSource().getClass())) {
int length = 0;

Expand All @@ -453,8 +458,13 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
long nWrite = 0;
synchronized (fc) {
while (fileLength != l) {
if(handler.isFailed()) {
logger.debug("Stalled error");
throw new FileUploadStalledException();
}
try {
nWrite = fc.transferTo(fileLength, l, target);

if (nWrite == 0) {
logger.info("Waiting for writing...");
try {
Expand All @@ -463,6 +473,9 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
logger.trace(e.getMessage(), e);
}
}
else {
handler.writeHappened();
}
} catch (IOException ex) {
String message = ex.getMessage();

Expand All @@ -482,6 +495,8 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
fileLength += nWrite;
}
}
handler.completed();

fc.close();

length += handleFileEnd(target, filePart);
Expand Down Expand Up @@ -541,7 +556,7 @@ private long handleMultiPart(WritableByteChannel target, Part currentPart) throw
return handleStringPart(target, (StringPart) currentPart);
} else if (currentPart.getClass().equals(FilePart.class)) {
FilePart filePart = (FilePart) currentPart;

return handleFilePart(target, filePart);
}
return 0;
Expand Down