diff --git a/build.gradle b/build.gradle index 14e6f7b..c886089 100644 --- a/build.gradle +++ b/build.gradle @@ -16,8 +16,8 @@ allprojects { // We compile the library using Java 1.7 compatibility // in order to ensure interoperability with older Android platforms. -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +sourceCompatibility = 1.9 +targetCompatibility = 1.9 // load version number from file def config = new ConfigSlurper().parse(new File("${projectDir}/src/main/resources/tus-java-client-version/version.properties").toURI().toURL()) diff --git a/src/main/java/io/tus/java/client/ProtocolException.java b/src/main/java/io/tus/java/client/ProtocolException.java index 79cd722..1a8d993 100644 --- a/src/main/java/io/tus/java/client/ProtocolException.java +++ b/src/main/java/io/tus/java/client/ProtocolException.java @@ -1,7 +1,9 @@ package io.tus.java.client; import java.io.IOException; +import java.io.InputStream; import java.net.HttpURLConnection; +import java.nio.charset.StandardCharsets; /** * This exception is thrown if the server sends a request with an unexpected status code or @@ -19,7 +21,7 @@ public ProtocolException(String message) { } /** - Instantiates a new Object of type {@link ProtocolException}. + * Instantiates a new Object of type {@link ProtocolException}. * @param message Message to be thrown with the exception. * @param connection {@link HttpURLConnection}, where the error occurred. */ @@ -29,7 +31,26 @@ public ProtocolException(String message, HttpURLConnection connection) { } /** - * Returns the {@link HttpURLConnection} instances, which caused the error. + * Create a new {@link ProtocolException} instance caused by an unexpected status code. + * @param connection {@link HttpURLConnection}, where the error occurred. + * @param action Description of the action when the error occurred. + * @return {@link HttpURLConnection} + */ + static ProtocolException unexpectedStatusCode(HttpURLConnection connection, String action) throws IOException { + int code = connection.getResponseCode(); + String response = "n/a"; + //System.out.println(connection.getInputStream()); + InputStream responseStream = connection.getErrorStream(); + if (responseStream != null) { + response = new String(responseStream.readAllBytes(), StandardCharsets.UTF_8).trim(); + } + + return new ProtocolException( + "unexpected status code (" + code + ") while " + action + "; response is: " + response, connection); + } + + /** + * Returns the {@link HttpURLConnection} instance which caused the error. * @return {@link HttpURLConnection} */ public HttpURLConnection getCausingConnection() { diff --git a/src/main/java/io/tus/java/client/TusClient.java b/src/main/java/io/tus/java/client/TusClient.java index a5cac6a..3e2eb5d 100644 --- a/src/main/java/io/tus/java/client/TusClient.java +++ b/src/main/java/io/tus/java/client/TusClient.java @@ -208,8 +208,7 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept int responseCode = connection.getResponseCode(); if (!(responseCode >= 200 && responseCode < 300)) { - throw new ProtocolException( - "unexpected status code (" + responseCode + ") while creating upload", connection); + throw ProtocolException.unexpectedStatusCode(connection, "creating upload"); } String urlStr = connection.getHeaderField("Location"); @@ -303,8 +302,7 @@ public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNul int responseCode = connection.getResponseCode(); if (!(responseCode >= 200 && responseCode < 300)) { - throw new ProtocolException( - "unexpected status code (" + responseCode + ") while resuming upload", connection); + throw ProtocolException.unexpectedStatusCode(connection, "resuming upload"); } String offsetStr = connection.getHeaderField("Upload-Offset"); diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index d84bd8b..5b9083a 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -337,11 +337,8 @@ private void finishConnection() throws ProtocolException, IOException { if (connection != null) { int responseCode = connection.getResponseCode(); - connection.disconnect(); - if (!(responseCode >= 200 && responseCode < 300)) { - throw new ProtocolException("unexpected status code (" + responseCode + ") while uploading chunk", - connection); + throw ProtocolException.unexpectedStatusCode(connection, "uploading chunk"); } // TODO detect changes and seek accordingly @@ -358,6 +355,7 @@ private void finishConnection() throws ProtocolException, IOException { connection); } + connection.disconnect(); connection = null; } }