001/** 002 * 003 * Copyright © 2014-2017 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.sm.provider; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.jivesoftware.smack.packet.AbstractTextElement; 024import org.jivesoftware.smack.packet.StanzaErrorTextElement; 025import org.jivesoftware.smack.packet.XMPPError; 026import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer; 027import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest; 028import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled; 029import org.jivesoftware.smack.sm.packet.StreamManagement.Failed; 030import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed; 031import org.jivesoftware.smack.util.ParserUtils; 032 033import org.xmlpull.v1.XmlPullParser; 034import org.xmlpull.v1.XmlPullParserException; 035 036public class ParseStreamManagement { 037 038 public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException { 039 ParserUtils.assertAtStartTag(parser); 040 boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false); 041 String id = parser.getAttributeValue("", "id"); 042 String location = parser.getAttributeValue("", "location"); 043 int max = ParserUtils.getIntegerAttribute(parser, "max", -1); 044 parser.next(); 045 ParserUtils.assertAtEndTag(parser); 046 return new Enabled(id, resume, location, max); 047 } 048 049 public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException { 050 ParserUtils.assertAtStartTag(parser); 051 String name; 052 XMPPError.Condition condition = null; 053 List<StanzaErrorTextElement> textElements = new ArrayList<>(4); 054 outerloop: 055 while (true) { 056 int event = parser.next(); 057 switch (event) { 058 case XmlPullParser.START_TAG: 059 name = parser.getName(); 060 String namespace = parser.getNamespace(); 061 if (XMPPError.NAMESPACE.equals(namespace)) { 062 if (name.equals(AbstractTextElement.ELEMENT)) { 063 String lang = ParserUtils.getXmlLang(parser); 064 String text = parser.nextText(); 065 StanzaErrorTextElement stanzaErrorTextElement = new StanzaErrorTextElement(text, lang); 066 textElements.add(stanzaErrorTextElement); 067 } else { 068 condition = XMPPError.Condition.fromString(name); 069 } 070 } 071 break; 072 case XmlPullParser.END_TAG: 073 name = parser.getName(); 074 if (Failed.ELEMENT.equals(name)) { 075 break outerloop; 076 } 077 break; 078 } 079 } 080 ParserUtils.assertAtEndTag(parser); 081 return new Failed(condition, textElements); 082 } 083 084 public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException { 085 ParserUtils.assertAtStartTag(parser); 086 long h = ParserUtils.getLongAttribute(parser, "h"); 087 String previd = parser.getAttributeValue("", "previd"); 088 parser.next(); 089 ParserUtils.assertAtEndTag(parser); 090 return new Resumed(h, previd); 091 } 092 093 public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException { 094 ParserUtils.assertAtStartTag(parser); 095 long h = ParserUtils.getLongAttribute(parser, "h"); 096 parser.next(); 097 ParserUtils.assertAtEndTag(parser); 098 return new AckAnswer(h); 099 } 100 101 public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException { 102 ParserUtils.assertAtStartTag(parser); 103 parser.next(); 104 ParserUtils.assertAtEndTag(parser); 105 return AckRequest.INSTANCE; 106 } 107}