Introduce NotAPubSubNodeException

Fixes SMACK-759.
This commit is contained in:
Florian Schmaus 2017-08-14 20:14:35 +02:00
parent b9ed22c732
commit 772e45da92
8 changed files with 86 additions and 80 deletions

View File

@ -39,6 +39,7 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.pubsub.EventElement;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
import org.jivesoftware.smackx.pubsub.PubSubFeature;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.filter.EventExtensionFilter;
@ -137,9 +138,10 @@ public final class PEPManager extends Manager {
* @throws InterruptedException
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotAPubSubNodeException
*/
public void publish(Item item, String node) throws NotConnectedException, InterruptedException,
NoResponseException, XMPPErrorException {
NoResponseException, XMPPErrorException, NotAPubSubNodeException {
XMPPConnection connection = connection();
PubSubManager pubSubManager = PubSubManager.getInstance(connection, connection.getUser().asEntityBareJid());
LeafNode pubSubNode = pubSubManager.getNode(node);

View File

@ -1,45 +0,0 @@
/**
*
* Copyright 2017 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.smackx.pubsub;
import org.jxmpp.jid.BareJid;
public abstract class PubSubAssertionError extends AssertionError {
/**
*
*/
private static final long serialVersionUID = 1L;
protected PubSubAssertionError(String message) {
super(message);
}
public static class DiscoInfoNodeAssertionError extends PubSubAssertionError {
/**
*
*/
private static final long serialVersionUID = 1L;
DiscoInfoNodeAssertionError(BareJid pubSubService, String nodeId) {
super("PubSub service '" + pubSubService + "' returned disco info result for node '" + nodeId
+ "', but it did not contain an Identity of type 'leaf' or 'collection' (and category 'pubsub'), which is not allowed according to XEP-60 5.3.");
}
}
}

View File

@ -18,6 +18,8 @@ package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jxmpp.jid.BareJid;
public abstract class PubSubException extends SmackException {
@ -27,6 +29,16 @@ public abstract class PubSubException extends SmackException {
*/
private static final long serialVersionUID = 1L;
private final String nodeId;
protected PubSubException(String nodeId) {
this.nodeId = nodeId;
}
public String getNodeId() {
return nodeId;
}
public static class NotALeafNodeException extends PubSubException {
/**
@ -34,21 +46,35 @@ public abstract class PubSubException extends SmackException {
*/
private static final long serialVersionUID = 1L;
private final String nodeId;
private final BareJid pubSubService;
NotALeafNodeException(String nodeId, BareJid pubSubService) {
this.nodeId = nodeId;
super(nodeId);
this.pubSubService = pubSubService;
}
public String getNodeId() {
return nodeId;
}
public BareJid getPubSubService() {
return pubSubService;
}
}
public static class NotAPubSubNodeException extends PubSubException {
/**
*
*/
private static final long serialVersionUID = 1L;
private final DiscoverInfo discoverInfo;
NotAPubSubNodeException(String nodeId, DiscoverInfo discoverInfo) {
super(nodeId);
this.discoverInfo = discoverInfo;
}
public DiscoverInfo getDiscoverInfo() {
return discoverInfo;
}
}
}

View File

@ -42,6 +42,7 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jivesoftware.smackx.pubsub.util.NodeUtils;
@ -229,8 +230,9 @@ public final class PubSubManager extends Manager {
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
* @throws NotAPubSubNodeException
*/
public <T extends Node> T getNode(String id) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException
public <T extends Node> T getNode(String id) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotAPubSubNodeException
{
Node node = nodeMap.get(id);
@ -249,10 +251,7 @@ public final class PubSubManager extends Manager {
node = new CollectionNode(this, id);
}
else {
// XEP-60 5.3 states that
// "The 'disco#info' result MUST include an identity with a category of 'pubsub' and a type of either 'leaf' or 'collection'."
// If this is not the case, then we are dealing with an PubSub implementation that doesn't follow the specification.
throw new PubSubAssertionError.DiscoInfoNodeAssertionError(pubSubService, id);
throw new PubSubException.NotAPubSubNodeException(id, infoReply);
}
nodeMap.put(id, node);
}
@ -278,6 +277,9 @@ public final class PubSubManager extends Manager {
try {
return getNode(id);
}
catch (NotAPubSubNodeException e) {
return createNode(id);
}
catch (XMPPErrorException e1) {
if (e1.getXMPPError().getCondition() == Condition.item_not_found) {
try {
@ -286,7 +288,13 @@ public final class PubSubManager extends Manager {
catch (XMPPErrorException e2) {
if (e2.getXMPPError().getCondition() == Condition.conflict) {
// The node was created in the meantime, re-try getNode(). Note that this case should be rare.
return getNode(id);
try {
return getNode(id);
}
catch (NotAPubSubNodeException e) {
// Should not happen
throw new IllegalStateException(e);
}
}
throw e2;
}
@ -313,10 +321,11 @@ public final class PubSubManager extends Manager {
* @throws NotConnectedException
* @throws InterruptedException
* @throws XMPPErrorException
* @throws NotAPubSubNodeException
* @since 4.2.1
*/
public LeafNode getLeafNode(String id) throws NotALeafNodeException, NoResponseException, NotConnectedException,
InterruptedException, XMPPErrorException {
InterruptedException, XMPPErrorException, NotAPubSubNodeException {
Node node;
try {
node = getNode(id);

View File

@ -30,6 +30,7 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.util.OmemoConstants;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
@ -52,9 +53,10 @@ public class OmemoInitializationTest extends AbstractOmemoIntegrationTest {
/**
* Tests, if the initialization is done properly.
* @throws NotAPubSubNodeException
*/
@SmackIntegrationTest
public void initializationTest() throws XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, SmackException.NotLoggedInException, CorruptedOmemoKeyException {
public void initializationTest() throws XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, SmackException.NotLoggedInException, CorruptedOmemoKeyException, NotAPubSubNodeException {
//test keys.
setUpOmemoManager(alice);
assertNotNull("IdentityKey must not be null after initialization.", store.loadOmemoIdentityKeyPair(alice));

View File

@ -34,8 +34,8 @@ import org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionExcep
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.CachedDeviceList;
import org.jivesoftware.smackx.omemo.util.OmemoConstants;
import org.jivesoftware.smackx.pubsub.PubSubAssertionError;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
import org.jivesoftware.smackx.pubsub.PubSubManager;
/**
@ -70,26 +70,26 @@ final class OmemoIntegrationTestHelper {
for (int id : deviceList.getAllDevices()) {
try {
pm.getLeafNode(OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(id)).deleteAllItems();
} catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | PubSubAssertionError.DiscoInfoNodeAssertionError e) {
} catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | NotAPubSubNodeException e) {
//Silent
}
try {
pm.deleteNode(OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(id));
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | PubSubAssertionError e) {
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
//Silent
}
}
try {
pm.getLeafNode(OmemoConstants.PEP_NODE_DEVICE_LIST).deleteAllItems();
} catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | PubSubAssertionError.DiscoInfoNodeAssertionError e) {
} catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | NotAPubSubNodeException e) {
//Silent
}
try {
pm.deleteNode(OmemoConstants.PEP_NODE_DEVICE_LIST);
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | PubSubAssertionError e) {
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
//Silent
}
}
@ -147,7 +147,7 @@ final class OmemoIntegrationTestHelper {
}
static void setUpOmemoManager(OmemoManager omemoManager) throws CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, PubSubException.NotALeafNodeException {
static void setUpOmemoManager(OmemoManager omemoManager) throws CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, PubSubException.NotALeafNodeException, NotAPubSubNodeException {
omemoManager.initialize();
OmemoBundleElement bundle = OmemoService.fetchBundle(omemoManager, omemoManager.getOwnDevice());
assertNotNull("Bundle must not be null.", bundle);

View File

@ -40,6 +40,7 @@ import org.jivesoftware.smackx.omemo.internal.CipherAndAuthTag;
import org.jivesoftware.smackx.omemo.internal.OmemoMessageInformation;
import org.jivesoftware.smackx.omemo.listener.OmemoMessageListener;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
import junit.framework.TestCase;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
@ -85,9 +86,15 @@ public class OmemoMessageSendingTest extends AbstractOmemoIntegrationTest {
* @throws UndecidedOmemoIdentityException
* @throws NoSuchAlgorithmException
* @throws CryptoFailedException
* @throws NotAPubSubNodeException
*/
@SmackIntegrationTest
public void messageSendingTest() throws CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NotLoggedInException, PubSubException.NotALeafNodeException, CannotEstablishOmemoSessionException, UndecidedOmemoIdentityException, NoSuchAlgorithmException, CryptoFailedException {
public void messageSendingTest()
throws CorruptedOmemoKeyException, InterruptedException, SmackException.NoResponseException,
SmackException.NotConnectedException, XMPPException.XMPPErrorException,
SmackException.NotLoggedInException, PubSubException.NotALeafNodeException,
CannotEstablishOmemoSessionException, UndecidedOmemoIdentityException, NoSuchAlgorithmException,
CryptoFailedException, PubSubException.NotAPubSubNodeException {
final String alicesSecret = "Hey Bob! I love you!";
final String bobsSecret = "I love you too, Alice."; //aww <3

View File

@ -82,8 +82,8 @@ import org.jivesoftware.smackx.omemo.util.OmemoMessageBuilder;
import org.jivesoftware.smackx.pep.PEPManager;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubAssertionError;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
@ -428,11 +428,11 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws XMPPException.XMPPErrorException
* @throws SmackException.NotConnectedException
* @throws SmackException.NoResponseException
* @throws PubSubAssertionError.DiscoInfoNodeAssertionError ejabberd bug: https://github.com/processone/ejabberd/issues/1717
* @throws NotAPubSubNodeException
*/
static LeafNode fetchDeviceListNode(OmemoManager omemoManager, BareJid contact)
throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException, PubSubAssertionError.DiscoInfoNodeAssertionError {
SmackException.NotConnectedException, SmackException.NoResponseException, NotAPubSubNodeException {
return PubSubManager.getInstance(omemoManager.getConnection(), contact).getLeafNode(PEP_NODE_DEVICE_LIST);
}
@ -447,9 +447,11 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws InterruptedException goes
* @throws SmackException.NoResponseException wrong
* @throws PubSubException.NotALeafNodeException when the device lists node is not a LeafNode
* @throws PubSubAssertionError.DiscoInfoNodeAssertionError ejabberd bug: https://github.com/processone/ejabberd/issues/1717
* @throws NotAPubSubNodeException
*/
static OmemoDeviceListElement fetchDeviceList(OmemoManager omemoManager, BareJid contact) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, PubSubException.NotALeafNodeException, PubSubAssertionError.DiscoInfoNodeAssertionError {
static OmemoDeviceListElement fetchDeviceList(OmemoManager omemoManager, BareJid contact)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException, PubSubException.NotALeafNodeException, NotAPubSubNodeException {
return extractDeviceListFrom(fetchDeviceListNode(omemoManager, contact));
}
@ -483,9 +485,9 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
e.getMessage());
}
catch (PubSubAssertionError.DiscoInfoNodeAssertionError bug) {
catch (PubSubException.NotAPubSubNodeException e) {
LOGGER.log(Level.WARNING, "Caught a PubSubAssertionError when fetching a deviceList node. " +
"This probably means that we're dealing with an ejabberd server and the LeafNode does not exist.");
"This probably means that we're dealing with an ejabberd server and the LeafNode does not exist.", e);
return true;
}
return false;
@ -507,9 +509,8 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
LOGGER.log(Level.WARNING, "Could not fetch device list of " + contact + ": " + e, e);
return;
}
catch (PubSubAssertionError.DiscoInfoNodeAssertionError bug) {
LOGGER.log(Level.WARNING, "Caught a PubSubAssertionError when fetching a deviceList node. " +
"This probably means that the LeafNode does not exist.");
catch (NotAPubSubNodeException e) {
LOGGER.log(Level.WARNING, "Could not fetch device list of " + contact ,e);
return;
}
@ -527,9 +528,13 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
* @throws InterruptedException goes
* @throws SmackException.NoResponseException wrong
* @throws PubSubException.NotALeafNodeException when the bundles node is not a LeafNode
* @throws NotAPubSubNodeException
*/
static OmemoBundleVAxolotlElement fetchBundle(OmemoManager omemoManager, OmemoDevice contact) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, PubSubException.NotALeafNodeException, PubSubAssertionError.DiscoInfoNodeAssertionError {
LeafNode node = PubSubManager.getInstance(omemoManager.getConnection(), contact.getJid()).getLeafNode(PEP_NODE_BUNDLE_FROM_DEVICE_ID(contact.getDeviceId()));
static OmemoBundleVAxolotlElement fetchBundle(OmemoManager omemoManager, OmemoDevice contact)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
SmackException.NoResponseException, PubSubException.NotALeafNodeException, NotAPubSubNodeException {
LeafNode node = PubSubManager.getInstance(omemoManager.getConnection(), contact.getJid()).getLeafNode(
PEP_NODE_BUNDLE_FROM_DEVICE_ID(contact.getDeviceId()));
return extractBundleFrom(node);
}
@ -666,7 +671,7 @@ public abstract class OmemoService<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey,
OmemoBundleVAxolotlElement bundle;
try {
bundle = fetchBundle(omemoManager, device);
} catch (SmackException | XMPPException.XMPPErrorException | InterruptedException | PubSubAssertionError e) {
} catch (SmackException | XMPPException.XMPPErrorException | InterruptedException e) {
throw new CannotEstablishOmemoSessionException(device, e);
}