1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Improve parsing exception callback.

Fixes SMACK-631.
This commit is contained in:
Florian Schmaus 2015-05-18 12:35:13 +02:00
parent 21c7007212
commit 0c8199650b
5 changed files with 31 additions and 33 deletions

View file

@ -71,7 +71,6 @@ import org.jivesoftware.smack.packet.PlainStreamElement;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
import org.jivesoftware.smack.parsing.UnparsablePacket;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.DNSUtil;
@ -956,10 +955,10 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
catch (Exception e) {
CharSequence content = PacketParserUtils.parseContentDepth(parser,
parserDepth);
UnparsablePacket message = new UnparsablePacket(content, e);
UnparseableStanza message = new UnparseableStanza(content, e);
ParsingExceptionCallback callback = getParsingExceptionCallback();
if (callback != null) {
callback.handleUnparsablePacket(message);
callback.handleUnparsableStanza(message);
}
}
ParserUtils.assertAtEndTag(parser);

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Florian Schmaus.
* Copyright 2013-2015 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,20 +15,19 @@
* limitations under the License.
*/
package org.jivesoftware.smack.parsing;
package org.jivesoftware.smack;
/**
* Representation of an unparsable packet.
* Representation of an unparsable stanza.
*
* @author Florian Schmaus
*
*/
public class UnparsablePacket {
public class UnparseableStanza {
private final CharSequence content;
private final Exception e;
public UnparsablePacket(final CharSequence content, final Exception e) {
UnparseableStanza(CharSequence content, Exception e) {
this.content = content;
this.e = e;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Florian Schmaus.
* Copyright 2013-2015 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,18 +20,19 @@ package org.jivesoftware.smack.parsing;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.UnparseableStanza;
/**
* Simple parsing exception callback that only logs the encountered parsing exception to java util logging.
*
* @author Florian Schmaus
*
*/
public class ExceptionLoggingCallback extends ParsingExceptionCallback {
public class ExceptionLoggingCallback implements ParsingExceptionCallback {
private static final Logger LOGGER = Logger.getLogger(ExceptionLoggingCallback.class.getName());
@Override
public void handleUnparsablePacket(UnparsablePacket unparsed) throws Exception {
LOGGER.log(Level.SEVERE, "Smack message parsing exception: ", unparsed.getParsingException());
LOGGER.severe("Unparsed content: " + unparsed.getContent());
public void handleUnparsableStanza(UnparseableStanza unparsed) throws Exception {
LOGGER.log(Level.SEVERE, "Smack message parsing exception. Content: '" + unparsed.getContent() + "'", unparsed.getParsingException());
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Florian Schmaus.
* Copyright 2013-2015 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,18 +18,19 @@
package org.jivesoftware.smack.parsing;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.UnparseableStanza;
/**
* Parsing exception callback class that simply throws the encountered parsing exception. This usually leads to an
* {@link ConnectionListener#connectionClosedOnError(Exception)} disconnect of the connection.
* {@link ConnectionListener#connectionClosedOnError(Exception)} and disconnect of the connection.
*
* @author Florian Schmaus
*
*/
public class ExceptionThrowingCallback extends ParsingExceptionCallback {
public class ExceptionThrowingCallback implements ParsingExceptionCallback {
@Override
public void handleUnparsablePacket(UnparsablePacket packetData) throws Exception {
public void handleUnparsableStanza(UnparseableStanza packetData) throws Exception {
throw packetData.getParsingException();
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013 Florian Schmaus.
* Copyright 2013-2015 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,31 +16,29 @@
*/
package org.jivesoftware.smack.parsing;
import org.jivesoftware.smack.UnparseableStanza;
/**
* Base class to receive parsing exceptions.
*
* Interface to receive parsing exceptions.
* <p>
* If this class is used as callback, then Smack will silently ignore the stanza that caused the parsing exception and
* place the parser after the faulty stanza.
*
* Subclasses may or may not override certain methods of this class. Each of these methods will receive the exception
* that caused the parsing error and an instance of an Unparsed Stanza(/Packet) type. The latter can be used to inspect the
* stanza that caused the parsing error by using the getContent() (for example {@link UnparsablePacket#getContent()})
* method.
*
* </p>
* <p>
* Smack provides 2 predefined ParsingExceptionCallback's: {@link ExceptionLoggingCallback} and {@link ExceptionThrowingCallback}.
* </p>
*
* @author Florian Schmaus
*
*/
public abstract class ParsingExceptionCallback {
public interface ParsingExceptionCallback {
/**
* Called when parsing an message stanza caused an exception.
* Called when parsing a stanza caused an exception.
*
* @param stanzaData
* the raw message stanza data that caused the exception
* @param stanzaData the raw stanza data that caused the exception
* @throws Exception
*/
public void handleUnparsablePacket(UnparsablePacket stanzaData) throws Exception {
}
public void handleUnparsableStanza(UnparseableStanza stanzaData) throws Exception;
}