SlotRequest.java

  1. /**
  2.  *
  3.  * Copyright © 2017 Grigory Fedorov
  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.element;

  18. import org.jivesoftware.smack.packet.IQ;

  19. import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;

  20. import org.jxmpp.jid.DomainBareJid;

  21. /**
  22.  * Upload slot request.

  23.  * @author Grigory Fedorov
  24.  * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
  25.  */
  26. public class SlotRequest extends IQ {
  27.     public static final String ELEMENT = "request";
  28.     public static final String NAMESPACE = HttpFileUploadManager.NAMESPACE;

  29.     protected final String filename;
  30.     protected final long size;
  31.     protected final String contentType;

  32.     public SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size) {
  33.         this(uploadServiceAddress, filename, size, null);
  34.     }

  35.     /**
  36.      * Create new slot request.
  37.      *
  38.      * @param uploadServiceAddress the XMPP address of the service to request the slot from.
  39.      * @param filename name of file
  40.      * @param size size of file in bytes
  41.      * @param contentType file content type or null
  42.      * @throws IllegalArgumentException if size is less than or equal to zero
  43.      */
  44.     public SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size, String contentType) {
  45.         this(uploadServiceAddress, filename, size, contentType, NAMESPACE);
  46.     }

  47.     protected SlotRequest(DomainBareJid uploadServiceAddress, String filename, long size, String contentType, String namespace) {
  48.         super(ELEMENT, namespace);

  49.         if (size <= 0) {
  50.             throw new IllegalArgumentException("File fileSize must be greater than zero.");
  51.         }

  52.         this.filename = filename;
  53.         this.size = size;
  54.         this.contentType = contentType;

  55.         setType(Type.get);
  56.         setTo(uploadServiceAddress);
  57.     }

  58.     public String getFilename() {
  59.         return filename;
  60.     }

  61.     public long getSize() {
  62.         return size;
  63.     }

  64.     public String getContentType() {
  65.         return contentType;
  66.     }

  67.     @Override
  68.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  69.         xml.attribute("filename", filename);
  70.         xml.attribute("size", String.valueOf(size));
  71.         xml.optAttribute("content-type", contentType);
  72.         xml.setEmptyElement();
  73.         return xml;
  74.     }
  75. }