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.provider.ExtensionElementProvider;
024import org.jivesoftware.smackx.jingleold.media.ContentInfo;
025import org.xmlpull.v1.XmlPullParser;
026
027public class JingleError implements ExtensionElement {
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    @Override
077    public String toXML() {
078        StringBuilder buf = new StringBuilder();
079        if (message != null) {
080            buf.append("<error type=\"cancel\">");
081            buf.append('<').append(message).append(" xmlns=\"").append(NAMESPACE).append(
082                    "\"/>");
083            buf.append("</error>");
084        }
085        return buf.toString();
086    }
087
088    /**
089     * Returns a Action instance associated with the String value.
090     */
091    public static JingleError fromString(String value) {
092        if (value != null) {
093            value = value.toLowerCase(Locale.US);
094            if (value.equals("out-of-order")) {
095                return OUT_OF_ORDER;
096            } else if (value.equals("unknown-session")) {
097                return UNKNOWN_SESSION;
098            } else if (value.equals("unsupported-content")) {
099                return UNSUPPORTED_CONTENT;
100            } else if (value.equals("unsupported-transports")) {
101                return UNSUPPORTED_TRANSPORTS;
102            } else if (value.equals("unsupported-codecs")) {
103                return NO_COMMON_PAYLOAD;
104            } else if (value.equals("negotiation-error")) {
105                return NEGOTIATION_ERROR;
106            } else if (value.equals("malformed-stanza")) {
107                return MALFORMED_STANZA;
108            }
109
110        }
111        return null;
112    }
113
114    @Override
115    public String toString() {
116        return getMessage();
117    }
118
119    @Override
120    public String getElementName() {
121        return message;
122    }
123
124    @Override
125    public String getNamespace() {
126        return NAMESPACE;
127    }
128
129    public static class Provider extends ExtensionElementProvider<ExtensionElement> {
130
131           private ExtensionElement audioInfo;
132
133           /**
134            * Empty constructor.
135            */
136           public Provider() {
137           }
138
139           /**
140            * Parse a JingleDescription.Audio extension.
141            */
142           @Override
143           public ExtensionElement parse(XmlPullParser parser, int initialDepth) {
144               ExtensionElement result = null;
145
146               if (audioInfo != null) {
147                   result = audioInfo;
148               } else {
149                   String elementName = parser.getName();
150
151                   // Try to get an Audio content info
152                   ContentInfo mi = ContentInfo.Audio.fromString(elementName);
153                   if (mi != null) {
154                       result = new JingleContentInfo.Audio(mi);
155                   }
156               }
157               return result;
158           }
159    }
160}