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