001/** 002 * 003 * Copyright © 2016-2020 Florian Schmaus and Fernando Ramirez 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.mam.element; 018 019import java.util.List; 020 021import org.jivesoftware.smack.packet.Element; 022import org.jivesoftware.smack.packet.ExtensionElement; 023import org.jivesoftware.smack.packet.Message; 024import org.jivesoftware.smack.packet.MessageView; 025import org.jivesoftware.smack.packet.XmlElement; 026import org.jivesoftware.smack.util.StringUtils; 027import org.jivesoftware.smack.util.XmlStringBuilder; 028 029import org.jivesoftware.smackx.forward.packet.Forwarded; 030 031import org.jxmpp.jid.Jid; 032 033/** 034 * MAM elements. 035 * 036 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message 037 * Archive Management</a> 038 * @author Fernando Ramirez and Florian Schmaus 039 * 040 */ 041public class MamElements { 042 043 /** 044 * MAM result extension class. 045 * 046 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message 047 * Archive Management</a> 048 * 049 */ 050 public abstract static class MamResultExtension implements ExtensionElement { 051 052 /** 053 * result element. 054 */ 055 public static final String ELEMENT = "result"; 056 057 /** 058 * id of the result. 059 */ 060 private final String id; 061 062 /** 063 * the forwarded element. 064 */ 065 private final Forwarded<Message> forwarded; 066 067 /** 068 * the query id. 069 */ 070 private String queryId; 071 072 protected final MamVersion version; 073 074 /** 075 * MAM result extension constructor. 076 * 077 * @param version TODO javadoc me please 078 * @param queryId TODO javadoc me please 079 * @param id TODO javadoc me please 080 * @param forwarded TODO javadoc me please 081 */ 082 public MamResultExtension(MamVersion version, String queryId, String id, Forwarded<Message> forwarded) { 083 if (StringUtils.isEmpty(id)) { 084 throw new IllegalArgumentException("id must not be null or empty"); 085 } 086 if (forwarded == null) { 087 throw new IllegalArgumentException("forwarded must no be null"); 088 } 089 if (version == null) { 090 throw new IllegalArgumentException("version must not be null"); 091 } 092 this.version = version; 093 this.id = id; 094 this.forwarded = forwarded; 095 this.queryId = queryId; 096 } 097 098 /** 099 * Get the id. 100 * 101 * @return the id 102 */ 103 public String getId() { 104 return id; 105 } 106 107 /** 108 * Get the forwarded element. 109 * 110 * @return the forwarded element 111 */ 112 public Forwarded<Message> getForwarded() { 113 return forwarded; 114 } 115 116 /** 117 * Get query id. 118 * 119 * @return the query id 120 */ 121 public final String getQueryId() { 122 return queryId; 123 } 124 125 @Override 126 public String getElementName() { 127 return ELEMENT; 128 } 129 130 @Override 131 public final String getNamespace() { 132 return version.getNamespace(); 133 } 134 135 @Override 136 public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 137 XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace); 138 139 xml.optAttribute("queryid", getQueryId()); 140 xml.optAttribute("id", getId()); 141 xml.rightAngleBracket(); 142 143 xml.append(getForwarded()); 144 145 xml.closeElement(this); 146 return xml; 147 } 148 149 public static MamResultExtension from(MessageView message) { 150 for (XmlElement extension : message.getExtensions()) { 151 if (extension instanceof MamResultExtension) { 152 return (MamResultExtension) extension; 153 } 154 } 155 156 return null; 157 } 158 159 } 160 161 /** 162 * Always JID list element class for the MamPrefsIQ. 163 * 164 */ 165 public static class AlwaysJidListElement implements Element { 166 167 /** 168 * list of JIDs. 169 */ 170 private final List<Jid> alwaysJids; 171 172 /** 173 * Always JID list element constructor. 174 * 175 * @param alwaysJids TODO javadoc me please 176 */ 177 AlwaysJidListElement(List<Jid> alwaysJids) { 178 this.alwaysJids = alwaysJids; 179 } 180 181 @Override 182 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 183 XmlStringBuilder xml = new XmlStringBuilder(); 184 xml.openElement("always"); 185 186 for (Jid jid : alwaysJids) { 187 xml.element("jid", jid); 188 } 189 190 xml.closeElement("always"); 191 return xml; 192 } 193 } 194 195 /** 196 * Never JID list element class for the MamPrefsIQ. 197 * 198 */ 199 public static class NeverJidListElement implements Element { 200 201 /** 202 * list of JIDs 203 */ 204 private List<Jid> neverJids; 205 206 /** 207 * Never JID list element constructor. 208 * 209 * @param neverJids TODO javadoc me please 210 */ 211 public NeverJidListElement(List<Jid> neverJids) { 212 this.neverJids = neverJids; 213 } 214 215 @Override 216 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 217 XmlStringBuilder xml = new XmlStringBuilder(); 218 xml.openElement("never"); 219 220 for (Jid jid : neverJids) { 221 xml.element("jid", jid); 222 } 223 224 xml.closeElement("never"); 225 return xml; 226 } 227 } 228 229}