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