mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-10 18:15:58 +01:00
Refactor Bind and Roster IQ parsing code into providers
This commit is contained in:
parent
0e4196ae4b
commit
1de2fc2a81
5 changed files with 155 additions and 91 deletions
|
@ -29,6 +29,11 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.compression.Java7ZlibInputOutputStream;
|
import org.jivesoftware.smack.compression.Java7ZlibInputOutputStream;
|
||||||
import org.jivesoftware.smack.initializer.SmackInitializer;
|
import org.jivesoftware.smack.initializer.SmackInitializer;
|
||||||
|
import org.jivesoftware.smack.packet.Bind;
|
||||||
|
import org.jivesoftware.smack.packet.RosterPacket;
|
||||||
|
import org.jivesoftware.smack.provider.BindIQProvider;
|
||||||
|
import org.jivesoftware.smack.provider.ProviderManager;
|
||||||
|
import org.jivesoftware.smack.provider.RosterPacketProvider;
|
||||||
import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism;
|
import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism;
|
||||||
import org.jivesoftware.smack.util.FileUtils;
|
import org.jivesoftware.smack.util.FileUtils;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
@ -133,6 +138,9 @@ public final class SmackInitialization {
|
||||||
|
|
||||||
SASLAuthentication.registerSASLMechanism(new SCRAMSHA1Mechanism());
|
SASLAuthentication.registerSASLMechanism(new SCRAMSHA1Mechanism());
|
||||||
|
|
||||||
|
ProviderManager.addIQProvider(RosterPacket.ELEMENT, RosterPacket.NAMESPACE, RosterPacketProvider.INSTANCE);
|
||||||
|
ProviderManager.addIQProvider(Bind.ELEMENT, Bind.NAMESPACE, new BindIQProvider());
|
||||||
|
|
||||||
SmackConfiguration.smackInitialized = true;
|
SmackConfiguration.smackInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright © 2003-2007 Jive Software, 2014 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.provider;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.packet.Bind;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
|
public class BindIQProvider extends IQProvider<Bind> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Bind parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
|
||||||
|
SmackException {
|
||||||
|
String name;
|
||||||
|
Bind bind = null;
|
||||||
|
outerloop: while (true) {
|
||||||
|
int eventType = parser.next();
|
||||||
|
switch (eventType) {
|
||||||
|
case XmlPullParser.START_TAG:
|
||||||
|
name = parser.getName();
|
||||||
|
switch (name) {
|
||||||
|
case "resource":
|
||||||
|
bind = Bind.newSet(parser.nextText());
|
||||||
|
break;
|
||||||
|
case "jid":
|
||||||
|
bind = Bind.newResult(parser.nextText());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XmlPullParser.END_TAG:
|
||||||
|
if (parser.getDepth() == initialDepth) {
|
||||||
|
break outerloop;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bind;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright © 2003-2007 Jive Software, 2014 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.provider;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.packet.RosterPacket;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
|
public class RosterPacketProvider extends IQProvider<RosterPacket> {
|
||||||
|
|
||||||
|
public static final RosterPacketProvider INSTANCE = new RosterPacketProvider();
|
||||||
|
|
||||||
|
private RosterPacketProvider() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RosterPacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
|
||||||
|
SmackException {
|
||||||
|
RosterPacket roster = new RosterPacket();
|
||||||
|
RosterPacket.Item item = null;
|
||||||
|
|
||||||
|
String version = parser.getAttributeValue("", "ver");
|
||||||
|
roster.setVersion(version);
|
||||||
|
|
||||||
|
outerloop: while (true) {
|
||||||
|
int eventType = parser.next();
|
||||||
|
switch(eventType) {
|
||||||
|
case XmlPullParser.START_TAG:
|
||||||
|
String startTag = parser.getName();
|
||||||
|
switch (startTag) {
|
||||||
|
case "item":
|
||||||
|
String jid = parser.getAttributeValue("", "jid");
|
||||||
|
String name = parser.getAttributeValue("", "name");
|
||||||
|
// Create packet.
|
||||||
|
item = new RosterPacket.Item(jid, name);
|
||||||
|
// Set status.
|
||||||
|
String ask = parser.getAttributeValue("", "ask");
|
||||||
|
RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask);
|
||||||
|
item.setItemStatus(status);
|
||||||
|
// Set type.
|
||||||
|
String subscription = parser.getAttributeValue("", "subscription");
|
||||||
|
RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription != null ? subscription : "none");
|
||||||
|
item.setItemType(type);
|
||||||
|
break;
|
||||||
|
case "group":
|
||||||
|
// TODO item!= null
|
||||||
|
final String groupName = parser.nextText();
|
||||||
|
if (groupName != null && groupName.trim().length() > 0) {
|
||||||
|
item.addGroupName(groupName);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case XmlPullParser.END_TAG:
|
||||||
|
String endTag = parser.getName();
|
||||||
|
switch(endTag) {
|
||||||
|
case "item":
|
||||||
|
roster.addRosterItem(item);
|
||||||
|
break;
|
||||||
|
case "query":
|
||||||
|
break outerloop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return roster;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -31,14 +31,12 @@ import java.util.logging.Logger;
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.compress.packet.Compress;
|
import org.jivesoftware.smack.compress.packet.Compress;
|
||||||
import org.jivesoftware.smack.packet.Bind;
|
|
||||||
import org.jivesoftware.smack.packet.DefaultPacketExtension;
|
import org.jivesoftware.smack.packet.DefaultPacketExtension;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
import org.jivesoftware.smack.packet.Presence;
|
import org.jivesoftware.smack.packet.Presence;
|
||||||
import org.jivesoftware.smack.packet.RosterPacket;
|
|
||||||
import org.jivesoftware.smack.packet.StartTls;
|
import org.jivesoftware.smack.packet.StartTls;
|
||||||
import org.jivesoftware.smack.packet.StreamError;
|
import org.jivesoftware.smack.packet.StreamError;
|
||||||
import org.jivesoftware.smack.packet.XMPPError;
|
import org.jivesoftware.smack.packet.XMPPError;
|
||||||
|
@ -620,16 +618,6 @@ public class PacketParserUtils {
|
||||||
case "error":
|
case "error":
|
||||||
error = PacketParserUtils.parseError(parser);
|
error = PacketParserUtils.parseError(parser);
|
||||||
break;
|
break;
|
||||||
case RosterPacket.ELEMENT:
|
|
||||||
if (namespace.equals(RosterPacket.NAMESPACE)) {
|
|
||||||
iqPacket = parseRoster(parser);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Bind.ELEMENT:
|
|
||||||
if (namespace.equals(Bind.NAMESPACE)) {
|
|
||||||
iqPacket = parseResourceBinding(parser);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Otherwise, see if there is a registered provider for
|
// Otherwise, see if there is a registered provider for
|
||||||
// this element name and namespace.
|
// this element name and namespace.
|
||||||
default:
|
default:
|
||||||
|
@ -703,82 +691,6 @@ public class PacketParserUtils {
|
||||||
return iqPacket;
|
return iqPacket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RosterPacket parseRoster(XmlPullParser parser) throws XmlPullParserException, IOException {
|
|
||||||
RosterPacket roster = new RosterPacket();
|
|
||||||
boolean done = false;
|
|
||||||
RosterPacket.Item item = null;
|
|
||||||
|
|
||||||
String version = parser.getAttributeValue("", "ver");
|
|
||||||
roster.setVersion(version);
|
|
||||||
|
|
||||||
while (!done) {
|
|
||||||
int eventType = parser.next();
|
|
||||||
if (eventType == XmlPullParser.START_TAG) {
|
|
||||||
if (parser.getName().equals("item")) {
|
|
||||||
String jid = parser.getAttributeValue("", "jid");
|
|
||||||
String name = parser.getAttributeValue("", "name");
|
|
||||||
// Create packet.
|
|
||||||
item = new RosterPacket.Item(jid, name);
|
|
||||||
// Set status.
|
|
||||||
String ask = parser.getAttributeValue("", "ask");
|
|
||||||
RosterPacket.ItemStatus status = RosterPacket.ItemStatus.fromString(ask);
|
|
||||||
item.setItemStatus(status);
|
|
||||||
// Set type.
|
|
||||||
String subscription = parser.getAttributeValue("", "subscription");
|
|
||||||
RosterPacket.ItemType type = RosterPacket.ItemType.valueOf(subscription != null ? subscription : "none");
|
|
||||||
item.setItemType(type);
|
|
||||||
}
|
|
||||||
else if (parser.getName().equals("group") && item!= null) {
|
|
||||||
final String groupName = parser.nextText();
|
|
||||||
if (groupName != null && groupName.trim().length() > 0) {
|
|
||||||
item.addGroupName(groupName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (eventType == XmlPullParser.END_TAG) {
|
|
||||||
if (parser.getName().equals("item")) {
|
|
||||||
roster.addRosterItem(item);
|
|
||||||
}
|
|
||||||
if (parser.getName().equals("query")) {
|
|
||||||
done = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return roster;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Bind parseResourceBinding(XmlPullParser parser) throws IOException,
|
|
||||||
XmlPullParserException {
|
|
||||||
assert (parser.getEventType() == XmlPullParser.START_TAG);
|
|
||||||
int initalDepth = parser.getDepth();
|
|
||||||
String name;
|
|
||||||
Bind bind = null;
|
|
||||||
outerloop: while (true) {
|
|
||||||
int eventType = parser.next();
|
|
||||||
switch (eventType) {
|
|
||||||
case XmlPullParser.START_TAG:
|
|
||||||
name = parser.getName();
|
|
||||||
switch (name) {
|
|
||||||
case "resource":
|
|
||||||
bind = Bind.newSet(parser.nextText());
|
|
||||||
break;
|
|
||||||
case "jid":
|
|
||||||
bind = Bind.newResult(parser.nextText());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case XmlPullParser.END_TAG:
|
|
||||||
name = parser.getName();
|
|
||||||
if (name.equals(Bind.ELEMENT) && parser.getDepth() == initalDepth) {
|
|
||||||
break outerloop;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert (parser.getEventType() == XmlPullParser.END_TAG);
|
|
||||||
return bind;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the available SASL mechanisms reported from the server.
|
* Parse the available SASL mechanisms reported from the server.
|
||||||
*
|
*
|
||||||
|
|
|
@ -17,9 +17,10 @@
|
||||||
|
|
||||||
package org.jivesoftware.smackx.xdata.provider;
|
package org.jivesoftware.smackx.xdata.provider;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.packet.RosterPacket;
|
import org.jivesoftware.smack.packet.RosterPacket;
|
||||||
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.provider.RosterPacketProvider;
|
||||||
import org.jivesoftware.smackx.xdata.FormField;
|
import org.jivesoftware.smackx.xdata.FormField;
|
||||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
@ -37,7 +38,8 @@ import java.util.List;
|
||||||
public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataForm parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
|
public DataForm parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
|
||||||
|
SmackException {
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
DataForm dataForm = new DataForm(parser.getAttributeValue("", "type"));
|
DataForm dataForm = new DataForm(parser.getAttributeValue("", "type"));
|
||||||
while (!done) {
|
while (!done) {
|
||||||
|
@ -60,7 +62,7 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
||||||
}
|
}
|
||||||
// See XEP-133 Example 32 for a corner case where the data form contains this extension.
|
// See XEP-133 Example 32 for a corner case where the data form contains this extension.
|
||||||
else if (parser.getName().equals(RosterPacket.ELEMENT) && parser.getNamespace().equals(RosterPacket.NAMESPACE)) {
|
else if (parser.getName().equals(RosterPacket.ELEMENT) && parser.getNamespace().equals(RosterPacket.NAMESPACE)) {
|
||||||
dataForm.addExtensionElement(PacketParserUtils.parseRoster(parser));
|
dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
|
||||||
}
|
}
|
||||||
} else if (eventType == XmlPullParser.END_TAG) {
|
} else if (eventType == XmlPullParser.END_TAG) {
|
||||||
if (parser.getName().equals(dataForm.getElementName())) {
|
if (parser.getName().equals(dataForm.getElementName())) {
|
||||||
|
|
Loading…
Reference in a new issue