1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-13 02:59:39 +02:00

Compare commits

...

5 commits

Author SHA1 Message Date
Florian Schmaus
9fd5bc7873 [pubsub] Make Fillable(Configure|Subscribe)Form constructors package-private
Those are not meant to be used directly.
2020-05-15 11:54:49 +02:00
Florian Schmaus
fec928ffe7 [pubsub] Add missing "FillableSubscribeForm SubscribeForm.getFillableForm()" 2020-05-15 11:54:49 +02:00
Florian Schmaus
565397f632 [pubsub] Make FillableSubscribeForm implement SubscribeFormReader 2020-05-15 11:54:49 +02:00
Florian Schmaus
4cb214ef8f
Merge pull request #367 from vanitasvitae/carbonEnable
Allow for offline configuration of CarbonManager
2020-05-15 11:31:23 +02:00
ffb8729ba7 Allow for offline configuration of CarbonManager 2020-05-13 22:28:48 +02:00
4 changed files with 41 additions and 9 deletions

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2013-2014 Georg Lukas, 2017-2020 Florian Schmaus
* Copyright 2013-2014 Georg Lukas, 2017-2020 Florian Schmaus, 2020 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,6 +20,8 @@ import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.AsyncButOrdered;
import org.jivesoftware.smack.ConnectionCreationListener;
@ -71,10 +73,14 @@ import org.jxmpp.jid.EntityFullJid;
*
* @author Georg Lukas
* @author Florian Schmaus
* @author Paul Schaub
*/
public final class CarbonManager extends Manager {
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<XMPPConnection, CarbonManager>();
private static final Logger LOGGER = Logger.getLogger(CarbonManager.class.getName());
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<>();
private static boolean ENABLED_BY_DEFAULT = false;
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@ -99,11 +105,22 @@ public final class CarbonManager extends Manager {
private final Set<CarbonCopyReceivedListener> listeners = new CopyOnWriteArraySet<>();
private volatile boolean enabled_state = false;
private volatile boolean enabledByDefault = ENABLED_BY_DEFAULT;
private final StanzaListener carbonsListener;
private final AsyncButOrdered<BareJid> carbonsListenerAsyncButOrdered = new AsyncButOrdered<>();
/**
* Should Carbons be automatically be enabled once the connection is authenticated?
* Default: false
*
* @param enabledByDefault new default value
*/
public static void setEnabledByDefault(boolean enabledByDefault) {
ENABLED_BY_DEFAULT = enabledByDefault;
}
private CarbonManager(XMPPConnection connection) {
super(connection);
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
@ -111,7 +128,7 @@ public final class CarbonManager extends Manager {
carbonsListener = new StanzaListener() {
@Override
public void processStanza(final Stanza stanza) throws NotConnectedException, InterruptedException {
public void processStanza(final Stanza stanza) {
final Message wrappingMessage = (Message) stanza;
final CarbonExtension carbonExtension = CarbonExtension.from(wrappingMessage);
final Direction direction = carbonExtension.getDirection();
@ -145,6 +162,13 @@ public final class CarbonManager extends Manager {
if (!resumed) {
// Non-resumed XMPP sessions always start with disabled carbons
enabled_state = false;
try {
if (shouldCarbonsBeEnabled() && isSupportedByServer()) {
setCarbonsEnabled(true);
}
} catch (InterruptedException | XMPPErrorException | NotConnectedException | NoResponseException e) {
LOGGER.log(Level.WARNING, "Cannot check for Carbon support and / or enable carbons.", e);
}
}
addCarbonsListener(connection);
}
@ -239,12 +263,10 @@ public final class CarbonManager extends Manager {
* You should first check for support using isSupportedByServer().
*
* @param new_state whether carbons should be enabled or disabled
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
* @deprecated use {@link #enableCarbonsAsync(ExceptionCallback)} or {@link #disableCarbonsAsync(ExceptionCallback)} instead.
*/
@Deprecated
public void sendCarbonsEnabled(final boolean new_state) throws NotConnectedException, InterruptedException {
public void sendCarbonsEnabled(final boolean new_state) {
sendUseCarbons(new_state, null);
}
@ -281,6 +303,7 @@ public final class CarbonManager extends Manager {
}
private void sendUseCarbons(final boolean use, ExceptionCallback<Exception> exceptionCallback) {
enabledByDefault = use;
IQ setIQ = carbonsEnabledIQ(use);
SmackFuture<IQ, Exception> future = connection().sendIqRequestAsync(setIQ);
@ -310,6 +333,7 @@ public final class CarbonManager extends Manager {
*/
public synchronized void setCarbonsEnabled(final boolean new_state) throws NoResponseException,
XMPPErrorException, NotConnectedException, InterruptedException {
enabledByDefault = new_state;
if (enabled_state == new_state)
return;
@ -350,6 +374,10 @@ public final class CarbonManager extends Manager {
return this.enabled_state;
}
private boolean shouldCarbonsBeEnabled() {
return enabledByDefault;
}
/**
* Mark a message as "private", so it will not be carbon-copied.
*

View file

@ -36,7 +36,7 @@ import org.jxmpp.jid.Jid;
public class FillableConfigureForm extends FillableForm implements ConfigureFormReader {
public FillableConfigureForm(DataForm dataForm) {
FillableConfigureForm(DataForm dataForm) {
super(dataForm);
}

View file

@ -26,9 +26,9 @@ import org.jivesoftware.smackx.xdata.ListMultiFormField;
import org.jivesoftware.smackx.xdata.form.FillableForm;
import org.jivesoftware.smackx.xdata.packet.DataForm;
public class FillableSubscribeForm extends FillableForm {
public class FillableSubscribeForm extends FillableForm implements SubscribeFormReader {
public FillableSubscribeForm(DataForm dataForm) {
FillableSubscribeForm(DataForm dataForm) {
super(dataForm);
}

View file

@ -26,4 +26,8 @@ public class SubscribeForm extends Form implements SubscribeFormReader {
ensureFormType(dataForm, FORM_TYPE);
}
@Override
public FillableSubscribeForm getFillableForm() {
return new FillableSubscribeForm(getDataForm());
}
}