Skip to content

Commit

Permalink
Convert exceptions while parsing to SmackParsingException. (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrozev authored Sep 13, 2023
1 parent c88e006 commit 4664207
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.jitsi.xmpp.extensions;

import java.io.*;

import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;
import org.jivesoftware.smack.xml.XmlPullParser.*;
Expand All @@ -26,7 +26,7 @@
* Implements an {@link IqProvider} for empty elements.
*/
public abstract class EmptyElementIqProvider<T extends IQ>
extends IqProvider<T>
extends SafeParseIqProvider<T>
{
private final String element;

Expand All @@ -49,9 +49,10 @@ protected EmptyElementIqProvider(String element, String namespace)
* @return a new <tt>IQ</tt> instance parsed from the specified IQ
* sub-document
*/
public final T parse(XmlPullParser parser, int initialDepth,
@Override
protected T doParse(XmlPullParser parser, int initialDepth,
IqData data, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
throws XmlPullParserException, IOException
{
T iq;

Expand Down
77 changes: 77 additions & 0 deletions src/main/java/org/jitsi/xmpp/extensions/SafeParseIqProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright @ 2023 - present 8x8, Inc.
*
* 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.jitsi.xmpp.extensions;

import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;

import java.io.*;

/**
* Catch all unexpected exceptions while parsing and convert them to a {@link SmackParsingException}.
*
* Implementations should override either {@link #doParse(XmlPullParser)} or
* {@link #doParse(XmlPullParser, int, IqData, XmlEnvironment)} if they need the extra parameters.
*/
public abstract class SafeParseIqProvider<I extends IQ> extends IqProvider<I>
{
@Override
public I parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
{
try
{
return doParse(parser, initialDepth, iqData, xmlEnvironment);
}
catch (XmlPullParserException | IOException | SmackParsingException e)
{
throw e;
}
catch (Exception e)
{
throw new OtherSmackParsingException(e);
}
}

/**
* Default implementation which ignores all params except {@code parser}.
* Override if the extra parameters are needed.
*/
protected I doParse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws Exception
{
return doParse(parser);
}

/**
* Override this method if the extra parameters are not needed.
*/
protected I doParse(XmlPullParser parser)
throws Exception
{
throw new SmackParsingException("Not implemented");
}

static class OtherSmackParsingException extends SmackParsingException
{
OtherSmackParsingException(Exception e)
{
super(e);
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/org/jitsi/xmpp/extensions/coin/CoinIQProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.jitsi.xmpp.extensions.*;

import org.jivesoftware.smack.packet.*;
import org.jitsi.xmpp.extensions.colibri2.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;
Expand All @@ -30,7 +30,7 @@
* @author Sebastien Vincent
*/
public class CoinIQProvider
extends IqProvider<CoinIQ>
extends SafeParseIqProvider<CoinIQ>
{
/**
* Provider for description packet extension.
Expand Down Expand Up @@ -110,10 +110,12 @@ public CoinIQProvider()
*
* @param parser XML parser
* @return <tt>CoinIQ</tt>
* @throws Exception if something goes wrong during parsing
* @throws XmlPullParserException if something goes wrong during parsing
* @throws IOException if something goes wrong during parsing
* @throws SmackParsingException if something goes wrong during parsing
*/
@Override
public CoinIQ parse(XmlPullParser parser, int initialDepth, IqData data, XmlEnvironment xmlEnvironment)
protected CoinIQ doParse(XmlPullParser parser)
throws XmlPullParserException, IOException, SmackParsingException
{
CoinIQ coinIQ = new CoinIQ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import java.io.*;
import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jitsi.xmpp.extensions.colibri2.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;

Expand All @@ -31,7 +30,7 @@
* @author Boris Grozev
*/
public class ColibriStatsIqProvider
extends IqProvider<ColibriStatsIQ>
extends SafeParseIqProvider<ColibriStatsIQ>
{
/** Initializes a new <tt>ColibriIQProvider</tt> instance. */
public ColibriStatsIqProvider()
Expand Down Expand Up @@ -64,8 +63,9 @@ public ColibriStatsIqProvider()
* @return a new <tt>IQ</tt> instance parsed from the specified IQ
* sub-document
*/
public ColibriStatsIQ parse(XmlPullParser parser, int initialDepth, IqData data, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
@Override
protected ColibriStatsIQ doParse(XmlPullParser parser)
throws XmlPullParserException, IOException
{
String namespace = parser.getNamespace();
ColibriStatsIQ iq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package org.jitsi.xmpp.extensions.colibri2;

import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;

import java.io.*;

/**
* Provider for Colibri2 conference-modify IQs.
*/
public class ConferenceModifiedIQProvider extends IqProvider<ConferenceModifiedIQ>
public class ConferenceModifiedIQProvider extends SafeParseIqProvider<ConferenceModifiedIQ>
{
@Override
public ConferenceModifiedIQ parse(
protected ConferenceModifiedIQ doParse(
XmlPullParser parser,
int initialDepth,
IqData iqData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package org.jitsi.xmpp.extensions.colibri2;

import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;

import java.io.*;

/**
* Provider for Colibri2 conference-modify IQs.
*/
public class ConferenceModifyIQProvider extends IqProvider<ConferenceModifyIQ>
public class ConferenceModifyIQProvider extends SafeParseIqProvider<ConferenceModifyIQ>
{
@Override
public ConferenceModifyIQ parse(
protected ConferenceModifyIQ doParse(
XmlPullParser parser,
int initialDepth,
IqData iqData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
package org.jitsi.xmpp.extensions.inputevt;

import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.xml.*;

import java.io.*;

/**
* Implements an <tt>IQProvider</tt> which parses incoming <tt>InputEvtIQ</tt>s.
*
* @author Sebastien Vincent
*/
public class InputEvtIQProvider
extends IqProvider<InputEvtIQ>
extends SafeParseIqProvider<InputEvtIQ>
{
/**
* Parse the Input IQ sub-document and returns the corresponding
Expand All @@ -39,13 +35,11 @@ public class InputEvtIQProvider
* @throws Exception if something goes wrong during parsing
*/
@Override
public InputEvtIQ parse(XmlPullParser parser, int initialDepth, IqData data, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
protected InputEvtIQ doParse(XmlPullParser parser)
throws Exception
{
InputEvtIQ inputEvtIQ = new InputEvtIQ();
InputEvtAction action
= InputEvtAction.parseString(
parser.getAttributeValue("", InputEvtIQ.ACTION_ATTR_NAME));
InputEvtAction action = InputEvtAction.parseString(parser.getAttributeValue("", InputEvtIQ.ACTION_ATTR_NAME));

inputEvtIQ.setAction(action);

Expand All @@ -57,21 +51,20 @@ public InputEvtIQ parse(XmlPullParser parser, int initialDepth, IqData data, Xml
{
case START_ELEMENT:
// <remote-control>
if (RemoteControlExtensionProvider.ELEMENT_REMOTE_CONTROL
.equals(parser.getName()))
if (RemoteControlExtensionProvider.ELEMENT_REMOTE_CONTROL.equals(parser.getName()))
{
RemoteControlExtensionProvider provider
= new RemoteControlExtensionProvider();
RemoteControlExtension item
= provider.parse(parser);
RemoteControlExtensionProvider provider = new RemoteControlExtensionProvider();
RemoteControlExtension item = provider.parse(parser);

inputEvtIQ.addRemoteControl(item);
}
break;

case END_ELEMENT:
if (InputEvtIQ.ELEMENT.equals(parser.getName()))
{
done = true;
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@

import org.apache.commons.lang3.StringUtils;

import org.jitsi.xmpp.extensions.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.util.*;
import org.jivesoftware.smack.xml.*;
import org.jxmpp.jid.*;
import org.jxmpp.jid.impl.*;

import java.io.*;

/**
* Parses {@link JibriIq}.
*/
public class JibriIqProvider
extends IqProvider<JibriIq>
extends SafeParseIqProvider<JibriIq>
{
/**
* {@inheritDoc}
*/
@Override
public JibriIq parse(XmlPullParser parser, int initialDepth, IqData data, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
protected JibriIq doParse(XmlPullParser parser, int initialDepth, IqData data, XmlEnvironment xmlEnvironment)
throws Exception
{
String namespace = parser.getNamespace();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,22 @@
import org.jitsi.xmpp.extensions.*;

import org.jitsi.xmpp.extensions.colibri.*;
import org.jitsi.xmpp.extensions.colibri2.*;
import org.jitsi.xmpp.extensions.condesc.*;
import org.jitsi.xmpp.extensions.jitsimeet.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.util.*;
import org.jivesoftware.smack.xml.*;
import org.jxmpp.jid.*;
import org.jxmpp.jid.impl.*;

import java.io.*;

/**
* An implementation of a Jingle IQ provider that parses incoming Jingle IQs.
*
* @author Emil Ivov
*/
public class JingleIQProvider extends IqProvider<JingleIQ>
public class JingleIQProvider extends SafeParseIqProvider<JingleIQ>
{
/**
* Creates a new instance of the <tt>JingleIQProvider</tt> and register all
Expand Down Expand Up @@ -258,8 +256,8 @@ public JingleIQProvider()
* @throws Exception if an error occurs parsing the XML.
*/
@Override
public JingleIQ parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
protected JingleIQ doParse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws Exception
{
//let's first handle the "jingle" element params.
JingleAction action = JingleAction.parseString(parser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@

import org.jitsi.xmpp.extensions.*;

import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.parsing.*;
import org.jitsi.xmpp.extensions.colibri2.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.xml.*;

import java.io.*;

/**
* Provider for the <tt>JingleInfoQueryIQ</tt>.
*
* @author Sebastien Vincent
*/
public class JingleInfoQueryIQProvider
extends IqProvider<JingleInfoQueryIQ>
extends SafeParseIqProvider<JingleInfoQueryIQ>
{
/**
* STUN packet extension provider.
Expand Down Expand Up @@ -63,8 +60,8 @@ public JingleInfoQueryIQProvider()
* @throws Exception if an error occurs parsing the XML.
*/
@Override
public JingleInfoQueryIQ parse(XmlPullParser parser, int initialDepth, IqData data, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException
protected JingleInfoQueryIQ doParse(XmlPullParser parser)
throws Exception
{
boolean done = false;
JingleInfoQueryIQ iq = new JingleInfoQueryIQ();
Expand Down
Loading

0 comments on commit 4664207

Please sign in to comment.