001/**
002 *
003 * Copyright the original author or authors
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.shim.packet;
018
019import java.util.Collections;
020import java.util.List;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.Stanza;
026import org.jivesoftware.smack.util.XmlStringBuilder;
027
028/**
029 * Extension representing a list of headers as specified in <a href="http://xmpp.org/extensions/xep-0131">Stanza Headers and Internet Metadata (SHIM)</a>.
030 *
031 * @see Header
032 *
033 * @author Robin Collier
034 */
035public class HeadersExtension implements ExtensionElement {
036    public static final String ELEMENT = "headers";
037    public static final String NAMESPACE = "http://jabber.org/protocol/shim";
038    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
039
040    private final List<Header> headers;
041
042    public HeadersExtension(List<Header> headerList) {
043        if (headerList != null) {
044            headers = Collections.unmodifiableList(headerList);
045        } else {
046            headers = Collections.emptyList();
047        }
048    }
049
050    public List<Header> getHeaders() {
051        return headers;
052    }
053
054    @Override
055    public String getElementName() {
056        return ELEMENT;
057    }
058
059    @Override
060    public String getNamespace() {
061        return NAMESPACE;
062    }
063
064    @Override
065    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
066        XmlStringBuilder xml = new XmlStringBuilder(this);
067        xml.rightAngleBracket();
068        xml.append(headers);
069        xml.closeElement(this);
070        return xml;
071    }
072
073    /**
074     * Return the SHIM headers extension of this stanza or null if there is none.
075     *
076     * @param packet TODO javadoc me please
077     * @return the headers extension or null.
078     */
079    public static HeadersExtension from(Stanza packet) {
080        return packet.getExtension(HeadersExtension.class);
081    }
082}