1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-24 21:12:05 +01:00

Merge remote-tracking branch 'my/master'

This commit is contained in:
Florian Schmaus 2016-12-07 21:20:02 +01:00
commit b558a128c3
30 changed files with 203 additions and 136 deletions

View file

@ -104,5 +104,18 @@
<property name="checkEmptyJavadoc" value="true"/> <property name="checkEmptyJavadoc" value="true"/>
<property name="checkHtml" value="false"/> <property name="checkHtml" value="false"/>
</module> </module>
<module name="ParenPad">
</module>
<module name="NoWhitespaceAfter">
<property name="tokens" value="INC
, DEC
, UNARY_MINUS
, UNARY_PLUS
, BNOT, LNOT
, DOT
, ARRAY_DECLARATOR
, INDEX_OP
"/>
</module>
</module> </module>
</module> </module>

View file

@ -248,11 +248,13 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/ */
private final ExecutorService cachedExecutorService = Executors.newCachedThreadPool( private final ExecutorService cachedExecutorService = Executors.newCachedThreadPool(
// @formatter:off // @formatter:off
// CHECKSTYLE:OFF
new SmackExecutorThreadFactory( // threadFactory new SmackExecutorThreadFactory( // threadFactory
this, this,
"Cached Executor" "Cached Executor"
) )
// @formatter:on // @formatter:on
// CHECKSTYLE:ON
); );
/** /**

View file

@ -17,9 +17,11 @@
package org.jivesoftware.smack; package org.jivesoftware.smack;
import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StreamError; import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
import org.jxmpp.jid.Jid;
/** /**
* A generic exception that is thrown when an error occurs performing an * A generic exception that is thrown when an error occurs performing an
@ -72,48 +74,28 @@ public abstract class XMPPException extends Exception {
*/ */
private static final long serialVersionUID = 212790389529249604L; private static final long serialVersionUID = 212790389529249604L;
private final XMPPError error; private final XMPPError error;
private final Stanza stanza;
/**
* Creates a new XMPPErrorException with the given builder.
*
* @param xmppErrorBuilder
* @deprecated Use {@link #XMPPErrorException(Stanza, XMPPError)} instead.
*/
@Deprecated
public XMPPErrorException(XMPPError.Builder xmppErrorBuilder) { public XMPPErrorException(XMPPError.Builder xmppErrorBuilder) {
this(xmppErrorBuilder.build()); this(null, xmppErrorBuilder.build());
} }
/** /**
* Creates a new XMPPException with the XMPPError that was the root case of the exception. * Creates a new XMPPErrorException with the XMPPError that was the root case of the exception.
* *
* @param error the root cause of the exception. * @param error the root cause of the exception.
*/ */
public XMPPErrorException(XMPPError error) { public XMPPErrorException(Stanza stanza, XMPPError error) {
super(); super();
this.error = error; this.error = error;
} this.stanza = stanza;
/**
* Creates a new XMPPException with a description of the exception, an XMPPError, and the
* Throwable that was the root cause of the exception.
*
* @param message a description of the exception.
* @param error the root cause of the exception.
* @param wrappedThrowable the root cause of the exception.
* @deprecated use {@link #XMPPException.XMPPErrorException(XMPPError)} instead.
*/
@Deprecated
public XMPPErrorException(String message, XMPPError error, Throwable wrappedThrowable) {
super(message, wrappedThrowable);
this.error = error;
}
/**
* Creates a new XMPPException with a description of the exception and the XMPPException
* that was the root cause of the exception.
*
* @param message a description of the exception.
* @param error the root cause of the exception.
* @deprecated use {@link #XMPPException.XMPPErrorException(XMPPError)} instead.
*/
@Deprecated
public XMPPErrorException(String message, XMPPError error) {
super(message);
this.error = error;
} }
/** /**
@ -128,23 +110,53 @@ public abstract class XMPPException extends Exception {
@Override @Override
public String getMessage() { public String getMessage() {
String superMessage = super.getMessage(); StringBuilder sb = new StringBuilder();
if (superMessage != null) {
return superMessage; if (stanza != null) {
Jid from = stanza.getFrom();
if (from != null) {
sb.append("XMPP error reply received from " + from + ": ");
} }
else {
return error.toString();
} }
sb.append(error);
return sb.toString();
} }
public static void ifHasErrorThenThrow(Stanza packet) throws XMPPErrorException { public static void ifHasErrorThenThrow(Stanza packet) throws XMPPErrorException {
XMPPError xmppError = packet.getError(); XMPPError xmppError = packet.getError();
if (xmppError != null) { if (xmppError != null) {
throw new XMPPErrorException(xmppError); throw new XMPPErrorException(packet, xmppError);
} }
} }
} }
public static class FailedNonzaException extends XMPPException {
/**
*
*/
private static final long serialVersionUID = 1L;
private final XMPPError.Condition condition;
private final Nonza nonza;
public FailedNonzaException(Nonza nonza, XMPPError.Condition condition) {
this.condition = condition;
this.nonza = nonza;
}
public XMPPError.Condition getCondition() {
return condition;
}
public Nonza getNonza() {
return nonza;
}
}
public static class StreamErrorException extends XMPPException { public static class StreamErrorException extends XMPPException {
/** /**
* *

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright © 2015 Florian Schmaus * Copyright © 2015-2016 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ public class UnparsedIQ extends IQ {
@Override @Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
throw new UnsupportedOperationException(); xml.escape(content);
return xml;
} }
} }

View file

@ -983,9 +983,9 @@ public class PacketParserUtils {
private static String getLanguageAttribute(XmlPullParser parser) { private static String getLanguageAttribute(XmlPullParser parser) {
// CHECKSTYLE:OFF // CHECKSTYLE:OFF
for (int i = 0; i < parser.getAttributeCount(); i++) { for (int i = 0; i < parser.getAttributeCount(); i++) {
// CHECKSTYLE:ON
String attributeName = parser.getAttributeName(i); String attributeName = parser.getAttributeName(i);
if ( "xml:lang".equals(attributeName) || if ( "xml:lang".equals(attributeName) ||
// CHECKSTYLE:ON
("lang".equals(attributeName) && ("lang".equals(attributeName) &&
"xml".equals(parser.getAttributePrefix(i)))) { "xml".equals(parser.getAttributePrefix(i)))) {
// CHECKSTYLE:OFF // CHECKSTYLE:OFF

View file

@ -169,7 +169,7 @@ public final class BlockingCommandManager extends Manager {
BlockListIQ blockListIQ = new BlockListIQ(); BlockListIQ blockListIQ = new BlockListIQ();
BlockListIQ blockListIQResult = connection().createPacketCollectorAndSend(blockListIQ).nextResultOrThrow(); BlockListIQ blockListIQResult = connection().createPacketCollectorAndSend(blockListIQ).nextResultOrThrow();
blockListCached = blockListIQResult.getJids(); blockListCached = blockListIQResult.getBlockedJidsCopy();
return Collections.unmodifiableList(blockListCached); return Collections.unmodifiableList(blockListCached);
} }

View file

@ -16,6 +16,7 @@
*/ */
package org.jivesoftware.smackx.blocking.element; package org.jivesoftware.smackx.blocking.element;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -52,9 +53,10 @@ public class BlockListIQ extends IQ {
public BlockListIQ(List<Jid> jids) { public BlockListIQ(List<Jid> jids) {
super(ELEMENT, NAMESPACE); super(ELEMENT, NAMESPACE);
if (jids == null) { if (jids == null) {
jids = Collections.emptyList(); this.jids = Collections.emptyList();
} else {
this.jids = Collections.unmodifiableList(jids);
} }
this.jids = jids;
} }
/** /**
@ -65,14 +67,23 @@ public class BlockListIQ extends IQ {
} }
/** /**
* Get the JIDs. * Get the JIDs as unmodifiable list.
* *
* @return the JIDs * @return the blocked JIDs
*/ */
public List<Jid> getJids() { public List<Jid> getBlockedJids() {
return jids; return jids;
} }
/**
* Get a copy of the blocked list JIDs. This copy is modifiable.
*
* @return the blocked JIDs
*/
public List<Jid> getBlockedJidsCopy() {
return new ArrayList<>(getBlockedJids());
}
@Override @Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
if (jids.isEmpty()) { if (jids.isEmpty()) {

View file

@ -280,7 +280,7 @@ public class Socks5BytestreamRequest implements BytestreamRequest {
XMPPError.Builder error = XMPPError.from(XMPPError.Condition.item_not_found, errorMessage); XMPPError.Builder error = XMPPError.from(XMPPError.Condition.item_not_found, errorMessage);
IQ errorIQ = IQ.createErrorResponse(this.bytestreamRequest, error); IQ errorIQ = IQ.createErrorResponse(this.bytestreamRequest, error);
this.manager.getConnection().sendStanza(errorIQ); this.manager.getConnection().sendStanza(errorIQ);
throw new XMPPErrorException(error); throw new XMPPErrorException(errorIQ, error.build());
} }
/** /**

View file

@ -615,6 +615,7 @@ public final class AdHocCommandManager extends Manager {
* @return the command instance to execute. * @return the command instance to execute.
* @throws XMPPErrorException if there is problem creating the new instance. * @throws XMPPErrorException if there is problem creating the new instance.
*/ */
@SuppressWarnings("deprecation")
private LocalCommand newInstanceOfCmd(String commandNode, String sessionID) throws XMPPErrorException private LocalCommand newInstanceOfCmd(String commandNode, String sessionID) throws XMPPErrorException
{ {
AdHocCommandInfo commandInfo = commands.get(commandNode); AdHocCommandInfo commandInfo = commands.get(commandNode);

View file

@ -95,8 +95,14 @@ public class AdHocCommandDataProvider extends IQProvider<AdHocCommandData> {
adHocCommandData.setForm(dataFormProvider.parse(parser)); adHocCommandData.setForm(dataFormProvider.parse(parser));
} }
else if (parser.getName().equals("note")) { else if (parser.getName().equals("note")) {
AdHocCommandNote.Type type = AdHocCommandNote.Type.valueOf( String typeString = parser.getAttributeValue("", "type");
parser.getAttributeValue("", "type")); AdHocCommandNote.Type type;
if (typeString != null) {
type = AdHocCommandNote.Type.valueOf(typeString);
} else {
// Type is optional and 'info' if not present.
type = AdHocCommandNote.Type.info;
}
String value = parser.nextText(); String value = parser.nextText();
adHocCommandData.addNote(new AdHocCommandNote(type, value)); adHocCommandData.addNote(new AdHocCommandNote(type, value));
} }

View file

@ -327,7 +327,7 @@ public final class FileTransferNegotiator extends Manager {
} }
else { else {
throw new XMPPErrorException(iqResponse.getError()); throw new XMPPErrorException(iqResponse, iqResponse.getError());
} }
} }
else { else {

View file

@ -47,13 +47,13 @@ public class GetBlockingListTest {
public void checkBlockListIQ() throws Exception { public void checkBlockListIQ() throws Exception {
IQ iq = (IQ) PacketParserUtils.parseStanza(blockListIQExample); IQ iq = (IQ) PacketParserUtils.parseStanza(blockListIQExample);
BlockListIQ blockListIQ = (BlockListIQ) iq; BlockListIQ blockListIQ = (BlockListIQ) iq;
Assert.assertEquals(2, blockListIQ.getJids().size()); Assert.assertEquals(2, blockListIQ.getBlockedJids().size());
Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockListIQ.getJids().get(0)); Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockListIQ.getBlockedJids().get(0));
Assert.assertEquals(JidCreate.from("iago@shakespeare.lit"), blockListIQ.getJids().get(1)); Assert.assertEquals(JidCreate.from("iago@shakespeare.lit"), blockListIQ.getBlockedJids().get(1));
IQ iq2 = (IQ) PacketParserUtils.parseStanza(emptyBlockListIQExample); IQ iq2 = (IQ) PacketParserUtils.parseStanza(emptyBlockListIQExample);
BlockListIQ emptyBlockListIQ = (BlockListIQ) iq2; BlockListIQ emptyBlockListIQ = (BlockListIQ) iq2;
Assert.assertEquals(0, emptyBlockListIQ.getJids().size()); Assert.assertEquals(0, emptyBlockListIQ.getBlockedJids().size());
} }
} }

View file

@ -30,7 +30,6 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.EntityFullJid; import org.jxmpp.jid.EntityFullJid;
@ -113,8 +112,7 @@ public class ConnectionUtils {
public Stanza answer(InvocationOnMock invocation) throws Throwable { public Stanza answer(InvocationOnMock invocation) throws Throwable {
Stanza packet = protocol.getResponses().poll(); Stanza packet = protocol.getResponses().poll();
if (packet == null) return packet; if (packet == null) return packet;
XMPPError xmppError = packet.getError(); XMPPErrorException.ifHasErrorThenThrow(packet);
if (xmppError != null) throw new XMPPErrorException(xmppError);
return packet; return packet;
} }
}; };

View file

@ -346,37 +346,62 @@ public class RosterPacket extends IQ {
* have a subscription to the user's presence; this is the default value, so if the * have a subscription to the user's presence; this is the default value, so if the
* subscription attribute is not included then the state is to be understood as "none". * subscription attribute is not included then the state is to be understood as "none".
*/ */
none, none('⊥'),
/** /**
* The user has a subscription to the contact's presence, but the contact does not have a * The user has a subscription to the contact's presence, but the contact does not have a
* subscription to the user's presence. * subscription to the user's presence.
*/ */
to, to('←'),
/** /**
* The contact has a subscription to the user's presence, but the user does not have a * The contact has a subscription to the user's presence, but the user does not have a
* subscription to the contact's presence. * subscription to the contact's presence.
*/ */
from, from('→'),
/** /**
* The user and the contact have subscriptions to each other's presence (also called a * The user and the contact have subscriptions to each other's presence (also called a
* "mutual subscription"). * "mutual subscription").
*/ */
both, both('↔'),
/** /**
* The user wishes to stop receiving presence updates from the subscriber. * The user wishes to stop receiving presence updates from the subscriber.
*/ */
remove, remove('⚡'),
; ;
private static final char ME = '●';
private final String symbol;
private ItemType(char secondSymbolChar) {
StringBuilder sb = new StringBuilder(2);
sb.append(ME).append(secondSymbolChar);
symbol = sb.toString();
}
public static ItemType fromString(String string) { public static ItemType fromString(String string) {
if (StringUtils.isNullOrEmpty(string)) { if (StringUtils.isNullOrEmpty(string)) {
return none; return none;
} }
return ItemType.valueOf(string.toLowerCase(Locale.US)); return ItemType.valueOf(string.toLowerCase(Locale.US));
} }
/**
* Get a String containing symbols representing the item type. The first symbol in the
* string is a big dot, representing the local entity. The second symbol represents the
* established subscription relation and is typically an arrow. The head(s) of the arrow
* point in the direction presence messages are sent. For example, if there is only a head
* pointing to the big dot, then the local user will receive presence information from the
* remote entity.
*
* @return the symbolic representation of this item type.
*/
public String asSymbol() {
return symbol;
}
} }
} }

View file

@ -35,6 +35,7 @@ import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework.TestRunRes
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -97,8 +98,9 @@ public class SmackIntegrationTestFrameworkUnitTest {
@SmackIntegrationTest @SmackIntegrationTest
public void throwRuntimeExceptionTest() throws XMPPErrorException { public void throwRuntimeExceptionTest() throws XMPPErrorException {
throw new XMPPException.XMPPErrorException( Message message = new Message();
XMPPError.from(XMPPError.Condition.bad_request, DESCRIPTIVE_TEXT)); throw new XMPPException.XMPPErrorException(message,
XMPPError.from(XMPPError.Condition.bad_request, DESCRIPTIVE_TEXT).build());
} }
} }

View file

@ -29,6 +29,7 @@ import java.util.logging.Logger;
* @author rob@iharder.net * @author rob@iharder.net
* @version 2.2.1 * @version 2.2.1
*/ */
// CHECKSTYLE:OFF
public final class Base64 public final class Base64
{ {
private static final Logger LOGGER = Logger.getLogger(Base64.class.getName()); private static final Logger LOGGER = Logger.getLogger(Base64.class.getName());
@ -1682,4 +1683,4 @@ public final class Base64
} // end class Base64 } // end class Base64
// CHECKSTYLE:ON

View file

@ -31,10 +31,10 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.ConnectionException; import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.SmackException.SecurityRequiredByServerException; import org.jivesoftware.smack.SmackException.SecurityRequiredByServerException;
import org.jivesoftware.smack.SynchronizationPoint; import org.jivesoftware.smack.SynchronizationPoint;
import org.jivesoftware.smack.XMPPException.FailedNonzaException;
import org.jivesoftware.smack.XMPPException.StreamErrorException; import org.jivesoftware.smack.XMPPException.StreamErrorException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.compress.packet.Compressed; import org.jivesoftware.smack.compress.packet.Compressed;
import org.jivesoftware.smack.compression.XMPPInputOutputStream; import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.filter.StanzaFilter;
@ -67,7 +67,6 @@ import org.jivesoftware.smack.sm.packet.StreamManagement.StreamManagementFeature
import org.jivesoftware.smack.sm.predicates.Predicate; import org.jivesoftware.smack.sm.predicates.Predicate;
import org.jivesoftware.smack.sm.provider.ParseStreamManagement; import org.jivesoftware.smack.sm.provider.ParseStreamManagement;
import org.jivesoftware.smack.packet.Nonza; import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.proxy.ProxyInfo; import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown; import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
import org.jivesoftware.smack.util.Async; import org.jivesoftware.smack.util.Async;
@ -221,10 +220,10 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
*/ */
private String smSessionId; private String smSessionId;
private final SynchronizationPoint<XMPPException> smResumedSyncPoint = new SynchronizationPoint<XMPPException>( private final SynchronizationPoint<FailedNonzaException> smResumedSyncPoint = new SynchronizationPoint<>(
this, "stream resumed element"); this, "stream resumed element");
private final SynchronizationPoint<XMPPException> smEnabledSyncPoint = new SynchronizationPoint<XMPPException>( private final SynchronizationPoint<SmackException> smEnabledSyncPoint = new SynchronizationPoint<>(
this, "stream enabled element"); this, "stream enabled element");
/** /**
@ -1096,10 +1095,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
if (enabled.isResumeSet()) { if (enabled.isResumeSet()) {
smSessionId = enabled.getId(); smSessionId = enabled.getId();
if (StringUtils.isNullOrEmpty(smSessionId)) { if (StringUtils.isNullOrEmpty(smSessionId)) {
XMPPError.Builder builder = XMPPError.getBuilder(XMPPError.Condition.bad_request); SmackException xmppException = new SmackException("Stream Management 'enabled' element with resume attribute but without session id received");
builder.setDescriptiveEnText("Stream Management 'enabled' element with resume attribute but without session id received");
XMPPErrorException xmppException = new XMPPErrorException(
builder);
smEnabledSyncPoint.reportFailure(xmppException); smEnabledSyncPoint.reportFailure(xmppException);
throw xmppException; throw xmppException;
} }
@ -1115,8 +1111,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
break; break;
case Failed.ELEMENT: case Failed.ELEMENT:
Failed failed = ParseStreamManagement.failed(parser); Failed failed = ParseStreamManagement.failed(parser);
XMPPError.Builder xmppError = XMPPError.getBuilder(failed.getXMPPErrorCondition()); FailedNonzaException xmppException = new FailedNonzaException(failed, failed.getXMPPErrorCondition());
XMPPException xmppException = new XMPPErrorException(xmppError);
// If only XEP-198 would specify different failure elements for the SM // If only XEP-198 would specify different failure elements for the SM
// enable and SM resume failure case. But this is not the case, so we // enable and SM resume failure case. But this is not the case, so we
// need to determine if this is a 'Failed' response for either 'Enable' // need to determine if this is a 'Failed' response for either 'Enable'
@ -1128,7 +1123,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
if (!smEnabledSyncPoint.requestSent()) { if (!smEnabledSyncPoint.requestSent()) {
throw new IllegalStateException("Failed element received but SM was not previously enabled"); throw new IllegalStateException("Failed element received but SM was not previously enabled");
} }
smEnabledSyncPoint.reportFailure(xmppException); smEnabledSyncPoint.reportFailure(new SmackException(xmppException));
// Report success for last lastFeaturesReceived so that in case a // Report success for last lastFeaturesReceived so that in case a
// failed resumption, we can continue with normal resource binding. // failed resumption, we can continue with normal resource binding.
// See text of XEP-198 5. below Example 11. // See text of XEP-198 5. below Example 11.