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