2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
*
|
2018-04-07 21:25:40 +02:00
|
|
|
* Copyright © 2014-2018 Florian Schmaus
|
2014-09-11 09:49:16 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-01-18 11:03:03 +01:00
|
|
|
package org.jivesoftware.smack.sm.provider;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2017-06-17 11:43:49 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
2017-06-17 11:43:49 +02:00
|
|
|
import org.jivesoftware.smack.packet.AbstractTextElement;
|
2018-04-07 21:25:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError;
|
2017-06-17 11:43:49 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
|
2015-01-18 11:03:03 +01:00
|
|
|
import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
|
|
|
|
import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
|
|
|
|
import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled;
|
|
|
|
import org.jivesoftware.smack.sm.packet.StreamManagement.Failed;
|
|
|
|
import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.util.ParserUtils;
|
2019-05-06 22:06:13 +02:00
|
|
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
|
|
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
public class ParseStreamManagement {
|
|
|
|
|
|
|
|
public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException {
|
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false);
|
|
|
|
String id = parser.getAttributeValue("", "id");
|
|
|
|
String location = parser.getAttributeValue("", "location");
|
|
|
|
int max = ParserUtils.getIntegerAttribute(parser, "max", -1);
|
|
|
|
parser.next();
|
|
|
|
ParserUtils.assertAtEndTag(parser);
|
|
|
|
return new Enabled(id, resume, location, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException {
|
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
String name;
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError.Condition condition = null;
|
2017-06-17 11:43:49 +02:00
|
|
|
List<StanzaErrorTextElement> textElements = new ArrayList<>(4);
|
2014-09-11 09:49:16 +02:00
|
|
|
outerloop:
|
2017-05-23 16:45:04 +02:00
|
|
|
while (true) {
|
2019-05-06 22:06:13 +02:00
|
|
|
XmlPullParser.Event event = parser.next();
|
2014-09-11 09:49:16 +02:00
|
|
|
switch (event) {
|
2019-05-06 22:06:13 +02:00
|
|
|
case START_ELEMENT:
|
2014-09-11 09:49:16 +02:00
|
|
|
name = parser.getName();
|
|
|
|
String namespace = parser.getNamespace();
|
2018-06-20 15:55:48 +02:00
|
|
|
if (StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE.equals(namespace)) {
|
2017-06-17 11:43:49 +02:00
|
|
|
if (name.equals(AbstractTextElement.ELEMENT)) {
|
|
|
|
String lang = ParserUtils.getXmlLang(parser);
|
|
|
|
String text = parser.nextText();
|
|
|
|
StanzaErrorTextElement stanzaErrorTextElement = new StanzaErrorTextElement(text, lang);
|
|
|
|
textElements.add(stanzaErrorTextElement);
|
|
|
|
} else {
|
2018-04-07 21:25:40 +02:00
|
|
|
condition = StanzaError.Condition.fromString(name);
|
2017-06-17 11:43:49 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-05-06 22:06:13 +02:00
|
|
|
case END_ELEMENT:
|
2014-09-11 09:49:16 +02:00
|
|
|
name = parser.getName();
|
|
|
|
if (Failed.ELEMENT.equals(name)) {
|
|
|
|
break outerloop;
|
|
|
|
}
|
|
|
|
break;
|
2019-05-06 22:06:13 +02:00
|
|
|
default:
|
|
|
|
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
|
|
|
|
break;
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ParserUtils.assertAtEndTag(parser);
|
2017-06-17 11:43:49 +02:00
|
|
|
return new Failed(condition, textElements);
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
|
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
long h = ParserUtils.getLongAttribute(parser, "h");
|
|
|
|
String previd = parser.getAttributeValue("", "previd");
|
|
|
|
parser.next();
|
|
|
|
ParserUtils.assertAtEndTag(parser);
|
|
|
|
return new Resumed(h, previd);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException {
|
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
long h = ParserUtils.getLongAttribute(parser, "h");
|
|
|
|
parser.next();
|
|
|
|
ParserUtils.assertAtEndTag(parser);
|
|
|
|
return new AckAnswer(h);
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:22:05 +01:00
|
|
|
public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException {
|
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
parser.next();
|
|
|
|
ParserUtils.assertAtEndTag(parser);
|
|
|
|
return AckRequest.INSTANCE;
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|