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