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    protected Slot(URL putUrl, URL getUrl, Map<String, String> headers, String namespace) {
050        super(ELEMENT, namespace);
051        setType(Type.result);
052        this.putUrl = putUrl;
053        this.getUrl = getUrl;
054        if (headers == null) {
055            this.headers = Collections.emptyMap();
056        } else {
057            this.headers = Collections.unmodifiableMap(headers);
058        }
059    }
060
061    public URL getPutUrl() {
062        return putUrl;
063    }
064
065    public URL getGetUrl() {
066        return getUrl;
067    }
068
069    public Map<String, String> getHeaders() {
070        return headers;
071    }
072
073    @Override
074    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
075        xml.rightAngleBracket();
076
077        xml.halfOpenElement("put").attribute("url", putUrl.toString());
078        if (headers.isEmpty()) {
079            xml.closeEmptyElement();
080        } else {
081            xml.rightAngleBracket();
082            for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
083                xml.halfOpenElement("header").attribute("name", entry.getKey()).rightAngleBracket();
084                xml.escape(entry.getValue());
085                xml.closeElement("header");
086            }
087            xml.closeElement("put");
088        }
089
090        xml.halfOpenElement("get").attribute("url", getUrl.toString()).closeEmptyElement();
091
092        return xml;
093    }
094}