HeadersExtension.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.shim.packet;

  18. import java.util.Collections;
  19. import java.util.List;

  20. import org.jivesoftware.smack.packet.Stanza;
  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. /**
  24.  * Extension representing a list of headers as specified in <a href="http://xmpp.org/extensions/xep-0131">Stanza Headers and Internet Metadata (SHIM)</a>
  25.  *
  26.  * @see Header
  27.  *
  28.  * @author Robin Collier
  29.  */
  30. public class HeadersExtension implements ExtensionElement {
  31.     public static final String ELEMENT = "headers";
  32.     public static final String NAMESPACE = "http://jabber.org/protocol/shim";

  33.     private final List<Header> headers;

  34.     public HeadersExtension(List<Header> headerList) {
  35.         if (headerList != null) {
  36.             headers = Collections.unmodifiableList(headerList);
  37.         } else {
  38.             headers = Collections.emptyList();
  39.         }
  40.     }

  41.     public List<Header> getHeaders() {
  42.         return headers;
  43.     }

  44.     public String getElementName() {
  45.         return ELEMENT;
  46.     }

  47.     public String getNamespace() {
  48.         return NAMESPACE;
  49.     }

  50.     @Override
  51.     public XmlStringBuilder toXML() {
  52.         XmlStringBuilder xml = new XmlStringBuilder(this);
  53.         xml.rightAngleBracket();
  54.         xml.append(headers);
  55.         xml.closeElement(this);
  56.         return xml;
  57.     }

  58.     /**
  59.      * Return the SHIM headers extension of this stanza or null if there is none.
  60.      *
  61.      * @param packet
  62.      * @return the headers extension or null.
  63.      */
  64.     public static HeadersExtension from(Stanza packet) {
  65.         return packet.getExtension(ELEMENT, NAMESPACE);
  66.     }
  67. }