001/** 002 * 003 * Copyright 2003-2005 Jive Software. 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 */ 017 018package org.jivesoftware.smackx.jingle.packet; 019 020import java.util.Locale; 021 022import org.jivesoftware.smack.packet.PacketExtension; 023import org.jivesoftware.smack.provider.PacketExtensionProvider; 024import org.jivesoftware.smackx.jingle.media.ContentInfo; 025import org.xmlpull.v1.XmlPullParser; 026 027public class JingleError implements PacketExtension { 028 029 public static String NAMESPACE = "urn:xmpp:tmp:jingle:errors"; 030 031 public static final JingleError OUT_OF_ORDER = new JingleError("out-of-order"); 032 033 public static final JingleError UNKNOWN_SESSION = new JingleError("unknown-session"); 034 035 public static final JingleError UNSUPPORTED_CONTENT = new JingleError( 036 "unsupported-content"); 037 038 public static final JingleError UNSUPPORTED_TRANSPORTS = new JingleError( 039 "unsupported-transports"); 040 041 // Non standard error messages 042 043 public static final JingleError NO_COMMON_PAYLOAD = new JingleError( 044 "unsupported-codecs"); 045 046 public static final JingleError NEGOTIATION_ERROR = new JingleError( 047 "negotiation-error"); 048 049 public static final JingleError MALFORMED_STANZA = new JingleError("malformed-stanza"); 050 051 private String message; 052 053 /** 054 * Creates a new error with the specified code and message. 055 * 056 * @param message a message describing the error. 057 */ 058 public JingleError(final String message) { 059 this.message = message; 060 } 061 062 /** 063 * Returns the message describing the error, or null if there is no message. 064 * 065 * @return the message describing the error, or null if there is no message. 066 */ 067 public String getMessage() { 068 return message; 069 } 070 071 /** 072 * Returns the error as XML. 073 * 074 * @return the error as XML. 075 */ 076 public String toXML() { 077 StringBuilder buf = new StringBuilder(); 078 if (message != null) { 079 buf.append("<error type=\"cancel\">"); 080 buf.append("<").append(message).append(" xmlns=\"").append(NAMESPACE).append( 081 "\"/>"); 082 buf.append("</error>"); 083 } 084 return buf.toString(); 085 } 086 087 /** 088 * Returns a Action instance associated with the String value. 089 */ 090 public static JingleError fromString(String value) { 091 if (value != null) { 092 value = value.toLowerCase(Locale.US); 093 if (value.equals("out-of-order")) { 094 return OUT_OF_ORDER; 095 } else if (value.equals("unknown-session")) { 096 return UNKNOWN_SESSION; 097 } else if (value.equals("unsupported-content")) { 098 return UNSUPPORTED_CONTENT; 099 } else if (value.equals("unsupported-transports")) { 100 return UNSUPPORTED_TRANSPORTS; 101 } else if (value.equals("unsupported-codecs")) { 102 return NO_COMMON_PAYLOAD; 103 } else if (value.equals("negotiation-error")) { 104 return NEGOTIATION_ERROR; 105 } else if (value.equals("malformed-stanza")) { 106 return MALFORMED_STANZA; 107 } 108 109 } 110 return null; 111 } 112 113 public String toString() { 114 return getMessage(); 115 } 116 117 public String getElementName() { 118 return message; 119 } 120 121 public String getNamespace() { 122 return NAMESPACE; 123 } 124 125 public static class Provider implements PacketExtensionProvider { 126 127 private PacketExtension audioInfo; 128 129 /** 130 * Empty constructor. 131 */ 132 public Provider() { 133 } 134 135 /** 136 * Parse a JingleDescription.Audio extension. 137 */ 138 public PacketExtension parseExtension(final XmlPullParser parser) 139 throws Exception { 140 PacketExtension result = null; 141 142 if (audioInfo != null) { 143 result = audioInfo; 144 } else { 145 String elementName = parser.getName(); 146 147 // Try to get an Audio content info 148 ContentInfo mi = ContentInfo.Audio.fromString(elementName); 149 if (mi != null) { 150 result = new JingleContentInfo.Audio(mi); 151 } 152 } 153 return result; 154 } 155 } 156}