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).
OmemoManagerGetInstanceJid
vanitasvitae 5 years ago
parent 9d63baf55a
commit ee66cb629c
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
  1. 4
      smack-core/src/main/java/org/jivesoftware/smack/SmackException.java
  2. 10
      smack-integration-test/src/main/java/org/jivesoftware/smackx/omemo/AbstractOmemoIntegrationTest.java
  3. 2
      smack-integration-test/src/main/java/org/jivesoftware/smackx/omemo/OmemoInitializationTest.java
  4. 2
      smack-integration-test/src/main/java/org/jivesoftware/smackx/omemo/OmemoKeyTransportTest.java
  5. 2
      smack-integration-test/src/main/java/org/jivesoftware/smackx/omemo/OmemoMessageSendingTest.java
  6. 2
      smack-integration-test/src/main/java/org/jivesoftware/smackx/omemo/OmemoSessionRenegotiationTest.java
  7. 2
      smack-integration-test/src/main/java/org/jivesoftware/smackx/omemo/OmemoStoreTest.java
  8. 60
      smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java

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

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

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

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

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

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

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

@ -32,7 +32,6 @@ import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.Manager;
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.Message;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smackx.carbons.CarbonManager;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
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.EntityFullJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
/**
* Manager that allows sending messages encrypted with OMEMO.
@ -101,6 +97,7 @@ public final class OmemoManager extends Manager {
private OmemoService<?,?,?,?,?,?,?,?,?>.OmemoStanzaListener omemoStanzaListener;
private OmemoService<?,?,?,?,?,?,?,?,?>.OmemoCarbonCopyListener omemoCarbonCopyListener;
private final BareJid ownBareJid;
private int deviceId;
/**
@ -108,21 +105,15 @@ public final class OmemoManager extends Manager {
*
* @param connection connection
*/
private OmemoManager(XMPPConnection connection, int deviceId) {
private OmemoManager(XMPPConnection connection, BareJid user, int deviceId) {
super(connection);
setConnectionListener();
this.ownBareJid = user;
this.deviceId = deviceId;
service = OmemoService.getInstance();
}
/**
* 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) {
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, BareJid user, Integer deviceId) {
WeakHashMap<Integer,OmemoManager> managersOfConnection = INSTANCES.get(connection);
if (managersOfConnection == null) {
managersOfConnection = new WeakHashMap<>();
@ -135,12 +126,31 @@ public final class OmemoManager extends Manager {
OmemoManager manager = managersOfConnection.get(deviceId);
if (manager == null) {
manager = new OmemoManager(connection, deviceId);
manager = new OmemoManager(connection, user, deviceId);
managersOfConnection.put(deviceId, 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.
* 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
* @return OmemoManager
*/
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection) {
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection) throws SmackException.NotLoggedInException {
BareJid user;
if (connection.getUser() != null) {
user = connection.getUser().asBareJid();
} else {
//This might be dangerous
try {
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.");
}
throw new SmackException.NotLoggedInException("If you want to get an instance of the OmemoManager before " +
"login, you must use OmemoManager.getInstanceFor(connection, barejid).");
}
return getInstanceFor(connection, user);
}
public synchronized static OmemoManager getInstanceFor(XMPPConnection connection, BareJid user) {
int defaultDeviceId = OmemoService.getInstance().getOmemoStoreBackend().getDefaultDeviceId(user);
if (defaultDeviceId < 1) {
defaultDeviceId = randomDeviceId();
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();
chatMessage.setFrom(connection().getUser().asBareJid());
chatMessage.setFrom(getOwnJid());
chatMessage.addExtension(encrypted);
if (OmemoConfiguration.getAddOmemoHintBody()) {
@ -705,9 +713,7 @@ public final class OmemoManager extends Manager {
* @return bareJid
*/
public BareJid getOwnJid() {
EntityFullJid fullJid = connection().getUser();
if (fullJid == null) return null;
return fullJid.asBareJid();
return ownBareJid;
}
/**

Loading…
Cancel
Save