001/** 002 * 003 * Copyright 2003-2007 Jive Software, 2019-2021 Florian Schmaus 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.smack.packet; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.HashSet; 022import java.util.List; 023import java.util.Set; 024 025import org.jivesoftware.smack.packet.Message.Subject; 026import org.jivesoftware.smack.util.Objects; 027 028public interface MessageView extends StanzaView { 029 030 /** 031 * Returns the type of the message. If no type has been set this method will return {@link 032 * org.jivesoftware.smack.packet.Message.Type#normal}. 033 * 034 * @return the type of the message. 035 */ 036 Message.Type getType(); 037 038 /** 039 * Returns the default subject of the message, or null if the subject has not been set. 040 * The subject is a short description of message contents. 041 * <p> 042 * The default subject of a message is the subject that corresponds to the message's language. 043 * (see {@link #getLanguage()}) or if no language is set to the applications default 044 * language (see {@link Stanza#getDefaultLanguage()}). 045 * 046 * @return the subject of the message. 047 */ 048 default String getSubject() { 049 return getSubject(null); 050 } 051 052 /** 053 * Returns the subject corresponding to the language. If the language is null, the method result 054 * will be the same as {@link #getSubject()}. Null will be returned if the language does not have 055 * a corresponding subject. 056 * 057 * @param language the language of the subject to return. 058 * @return the subject related to the passed in language. 059 */ 060 default String getSubject(String language) { 061 Subject subject = getMessageSubject(language); 062 return subject == null ? null : subject.getSubject(); 063 } 064 065 default Message.Subject getMessageSubject(String language) { 066 language = Stanza.determineLanguage(this, language); 067 for (Message.Subject subject : getSubjects()) { 068 if (Objects.equals(language, subject.getLanguage()) 069 || (subject.getLanguage() == null && Objects.equals(getLanguage(), language))) { 070 return subject; 071 } 072 } 073 return null; 074 } 075 076 /** 077 * Returns a set of all subjects in this Message, including the default message subject accessible 078 * from {@link #getSubject()}. 079 * 080 * @return a collection of all subjects in this message. 081 */ 082 default Set<Message.Subject> getSubjects() { 083 List<Message.Subject> subjectList = getExtensions(Subject.class); 084 085 Set<Message.Subject> subjects = new HashSet<>(subjectList.size()); 086 subjects.addAll(subjectList); 087 088 return subjects; 089 } 090 091 /** 092 * Returns all the languages being used for the subjects, not including the default subject. 093 * 094 * @return the languages being used for the subjects. 095 */ 096 default List<String> getSubjectLanguages() { 097 Message.Subject defaultSubject = getMessageSubject(null); 098 List<String> languages = new ArrayList<String>(); 099 for (Message.Subject subject : getExtensions(Message.Subject.class)) { 100 if (!subject.equals(defaultSubject)) { 101 languages.add(subject.getLanguage()); 102 } 103 } 104 return Collections.unmodifiableList(languages); 105 } 106 107 /** 108 * Returns the default body of the message, or null if the body has not been set. The body 109 * is the main message contents. 110 * <p> 111 * The default body of a message is the body that corresponds to the message's language. 112 * (see {@link #getLanguage()}) or if no language is set to the applications default 113 * language (see {@link Stanza#getDefaultLanguage()}). 114 * 115 * @return the body of the message. 116 */ 117 default String getBody() { 118 return getBody(getLanguage()); 119 } 120 121 /** 122 * Returns the body corresponding to the language. If the language is null, the method result 123 * will be the same as {@link #getBody()}. Null will be returned if the language does not have 124 * a corresponding body. 125 * 126 * @param language the language of the body to return. 127 * @return the body related to the passed in language. 128 * @since 3.0.2 129 */ 130 default String getBody(String language) { 131 Message.Body body = getMessageBody(language); 132 return body == null ? null : body.getMessage(); 133 } 134 135 default Message.Body getMessageBody(String language) { 136 language = Stanza.determineLanguage(this, language); 137 for (Message.Body body : getBodies()) { 138 if (Objects.equals(language, body.getLanguage()) || (language != null && language.equals(getLanguage()) && body.getLanguage() == null)) { 139 return body; 140 } 141 } 142 return null; 143 } 144 145 /** 146 * Returns a set of all bodies in this Message, including the default message body accessible 147 * from {@link #getBody()}. 148 * 149 * @return a collection of all bodies in this Message. 150 * @since 3.0.2 151 */ 152 default Set<Message.Body> getBodies() { 153 List<XmlElement> bodiesList = getExtensions(Message.Body.QNAME); 154 Set<Message.Body> resultSet = new HashSet<>(bodiesList.size()); 155 for (XmlElement extensionElement : bodiesList) { 156 Message.Body body = (Message.Body) extensionElement; 157 resultSet.add(body); 158 } 159 return resultSet; 160 } 161 162 /** 163 * Returns all the languages being used for the bodies, not including the default body. 164 * 165 * @return the languages being used for the bodies. 166 * @since 3.0.2 167 */ 168 default List<String> getBodyLanguages() { 169 Message.Body defaultBody = getMessageBody(null); 170 List<String> languages = new ArrayList<String>(); 171 for (Message.Body body : getBodies()) { 172 if (!body.equals(defaultBody)) { 173 languages.add(body.getLanguage()); 174 } 175 } 176 return Collections.unmodifiableList(languages); 177 } 178 179 /** 180 * Returns the thread id of the message, which is a unique identifier for a sequence 181 * of "chat" messages. If no thread id is set, <code>null</code> will be returned. 182 * 183 * @return the thread id of the message, or <code>null</code> if it doesn't exist. 184 */ 185 default String getThread() { 186 Message.Thread thread = getExtension(Message.Thread.class); 187 if (thread == null) { 188 return null; 189 } 190 return thread.getThread(); 191 } 192}