use of org.jivesoftware.smack.xml.XmlPullParserException in project Smack by igniterealtime.
the class PacketParserUtils method parsePresence.
/**
* Parses a presence packet.
*
* @param parser the XML parser, positioned at the start of a presence packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return a Presence packet.
* @throws IOException if an I/O error occurred.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
PresenceBuilder presence = parseCommonStanzaAttributes(stanzaId -> StanzaBuilder.buildPresence(stanzaId), parser, outerXmlEnvironment);
Presence.Type type = Presence.Type.available;
String typeString = parser.getAttributeValue("", "type");
if (typeString != null && !typeString.equals("")) {
type = Presence.Type.fromString(typeString);
}
presence.ofType(type);
// Parse sub-elements
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String elementName = parser.getName();
String namespace = parser.getNamespace();
switch(elementName) {
case "status":
presence.setStatus(parser.nextText());
break;
case "priority":
Byte priority = ParserUtils.getByteAttributeFromNextText(parser);
presence.setPriority(priority);
break;
case "show":
String modeText = parser.nextText();
if (StringUtils.isNotEmpty(modeText)) {
presence.setMode(Presence.Mode.fromString(modeText));
} else {
// Some implementations send presence stanzas with a
// '<show />' element, which is a invalid XMPP presence
// stanza according to RFC 6121 4.7.2.1
LOGGER.warning("Empty or null mode text in presence show element form " + presence + "' which is invalid according to RFC6121 4.7.2.1");
}
break;
case "error":
presence.setError(parseError(parser, presenceXmlEnvironment));
break;
default:
// failing completely here. See SMACK-390 for more information.
try {
XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
presence.addExtension(extensionElement);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
}
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
return presence.build();
}
use of org.jivesoftware.smack.xml.XmlPullParserException in project Smack by igniterealtime.
the class STUNResolver method loadSTUNServers.
/**
* Load the STUN configuration from a stream.
*
* @param stunConfigStream An InputStream with the configuration file.
* @return A list of loaded servers
*/
public ArrayList<STUNService> loadSTUNServers(java.io.InputStream stunConfigStream) {
ArrayList<STUNService> serversList = new ArrayList<>();
String serverName;
int serverPort;
try {
XmlPullParser parser = PacketParserUtils.getParserFor(stunConfigStream);
XmlPullParser.Event eventType = parser.getEventType();
do {
if (eventType == XmlPullParser.Event.START_ELEMENT) {
// Parse a STUN server definition
if (parser.getName().equals("stunServer")) {
serverName = null;
serverPort = -1;
// Parse the hostname
parser.next();
parser.next();
serverName = parser.nextText();
// Parse the port
parser.next();
parser.next();
serverPort = Integer.parseInt(parser.nextText());
// it to the list.
if (serverName != null && serverPort != -1) {
STUNService service = new STUNService(serverName, serverPort);
serversList.add(service);
}
}
}
eventType = parser.next();
} while (eventType != XmlPullParser.Event.END_DOCUMENT);
} catch (XmlPullParserException e) {
LOGGER.log(Level.SEVERE, "Exception", e);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception", e);
}
currentServer = bestSTUNServer(serversList);
return serversList;
}
use of org.jivesoftware.smack.xml.XmlPullParserException in project Smack by igniterealtime.
the class DirectoryRosterStore method readEntry.
@SuppressWarnings("DefaultCharset")
private static Item readEntry(File file) {
Reader reader;
try {
// TODO: Use Files.newBufferedReader() once Smack's minimum Android API level is 26 or higher.
reader = new FileReader(file);
} catch (FileNotFoundException e) {
LOGGER.log(Level.FINE, "Roster entry file not found", e);
return null;
}
try {
XmlPullParser parser = PacketParserUtils.getParserFor(reader);
Item item = RosterPacketProvider.parseItem(parser);
reader.close();
return item;
} catch (XmlPullParserException | IOException | IllegalArgumentException e) {
boolean deleted = file.delete();
String message = "Exception while parsing roster entry.";
if (deleted) {
message += " File was deleted.";
}
LOGGER.log(Level.SEVERE, message, e);
return null;
}
}
use of org.jivesoftware.smack.xml.XmlPullParserException in project Smack by igniterealtime.
the class ModularXmppClientToServerConnection method parseAndProcessElement.
private void parseAndProcessElement(String element) {
try {
XmlPullParser parser = PacketParserUtils.getParserFor(element);
// Skip the enclosing stream open what is guaranteed to be there.
parser.next();
XmlPullParser.Event event = parser.getEventType();
outerloop: while (true) {
switch(event) {
case START_ELEMENT:
final String name = parser.getName();
// Note that we don't handle "stream" here as it's done in the splitter.
switch(name) {
case Message.ELEMENT:
case IQ.IQ_ELEMENT:
case Presence.ELEMENT:
try {
parseAndProcessStanza(parser);
} finally {
// TODO: Here would be the following stream management code.
// clientHandledStanzasCount = SMUtils.incrementHeight(clientHandledStanzasCount);
}
break;
case "error":
StreamError streamError = PacketParserUtils.parseStreamError(parser, null);
StreamErrorException streamErrorException = new StreamErrorException(streamError);
currentXmppException = streamErrorException;
notifyWaitingThreads();
throw streamErrorException;
case "features":
parseFeatures(parser);
afterFeaturesReceived();
break;
default:
parseAndProcessNonza(parser);
break;
}
break;
case END_DOCUMENT:
break outerloop;
// fall out
default:
}
event = parser.next();
}
} catch (XmlPullParserException | IOException | InterruptedException | StreamErrorException | SmackParsingException e) {
notifyConnectionError(e);
}
}
use of org.jivesoftware.smack.xml.XmlPullParserException in project Smack by igniterealtime.
the class ModularXmppClientToServerConnectionInternal method onStreamOpen.
public final String onStreamOpen(String streamOpen) {
XmlPullParser streamOpenParser;
try {
streamOpenParser = PacketParserUtils.getParserFor(streamOpen);
} catch (XmlPullParserException | IOException e) {
// Should never happen.
throw new AssertionError(e);
}
String streamClose = onStreamOpen(streamOpenParser);
return streamClose;
}
Aggregations