001/**
002 *
003 * Copyright © 2017 Grigory Fedorov
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.element;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
021import org.jxmpp.jid.DomainBareJid;
022
023/**
024 * Upload slot request.
025
026 * @author Grigory Fedorov
027 * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
028 */
029public class SlotRequest extends IQ {
030    public static final String ELEMENT = "request";
031    public static final String NAMESPACE = HttpFileUploadManager.NAMESPACE;
032
033    private final String filename;
034    private final long size;
035    private final String contentType;
036
037    public SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size) {
038        this(uploadServiceAddress, filename, size, null);
039    }
040
041    /**
042     * Create new slot request.
043     *
044     * @param uploadServiceAddress the XMPP address of the service to request the slot from.
045     * @param filename name of file
046     * @param size size of file in bytes
047     * @param contentType file content type or null
048     * @throws IllegalArgumentException if size is less than or equal to zero
049     */
050    public SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size, String contentType) {
051        this(uploadServiceAddress, filename, size, contentType, NAMESPACE);
052    }
053
054    protected SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size, String contentType, String namespace) {
055        super(ELEMENT, namespace);
056
057        if (size <= 0) {
058            throw new IllegalArgumentException("File fileSize must be greater than zero.");
059        }
060
061        this.filename = filename;
062        this.size = size;
063        this.contentType = contentType;
064
065        setType(Type.get);
066        setTo(uploadServiceAddress);
067    }
068
069    public String getFilename() {
070        return filename;
071    }
072
073    public long getSize() {
074        return size;
075    }
076
077    public String getContentType() {
078        return contentType;
079    }
080
081    @Override
082    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
083        xml.rightAngleBracket();
084        xml.element("filename", filename);
085        xml.element("size", String.valueOf(size));
086        xml.optElement("content-type", contentType);
087        return xml;
088    }
089}