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.Stanza; 023import org.jivesoftware.smack.packet.ExtensionElement; 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 public String getElementName() { 052 return ELEMENT; 053 } 054 055 public String getNamespace() { 056 return NAMESPACE; 057 } 058 059 @Override 060 public XmlStringBuilder toXML() { 061 XmlStringBuilder xml = new XmlStringBuilder(this); 062 xml.rightAngleBracket(); 063 xml.append(headers); 064 xml.closeElement(this); 065 return xml; 066 } 067 068 /** 069 * Return the SHIM headers extension of this stanza or null if there is none. 070 * 071 * @param packet 072 * @return the headers extension or null. 073 */ 074 public static HeadersExtension from(Stanza packet) { 075 return packet.getExtension(ELEMENT, NAMESPACE); 076 } 077}