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 java.net.URL;
020import java.util.Collections;
021import java.util.Map;
022
023import org.jivesoftware.smack.packet.IQ;
024
025/**
026 * Slot responded by upload service.
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 Slot extends IQ {
032
033    public static final String ELEMENT = "slot";
034    public static final String NAMESPACE = SlotRequest.NAMESPACE;
035
036    protected final URL putUrl;
037    protected final URL getUrl;
038
039    private final Map<String, String> headers;
040
041    public Slot(URL putUrl, URL getUrl) {
042        this(putUrl, getUrl, null);
043    }
044
045    public Slot(URL putUrl, URL getUrl, Map<String, String> headers) {
046        this(putUrl, getUrl, headers, NAMESPACE);
047    }
048
049    @SuppressWarnings("this-escape")
050    protected Slot(URL putUrl, URL getUrl, Map<String, String> headers, String namespace) {
051        super(ELEMENT, namespace);
052        setType(Type.result);
053        this.putUrl = putUrl;
054        this.getUrl = getUrl;
055        if (headers == null) {
056            this.headers = Collections.emptyMap();
057        } else {
058            this.headers = Collections.unmodifiableMap(headers);
059        }
060    }
061
062    public URL getPutUrl() {
063        return putUrl;
064    }
065
066    public URL getGetUrl() {
067        return getUrl;
068    }
069
070    public Map<String, String> getHeaders() {
071        return headers;
072    }
073
074    @Override
075    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
076        xml.rightAngleBracket();
077
078        xml.halfOpenElement("put").attribute("url", putUrl.toString());
079        if (headers.isEmpty()) {
080            xml.closeEmptyElement();
081        } else {
082            xml.rightAngleBracket();
083            for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
084                xml.halfOpenElement("header").attribute("name", entry.getKey()).rightAngleBracket();
085                xml.escape(entry.getValue());
086                xml.closeElement("header");
087            }
088            xml.closeElement("put");
089        }
090
091        xml.halfOpenElement("get").attribute("url", getUrl.toString()).closeEmptyElement();
092
093        return xml;
094    }
095}