mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-08 14:11:07 +01:00
Add support for SASL failure 'text' elements
- Also move toString() into TopLevelStreamElement. - Fix SASLFailure toXML xmlnsAttribute(NAMESPACE) - Improve SASLFailure parsing - And introduce XmlUnitUtils
This commit is contained in:
parent
59d3f55003
commit
646a4a6f90
9 changed files with 160 additions and 47 deletions
|
|
@ -372,11 +372,6 @@ public abstract class Packet extends TopLevelStreamElement {
|
|||
return DEFAULT_LANGUAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toXML().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to, from, id and 'xml:lang' attributes
|
||||
*
|
||||
|
|
|
|||
|
|
@ -230,18 +230,6 @@ public final class Presence extends Packet {
|
|||
return buf;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(type);
|
||||
if (mode != null) {
|
||||
buf.append(": ").append(mode);
|
||||
}
|
||||
if (getStatus() != null) {
|
||||
buf.append(" (").append(getStatus()).append(")");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* An enum to represent the presence type. Note that presence type is often confused
|
||||
* with presence mode. Generally, if a user is signed in to a server, they have a presence
|
||||
|
|
|
|||
|
|
@ -23,4 +23,9 @@ package org.jivesoftware.smack.packet;
|
|||
*/
|
||||
public abstract class TopLevelStreamElement implements Element {
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return toXML().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,10 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.sasl.packet;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jivesoftware.smack.packet.PlainStreamElement;
|
||||
import org.jivesoftware.smack.sasl.SASLError;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
|
@ -159,8 +163,18 @@ public class SaslStreamElements {
|
|||
|
||||
private final SASLError saslError;
|
||||
private final String saslErrorString;
|
||||
private final Map<String, String> descriptiveTexts;
|
||||
|
||||
public SASLFailure(String saslError) {
|
||||
this(saslError, null);
|
||||
}
|
||||
|
||||
public SASLFailure(String saslError, Map<String, String> descriptiveTexts) {
|
||||
if (descriptiveTexts != null) {
|
||||
this.descriptiveTexts = descriptiveTexts;
|
||||
} else {
|
||||
this.descriptiveTexts = Collections.emptyMap();
|
||||
}
|
||||
SASLError error = SASLError.fromString(saslError);
|
||||
if (error == null) {
|
||||
// RFC6120 6.5 states that unknown condition must be treat as generic authentication
|
||||
|
|
@ -189,11 +203,48 @@ public class SaslStreamElements {
|
|||
return saslErrorString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the descriptive text of this SASLFailure.
|
||||
* <p>
|
||||
* Returns the descriptive text of this SASLFailure in the system default language if possible. May return null.
|
||||
* </p>
|
||||
*
|
||||
* @return the descriptive text or null.
|
||||
*/
|
||||
public String getDescriptiveText() {
|
||||
String defaultLocale = Locale.getDefault().getLanguage();
|
||||
String descriptiveText = getDescriptiveText(defaultLocale);
|
||||
if (descriptiveText == null) {
|
||||
descriptiveText = getDescriptiveText(null);
|
||||
}
|
||||
return descriptiveText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the descriptive test of this SASLFailure.
|
||||
* <p>
|
||||
* Returns the descriptive text of this SASLFailure in the given language. May return null if not available.
|
||||
* </p>
|
||||
*
|
||||
* @param xmllang the language.
|
||||
* @return the descriptive text or null.
|
||||
*/
|
||||
public String getDescriptiveText(String xmllang) {
|
||||
return descriptiveTexts.get(xmllang);
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement(ELEMENT).xmlnsAttribute(ELEMENT).rightAngleBracket();
|
||||
xml.halfOpenElement(ELEMENT).xmlnsAttribute(NAMESPACE).rightAngleBracket();
|
||||
xml.emptyElement(saslErrorString);
|
||||
for (Map.Entry<String, String> entry : descriptiveTexts.entrySet()) {
|
||||
String xmllang = entry.getKey();
|
||||
String text = entry.getValue();
|
||||
xml.halfOpenElement("text").xmllangAttribute(xmllang).rightAngleBracket();
|
||||
xml.escape(text);
|
||||
xml.closeElement("text");
|
||||
}
|
||||
xml.closeElement(ELEMENT);
|
||||
return xml;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,11 @@ import java.io.StringReader;
|
|||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -765,26 +767,37 @@ public class PacketParserUtils {
|
|||
*
|
||||
* @param parser the XML parser.
|
||||
* @return a SASL Failure packet.
|
||||
* @throws Exception if an exception occurs while parsing the packet.
|
||||
* @throws IOException
|
||||
* @throws XmlPullParserException
|
||||
*/
|
||||
public static SASLFailure parseSASLFailure(XmlPullParser parser) throws Exception {
|
||||
public static SASLFailure parseSASLFailure(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
final int initialDepth = parser.getDepth();
|
||||
String condition = null;
|
||||
boolean done = false;
|
||||
while (!done) {
|
||||
Map<String, String> descriptiveTexts = new HashMap<String, String>();
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (!parser.getName().equals("failure")) {
|
||||
switch (eventType) {
|
||||
case XmlPullParser.START_TAG:
|
||||
String name = parser.getName();
|
||||
if (name.equals("text")) {
|
||||
String xmllang = getLanguageAttribute(parser);
|
||||
String text = parser.nextText();
|
||||
String previousValue = descriptiveTexts.put(xmllang, text);
|
||||
assert(previousValue == null);
|
||||
}
|
||||
else {
|
||||
assert(condition == null);
|
||||
condition = parser.getName();
|
||||
}
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("failure")) {
|
||||
done = true;
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
if (parser.getDepth() == initialDepth) {
|
||||
break outerloop;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new SASLFailure(condition);
|
||||
return new SASLFailure(condition, descriptiveTexts);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue