Slot.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 java.net.URL;
  19. import java.util.Collections;
  20. import java.util.Map;

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

  22. /**
  23.  * Slot responded by upload service.
  24.  *
  25.  * @author Grigory Fedorov
  26.  * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
  27.  */
  28. public class Slot extends IQ {

  29.     public static final String ELEMENT = "slot";
  30.     public static final String NAMESPACE = SlotRequest.NAMESPACE;

  31.     protected final URL putUrl;
  32.     protected final URL getUrl;

  33.     private final Map<String, String> headers;

  34.     public Slot(URL putUrl, URL getUrl) {
  35.         this(putUrl, getUrl, null);
  36.     }

  37.     public Slot(URL putUrl, URL getUrl, Map<String, String> headers) {
  38.         this(putUrl, getUrl, headers, NAMESPACE);
  39.     }

  40.     protected Slot(URL putUrl, URL getUrl, Map<String, String> headers, String namespace) {
  41.         super(ELEMENT, namespace);
  42.         setType(Type.result);
  43.         this.putUrl = putUrl;
  44.         this.getUrl = getUrl;
  45.         if (headers == null) {
  46.             this.headers = Collections.emptyMap();
  47.         } else {
  48.             this.headers = Collections.unmodifiableMap(headers);
  49.         }
  50.     }

  51.     public URL getPutUrl() {
  52.         return putUrl;
  53.     }

  54.     public URL getGetUrl() {
  55.         return getUrl;
  56.     }

  57.     public Map<String, String> getHeaders() {
  58.         return headers;
  59.     }

  60.     @Override
  61.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  62.         xml.rightAngleBracket();

  63.         xml.halfOpenElement("put").attribute("url", putUrl.toString());
  64.         if (headers.isEmpty()) {
  65.             xml.closeEmptyElement();
  66.         } else {
  67.             xml.rightAngleBracket();
  68.             for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
  69.                 xml.halfOpenElement("header").attribute("name", entry.getKey()).rightAngleBracket();
  70.                 xml.escape(entry.getValue());
  71.                 xml.closeElement("header");
  72.             }
  73.             xml.closeElement("put");
  74.         }

  75.         xml.halfOpenElement("get").attribute("url", getUrl.toString()).closeEmptyElement();

  76.         return xml;
  77.     }
  78. }