1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +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:
Florian Schmaus 2017-06-17 11:43:49 +02:00
parent 1448fa4632
commit 813219179f
5 changed files with 158 additions and 13 deletions

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -16,8 +16,12 @@
*/
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.Nonza;
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -189,30 +193,46 @@ public class StreamManagement {
public static class Failed implements Nonza {
public static final String ELEMENT = "failed";
private XMPPError.Condition condition;
private final XMPPError.Condition condition;
private final List<StanzaErrorTextElement> textElements;
public Failed() {
this(null, null);
}
public Failed(XMPPError.Condition condition) {
public Failed(XMPPError.Condition condition, List<StanzaErrorTextElement> textElements) {
this.condition = condition;
if (textElements == null) {
this.textElements = Collections.emptyList();
} else {
this.textElements = Collections.unmodifiableList(textElements);
}
}
public XMPPError.Condition getXMPPErrorCondition() {
return condition;
}
public List<StanzaErrorTextElement> getTextElements() {
return textElements;
}
@Override
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
if (condition != null) {
xml.rightAngleBracket();
xml.append(condition.toString());
xml.xmlnsAttribute(XMPPError.NAMESPACE);
xml.closeElement(ELEMENT);
}
else {
if (condition == null && textElements.isEmpty()) {
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;
}

View file

@ -17,7 +17,11 @@
package org.jivesoftware.smack.sm.provider;
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.sm.packet.StreamManagement.AckAnswer;
import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
@ -46,6 +50,7 @@ public class ParseStreamManagement {
ParserUtils.assertAtStartTag(parser);
String name;
XMPPError.Condition condition = null;
List<StanzaErrorTextElement> textElements = new ArrayList<>(4);
outerloop:
while (true) {
int event = parser.next();
@ -54,7 +59,14 @@ public class ParseStreamManagement {
name = parser.getName();
String namespace = parser.getNamespace();
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;
case XmlPullParser.END_TAG:
@ -66,7 +78,7 @@ public class ParseStreamManagement {
}
}
ParserUtils.assertAtEndTag(parser);
return new Failed(condition);
return new Failed(condition, textElements);
}
public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {

View file

@ -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");
* 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 java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.sm.packet.StreamManagement;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -104,6 +106,30 @@ public class ParseStreamManagementTest {
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
public void testParseResumed() throws Exception {
long handledPackets = 42;
@ -139,7 +165,6 @@ public class ParseStreamManagementTest {
assertThat(acknowledgementPacket.getHandledCount(), equalTo(handledPackets));
}
private static Properties initOutputProperties() {
Properties properties = new Properties();
properties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");