From 1b0e19f699a7e7d4f543b482bccd6be353cd91a6 Mon Sep 17 00:00:00 2001 From: Micha Date: Wed, 9 Feb 2022 18:32:46 +0100 Subject: [PATCH] Add custom HttpUploadExceptions for more information in case of IOExceptions --- .../AbstractHttpUploadException.java | 101 ++++++++++++++++++ .../httpfileupload/HttpFileUploadManager.java | 9 +- 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/AbstractHttpUploadException.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/AbstractHttpUploadException.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/AbstractHttpUploadException.java new file mode 100644 index 000000000..1eb51f2ca --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/AbstractHttpUploadException.java @@ -0,0 +1,101 @@ +/** + * + * Copyright 2022 Micha Kurvers + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.httpfileupload; + +import java.io.IOException; +import java.net.URL; + +import org.jivesoftware.smackx.httpfileupload.element.Slot; +/** + * An exception class to provide additional information in case of exceptions during file uploading. + * + */ +public abstract class AbstractHttpUploadException extends IOException { + + private static final long serialVersionUID = 1L; + private final long fileSize; + private final Slot slot; + + protected AbstractHttpUploadException(long fileSize, Slot slot, String message) { + this(fileSize, slot, message, null); + } + + protected AbstractHttpUploadException(long fileSize, Slot slot, String message, Throwable wrappedThrowable) { + super(message, wrappedThrowable); + this.fileSize = fileSize; + this.slot = slot; + } + + public long getFileSize() { + return fileSize; + } + + public URL getPutUrl() { + return slot.getPutUrl(); + } + + public Slot getSlot() { + return slot; + } + + /** + * Exception thrown when http response returned after upload is not 200. + */ + public static class HttpUploadErrorException extends AbstractHttpUploadException { + + private static final long serialVersionUID = 8494356028399474995L; + private final int httpStatus; + private final String responseMsg; + + public HttpUploadErrorException(int httpStatus, String responseMsg, long fileSize, Slot slot) { + super(fileSize, slot, "Error response " + httpStatus + " from server during file upload: " + + responseMsg + ", file size: " + fileSize + ", put URL: " + + slot.getPutUrl()); + this.httpStatus = httpStatus; + this.responseMsg = responseMsg; + } + + public int getHttpStatus() { + return httpStatus; + } + + public String getResponseMsg() { + return responseMsg; + } + + } + + /** + * Exception thrown when an unexpected exception occurred during the upload. + */ + public static class HttpUploadIOException extends AbstractHttpUploadException { + + private static final long serialVersionUID = 5940866318073349451L; + private final IOException wrappedIOException; + + public HttpUploadIOException(long fileSize, Slot slot, IOException cause) { + super(fileSize, slot, "Unexpected error occurred during file upload, file size: " + fileSize + + ", put Url: " + slot.getPutUrl(), cause); + this.wrappedIOException = cause; + } + + public IOException getCausingIOException() { + return this.wrappedIOException; + } + + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java index 3f98a5056..338d519aa 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java @@ -49,6 +49,8 @@ import org.jivesoftware.smack.proxy.ProxyInfo; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.packet.DiscoverInfo; +import org.jivesoftware.smackx.httpfileupload.AbstractHttpUploadException.HttpUploadErrorException; +import org.jivesoftware.smackx.httpfileupload.AbstractHttpUploadException.HttpUploadIOException; import org.jivesoftware.smackx.httpfileupload.UploadService.Version; import org.jivesoftware.smackx.httpfileupload.element.Slot; import org.jivesoftware.smackx.httpfileupload.element.SlotRequest; @@ -494,11 +496,12 @@ public final class HttpFileUploadManager extends Manager { case HttpURLConnection.HTTP_NO_CONTENT: break; default: - throw new IOException("Error response " + status + " from server during file upload: " - + urlConnection.getResponseMessage() + ", file size: " + fileSize + ", put URL: " - + putUrl); + throw new HttpUploadErrorException(status, urlConnection.getResponseMessage(), fileSize, slot); } } + catch (IOException e) { + throw new HttpUploadIOException(fileSize, slot, e); + } finally { urlConnection.disconnect(); }