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    protected SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size, String contentType, String namespace) {
057        super(ELEMENT, namespace);
058
059        if (size <= 0) {
060            throw new IllegalArgumentException("File fileSize must be greater than zero.");
061        }
062
063        this.filename = filename;
064        this.size = size;
065        this.contentType = contentType;
066
067        setType(Type.get);
068        setTo(uploadServiceAddress);
069    }
070
071    public String getFilename() {
072        return filename;
073    }
074
075    public long getSize() {
076        return size;
077    }
078
079    public String getContentType() {
080        return contentType;
081    }
082
083    @Override
084    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
085        xml.attribute("filename", filename);
086        xml.attribute("size", String.valueOf(size));
087        xml.optAttribute("content-type", contentType);
088        xml.setEmptyElement();
089        return xml;
090    }
091}