001/**
002 *
003 * Copyright 2014-2015 Florian Schmaus
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 */
017package org.jivesoftware.smack.packet;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Locale;
024import java.util.Map;
025
026import org.jivesoftware.smack.util.ExceptionUtil;
027import org.jivesoftware.smack.util.Objects;
028import org.jivesoftware.smack.util.PacketUtil;
029import org.jivesoftware.smack.util.XmlStringBuilder;
030
031public class AbstractError {
032
033    protected final String textNamespace;
034    protected final Map<String, String> descriptiveTexts;
035    protected final List<ExtensionElement> extensions;
036
037
038    protected AbstractError(Map<String, String> descriptiveTexts) {
039        this(descriptiveTexts, null);
040    }
041
042    protected AbstractError(Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
043        this(descriptiveTexts, null, extensions);
044    }
045
046    protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<ExtensionElement> extensions) {
047        if (descriptiveTexts != null) {
048            this.descriptiveTexts = descriptiveTexts;
049        } else {
050            this.descriptiveTexts = Collections.emptyMap();
051        }
052        this.textNamespace = textNamespace;
053        if (extensions != null) {
054            this.extensions = extensions;
055        } else {
056            this.extensions = Collections.emptyList();
057        }
058    }
059
060    /**
061     * Get the descriptive text of this SASLFailure.
062     * <p>
063     * Returns the descriptive text of this SASLFailure in the system default language if possible. May return null.
064     * </p>
065     *
066     * @return the descriptive text or null.
067     */
068    public String getDescriptiveText() {
069        if (descriptiveTexts.isEmpty())
070            return null;
071        // attempt to obtain the text in the user's locale, the English text, or the "" default
072        Locale l = Locale.getDefault();
073        String[] tags = new String[] {
074                l.getLanguage() + "-" + l.getCountry() + "-" + l.getVariant(),
075                l.getLanguage() + "-" + l.getCountry(),
076                l.getLanguage(),
077                "en",
078                ""
079        };
080        for (String tag : tags) {
081            String descriptiveText = getDescriptiveText(tag);
082            if (descriptiveText != null)
083                return descriptiveText;
084        }
085        return descriptiveTexts.values().iterator().next();
086    }
087
088    /**
089     * Get the descriptive test of this SASLFailure.
090     * <p>
091     * Returns the descriptive text of this SASLFailure in the given language. May return null if not available.
092     * </p>
093     *
094     * @param xmllang the language.
095     * @return the descriptive text or null.
096     */
097    public String getDescriptiveText(String xmllang) {
098        Objects.requireNonNull(xmllang, "xmllang must not be null");
099        return descriptiveTexts.get(xmllang);
100    }
101
102    /**
103     * Returns the first stanza extension that matches the specified element name and
104     * namespace, or <code>null</code> if it doesn't exist.
105     *
106     * @param elementName the XML element name of the stanza extension.
107     * @param namespace the XML element namespace of the stanza extension.
108     * @param <PE> type of the ExtensionElement.
109     * @return the extension, or <code>null</code> if it doesn't exist.
110     */
111    public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
112        return PacketUtil.extensionElementFrom(extensions, elementName, namespace);
113    }
114
115    protected void addDescriptiveTextsAndExtensions(XmlStringBuilder xml) {
116        for (Map.Entry<String, String> entry : descriptiveTexts.entrySet()) {
117            String xmllang = entry.getKey();
118            String text = entry.getValue();
119            xml.halfOpenElement("text").xmlnsAttribute(textNamespace)
120                    .optXmlLangAttribute(xmllang)
121                    .rightAngleBracket();
122            xml.escape(text);
123            xml.closeElement("text");
124        }
125        xml.append(extensions);
126    }
127
128    public abstract static class Builder<B extends Builder<B>> {
129        protected String textNamespace;
130        protected Map<String, String> descriptiveTexts;
131        protected List<ExtensionElement> extensions;
132
133        public B setDescriptiveTexts(Map<String, String> descriptiveTexts) {
134            if (descriptiveTexts == null) {
135                this.descriptiveTexts = null;
136                return getThis();
137            }
138            for (String key : descriptiveTexts.keySet()) {
139                if (key == null) {
140                    throw new IllegalArgumentException("descriptiveTexts cannot contain null key");
141                }
142            }
143            if (this.descriptiveTexts == null) {
144                this.descriptiveTexts = descriptiveTexts;
145            }
146            else {
147                this.descriptiveTexts.putAll(descriptiveTexts);
148            }
149            return getThis();
150        }
151
152        public B setDescriptiveEnText(String descriptiveEnText) {
153            if (descriptiveTexts == null) {
154                descriptiveTexts = new HashMap<>();
155            }
156            descriptiveTexts.put("en", descriptiveEnText);
157            return getThis();
158        }
159
160        public B setDescriptiveEnText(String descriptiveEnText, Exception exception) {
161            StringBuilder sb = new StringBuilder(512);
162            sb.append(descriptiveEnText)
163                .append('\n');
164
165            String stacktrace = ExceptionUtil.getStackTrace(exception);
166            sb.append(stacktrace);
167
168            return setDescriptiveEnText(sb.toString());
169        }
170
171        public B setTextNamespace(String textNamespace) {
172            this.textNamespace = textNamespace;
173            return getThis();
174        }
175
176        public B setExtensions(List<ExtensionElement> extensions) {
177            if (this.extensions == null) {
178                this.extensions = extensions;
179            }
180            else {
181                this.extensions.addAll(extensions);
182            }
183            return getThis();
184        }
185
186        public B addExtension(ExtensionElement extension) {
187            if (extensions == null) {
188                extensions = new ArrayList<>();
189            }
190            extensions.add(extension);
191            return getThis();
192        }
193
194        protected abstract B getThis();
195    }
196}