001/** 002 * 003 * Copyright 2003-2007 Jive Software, 2015 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.Arrays; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Locale; 023import java.util.Map; 024import java.util.logging.Logger; 025 026import org.jivesoftware.smack.util.Objects; 027import org.jivesoftware.smack.util.StringUtils; 028import org.jivesoftware.smack.util.XmlStringBuilder; 029 030/** 031 * Represents an XMPP error sub-packet. Typically, a server responds to a request that has 032 * problems by sending the stanza(/packet) back and including an error packet. Each error has a type, 033 * error condition as well as as an optional text explanation. Typical errors are:<p> 034 * 035 * <table border=1> 036 * <caption>XMPP Errors</caption> 037 * <hr><td><b>XMPP Error Condition</b></td><td><b>Type</b></td><td><b>RFC 6120 Section</b></td></hr> 038 * <tr><td>bad-request</td><td>MODIFY</td><td>8.3.3.1</td></tr> 039 * <tr><td>conflict</td><td>CANCEL</td><td>8.3.3.2</td></tr> 040 * <tr><td>feature-not-implemented</td><td>CANCEL</td><td>8.3.3.3</td></tr> 041 * <tr><td>forbidden</td><td>AUTH</td><td>8.3.3.4</td></tr> 042 * <tr><td>gone</td><td>CANCEL</td><td>8.3.3.5</td></tr> 043 * <tr><td>internal-server-error</td><td>WAIT</td><td>8.3.3.6</td></tr> 044 * <tr><td>item-not-found</td><td>CANCEL</td><td>8.3.3.7</td></tr> 045 * <tr><td>jid-malformed</td><td>MODIFY</td><td>8.3.3.8</td></tr> 046 * <tr><td>not-acceptable</td><td>MODIFY</td><td>8.3.3.9</td></tr> 047 * <tr><td>not-allowed</td><td>CANCEL</td><td>8.3.3.10</td></tr> 048 * <tr><td>not-authorized</td><td>AUTH</td><td>8.3.3.11</td></tr> 049 * <tr><td>policy-violation</td><td>MODIFY</td><td>8.3.3.12</td></tr> 050 * <tr><td>recipient-unavailable</td><td>WAIT</td><td>8.3.3.13</td></tr> 051 * <tr><td>redirect</td><td>MODIFY</td><td>8.3.3.14</td></tr> 052 * <tr><td>registration-required</td><td>AUTH</td><td>8.3.3.15</td></tr> 053 * <tr><td>remote-server-not-found</td><td>CANCEL</td><td>8.3.3.16</td></tr> 054 * <tr><td>remote-server-timeout</td><td>WAIT</td><td>8.3.3.17</td></tr> 055 * <tr><td>resource-constraint</td><td>WAIT</td><td>8.3.3.18</td></tr> 056 * <tr><td>service-unavailable</td><td>CANCEL</td><td>8.3.3.19</td></tr> 057 * <tr><td>subscription-required</td><td>AUTH</td><td>8.3.3.20</td></tr> 058 * <tr><td>undefined-condition</td><td>MODIFY</td><td>8.3.3.21</td></tr> 059 * <tr><td>unexpected-request</td><td>WAIT</td><td>8.3.3.22</td></tr> 060 * </table> 061 * 062 * @author Matt Tucker 063 * @see <a href="http://xmpp.org/rfcs/rfc6120.html#stanzas-error-syntax">RFC 6120 - 8.3.2 Syntax: The Syntax of XMPP error stanzas</a> 064 */ 065// TODO Rename this class to StanzaError (RFC 6120 ยง 8.3) in Smack 4.3, as this is what this class actually is. SMACK-769 066// TODO Use StanzaErrorTextElement here. 067public class XMPPError extends AbstractError { 068 069 public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas"; 070 public static final String ERROR = "error"; 071 072 private static final Logger LOGGER = Logger.getLogger(XMPPError.class.getName()); 073 static final Map<Condition, Type> CONDITION_TO_TYPE = new HashMap<Condition, Type>(); 074 075 static { 076 CONDITION_TO_TYPE.put(Condition.bad_request, Type.MODIFY); 077 CONDITION_TO_TYPE.put(Condition.conflict, Type.CANCEL); 078 CONDITION_TO_TYPE.put(Condition.feature_not_implemented, Type.CANCEL); 079 CONDITION_TO_TYPE.put(Condition.forbidden, Type.AUTH); 080 CONDITION_TO_TYPE.put(Condition.gone, Type.CANCEL); 081 CONDITION_TO_TYPE.put(Condition.internal_server_error, Type.CANCEL); 082 CONDITION_TO_TYPE.put(Condition.item_not_found, Type.CANCEL); 083 CONDITION_TO_TYPE.put(Condition.jid_malformed, Type.MODIFY); 084 CONDITION_TO_TYPE.put(Condition.not_acceptable, Type.MODIFY); 085 CONDITION_TO_TYPE.put(Condition.not_allowed, Type.CANCEL); 086 CONDITION_TO_TYPE.put(Condition.not_authorized, Type.AUTH); 087 CONDITION_TO_TYPE.put(Condition.policy_violation, Type.MODIFY); 088 CONDITION_TO_TYPE.put(Condition.recipient_unavailable, Type.WAIT); 089 CONDITION_TO_TYPE.put(Condition.redirect, Type.MODIFY); 090 CONDITION_TO_TYPE.put(Condition.registration_required, Type.AUTH); 091 CONDITION_TO_TYPE.put(Condition.remote_server_not_found, Type.CANCEL); 092 CONDITION_TO_TYPE.put(Condition.remote_server_timeout, Type.WAIT); 093 CONDITION_TO_TYPE.put(Condition.resource_constraint, Type.WAIT); 094 CONDITION_TO_TYPE.put(Condition.service_unavailable, Type.CANCEL); 095 CONDITION_TO_TYPE.put(Condition.subscription_required, Type.AUTH); 096 CONDITION_TO_TYPE.put(Condition.undefined_condition, Type.MODIFY); 097 CONDITION_TO_TYPE.put(Condition.unexpected_request, Type.WAIT); 098 } 099 100 private final Condition condition; 101 private final String conditionText; 102 private final String errorGenerator; 103 private final Type type; 104 private final Stanza stanza; 105 106 // TODO: Deprecated constructors 107 // deprecate in 4.3 108 109 /** 110 * Create a new XMPPError. 111 * 112 * @param condition 113 * @deprecated use {@link Builder} instead. 114 */ 115 @Deprecated 116 public XMPPError(Condition condition) { 117 this(condition, null, null, null, null, null, null); 118 } 119 120 /** 121 * Create a new XMPPError. 122 * 123 * @param condition 124 * @param applicationSpecificCondition 125 * @deprecated use {@link Builder} instead. 126 */ 127 @Deprecated 128 public XMPPError(Condition condition, ExtensionElement applicationSpecificCondition) { 129 this(condition, null, null, null, null, Arrays.asList(applicationSpecificCondition), null); 130 } 131 132 /** 133 * Creates a new error with the specified type, condition and message. 134 * This constructor is used when the condition is not recognized automatically by XMPPError 135 * i.e. there is not a defined instance of ErrorCondition or it does not apply the default 136 * specification. 137 * 138 * @param type the error type. 139 * @param condition the error condition. 140 * @param descriptiveTexts 141 * @param extensions list of stanza(/packet) extensions 142 * @deprecated use {@link Builder} instead. 143 */ 144 @Deprecated 145 public XMPPError(Condition condition, String conditionText, String errorGenerator, Type type, Map<String, String> descriptiveTexts, 146 List<ExtensionElement> extensions) { 147 this(condition, conditionText, errorGenerator, type, descriptiveTexts, extensions, null); 148 } 149 150 /** 151 * Creates a new error with the specified type, condition and message. 152 * This constructor is used when the condition is not recognized automatically by XMPPError 153 * i.e. there is not a defined instance of ErrorCondition or it does not apply the default 154 * specification. 155 * 156 * @param type the error type. 157 * @param condition the error condition. 158 * @param descriptiveTexts 159 * @param extensions list of stanza(/packet) extensions 160 * @param stanza the stanza carrying this XMPP error. 161 */ 162 public XMPPError(Condition condition, String conditionText, String errorGenerator, Type type, Map<String, String> descriptiveTexts, 163 List<ExtensionElement> extensions, Stanza stanza) { 164 super(descriptiveTexts, NAMESPACE, extensions); 165 this.condition = Objects.requireNonNull(condition, "condition must not be null"); 166 this.stanza = stanza; 167 // Some implementations may send the condition as non-empty element containing the empty string, that is 168 // <condition xmlns='foo'></condition>, in this case the parser may calls this constructor with the empty string 169 // as conditionText, therefore reset it to null if it's the empty string 170 if (StringUtils.isNullOrEmpty(conditionText)) { 171 conditionText = null; 172 } 173 if (conditionText != null) { 174 switch (condition) { 175 case gone: 176 case redirect: 177 break; 178 default: 179 throw new IllegalArgumentException( 180 "Condition text can only be set with condtion types 'gone' and 'redirect', not " 181 + condition); 182 } 183 } 184 this.conditionText = conditionText; 185 this.errorGenerator = errorGenerator; 186 if (type == null) { 187 Type determinedType = CONDITION_TO_TYPE.get(condition); 188 if (determinedType == null) { 189 LOGGER.warning("Could not determine type for condition: " + condition); 190 determinedType = Type.CANCEL; 191 } 192 this.type = determinedType; 193 } else { 194 this.type = type; 195 } 196 } 197 198 /** 199 * Returns the error condition. 200 * 201 * @return the error condition. 202 */ 203 public Condition getCondition() { 204 return condition; 205 } 206 207 /** 208 * Returns the error type. 209 * 210 * @return the error type. 211 */ 212 public Type getType() { 213 return type; 214 } 215 216 public String getErrorGenerator() { 217 return errorGenerator; 218 } 219 220 public String getConditionText() { 221 return conditionText; 222 } 223 224 /** 225 * Get the stanza carrying the XMPP error. 226 * 227 * @return the stanza carrying the XMPP error. 228 * @since 4.2 229 */ 230 public Stanza getStanza() { 231 return stanza; 232 } 233 234 @Override 235 public String toString() { 236 StringBuilder sb = new StringBuilder("XMPPError: "); 237 sb.append(condition.toString()).append(" - ").append(type.toString()); 238 if (errorGenerator != null) { 239 sb.append(". Generated by ").append(errorGenerator); 240 } 241 return sb.toString(); 242 } 243 244 /** 245 * Returns the error as XML. 246 * 247 * @return the error as XML. 248 */ 249 public XmlStringBuilder toXML() { 250 XmlStringBuilder xml = new XmlStringBuilder(); 251 xml.halfOpenElement(ERROR); 252 xml.attribute("type", type.toString()); 253 xml.optAttribute("by", errorGenerator); 254 xml.rightAngleBracket(); 255 256 xml.halfOpenElement(condition.toString()); 257 xml.xmlnsAttribute(NAMESPACE); 258 if (conditionText != null) { 259 xml.rightAngleBracket(); 260 xml.escape(conditionText); 261 xml.closeElement(condition.toString()); 262 } 263 else { 264 xml.closeEmptyElement(); 265 } 266 267 addDescriptiveTextsAndExtensions(xml); 268 269 xml.closeElement(ERROR); 270 return xml; 271 } 272 273 public static XMPPError.Builder from(Condition condition, String descriptiveText) { 274 XMPPError.Builder builder = getBuilder().setCondition(condition); 275 if (descriptiveText != null) { 276 Map<String, String> descriptiveTexts = new HashMap<>(); 277 descriptiveTexts.put("en", descriptiveText); 278 builder.setDescriptiveTexts(descriptiveTexts); 279 } 280 return builder; 281 } 282 283 public static Builder getBuilder() { 284 return new Builder(); 285 } 286 287 public static Builder getBuilder(Condition condition) { 288 return getBuilder().setCondition(condition); 289 } 290 291 public static Builder getBuilder(XMPPError xmppError) { 292 return getBuilder().copyFrom(xmppError); 293 } 294 295 public static final class Builder extends AbstractError.Builder<Builder> { 296 private Condition condition; 297 private String conditionText; 298 private String errorGenerator; 299 private Type type; 300 private Stanza stanza; 301 302 private Builder() { 303 } 304 305 public Builder setCondition(Condition condition) { 306 this.condition = condition; 307 return this; 308 } 309 310 public Builder setType(Type type) { 311 this.type = type; 312 return this; 313 } 314 315 public Builder setConditionText(String conditionText) { 316 this.conditionText = conditionText; 317 return this; 318 } 319 320 public Builder setErrorGenerator(String errorGenerator) { 321 this.errorGenerator = errorGenerator; 322 return this; 323 } 324 325 public Builder setStanza(Stanza stanza) { 326 this.stanza = stanza; 327 return this; 328 } 329 330 public Builder copyFrom(XMPPError xmppError) { 331 setCondition(xmppError.getCondition()); 332 setType(xmppError.getType()); 333 setConditionText(xmppError.getConditionText()); 334 setErrorGenerator(xmppError.getErrorGenerator()); 335 setStanza(xmppError.getStanza()); 336 setDescriptiveTexts(xmppError.descriptiveTexts); 337 setTextNamespace(xmppError.textNamespace); 338 setExtensions(xmppError.extensions); 339 return this; 340 } 341 342 public XMPPError build() { 343 return new XMPPError(condition, conditionText, errorGenerator, type, descriptiveTexts, 344 extensions, stanza); 345 } 346 347 @Override 348 protected Builder getThis() { 349 return this; 350 } 351 } 352 /** 353 * A class to represent the type of the Error. The types are: 354 * 355 * <ul> 356 * <li>XMPPError.Type.WAIT - retry after waiting (the error is temporary) 357 * <li>XMPPError.Type.CANCEL - do not retry (the error is unrecoverable) 358 * <li>XMPPError.Type.MODIFY - retry after changing the data sent 359 * <li>XMPPError.Type.AUTH - retry after providing credentials 360 * <li>XMPPError.Type.CONTINUE - proceed (the condition was only a warning) 361 * </ul> 362 */ 363 public static enum Type { 364 WAIT, 365 CANCEL, 366 MODIFY, 367 AUTH, 368 CONTINUE; 369 370 @Override 371 public String toString() { 372 return name().toLowerCase(Locale.US); 373 } 374 375 public static Type fromString(String string) { 376 string = string.toUpperCase(Locale.US); 377 return Type.valueOf(string); 378 } 379 } 380 381 public enum Condition { 382 bad_request, 383 conflict, 384 feature_not_implemented, 385 forbidden, 386 gone, 387 internal_server_error, 388 item_not_found, 389 jid_malformed, 390 not_acceptable, 391 not_allowed, 392 not_authorized, 393 policy_violation, 394 recipient_unavailable, 395 redirect, 396 registration_required, 397 remote_server_not_found, 398 remote_server_timeout, 399 resource_constraint, 400 service_unavailable, 401 subscription_required, 402 undefined_condition, 403 unexpected_request; 404 405 @Override 406 public String toString() { 407 return this.name().replace('_', '-'); 408 } 409 410 public static Condition fromString(String string) { 411 // Backwards compatibility for older implementations still using RFC 3920. RFC 6120 412 // changed 'xml-not-well-formed' to 'not-well-formed'. 413 if ("xml-not-well-formed".equals(string)) { 414 string = "not-well-formed"; 415 } 416 string = string.replace('-', '_'); 417 Condition condition = null; 418 try { 419 condition = Condition.valueOf(string); 420 } catch (Exception e) { 421 throw new IllegalStateException("Could not transform string '" + string + "' to XMPPErrorCondition", e); 422 } 423 return condition; 424 } 425 } 426 427}