Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: thiagomoretto/ME-HTTP-Request
base: 24554a3281
...
head fork: thiagomoretto/ME-HTTP-Request
compare: 999ba87bf2
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
43 src/br/eng/moretto/me/MEHTTPRequest.java
@@ -19,15 +19,28 @@
import java.util.Map;
public class MEHTTPRequest {
+ public class PartiallyDownloadedException extends Exception {
+ private static final long serialVersionUID = -3031107770544555842L;
+ private long bytesRemaining;
+ public PartiallyDownloadedException(long bytesRemaining) {
+ this.bytesRemaining = bytesRemaining;
+ }
+ public long getBytesRemaining() {
+ return bytesRemaining;
+ }
+ }
+
public interface Listener {
public void on(MEHTTPRequest request);
}
final int maxRedirectionsCount = 5; // TODO: use it.
+ public static final String DOWNLOAD_PATH_APPENDIX = "-medownload";
// request
URL url;
String destinationPath = null;
+ String temporaryDestinationPath = null;
OutputStream destinationOutputStream = null;
MEHTTPRequest.Listener didRequestFinishedListener = null;
MEHTTPRequest.Listener didRequestFailedListener = null;
@@ -67,12 +80,17 @@ public MEHTTPRequest setDidRequestFailedListener(MEHTTPRequest.Listener listener
public MEHTTPRequest setDownloadDestinationPath(String path) {
this.destinationOutputStream = null;
- this.destinationPath = path;
+ if (path != null)
+ {
+ this.destinationPath = path;
+ this.temporaryDestinationPath = path + DOWNLOAD_PATH_APPENDIX;
+ }
return this;
}
public MEHTTPRequest setDestinationOutputStream(OutputStream destinationOutputStream) {
this.destinationPath = null;
+ this.temporaryDestinationPath = null;
this.destinationOutputStream = destinationOutputStream;
return this;
}
@@ -133,8 +151,9 @@ private void makeRequest(URL requestURL) {
urlConnection
.addRequestProperty(requestHeaderName, requestAdditionalHeaders.get(requestHeaderName));
- if (shouldAllowResumeDownloads && destinationPath != null) {
- File fd = new File(destinationPath);
+ if (shouldAllowResumeDownloads && temporaryDestinationPath != null)
+ {
+ File fd = new File(temporaryDestinationPath);
if (fd.isFile())
urlConnection.setRequestProperty("Range", "bytes=" + fd.length() + "-");
}
@@ -162,18 +181,28 @@ else if (shouldFollowRedirects &&
callListenerIfPresent(didRequestRedirectedListener);
}
else {
- if (destinationOutputStream != null)
+ if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK
+ && destinationOutputStream != null)
{
saveToOutputStream(urlConnection);
callListenerIfPresent(didRequestFinishedListener);
}
- else if (destinationPath != null)
+ else if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK
+ && destinationPath != null)
{
File fd = new File(destinationPath);
if (fd.isDirectory())
throw new Exception("Destination path is a directory. Please, append a file name.");
- saveToDestination(fd, urlConnection, shouldAllowResumeDownloads);
- callListenerIfPresent(didRequestFinishedListener);
+ File tempFd = new File(temporaryDestinationPath);
+ saveToDestination(tempFd, urlConnection, shouldAllowResumeDownloads);
+ if(tempFd.length() == urlConnection.getContentLength()) {
+ tempFd.renameTo(fd);
+ callListenerIfPresent(didRequestFinishedListener);
+ } else {
+ prepareResponse(urlConnection);
+ throw new PartiallyDownloadedException(
+ Math.abs(urlConnection.getContentLength() - tempFd.length()));
+ }
}
else
{
View
1  test/br/eng/moretto/me/tests/HttpTestServer.java
@@ -81,6 +81,7 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons
if (_mockFile != null)
{
File fd = new File(_mockFile);
+ response.setContentLength((int) fd.length());
FileInputStream fis = new FileInputStream(fd);
byte[] b = new byte[1];
OutputStream os = response.getOutputStream();
View
14 test/br/eng/moretto/me/tests/MEHTTPRequestTests.java
@@ -15,10 +15,12 @@
import org.junit.Test;
import br.eng.moretto.me.MEHTTPRequest;
+import br.eng.moretto.me.MEHTTPRequest.PartiallyDownloadedException;
public class MEHTTPRequestTests {
String urlString = "http://localhost:50036";
+ Exception lastException = null;
boolean stdOutRequestDidRedirectedListenerWasCalled,
stdOutRequestDidFinishedListenerWasCalled,
stdErrRequestDidFailedListenerWasCalled;
@@ -40,6 +42,7 @@ public void on(MEHTTPRequest request) {
@Override
public void on(MEHTTPRequest request) {
stdErrRequestDidFailedListenerWasCalled = true;
+ lastException = request.getException();
request.getException().printStackTrace(System.err);
}
};
@@ -54,6 +57,7 @@ static public void setupClass() throws Exception {
@Before
public void setup() throws Exception {
+ lastException = null;
stdOutRequestDidRedirectedListenerWasCalled = false;
stdOutRequestDidFinishedListenerWasCalled = false;
stdErrRequestDidFailedListenerWasCalled = false;
@@ -121,6 +125,10 @@ public void shouldMakeGetRequestAndSaveContentOfFileAllowingResume() throws Malf
httpTestServer.setMockResponseFile("fixtures/internet.jpg");
httpTestServer.setMockStopDownloadAtByte(1024);
+ File fi = new File("fixtures/internet.jpg");
+ File fd = new File("/tmp/temp_file_resume.jpg" + MEHTTPRequest.DOWNLOAD_PATH_APPENDIX);
+ fd.delete();
+
URL testURL = new URL(urlString);
MEHTTPRequest request = new MEHTTPRequest(testURL);
request .setShouldFollowRedirects(true)
@@ -130,13 +138,13 @@ public void shouldMakeGetRequestAndSaveContentOfFileAllowingResume() throws Malf
.setDownloadDestinationPath("/tmp/temp_file_resume.jpg")
.startSynchronous();
- File fi = new File("fixtures/internet.jpg");
- File fd = new File("/tmp/temp_file_resume.jpg");
+ fd = new File("/tmp/temp_file_resume.jpg" + MEHTTPRequest.DOWNLOAD_PATH_APPENDIX);
Assert.assertEquals(200, request.getResponseCode());
Assert.assertTrue(fd.exists());
Assert.assertEquals(1024, fd.length());
- Assert.assertTrue(stdOutRequestDidFinishedListenerWasCalled);
+ Assert.assertTrue(stdErrRequestDidFailedListenerWasCalled);
+ Assert.assertEquals(((PartiallyDownloadedException) lastException).getBytesRemaining(), fi.length() - 1024);
httpTestServer.setMockStopDownloadAtByte(-1);

No commit comments for this range

Something went wrong with that request. Please try again.