use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.
the class PacketParserUtils method parseError.
/**
* Parses error sub-packets.
*
* @param parser the XML parser.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an error sub-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 StanzaError parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
final int initialDepth = parser.getDepth();
Map<String, String> descriptiveTexts = null;
XmlEnvironment stanzaErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
List<XmlElement> extensions = new ArrayList<>();
StanzaError.Builder builder = StanzaError.getBuilder();
// Parse the error header
builder.setType(StanzaError.Type.fromString(parser.getAttributeValue("", "type")));
builder.setErrorGenerator(parser.getAttributeValue("", "by"));
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
switch(namespace) {
case StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE:
switch(name) {
case Stanza.TEXT:
descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
break;
default:
builder.setCondition(StanzaError.Condition.fromString(name));
String conditionText = parser.nextText();
if (!conditionText.isEmpty()) {
builder.setConditionText(conditionText);
}
break;
}
break;
default:
PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, stanzaErrorXmlEnvironment);
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
builder.setExtensions(extensions).setDescriptiveTexts(descriptiveTexts);
return builder.build();
}
use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.
the class PacketParserUtils method parseIQ.
/**
* Parses an IQ packet.
*
* @param parser the XML parser, positioned at the start of an IQ packet.
* @param outerXmlEnvironment the outer XML environment (optional).
* @return an IQ object.
* @throws XmlPullParserException if an error in the XML parser occurred.
* @throws XmppStringprepException if the provided string is invalid.
* @throws IOException if an I/O error occurred.
* @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
*/
public static IQ parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, XmppStringprepException, IOException, SmackParsingException {
ParserUtils.assertAtStartTag(parser);
final int initialDepth = parser.getDepth();
XmlEnvironment iqXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
IQ iqPacket = null;
StanzaError error = null;
IqData iqData = parseIqData(parser);
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String elementName = parser.getName();
String namespace = parser.getNamespace();
switch(elementName) {
case "error":
error = PacketParserUtils.parseError(parser, iqXmlEnvironment);
break;
// this element name and namespace.
default:
IqProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
if (provider != null) {
iqPacket = provider.parse(parser, iqData, outerXmlEnvironment);
} else // Note that if we reach this code, it is guaranteed that the result IQ contained a child element
// (RFC 6120 ยง 8.2.3 6) because otherwise we would have reached the END_ELEMENT first.
{
// No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
// so that the content of the IQ can be examined later on
iqPacket = new UnparsedIQ(elementName, namespace, parseElement(parser));
}
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
// Decide what to do when an IQ packet was not understood
if (iqPacket == null) {
switch(iqData.getType()) {
case error:
// If an IQ packet wasn't created above, create an empty error IQ packet.
iqPacket = new ErrorIQ(error);
break;
case result:
iqPacket = new EmptyResultIQ();
break;
default:
break;
}
}
// Set basic values on the iq packet.
iqPacket.setStanzaId(iqData.getStanzaId());
iqPacket.setTo(iqData.getTo());
iqPacket.setFrom(iqData.getFrom());
iqPacket.setType(iqData.getType());
iqPacket.setError(error);
return iqPacket;
}
use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.
the class JidPrepManager method requestJidPrep.
public String requestJidPrep(Jid jidPrepService, String jidToBePrepped) throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException {
JidPrepIq jidPrepRequest = new JidPrepIq(jidToBePrepped);
jidPrepRequest.setTo(jidPrepService);
JidPrepIq jidPrepResponse;
try {
jidPrepResponse = connection().sendIqRequestAndWaitForResponse(jidPrepRequest);
} catch (XMPPErrorException e) {
StanzaError stanzaError = e.getStanzaError();
if (stanzaError.getCondition() == StanzaError.Condition.jid_malformed) {
// or if the JID to prep was malformed. Assume the later is the case and return 'null'.
return null;
}
throw e;
}
return jidPrepResponse.getJid();
}
use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.
the class Socks5BytestreamRequest method cancelRequest.
/**
* Cancels the SOCKS5 Bytestream request by sending an error to the initiator and building a
* XMPP exception.
*
* @param streamHostsExceptions the stream hosts and their exceptions.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
* @throws CouldNotConnectToAnyProvidedSocks5Host as expected result.
* @throws NoSocks5StreamHostsProvided if no stream host was provided.
*/
private void cancelRequest(Map<StreamHost, Exception> streamHostsExceptions) throws NotConnectedException, InterruptedException, CouldNotConnectToAnyProvidedSocks5Host, NoSocks5StreamHostsProvided {
final Socks5Exception.NoSocks5StreamHostsProvided noHostsProvidedException;
final Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host couldNotConnectException;
final String errorMessage;
if (streamHostsExceptions.isEmpty()) {
noHostsProvidedException = new Socks5Exception.NoSocks5StreamHostsProvided();
couldNotConnectException = null;
errorMessage = noHostsProvidedException.getMessage();
} else {
noHostsProvidedException = null;
couldNotConnectException = Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host.construct(streamHostsExceptions);
errorMessage = couldNotConnectException.getMessage();
}
StanzaError error = StanzaError.from(StanzaError.Condition.item_not_found, errorMessage).build();
IQ errorIQ = IQ.createErrorResponse(this.bytestreamRequest, error);
this.manager.getConnection().sendStanza(errorIQ);
if (noHostsProvidedException != null) {
throw noHostsProvidedException;
} else {
throw couldNotConnectException;
}
}
use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.
the class Socks5BytestreamManager method replyRejectPacket.
/**
* Responses to the given packet's sender with an XMPP error that a SOCKS5 Bytestream is not
* accepted.
* <p>
* Specified in XEP-65 5.3.1 (Example 13)
* </p>
*
* @param packet Stanza that should be answered with a not-acceptable error
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
void replyRejectPacket(IQ packet) throws NotConnectedException, InterruptedException {
StanzaError xmppError = StanzaError.getBuilder(StanzaError.Condition.not_acceptable).build();
IQ errorIQ = IQ.createErrorResponse(packet, xmppError);
connection().sendStanza(errorIQ);
}
Aggregations