001/**
002 *
003 * Copyright 2022 Micha Kurvers
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.httpfileupload;
018
019import java.io.IOException;
020import java.net.URL;
021
022import org.jivesoftware.smackx.httpfileupload.element.Slot;
023/**
024 * An exception class to provide additional information in case of exceptions during file uploading.
025 *
026 */
027public abstract class AbstractHttpUploadException extends IOException {
028
029    private static final long serialVersionUID = 1L;
030    private final long fileSize;
031    private final Slot slot;
032
033    protected AbstractHttpUploadException(long fileSize, Slot slot, String message) {
034        this(fileSize, slot, message, null);
035    }
036
037    protected AbstractHttpUploadException(long fileSize, Slot slot, String message, Throwable wrappedThrowable) {
038        super(message, wrappedThrowable);
039        this.fileSize = fileSize;
040        this.slot = slot;
041    }
042
043    public long getFileSize() {
044        return fileSize;
045    }
046
047    public URL getPutUrl() {
048        return slot.getPutUrl();
049    }
050
051    public Slot getSlot() {
052        return slot;
053    }
054
055    /**
056     * Exception thrown when http response returned after upload is not 200.
057     */
058    public static class HttpUploadErrorException extends AbstractHttpUploadException {
059
060        private static final long serialVersionUID = 8494356028399474995L;
061        private final int httpStatus;
062        private final String responseMsg;
063
064        public HttpUploadErrorException(int httpStatus, String responseMsg, long fileSize, Slot slot) {
065            super(fileSize, slot, "Error response " + httpStatus + " from server during file upload: "
066                            + responseMsg + ", file size: " + fileSize + ", put URL: "
067                            + slot.getPutUrl());
068            this.httpStatus = httpStatus;
069            this.responseMsg = responseMsg;
070        }
071
072        public int getHttpStatus() {
073            return httpStatus;
074        }
075
076        public String getResponseMsg() {
077            return responseMsg;
078        }
079
080    }
081
082    /**
083     * Exception thrown when an unexpected exception occurred during the upload.
084     */
085    public static class HttpUploadIOException extends AbstractHttpUploadException {
086
087        private static final long serialVersionUID = 5940866318073349451L;
088        private final IOException wrappedIOException;
089
090        public HttpUploadIOException(long fileSize, Slot slot, IOException cause) {
091            super(fileSize, slot, "Unexpected error occurred during file upload, file size: " + fileSize
092                            + ", put Url: " + slot.getPutUrl(), cause);
093            this.wrappedIOException = cause;
094        }
095
096        public IOException getCausingIOException() {
097            return this.wrappedIOException;
098        }
099
100    }
101}