1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-09-14 20:11:48 +02:00

Bump ErrorProne to 2.5.1 and refactor Providers a bit

This also resulted in a refactoring of the Providers and parsing
Exceptions. NumberFormatException and ParseException can now be thrown
directly, the wrapping in a SmackParsingException is down at a higher
layer, i.e. in AbstractProvider.
This commit is contained in:
Florian Schmaus 2021-01-28 22:05:47 +01:00
parent 1df0763f92
commit a7b3303f3e
136 changed files with 574 additions and 551 deletions

View file

@ -12,7 +12,7 @@ buildscript {
plugins {
id 'ru.vyarus.animalsniffer' version '1.5.0'
id 'net.ltgt.errorprone' version '1.1.1'
id 'net.ltgt.errorprone' version '1.3.0'
// Use e.g. "gradle <task> taskTree" to show its dependency tree.
id 'com.dorongold.task-tree' version '1.5'
id 'com.github.kt3k.coveralls' version '2.10.2'
@ -218,6 +218,8 @@ allprojects {
// Disabled but should be re-enabled at some point
//'-Xep:InconsistentCapitalization:OFF',
'-Xep:MixedMutabilityReturnType:OFF',
// TODO: Re-enable once Smack's minimum Android SDK level is 26 or higher.
'-Xep:JavaUtilDate:OFF',
]
}
}
@ -293,7 +295,7 @@ tasks.withType(Javadoc) {
testImplementation 'org.mockito:mockito-inline:3.3.3'
testImplementation 'com.jamesmurty.utils:java-xmlbuilder:1.2'
errorprone 'com.google.errorprone:error_prone_core:2.3.4'
errorprone 'com.google.errorprone:error_prone_core:2.5.1'
errorproneJavac('com.google.errorprone:javac:9+181-r4173-1')
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2017 Florian Schmaus
* Copyright © 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,7 +19,6 @@ package org.jivesoftware.smackx.ping.android;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.logging.Logger;
@ -113,14 +112,14 @@ public final class ServerPingWithAlarmManager extends Manager {
@Override
public void onReceive(Context context, Intent intent) {
LOGGER.fine("Ping Alarm broadcast received");
Set<Entry<XMPPConnection, ServerPingWithAlarmManager>> managers;
Set<Map.Entry<XMPPConnection, ServerPingWithAlarmManager>> managers;
synchronized (ServerPingWithAlarmManager.class) {
// Make a copy to avoid ConcurrentModificationException when
// iterating directly over INSTANCES and the Set is modified
// concurrently by creating a new ServerPingWithAlarmManager.
managers = new HashSet<>(INSTANCES.entrySet());
}
for (Entry<XMPPConnection, ServerPingWithAlarmManager> entry : managers) {
for (Map.Entry<XMPPConnection, ServerPingWithAlarmManager> entry : managers) {
XMPPConnection connection = entry.getKey();
if (entry.getValue().isEnabled()) {
LOGGER.fine("Calling pingServerIfNecessary for connection "

View file

@ -249,7 +249,6 @@ public final class SASLAuthentication {
* Notification message saying that SASL authentication was successful. The next step
* would be to bind the resource.
* @param success result of the authentication.
* @throws SmackException if Smack detected an exceptional situation.
* @throws InterruptedException if the calling thread was interrupted.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws SmackSaslException if a SASL specific error occurred.

View file

@ -343,7 +343,7 @@ public final class StanzaCollector implements AutoCloseable {
*
* @param packet the stanza to process.
*/
protected void processStanza(Stanza packet) {
void processStanza(Stanza packet) {
if (packetFilter == null || packetFilter.accept(packet)) {
synchronized (this) {
if (resultQueue.size() == maxQueueSize) {

View file

@ -99,7 +99,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
private boolean featuresReceived;
protected boolean streamResumed;
private boolean streamResumed;
private GraphVertex<State> currentStateVertex;
@ -264,7 +264,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
walkStateGraph(walkStateGraphContext);
}
protected WalkStateGraphContext.Builder buildNewWalkTo(Class<? extends StateDescriptor> finalStateClass) {
private WalkStateGraphContext.Builder buildNewWalkTo(Class<? extends StateDescriptor> finalStateClass) {
return WalkStateGraphContext.builder(currentStateVertex.getElement().getStateDescriptor().getClass(), finalStateClass);
}
@ -279,7 +279,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
revertedState.resetState();
}
protected void walkStateGraph(WalkStateGraphContext walkStateGraphContext)
private void walkStateGraph(WalkStateGraphContext walkStateGraphContext)
throws XMPPException, IOException, SmackException, InterruptedException {
// Save a copy of the current state
GraphVertex<State> previousStateVertex = currentStateVertex;
@ -491,7 +491,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
}
}
protected SSLSession getSSLSession() {
private SSLSession getSSLSession() {
final XmppClientToServerTransport transport = activeTransport;
if (transport == null) {
return null;
@ -505,7 +505,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
notifyWaitingThreads();
}
protected void parseAndProcessElement(String element) {
private void parseAndProcessElement(String element) {
try {
XmlPullParser parser = PacketParserUtils.getParserFor(element);
@ -556,11 +556,11 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
}
}
protected synchronized void prepareToWaitForFeaturesReceived() {
private synchronized void prepareToWaitForFeaturesReceived() {
featuresReceived = false;
}
protected void waitForFeaturesReceived(String waitFor)
private void waitForFeaturesReceived(String waitFor)
throws InterruptedException, SmackException, XMPPException {
waitForConditionOrThrowConnectionException(() -> featuresReceived, waitFor);
}
@ -571,7 +571,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
return streamOpenAndCloseFactory.createStreamOpen(to, from, id, lang);
}
protected void newStreamOpenWaitForFeaturesSequence(String waitFor) throws InterruptedException,
private void newStreamOpenWaitForFeaturesSequence(String waitFor) throws InterruptedException,
SmackException, XMPPException {
prepareToWaitForFeaturesReceived();
@ -1028,7 +1028,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
return connectionStateMachineListeners.remove(connectionStateMachineListener);
}
protected void invokeConnectionStateMachineListener(ConnectionStateEvent connectionStateEvent) {
private void invokeConnectionStateMachineListener(ConnectionStateEvent connectionStateEvent) {
if (connectionStateMachineListeners.isEmpty()) {
return;
}
@ -1056,7 +1056,7 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
walkStateGraph(walkStateGraphContext);
}
protected Map<String, Object> getFilterStats() {
private Map<String, Object> getFilterStats() {
Collection<XmppInputOutputFilter> filters;
synchronized (this) {
if (inputOutputFilters.isEmpty() && previousInputOutputFilters != null) {

View file

@ -27,6 +27,7 @@ import org.jivesoftware.smack.util.Objects;
* @author Alexander Wenckus
*
*/
@SuppressWarnings("BadImport")
public final class IQTypeFilter extends FlexibleStanzaTypeFilter<IQ> {
public static final StanzaFilter GET = new IQTypeFilter(Type.get);

View file

@ -27,6 +27,7 @@ import org.jivesoftware.smack.packet.Message.Type;
* @see org.jivesoftware.smack.packet.Message.Type
* @author Ward Harold
*/
@SuppressWarnings("BadImport")
public final class MessageTypeFilter extends FlexibleStanzaTypeFilter<Message> {
public static final StanzaFilter NORMAL = new MessageTypeFilter(Type.normal);

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014 Florian Schmaus
* Copyright 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import org.jivesoftware.smack.util.Objects;
* A filter for Presence types. Returns true only if the stanza is an Presence stanza and it matches the type provided in the
* constructor.
*/
@SuppressWarnings("BadImport")
public final class PresenceTypeFilter extends FlexibleStanzaTypeFilter<Presence> {
public static final PresenceTypeFilter AVAILABLE = new PresenceTypeFilter(Type.available);

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2015 Florian Schmaus
* Copyright 2015-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.jivesoftware.smack.iqrequest;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
/**
* Convenience class to create IQ requests handlers.
@ -26,10 +25,10 @@ public abstract class AbstractIqRequestHandler implements IQRequestHandler {
private final String element;
private final String namespace;
private final Type type;
private final IQ.Type type;
private final Mode mode;
protected AbstractIqRequestHandler(String element, String namespace, Type type, Mode mode) {
protected AbstractIqRequestHandler(String element, String namespace, IQ.Type type, Mode mode) {
switch (type) {
case set:
case get:
@ -52,7 +51,7 @@ public abstract class AbstractIqRequestHandler implements IQRequestHandler {
}
@Override
public Type getType() {
public IQ.Type getType() {
return type;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2019-2020 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,12 +34,24 @@ public class SmackParsingException extends Exception {
super(message);
}
/**
* Deprecated, do not import.
* @deprecated do not import.
*/
@Deprecated
// TODO: Remove in Smack 4.6.
public static class SmackTextParseException extends SmackParsingException {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Deprecated, do not use.
* @param parsingException the exception.
* @deprecated do not use, simply throw ParseException.
*/
@Deprecated
public SmackTextParseException(ParseException parsingException) {
super(parsingException);
}

View file

@ -16,11 +16,15 @@
*/
package org.jivesoftware.smack.provider;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.text.ParseException;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.xml.XmlPullParserException;
public class AbstractProvider<E extends Element> {
@ -55,4 +59,51 @@ public class AbstractProvider<E extends Element> {
public final Class<E> getElementClass() {
return elementClass;
}
public static final class TextParseException extends SmackParsingException {
/**
*
*/
private static final long serialVersionUID = 1L;
private final ParseException parseException;
private TextParseException(ParseException parseException) {
super(parseException);
this.parseException = parseException;
}
public ParseException getParseException() {
return parseException;
}
}
public static final class NumberFormatParseException extends SmackParsingException {
/**
*
*/
private static final long serialVersionUID = 1L;
private NumberFormatParseException(NumberFormatException numberFormatException) {
super(numberFormatException);
}
}
protected interface WrappableParser<E> {
E parse() throws XmlPullParserException, IOException, SmackParsingException, ParseException;
}
protected static <E> E wrapExceptions(WrappableParser<E> parser)
throws XmlPullParserException, IOException, SmackParsingException {
E e;
try {
e = parser.parse();
} catch (ParseException parseException) {
throw new TextParseException(parseException);
} catch (NumberFormatException numberFormatException) {
throw new NumberFormatParseException(numberFormatException);
}
return e;
}
}

View file

@ -18,6 +18,7 @@
package org.jivesoftware.smack.provider;
import java.io.IOException;
import java.text.ParseException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IqData;
@ -50,7 +51,7 @@ public abstract class IQProvider<I extends IQ> extends IqProvider<I> {
final int initialDepth = parser.getDepth();
final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
I e = parse(parser, initialDepth, xmlEnvironment);
I e = wrapExceptions(() -> parse(parser, initialDepth, xmlEnvironment));
// XPP3 calling convention assert: Parser should be at end tag of the consumed/parsed element
ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
@ -59,11 +60,12 @@ public abstract class IQProvider<I extends IQ> extends IqProvider<I> {
@Override
public final I parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
throws XmlPullParserException, IOException, SmackParsingException, ParseException {
// Old-style IQ parsers do not need IqData.
return parse(parser, initialDepth, xmlEnvironment);
}
public abstract I parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException;
public abstract I parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException, ParseException;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2019 Florian Schmaus
* Copyright © 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -117,7 +117,7 @@ public class IntrospectionProvider{
* @param type the type of the property.
* @param value the encode String value to decode.
* @return the String value decoded into the specified type.
* @throws ClassNotFoundException
* @throws ClassNotFoundException if the provided class was not found.
*/
private static Object decode(Class<?> type, String value) throws ClassNotFoundException {
String name = type.getName();

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.jivesoftware.smack.provider;
import java.io.IOException;
import java.text.ParseException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IqData;
@ -37,10 +38,12 @@ public abstract class IqProvider<I extends IQ> extends AbstractProvider<I> {
final int initialDepth = parser.getDepth();
final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
return parse(parser, initialDepth, iqData, xmlEnvironment);
I i = wrapExceptions(() -> parse(parser, initialDepth, iqData, xmlEnvironment));
return i;
}
public abstract I parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException;
throws XmlPullParserException, IOException, SmackParsingException, ParseException;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2019 Florian Schmaus
* Copyright © 2014-2021 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,6 +18,7 @@
package org.jivesoftware.smack.provider;
import java.io.IOException;
import java.text.ParseException;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.XmlEnvironment;
@ -50,12 +51,13 @@ public abstract class Provider<E extends Element> extends AbstractProvider<E> {
final int initialDepth = parser.getDepth();
final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
E e = parse(parser, initialDepth, xmlEnvironment);
E e = wrapExceptions(() -> parse(parser, initialDepth, xmlEnvironment));
// XPP3 calling convention assert: Parser should be at end tag of the consumed/parsed element
ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
return e;
}
public abstract E parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException;
public abstract E parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException, ParseException;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014-2020 Florian Schmaus
* Copyright 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,6 @@ import java.util.Random;
import javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.SmackSaslException;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.ByteUtils;
@ -263,6 +262,7 @@ public abstract class ScramMechanism extends SASLMechanism {
}
/**
* Get the channel binding data.
*
* @return the Channel Binding data.
* @throws SmackSaslException if a SASL specific error occurred.
@ -361,7 +361,7 @@ public abstract class ScramMechanism extends SASLMechanism {
* @param key TODO javadoc me please
* @param str TODO javadoc me please
* @return the HMAC-SHA1 value of the input.
* @throws SmackException if Smack detected an exceptional situation.
* @throws SmackSaslException if Smack detected an exceptional situation.
*/
private byte[] hmac(byte[] key, byte[] str) throws SmackSaslException {
try {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2015-2020 Florian Schmaus
* Copyright © 2015-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
@ -251,7 +250,7 @@ public class MultiMap<K, V> {
public MultiMap<K, V> asUnmodifiableMultiMap() {
LinkedHashMap<K, List<V>> mapCopy = new LinkedHashMap<>(map.size());
for (Entry<K, List<V>> entry : map.entrySet()) {
for (Map.Entry<K, List<V>> entry : map.entrySet()) {
K key = entry.getKey();
List<V> values = entry.getValue();
@ -266,7 +265,7 @@ public class MultiMap<K, V> {
Map<K, List<V>> clonedMap = new LinkedHashMap<>(map.size());
// TODO: Use Map.forEach() once Smack's minimum Android API is 24 or higher.
for (Entry<K, List<V>> entry : map.entrySet()) {
for (Map.Entry<K, List<V>> entry : map.entrySet()) {
List<V> clonedList = CollectionUtil.newListWith(entry.getValue());
clonedMap.put(entry.getKey(), clonedList);
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2014-2019 Florian Schmaus
* Copyright © 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,7 +30,6 @@ import org.jivesoftware.smack.datatypes.UInt32;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.SmackParsingException.RequiredAttributeMissingException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
@ -308,30 +307,23 @@ public class ParserUtils {
return s;
}
public static Date getDateFromOptionalXep82String(String dateString) throws SmackTextParseException {
public static Date getDateFromOptionalXep82String(String dateString) throws ParseException {
if (dateString == null) {
return null;
}
return getDateFromXep82String(dateString);
}
public static Date getDateFromXep82String(String dateString) throws SmackTextParseException {
try {
return XmppDateTime.parseXEP0082Date(dateString);
} catch (ParseException e) {
throw new SmackParsingException.SmackTextParseException(e);
}
public static Date getDateFromXep82String(String dateString) throws ParseException {
return XmppDateTime.parseXEP0082Date(dateString);
}
public static Date getDateFromString(String dateString) throws SmackTextParseException {
try {
return XmppDateTime.parseDate(dateString);
} catch (ParseException e) {
throw new SmackParsingException.SmackTextParseException(e);
}
public static Date getDateFromString(String dateString) throws ParseException {
return XmppDateTime.parseDate(dateString);
}
public static Date getDateFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException, SmackTextParseException {
public static Date getDateFromNextText(XmlPullParser parser)
throws XmlPullParserException, IOException, ParseException {
String dateString = parser.nextText();
return getDateFromString(dateString);
}

View file

@ -104,6 +104,8 @@ public class Base32 {
for (int j = 0; j < blocklen; j++)
ds.writeByte((byte) (t[j] & 0xFF));
} catch (IOException e) {
// This should not happen.
throw new AssertionError(e);
}
}

View file

@ -53,7 +53,7 @@ public class ConnectionConfigurationTest {
private static final class DummyConnectionConfiguration extends ConnectionConfiguration {
protected DummyConnectionConfiguration(Builder builder) {
DummyConnectionConfiguration(Builder builder) {
super(builder);
}

View file

@ -20,17 +20,14 @@ import static org.junit.Assert.assertEquals;
import java.util.Map;
import org.jivesoftware.smack.packet.StanzaError.Condition;
import org.jivesoftware.smack.packet.StanzaError.Type;
import org.junit.Test;
public class XMPPErrorTest {
@Test
public void testConditionHasDefaultTypeMapping() throws NoSuchFieldException, IllegalAccessException {
Map<Condition, Type> conditionToTypeMap = StanzaError.CONDITION_TO_TYPE;
Map<StanzaError.Condition, StanzaError.Type> conditionToTypeMap = StanzaError.CONDITION_TO_TYPE;
assertEquals("CONDITION_TO_TYPE map is likely out of sync with Condition enum",
Condition.values().length,
StanzaError.Condition.values().length,
conditionToTypeMap.size());
}
}

View file

@ -0,0 +1,70 @@
/**
*
* Copyright 2021 Florian Schmaus.
*
* 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.jivesoftware.smack.provider;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.text.ParseException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.SmackTestUtil;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.junit.jupiter.api.Test;
public class AbstractProviderTest {
private static final ExtensionElementProvider<ExtensionElement> NUMBER_FORMAT_THROWING_PROVIDER = new ExtensionElementProvider<ExtensionElement>() {
@Override
public ExtensionElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException, ParseException {
throw new NumberFormatException();
}
};
private static final String MESSAGE = "dummy message";
private static final int VALUE = 14768234;
private static final ExtensionElementProvider<ExtensionElement> PARSE_EXCEPTION_THROWING_PROVIDER = new ExtensionElementProvider<ExtensionElement>() {
@Override
public ExtensionElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException, ParseException {
throw new ParseException(MESSAGE, VALUE);
}
};
@Test
public void testWrapsNumberFormatException() throws XmlPullParserException, IOException {
XmlPullParser parser = SmackTestUtil.createDummyParser();
assertThrows(AbstractProvider.NumberFormatParseException.class,
() -> NUMBER_FORMAT_THROWING_PROVIDER.parse(parser));
}
@Test
public void testWrapsParseException() throws XmlPullParserException, IOException {
XmlPullParser parser = SmackTestUtil.createDummyParser();
AbstractProvider.TextParseException testParseException = assertThrows(AbstractProvider.TextParseException.class,
() -> PARSE_EXCEPTION_THROWING_PROVIDER.parse(parser));
ParseException parseException = testParseException.getParseException();
assertEquals(MESSAGE, parseException.getMessage());
assertEquals(VALUE, parseException.getErrorOffset());
}
}

View file

@ -24,7 +24,6 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
@ -58,7 +57,7 @@ public class ThreadedDummyConnection extends DummyConnection {
replyPacket.setStanzaId(packet.getStanzaId());
replyPacket.setTo(packet.getFrom());
if (replyPacket.getType() == null) {
replyPacket.setType(Type.result);
replyPacket.setType(IQ.Type.result);
}
new ProcessQueue(replyQ).start();

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.provider.Provider;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smack.xml.XmlPullParserFactory;
@ -125,6 +126,11 @@ public class SmackTestUtil {
return parser;
}
public static XmlPullParser createDummyParser() throws XmlPullParserException, IOException {
String dummyElement = "<empty-element/>";
return PacketParserUtils.getParserFor(dummyElement);
}
@SuppressWarnings("unchecked")
private static <E extends Element, P extends Provider<E>> P providerClassToProvider(Class<P> providerClass) {
P provider;

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013-2014 Georg Lukas, 2020 Florian Schmaus.
* Copyright 2013-2014 Georg Lukas, 2020-2021 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.jivesoftware.smackx.carbons.provider;
import java.io.IOException;
import java.text.ParseException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.XmlEnvironment;
@ -40,7 +41,8 @@ import org.jivesoftware.smackx.forward.provider.ForwardedProvider;
public class CarbonManagerProvider extends ExtensionElementProvider<CarbonExtension> {
@Override
public CarbonExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
public CarbonExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException, ParseException {
Direction dir = Direction.valueOf(parser.getName());
Forwarded<Message> fwd = null;

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus
* Copyright 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,7 +31,6 @@ import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StanzaError.Condition;
import org.jivesoftware.smack.packet.StanzaError.Type;
import org.jivesoftware.smack.util.RandomUtil;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
@ -89,7 +88,7 @@ public final class DnsOverXmppManager extends Manager {
response = resolver.resolve(query);
} catch (IOException exception) {
StanzaError errorBuilder = StanzaError.getBuilder()
.setType(Type.CANCEL)
.setType(StanzaError.Type.CANCEL)
.setCondition(Condition.internal_server_error)
.setDescriptiveEnText("Exception while resolving your DNS query", exception)
.build()

View file

@ -27,7 +27,6 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.WeakHashMap;
import java.util.logging.Level;
@ -437,7 +436,7 @@ public final class HttpFileUploadManager extends Manager {
urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(fileSize);
urlConnection.setRequestProperty("Content-Type", "application/octet-stream");
for (Entry<String, String> header : slot.getHeaders().entrySet()) {
for (Map.Entry<String, String> header : slot.getHeaders().entrySet()) {
urlConnection.setRequestProperty(header.getKey(), header.getValue());
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016 Florian Schmaus
* Copyright 2016-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,7 +22,6 @@ import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.iot.provisioning.IoTProvisioningManager;
@ -70,7 +69,7 @@ public abstract class IoTManager extends Manager {
protected abstract class IoTIqRequestHandler extends AbstractIqRequestHandler {
protected IoTIqRequestHandler(String element, String namespace, Type type, Mode mode) {
protected IoTIqRequestHandler(String element, String namespace, IQ.Type type, Mode mode) {
super(element, namespace, type, mode);
}

View file

@ -23,7 +23,6 @@ import java.util.Iterator;
import org.jivesoftware.smackx.iot.control.ThingControlRequest;
import org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutRequest;
import org.jivesoftware.smackx.iot.discovery.element.Tag;
import org.jivesoftware.smackx.iot.discovery.element.Tag.Type;
import org.jivesoftware.smackx.iot.element.NodeInfo;
public final class Thing {
@ -112,35 +111,35 @@ public final class Thing {
public Builder setSerialNumber(String sn) {
final String name = "SN";
Tag tag = new Tag(name, Type.str, sn);
Tag tag = new Tag(name, Tag.Type.str, sn);
metaTags.put(name, tag);
return this;
}
public Builder setKey(String key) {
final String name = "KEY";
Tag tag = new Tag(name, Type.str, key);
Tag tag = new Tag(name, Tag.Type.str, key);
metaTags.put(name, tag);
return this;
}
public Builder setManufacturer(String manufacturer) {
final String name = "MAN";
Tag tag = new Tag(name, Type.str, manufacturer);
Tag tag = new Tag(name, Tag.Type.str, manufacturer);
metaTags.put(name, tag);
return this;
}
public Builder setModel(String model) {
final String name = "MODEL";
Tag tag = new Tag(name, Type.str, model);
Tag tag = new Tag(name, Tag.Type.str, model);
metaTags.put(name, tag);
return this;
}
public Builder setVersion(String version) {
final String name = "V";
Tag tag = new Tag(name, Type.num, version);
Tag tag = new Tag(name, Tag.Type.num, version);
metaTags.put(name, tag);
return this;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright © 2016-2019 Florian Schmaus
* Copyright © 2016-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,8 +24,6 @@ import java.util.List;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
@ -45,7 +43,8 @@ public class IoTFieldsExtensionProvider extends ExtensionElementProvider<IoTFiel
private static final Logger LOGGER = Logger.getLogger(IoTFieldsExtensionProvider.class.getName());
@Override
public IoTFieldsExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException, SmackTextParseException {
public IoTFieldsExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws IOException, XmlPullParserException, ParseException {
int seqNr = ParserUtils.getIntegerAttributeOrThrow(parser, "seqnr", "IoT data request <accepted/> without sequence number");
boolean done = ParserUtils.getBooleanAttribute(parser, "done", false);
List<NodeElement> nodes = new ArrayList<>();
@ -74,7 +73,7 @@ public class IoTFieldsExtensionProvider extends ExtensionElementProvider<IoTFiel
return new IoTFieldsExtension(seqNr, done, nodes);
}
public NodeElement parseNode(XmlPullParser parser) throws XmlPullParserException, IOException, SmackTextParseException {
public NodeElement parseNode(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {
final int initialDepth = parser.getDepth();
final NodeInfo nodeInfo = NodeInfoParser.parse(parser);
List<TimestampElement> timestampElements = new ArrayList<>();
@ -103,15 +102,11 @@ public class IoTFieldsExtensionProvider extends ExtensionElementProvider<IoTFiel
return new NodeElement(nodeInfo, timestampElements);
}
public TimestampElement parseTimestampElement(XmlPullParser parser) throws XmlPullParserException, IOException, SmackTextParseException {
public TimestampElement parseTimestampElement(XmlPullParser parser)
throws XmlPullParserException, IOException, ParseException {
final int initialDepth = parser.getDepth();
final String dateString = parser.getAttributeValue(null, "value");
Date date;
try {
date = XmppDateTime.parseDate(dateString);
} catch (ParseException e) {
throw new SmackParsingException.SmackTextParseException(e);
}
Date date = XmppDateTime.parseDate(dateString);
List<IoTDataField> fields = new ArrayList<>();
outerloop: while (true) {
final XmlPullParser.Event eventType = parser.next();

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2016-2019 Florian Schmaus
* Copyright 2016-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,7 +39,6 @@ import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
@ -201,7 +200,7 @@ public final class IoTProvisioningManager extends Manager {
}, FRIEND_MESSAGE);
connection.registerIQRequestHandler(
new AbstractIqRequestHandler(ClearCache.ELEMENT, ClearCache.NAMESPACE, Type.set, Mode.async) {
new AbstractIqRequestHandler(ClearCache.ELEMENT, ClearCache.NAMESPACE, IQ.Type.set, Mode.async) {
@Override
public IQ handleIQRequest(IQ iqRequest) {
if (!isFromProvisioningService(iqRequest, true)) {

View file

@ -44,7 +44,7 @@ public class JingleFileTransferProvider
extends JingleContentDescriptionProvider<JingleFileTransfer> {
@Override
public JingleFileTransfer parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
public JingleFileTransfer parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException {
ArrayList<JingleContentDescriptionChildElement> payloads = new ArrayList<>();
JingleFileTransferChild.Builder builder = JingleFileTransferChild.getBuilder();
@ -59,11 +59,7 @@ public class JingleFileTransferProvider
elementName = parser.getName();
switch (elementName) {
case JingleFileTransferChild.ELEM_DATE:
try {
builder.setDate(XmppDateTime.parseXEP0082Date(parser.nextText()));
} catch (ParseException e) {
throw new SmackParsingException.SmackTextParseException(e);
}
break;
case JingleFileTransferChild.ELEM_DESC: