use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class AbstractXMPPConnection method parseFeatures.
protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
streamFeatures.clear();
final int initialDepth = parser.getDepth();
while (true) {
XmlPullParser.Event eventType = parser.next();
if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
XmlElement streamFeature = null;
String name = parser.getName();
String namespace = parser.getNamespace();
switch(name) {
case StartTls.ELEMENT:
streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
break;
case Mechanisms.ELEMENT:
streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
break;
case Bind.ELEMENT:
streamFeature = Bind.Feature.INSTANCE;
break;
case Session.ELEMENT:
streamFeature = PacketParserUtils.parseSessionFeature(parser);
break;
case Compress.Feature.ELEMENT:
streamFeature = PacketParserUtils.parseCompressionFeature(parser);
break;
default:
ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
if (provider != null) {
streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
}
break;
}
if (streamFeature != null) {
addStreamFeature(streamFeature);
}
} else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
break;
}
}
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class PrivacyProviderTest method getParserFromXML.
private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(xml));
return parser;
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class PrivacyProviderTest method testDeclineLists.
/**
* Check the parser with an xml with empty lists. It includes the active,
* default and special list.
* To create the xml string based from an xml file, replace:\n with: "\n + "
*/
public void testDeclineLists() {
// Make the XML to test
String xml = "" + " <iq type='result' id='getlist1' to='romeo@example.net/orchard'> " + " <query xmlns='jabber:iq:privacy'> " + " <active/> " + " <default/> " + " </query> " + " </iq> ";
try {
// Create the xml parser
XmlPullParser parser = getParserFromXML(xml);
// Create a packet from the xml
Privacy packet = (Privacy) (new PrivacyProvider()).parse(parser);
assertNotNull(packet);
assertEquals(null, packet.getDefaultName());
assertEquals(null, packet.getActiveName());
assertEquals(true, packet.isDeclineActiveList());
assertEquals(true, packet.isDeclineDefaultList());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class SmackTestCase method parseURL.
/**
* Returns true if the given URL was found and parsed without problems. The file provided
* by the URL must contain information useful for the test case configuration, such us,
* host and port of the server.
*
* @param url the url of the file to parse.
* @return true if the given URL was found and parsed without problems.
*/
private boolean parseURL(URL url) {
boolean parsedOK = false;
InputStream systemStream = null;
try {
systemStream = url.openStream();
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(systemStream, "UTF-8");
XmlPullParser.Event eventType = parser.getEventType();
do {
if (eventType == START_ELEMENT) {
if (parser.getName().equals("host")) {
host = parser.nextText();
} else if (parser.getName().equals("port")) {
port = parseIntProperty(parser, port);
} else if (parser.getName().equals("serviceName")) {
serviceName = parser.nextText();
} else if (parser.getName().equals("chat")) {
chatDomain = parser.nextText();
} else if (parser.getName().equals("muc")) {
mucDomain = parser.nextText();
} else if (parser.getName().equals("username")) {
usernamePrefix = parser.nextText();
} else if (parser.getName().equals("password")) {
samePassword = "true".equals(parser.getAttributeValue(0));
passwordPrefix = parser.nextText();
} else if (parser.getName().equals("testAnonymousLogin")) {
testAnonymousLogin = "true".equals(parser.nextText());
} else if (parser.getName().equals("accountCreationParameters")) {
int numAttributes = parser.getAttributeCount();
String key = null;
String value = null;
for (int i = 0; i < numAttributes; i++) {
key = parser.getAttributeName(i);
value = parser.getAttributeValue(i);
accountCreationParameters.put(key, value);
}
} else if (parser.getName().equals("compressionEnabled")) {
compressionEnabled = "true".equals(parser.nextText());
}
}
eventType = parser.next();
} while (eventType != END_DOCUMENT);
parsedOK = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
systemStream.close();
} catch (Exception e) {
/* Do Nothing */
}
}
return parsedOK;
}
use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.
the class XMPPBOSHConnection method connectInternal.
@SuppressWarnings("deprecation")
@Override
protected void connectInternal() throws SmackException, InterruptedException {
done = false;
notified = false;
try {
// Ensure a clean starting state
if (client != null) {
client.close();
client = null;
}
sessionID = null;
// Initialize BOSH client
BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder.create(config.getURI(), config.getXMPPServiceDomain().toString());
if (config.isProxyEnabled()) {
cfgBuilder.setProxy(config.getProxyAddress(), config.getProxyPort());
}
cfgBuilder.setCompressionEnabled(config.isCompressionEnabled());
for (Map.Entry<String, String> h : config.getHttpHeaders().entrySet()) {
cfgBuilder.addHttpHeader(h.getKey(), h.getValue());
}
client = BOSHClient.create(cfgBuilder.build());
client.addBOSHClientConnListener(new BOSHConnectionListener());
client.addBOSHClientResponseListener(new BOSHPacketReader());
// Initialize the debugger
if (debugger != null) {
initDebugger();
}
// Send the session creation request
client.send(ComposableBody.builder().setNamespaceDefinition("xmpp", XMPP_BOSH_NS).setAttribute(BodyQName.createWithPrefix(XMPP_BOSH_NS, "version", "xmpp"), "1.0").build());
} catch (Exception e) {
throw new GenericConnectionException(e);
}
// Wait for the response from the server
synchronized (this) {
if (!connected) {
final long deadline = System.currentTimeMillis() + getReplyTimeout();
while (!notified) {
final long now = System.currentTimeMillis();
if (now >= deadline)
break;
wait(deadline - now);
}
}
}
// If there is no feedback, throw an remote server timeout error
if (!connected && !done) {
done = true;
String errorMessage = "Timeout reached for the connection to " + getHost() + ":" + getPort() + ".";
throw new SmackException.SmackMessageException(errorMessage);
}
try {
XmlPullParser parser = PacketParserUtils.getParserFor("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'/>");
onStreamOpen(parser);
} catch (XmlPullParserException | IOException e) {
throw new AssertionError("Failed to setup stream environment", e);
}
}
Aggregations