001/**
002 *
003 * Copyright 2020 Paul Schaub
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.urldata.element;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.packet.NamedElement;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.util.EqualsUtil;
026import org.jivesoftware.smack.util.HashCode;
027import org.jivesoftware.smack.util.XmlStringBuilder;
028
029import org.jivesoftware.smackx.urldata.http.element.CookieElement;
030import org.jivesoftware.smackx.urldata.http.element.HeaderElement;
031import org.jivesoftware.smackx.urldata.http.element.HttpAuthElement;
032
033/**
034 * The url-data element.
035 */
036public class UrlDataElement implements ExtensionElement {
037
038    public static final String ELEMENT = "url-data";
039    public static final String NAMESPACE = "http://jabber.org/protocol/url-data";
040    public static final String ATTR_TARGET = "target";
041    public static final String ATTR_SID = "sid";
042    public static final String XMLNS_HTTP = "xmlns:http";
043
044    public static final String SCHEME_HTTP = "http://jabber.org/protocol/url-data/scheme/http";
045
046    private final String target;
047    private final String sid;
048    private final List<HttpAuthElement> authParamElements = new ArrayList<>();
049    private final List<CookieElement> cookieElements = new ArrayList<>();
050    private final List<HeaderElement> headerElements = new ArrayList<>();
051
052    public UrlDataElement(String target,
053                          String sid) {
054        this(target, sid, null, null, null);
055    }
056
057    public UrlDataElement(String target,
058                          String sid,
059                          List<HttpAuthElement> authParamElements,
060                          List<CookieElement> cookieElements,
061                          List<HeaderElement> headerElements) {
062        this.target = target;
063        this.sid = sid;
064        if (authParamElements != null) {
065            this.authParamElements.addAll(authParamElements);
066        }
067        if (cookieElements != null) {
068            this.cookieElements.addAll(cookieElements);
069        }
070        if (headerElements != null) {
071            this.headerElements.addAll(headerElements);
072        }
073    }
074
075    public String getTarget() {
076        return target;
077    }
078
079    /**
080     * Return the optional stream identifier used for XEP-0095: Stream Initiation.
081     *
082     * @return stream identifier or null
083     */
084    public String getSid() {
085        return sid;
086    }
087
088    public List<HttpAuthElement> getAuthParameters() {
089        return authParamElements;
090    }
091
092    public List<CookieElement> getCookies() {
093        return cookieElements;
094    }
095
096    public List<HeaderElement> getHeaders() {
097        return headerElements;
098    }
099
100    private List<NamedElement> getMetaInformationElements() {
101        List<NamedElement> elements = new ArrayList<>();
102        elements.addAll(getAuthParameters());
103        elements.addAll(getCookies());
104        elements.addAll(getHeaders());
105        return elements;
106    }
107
108    @Override
109    public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
110        List<NamedElement> metaInformation = getMetaInformationElements();
111
112        XmlStringBuilder sb = new XmlStringBuilder(this);
113        if (!metaInformation.isEmpty()) {
114            sb.attribute(XMLNS_HTTP, SCHEME_HTTP);
115        }
116        sb.attribute(ATTR_TARGET, getTarget())
117                .optAttribute(ATTR_SID, getSid());
118        if (metaInformation.isEmpty()) {
119            return sb.closeEmptyElement();
120        } else {
121            return sb.rightAngleBracket()
122                    .append(metaInformation)
123                    .closeElement(this);
124        }
125    }
126
127    @Override
128    public String getNamespace() {
129        return NAMESPACE;
130    }
131
132    @Override
133    public String getElementName() {
134        return ELEMENT;
135    }
136
137    @Override
138    public int hashCode() {
139        return HashCode.builder()
140                .append(getElementName())
141                .append(getNamespace())
142                .append(getTarget())
143                .append(getAuthParameters())
144                .append(getCookies())
145                .append(getHeaders())
146                .build();
147    }
148
149    @Override
150    public boolean equals(Object obj) {
151        return EqualsUtil.equals(this, obj, (equalsBuilder, other) ->
152                equalsBuilder
153                        .append(getElementName(), other.getElementName())
154                        .append(getNamespace(), other.getNamespace())
155                        .append(getTarget(), other.getTarget())
156                        .append(getAuthParameters(), other.getAuthParameters())
157                        .append(getCookies(), other.getCookies())
158                        .append(getHeaders(), other.getHeaders()));
159    }
160}