AbstractHttpUploadException.java

  1. /**
  2.  *
  3.  * Copyright 2022 Micha Kurvers
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.httpfileupload;

  18. import java.io.IOException;
  19. import java.net.URL;

  20. import org.jivesoftware.smackx.httpfileupload.element.Slot;
  21. /**
  22.  * An exception class to provide additional information in case of exceptions during file uploading.
  23.  *
  24.  */
  25. public abstract class AbstractHttpUploadException extends IOException {

  26.     private static final long serialVersionUID = 1L;
  27.     private final long fileSize;
  28.     private final Slot slot;

  29.     protected AbstractHttpUploadException(long fileSize, Slot slot, String message) {
  30.         this(fileSize, slot, message, null);
  31.     }

  32.     protected AbstractHttpUploadException(long fileSize, Slot slot, String message, Throwable wrappedThrowable) {
  33.         super(message, wrappedThrowable);
  34.         this.fileSize = fileSize;
  35.         this.slot = slot;
  36.     }

  37.     public long getFileSize() {
  38.         return fileSize;
  39.     }

  40.     public URL getPutUrl() {
  41.         return slot.getPutUrl();
  42.     }

  43.     public Slot getSlot() {
  44.         return slot;
  45.     }

  46.     /**
  47.      * Exception thrown when http response returned after upload is not 200.
  48.      */
  49.     public static class HttpUploadErrorException extends AbstractHttpUploadException {

  50.         private static final long serialVersionUID = 8494356028399474995L;
  51.         private final int httpStatus;
  52.         private final String responseMsg;

  53.         public HttpUploadErrorException(int httpStatus, String responseMsg, long fileSize, Slot slot) {
  54.             super(fileSize, slot, "Error response " + httpStatus + " from server during file upload: "
  55.                             + responseMsg + ", file size: " + fileSize + ", put URL: "
  56.                             + slot.getPutUrl());
  57.             this.httpStatus = httpStatus;
  58.             this.responseMsg = responseMsg;
  59.         }

  60.         public int getHttpStatus() {
  61.             return httpStatus;
  62.         }

  63.         public String getResponseMsg() {
  64.             return responseMsg;
  65.         }

  66.     }

  67.     /**
  68.      * Exception thrown when an unexpected exception occurred during the upload.
  69.      */
  70.     public static class HttpUploadIOException extends AbstractHttpUploadException {

  71.         private static final long serialVersionUID = 5940866318073349451L;
  72.         private final IOException wrappedIOException;

  73.         public HttpUploadIOException(long fileSize, Slot slot, IOException cause) {
  74.             super(fileSize, slot, "Unexpected error occurred during file upload, file size: " + fileSize
  75.                             + ", put Url: " + slot.getPutUrl(), cause);
  76.             this.wrappedIOException = cause;
  77.         }

  78.         public IOException getCausingIOException() {
  79.             return this.wrappedIOException;
  80.         }

  81.     }
  82. }