1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-30 07:52:06 +01:00

Fix OmemoManager.getInstanceFor()

getInstanceFor(connection) and getInstanceFor(connection, deviceid)
now throw NotLoggedInExceptions. When such an exception is thrown,
the client should react by using getInstanceFor(connection, jid)
or getInstanceFor(connection, jid, deviceId).
This commit is contained in:
vanitasvitae 2017-07-11 12:45:36 +02:00
parent 9d63baf55a
commit ee66cb629c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 50 additions and 34 deletions

View file

@ -131,6 +131,10 @@ public class SmackException extends Exception {
public NotLoggedInException() { public NotLoggedInException() {
super("Client is not logged in"); super("Client is not logged in");
} }
public NotLoggedInException(String message) {
super("Client is not logged in. " + message);
}
} }
public static class AlreadyLoggedInException extends SmackException { public static class AlreadyLoggedInException extends SmackException {

View file

@ -16,6 +16,8 @@
*/ */
package org.jivesoftware.smackx.omemo; package org.jivesoftware.smackx.omemo;
import static org.junit.Assert.fail;
import java.io.File; import java.io.File;
import java.util.logging.Level; import java.util.logging.Level;
@ -65,7 +67,11 @@ public abstract class AbstractOmemoIntegrationTest extends AbstractSmackIntegrat
public void beforeTest() { public void beforeTest() {
LOGGER.log(Level.INFO, "START EXECUTION"); LOGGER.log(Level.INFO, "START EXECUTION");
OmemoIntegrationTestHelper.deletePath(storePath); OmemoIntegrationTestHelper.deletePath(storePath);
before(); try {
before();
} catch (SmackException.NotLoggedInException e) {
fail();
}
} }
@AfterClass @AfterClass
@ -75,7 +81,7 @@ public abstract class AbstractOmemoIntegrationTest extends AbstractSmackIntegrat
LOGGER.log(Level.INFO, "END EXECUTION"); LOGGER.log(Level.INFO, "END EXECUTION");
} }
public abstract void before(); public abstract void before() throws SmackException.NotLoggedInException;
public abstract void after(); public abstract void after();
} }

View file

@ -41,7 +41,7 @@ public class OmemoInitializationTest extends AbstractOmemoIntegrationTest {
private OmemoStore<?,?,?,?,?,?,?,?,?> store; private OmemoStore<?,?,?,?,?,?,?,?,?> store;
@Override @Override
public void before() { public void before() throws SmackException.NotLoggedInException {
alice = OmemoManager.getInstanceFor(conOne, 666); alice = OmemoManager.getInstanceFor(conOne, 666);
store = OmemoService.getInstance().getOmemoStoreBackend(); store = OmemoService.getInstance().getOmemoStoreBackend();
} }

View file

@ -55,7 +55,7 @@ public class OmemoKeyTransportTest extends AbstractOmemoIntegrationTest {
} }
@Override @Override
public void before() { public void before() throws SmackException.NotLoggedInException {
alice = OmemoManager.getInstanceFor(conOne, 11111); alice = OmemoManager.getInstanceFor(conOne, 11111);
bob = OmemoManager.getInstanceFor(conTwo, 222222); bob = OmemoManager.getInstanceFor(conTwo, 222222);
} }

View file

@ -60,7 +60,7 @@ public class OmemoMessageSendingTest extends AbstractOmemoIntegrationTest {
} }
@Override @Override
public void before() { public void before() throws SmackException.NotLoggedInException {
alice = OmemoManager.getInstanceFor(conOne, 123); alice = OmemoManager.getInstanceFor(conOne, 123);
bob = OmemoManager.getInstanceFor(conTwo, 345); bob = OmemoManager.getInstanceFor(conTwo, 345);
store = OmemoService.getInstance().getOmemoStoreBackend(); store = OmemoService.getInstance().getOmemoStoreBackend();

View file

@ -52,7 +52,7 @@ public class OmemoSessionRenegotiationTest extends AbstractOmemoIntegrationTest
} }
@Override @Override
public void before() { public void before() throws SmackException.NotLoggedInException {
alice = OmemoManager.getInstanceFor(conOne, 1337); alice = OmemoManager.getInstanceFor(conOne, 1337);
bob = OmemoManager.getInstanceFor(conTwo, 1009); bob = OmemoManager.getInstanceFor(conTwo, 1009);
store = OmemoService.getInstance().getOmemoStoreBackend(); store = OmemoService.getInstance().getOmemoStoreBackend();

View file

@ -47,7 +47,7 @@ public class OmemoStoreTest extends AbstractOmemoIntegrationTest {
} }
@Override @Override
public void before() { public void before() throws SmackException.NotLoggedInException {
alice = OmemoManager.getInstanceFor(conOne); alice = OmemoManager.getInstanceFor(conOne);
bob = OmemoManager.getInstanceFor(conOne); bob = OmemoManager.getInstanceFor(conOne);
} }

View file

@ -32,7 +32,6 @@ import java.util.WeakHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
@ -41,7 +40,6 @@ import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smackx.carbons.CarbonManager; import org.jivesoftware.smackx.carbons.CarbonManager;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement; import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
@ -79,8 +77,6 @@ import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.EntityFullJid; import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.FullJid; import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
/** /**
* Manager that allows sending messages encrypted with OMEMO. * Manager that allows sending messages encrypted with OMEMO.
@ -101,6 +97,7 @@ public final class OmemoManager extends Manager {
private OmemoService<?,?,?,?,?,?,?,?,?>.OmemoStanzaListener omemoStanzaListener; private OmemoService<?,?,?,?,?,?,?,?,?>.OmemoStanzaListener omemoStanzaListener;
private OmemoService<?,?,?,?,?,?,?,?,?>.OmemoCarbonCopyListener omemoCarbonCopyListener; private OmemoService<?,?,?,?,?,?,?,?,?>.OmemoCarbonCopyListener omemoCarbonCopyListener;
private final BareJid ownBareJid;
private int deviceId; private int deviceId;
/** /**
@ -108,21 +105,15 @@ public final class OmemoManager extends Manager {
* *
* @param connection connection * @param connection connection
*/ */
private OmemoManager(XMPPConnection connection, int deviceId) { private OmemoManager(XMPPConnection connection, BareJid user, int deviceId) {
super(connection); super(connection);
setConnectionListener(); setConnectionListener();
this.ownBareJid = user;
this.deviceId = deviceId; this.deviceId = deviceId;
service = OmemoService.getInstance(); service = OmemoService.getInstance();
} }
/** public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, BareJid user, Integer deviceId) {
* Get an instance of the OmemoManager for the given connection and deviceId.
*
* @param connection Connection
* @param deviceId deviceId of the Manager. If the deviceId is null, a random id will be generated.
* @return an OmemoManager
*/
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, Integer deviceId) {
WeakHashMap<Integer,OmemoManager> managersOfConnection = INSTANCES.get(connection); WeakHashMap<Integer,OmemoManager> managersOfConnection = INSTANCES.get(connection);
if (managersOfConnection == null) { if (managersOfConnection == null) {
managersOfConnection = new WeakHashMap<>(); managersOfConnection = new WeakHashMap<>();
@ -135,12 +126,31 @@ public final class OmemoManager extends Manager {
OmemoManager manager = managersOfConnection.get(deviceId); OmemoManager manager = managersOfConnection.get(deviceId);
if (manager == null) { if (manager == null) {
manager = new OmemoManager(connection, deviceId); manager = new OmemoManager(connection, user, deviceId);
managersOfConnection.put(deviceId, manager); managersOfConnection.put(deviceId, manager);
} }
return manager; return manager;
} }
/**
* Get an instance of the OmemoManager for the given connection and deviceId.
*
* @param connection Connection
* @param deviceId deviceId of the Manager. If the deviceId is null, a random id will be generated.
* @return an OmemoManager
*/
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, Integer deviceId) throws SmackException.NotLoggedInException {
BareJid user;
if (connection.getUser() != null) {
user = connection.getUser().asBareJid();
} else {
throw new SmackException.NotLoggedInException("If you want to get an instance of the OmemoManager before " +
"login, you must use OmemoManager.getInstanceFor(connection, barejid, deviceId).");
}
return getInstanceFor(connection, user, deviceId);
}
/** /**
* Get an instance of the OmemoManager for the given connection. * Get an instance of the OmemoManager for the given connection.
* This method creates the OmemoManager for the stored defaultDeviceId of the connections user. * This method creates the OmemoManager for the stored defaultDeviceId of the connections user.
@ -149,27 +159,25 @@ public final class OmemoManager extends Manager {
* @param connection connection * @param connection connection
* @return OmemoManager * @return OmemoManager
*/ */
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection) { public synchronized static OmemoManager getInstanceFor(XMPPConnection connection) throws SmackException.NotLoggedInException {
BareJid user; BareJid user;
if (connection.getUser() != null) { if (connection.getUser() != null) {
user = connection.getUser().asBareJid(); user = connection.getUser().asBareJid();
} else { } else {
//This might be dangerous throw new SmackException.NotLoggedInException("If you want to get an instance of the OmemoManager before " +
try { "login, you must use OmemoManager.getInstanceFor(connection, barejid).");
user = JidCreate.bareFrom(((AbstractXMPPConnection) connection).getConfiguration().getUsername());
} catch (XmppStringprepException e) {
throw new AssertionError("Username is not a valid Jid. " +
"Use OmemoManager.gerInstanceFor(Connection, deviceId) instead.");
}
} }
return getInstanceFor(connection, user);
}
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, BareJid user) {
int defaultDeviceId = OmemoService.getInstance().getOmemoStoreBackend().getDefaultDeviceId(user); int defaultDeviceId = OmemoService.getInstance().getOmemoStoreBackend().getDefaultDeviceId(user);
if (defaultDeviceId < 1) { if (defaultDeviceId < 1) {
defaultDeviceId = randomDeviceId(); defaultDeviceId = randomDeviceId();
OmemoService.getInstance().getOmemoStoreBackend().setDefaultDeviceId(user, defaultDeviceId); OmemoService.getInstance().getOmemoStoreBackend().setDefaultDeviceId(user, defaultDeviceId);
} }
return getInstanceFor(connection, defaultDeviceId); return getInstanceFor(connection, user, defaultDeviceId);
} }
/** /**
@ -438,7 +446,7 @@ public final class OmemoManager extends Manager {
} }
Message chatMessage = new Message(); Message chatMessage = new Message();
chatMessage.setFrom(connection().getUser().asBareJid()); chatMessage.setFrom(getOwnJid());
chatMessage.addExtension(encrypted); chatMessage.addExtension(encrypted);
if (OmemoConfiguration.getAddOmemoHintBody()) { if (OmemoConfiguration.getAddOmemoHintBody()) {
@ -705,9 +713,7 @@ public final class OmemoManager extends Manager {
* @return bareJid * @return bareJid
*/ */
public BareJid getOwnJid() { public BareJid getOwnJid() {
EntityFullJid fullJid = connection().getUser(); return ownBareJid;
if (fullJid == null) return null;
return fullJid.asBareJid();
} }
/** /**