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