mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-25 13:32:07 +01:00
Add support for <text/> elements in SM's <failed/> element
Also introduce AbstractTextElement and StanzaErrorTextElement. Fixes SMACK-760.
This commit is contained in:
parent
1448fa4632
commit
813219179f
5 changed files with 158 additions and 13 deletions
|
@ -0,0 +1,56 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright © 2017 Florian Schmaus
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
|
|
||||||
|
public abstract class AbstractTextElement implements ExtensionElement {
|
||||||
|
|
||||||
|
public static final String ELEMENT = "text";
|
||||||
|
|
||||||
|
private final String text;
|
||||||
|
private final String lang;
|
||||||
|
|
||||||
|
protected AbstractTextElement(String text, String lang) {
|
||||||
|
this.text = StringUtils.requireNotNullOrEmpty(text, "Text must not be null or empty");
|
||||||
|
this.lang = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getElementName() {
|
||||||
|
return ELEMENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public XmlStringBuilder toXML() {
|
||||||
|
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||||
|
xml.optXmlLangAttribute(lang);
|
||||||
|
xml.rightAngleBracket();
|
||||||
|
xml.escape(text);
|
||||||
|
xml.closeElement(this);
|
||||||
|
return xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final String getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final String getLang() {
|
||||||
|
return lang;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright © 2017 Florian Schmaus
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
|
public class StanzaErrorTextElement extends AbstractTextElement {
|
||||||
|
|
||||||
|
public static final String NAMESPACE = XMPPError.NAMESPACE;
|
||||||
|
|
||||||
|
public StanzaErrorTextElement(String text, String lang) {
|
||||||
|
super(text, lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNamespace() {
|
||||||
|
return NAMESPACE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,8 +16,12 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack.sm.packet;
|
package org.jivesoftware.smack.sm.packet;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.Nonza;
|
import org.jivesoftware.smack.packet.Nonza;
|
||||||
|
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
|
||||||
import org.jivesoftware.smack.packet.XMPPError;
|
import org.jivesoftware.smack.packet.XMPPError;
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
|
|
||||||
|
@ -189,30 +193,46 @@ public class StreamManagement {
|
||||||
public static class Failed implements Nonza {
|
public static class Failed implements Nonza {
|
||||||
public static final String ELEMENT = "failed";
|
public static final String ELEMENT = "failed";
|
||||||
|
|
||||||
private XMPPError.Condition condition;
|
private final XMPPError.Condition condition;
|
||||||
|
|
||||||
|
private final List<StanzaErrorTextElement> textElements;
|
||||||
|
|
||||||
|
|
||||||
public Failed() {
|
public Failed() {
|
||||||
|
this(null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Failed(XMPPError.Condition condition) {
|
public Failed(XMPPError.Condition condition, List<StanzaErrorTextElement> textElements) {
|
||||||
this.condition = condition;
|
this.condition = condition;
|
||||||
|
if (textElements == null) {
|
||||||
|
this.textElements = Collections.emptyList();
|
||||||
|
} else {
|
||||||
|
this.textElements = Collections.unmodifiableList(textElements);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public XMPPError.Condition getXMPPErrorCondition() {
|
public XMPPError.Condition getXMPPErrorCondition() {
|
||||||
return condition;
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<StanzaErrorTextElement> getTextElements() {
|
||||||
|
return textElements;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CharSequence toXML() {
|
public CharSequence toXML() {
|
||||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||||
if (condition != null) {
|
if (condition == null && textElements.isEmpty()) {
|
||||||
xml.rightAngleBracket();
|
|
||||||
xml.append(condition.toString());
|
|
||||||
xml.xmlnsAttribute(XMPPError.NAMESPACE);
|
|
||||||
xml.closeElement(ELEMENT);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
xml.closeEmptyElement();
|
xml.closeEmptyElement();
|
||||||
|
} else {
|
||||||
|
xml.rightAngleBracket();
|
||||||
|
if (condition != null) {
|
||||||
|
// TODO This should use StanzaError (formerly XMPPError) in Smack 4.3 (see SMACK-769)
|
||||||
|
xml.append(condition.toString());
|
||||||
|
xml.xmlnsAttribute(XMPPError.NAMESPACE);
|
||||||
|
}
|
||||||
|
xml.append(textElements);
|
||||||
|
xml.closeElement(ELEMENT);
|
||||||
}
|
}
|
||||||
return xml;
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,11 @@
|
||||||
package org.jivesoftware.smack.sm.provider;
|
package org.jivesoftware.smack.sm.provider;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.AbstractTextElement;
|
||||||
|
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
|
||||||
import org.jivesoftware.smack.packet.XMPPError;
|
import org.jivesoftware.smack.packet.XMPPError;
|
||||||
import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
|
import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
|
||||||
import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
|
import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
|
||||||
|
@ -46,6 +50,7 @@ public class ParseStreamManagement {
|
||||||
ParserUtils.assertAtStartTag(parser);
|
ParserUtils.assertAtStartTag(parser);
|
||||||
String name;
|
String name;
|
||||||
XMPPError.Condition condition = null;
|
XMPPError.Condition condition = null;
|
||||||
|
List<StanzaErrorTextElement> textElements = new ArrayList<>(4);
|
||||||
outerloop:
|
outerloop:
|
||||||
while (true) {
|
while (true) {
|
||||||
int event = parser.next();
|
int event = parser.next();
|
||||||
|
@ -54,7 +59,14 @@ public class ParseStreamManagement {
|
||||||
name = parser.getName();
|
name = parser.getName();
|
||||||
String namespace = parser.getNamespace();
|
String namespace = parser.getNamespace();
|
||||||
if (XMPPError.NAMESPACE.equals(namespace)) {
|
if (XMPPError.NAMESPACE.equals(namespace)) {
|
||||||
condition = XMPPError.Condition.fromString(name);
|
if (name.equals(AbstractTextElement.ELEMENT)) {
|
||||||
|
String lang = ParserUtils.getXmlLang(parser);
|
||||||
|
String text = parser.nextText();
|
||||||
|
StanzaErrorTextElement stanzaErrorTextElement = new StanzaErrorTextElement(text, lang);
|
||||||
|
textElements.add(stanzaErrorTextElement);
|
||||||
|
} else {
|
||||||
|
condition = XMPPError.Condition.fromString(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case XmlPullParser.END_TAG:
|
case XmlPullParser.END_TAG:
|
||||||
|
@ -66,7 +78,7 @@ public class ParseStreamManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParserUtils.assertAtEndTag(parser);
|
ParserUtils.assertAtEndTag(parser);
|
||||||
return new Failed(condition);
|
return new Failed(condition, textElements);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
|
public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2014 Vyacheslav Blinov
|
* Copyright 2014 Vyacheslav Blinov, 2017 Florian Schmaus
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -26,8 +26,10 @@ import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
|
||||||
import org.jivesoftware.smack.packet.XMPPError;
|
import org.jivesoftware.smack.packet.XMPPError;
|
||||||
import org.jivesoftware.smack.sm.packet.StreamManagement;
|
import org.jivesoftware.smack.sm.packet.StreamManagement;
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
@ -104,6 +106,30 @@ public class ParseStreamManagementTest {
|
||||||
assertTrue(failedPacket.getXMPPErrorCondition() == errorCondition);
|
assertTrue(failedPacket.getXMPPErrorCondition() == errorCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseFailedWithTExt() throws XmlPullParserException, IOException {
|
||||||
|
// @formatter:off
|
||||||
|
final String failedNonza = "<failed h='20' xmlns='urn:xmpp:sm:3'>"
|
||||||
|
+ "<item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
|
||||||
|
+ "<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>"
|
||||||
|
+ "Previous session timed out"
|
||||||
|
+ "</text>"
|
||||||
|
+ "</failed>";
|
||||||
|
// @formatter:on
|
||||||
|
XmlPullParser parser = PacketParserUtils.getParserFor(failedNonza);
|
||||||
|
|
||||||
|
StreamManagement.Failed failed = ParseStreamManagement.failed(parser);
|
||||||
|
|
||||||
|
assertEquals(XMPPError.Condition.item_not_found, failed.getXMPPErrorCondition());
|
||||||
|
|
||||||
|
List<StanzaErrorTextElement> textElements = failed.getTextElements();
|
||||||
|
assertEquals(1, textElements.size());
|
||||||
|
|
||||||
|
StanzaErrorTextElement textElement = textElements.get(0);
|
||||||
|
assertEquals("Previous session timed out", textElement.getText());
|
||||||
|
assertEquals("en", textElement.getLang());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseResumed() throws Exception {
|
public void testParseResumed() throws Exception {
|
||||||
long handledPackets = 42;
|
long handledPackets = 42;
|
||||||
|
@ -139,7 +165,6 @@ public class ParseStreamManagementTest {
|
||||||
assertThat(acknowledgementPacket.getHandledCount(), equalTo(handledPackets));
|
assertThat(acknowledgementPacket.getHandledCount(), equalTo(handledPackets));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static Properties initOutputProperties() {
|
private static Properties initOutputProperties() {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
properties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
|
|
Loading…
Reference in a new issue