mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 01:29:38 +02:00
Merge branch '4.2'
This commit is contained in:
commit
08c228ef99
21 changed files with 3190 additions and 59 deletions
|
@ -17,15 +17,16 @@
|
|||
package org.jivesoftware.smackx.bytestreams.ibb;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.jivesoftware.smack.AbstractConnectionClosedListener;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
|
@ -79,7 +80,7 @@ import org.jxmpp.jid.Jid;
|
|||
*
|
||||
* @author Henning Staib
|
||||
*/
|
||||
public final class InBandBytestreamManager implements BytestreamManager {
|
||||
public final class InBandBytestreamManager extends Manager implements BytestreamManager {
|
||||
|
||||
/**
|
||||
* Stanzas that can be used to encapsulate In-Band Bytestream data packets.
|
||||
|
@ -140,10 +141,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
private final static Random randomGenerator = new Random();
|
||||
|
||||
/* stores one InBandBytestreamManager for each XMPP connection */
|
||||
private final static Map<XMPPConnection, InBandBytestreamManager> managers = new HashMap<XMPPConnection, InBandBytestreamManager>();
|
||||
|
||||
/* XMPP connection */
|
||||
private final XMPPConnection connection;
|
||||
private final static Map<XMPPConnection, InBandBytestreamManager> managers = new WeakHashMap<>();
|
||||
|
||||
/*
|
||||
* assigns a user to a listener that is informed if an In-Band Bytestream request for this user
|
||||
|
@ -208,7 +206,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
* @param connection the XMPP connection
|
||||
*/
|
||||
private InBandBytestreamManager(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
super(connection);
|
||||
|
||||
// register bytestream open packet listener
|
||||
this.initiationListener = new InitiationListener(this);
|
||||
|
@ -434,11 +432,13 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
Open byteStreamRequest = new Open(sessionID, this.defaultBlockSize, this.stanza);
|
||||
byteStreamRequest.setTo(targetJID);
|
||||
|
||||
final XMPPConnection connection = connection();
|
||||
|
||||
// sending packet will throw exception on timeout or error reply
|
||||
connection.createStanzaCollectorAndSend(byteStreamRequest).nextResultOrThrow();
|
||||
|
||||
InBandBytestreamSession inBandBytestreamSession = new InBandBytestreamSession(
|
||||
this.connection, byteStreamRequest, targetJID);
|
||||
connection, byteStreamRequest, targetJID);
|
||||
this.sessions.put(sessionID, inBandBytestreamSession);
|
||||
|
||||
return inBandBytestreamSession;
|
||||
|
@ -454,7 +454,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
*/
|
||||
protected void replyRejectPacket(IQ request) throws NotConnectedException, InterruptedException {
|
||||
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.not_acceptable);
|
||||
this.connection.sendStanza(error);
|
||||
connection().sendStanza(error);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -467,7 +467,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
*/
|
||||
protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException, InterruptedException {
|
||||
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.resource_constraint);
|
||||
this.connection.sendStanza(error);
|
||||
connection().sendStanza(error);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -480,7 +480,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
*/
|
||||
protected void replyItemNotFoundPacket(IQ request) throws NotConnectedException, InterruptedException {
|
||||
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.item_not_found);
|
||||
this.connection.sendStanza(error);
|
||||
connection().sendStanza(error);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -501,7 +501,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
* @return the XMPP connection
|
||||
*/
|
||||
protected XMPPConnection getConnection() {
|
||||
return this.connection;
|
||||
return connection();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -548,6 +548,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
|
|||
* internal status, which includes removing this instance from the managers map.
|
||||
*/
|
||||
private void disableService() {
|
||||
final XMPPConnection connection = connection();
|
||||
|
||||
// remove manager from static managers map
|
||||
managers.remove(connection);
|
||||
|
|
|
@ -40,13 +40,12 @@ public class FaultTolerantNegotiator extends StreamNegotiator {
|
|||
|
||||
private final StreamNegotiator primaryNegotiator;
|
||||
private final StreamNegotiator secondaryNegotiator;
|
||||
private final XMPPConnection connection;
|
||||
|
||||
public FaultTolerantNegotiator(XMPPConnection connection, StreamNegotiator primary,
|
||||
StreamNegotiator secondary) {
|
||||
super(connection);
|
||||
this.primaryNegotiator = primary;
|
||||
this.secondaryNegotiator = secondary;
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,7 +63,7 @@ public class FaultTolerantNegotiator extends StreamNegotiator {
|
|||
@Override
|
||||
public InputStream createIncomingStream(final StreamInitiation initiation) throws SmackException, XMPPErrorException, InterruptedException {
|
||||
// This could be either an xep47 ibb 'open' iq or an xep65 streamhost iq
|
||||
IQ initationSet = initiateIncomingStream(connection, initiation);
|
||||
IQ initationSet = initiateIncomingStream(connection(), initiation);
|
||||
|
||||
StreamNegotiator streamNegotiator = determineNegotiator(initationSet);
|
||||
|
||||
|
|
|
@ -44,8 +44,6 @@ import org.jxmpp.jid.Jid;
|
|||
*/
|
||||
public class IBBTransferNegotiator extends StreamNegotiator {
|
||||
|
||||
private XMPPConnection connection;
|
||||
|
||||
private InBandBytestreamManager manager;
|
||||
|
||||
/**
|
||||
|
@ -54,7 +52,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
|
|||
* @param connection The connection which this negotiator works on.
|
||||
*/
|
||||
protected IBBTransferNegotiator(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
super(connection);
|
||||
this.manager = InBandBytestreamManager.getByteStreamManager(connection);
|
||||
}
|
||||
|
||||
|
@ -75,7 +73,7 @@ public class IBBTransferNegotiator extends StreamNegotiator {
|
|||
*/
|
||||
this.manager.ignoreBytestreamRequestOnce(initiation.getSessionID());
|
||||
|
||||
Stanza streamInitiation = initiateIncomingStream(this.connection, initiation);
|
||||
Stanza streamInitiation = initiateIncomingStream(connection(), initiation);
|
||||
return negotiateIncomingStream(streamInitiation);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,13 +43,11 @@ import org.jxmpp.jid.Jid;
|
|||
*/
|
||||
public class Socks5TransferNegotiator extends StreamNegotiator {
|
||||
|
||||
private XMPPConnection connection;
|
||||
|
||||
private Socks5BytestreamManager manager;
|
||||
|
||||
Socks5TransferNegotiator(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
this.manager = Socks5BytestreamManager.getBytestreamManager(this.connection);
|
||||
super(connection);
|
||||
this.manager = Socks5BytestreamManager.getBytestreamManager(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -75,7 +73,7 @@ public class Socks5TransferNegotiator extends StreamNegotiator {
|
|||
*/
|
||||
this.manager.ignoreBytestreamRequestOnce(initiation.getSessionID());
|
||||
|
||||
Stanza streamInitiation = initiateIncomingStream(this.connection, initiation);
|
||||
Stanza streamInitiation = initiateIncomingStream(connection(), initiation);
|
||||
return negotiateIncomingStream(streamInitiation);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.filetransfer;
|
||||
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
|
@ -42,7 +43,11 @@ import java.io.OutputStream;
|
|||
*
|
||||
* @author Alexander Wenckus
|
||||
*/
|
||||
public abstract class StreamNegotiator {
|
||||
public abstract class StreamNegotiator extends Manager {
|
||||
|
||||
protected StreamNegotiator(XMPPConnection connection) {
|
||||
super(connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* A event manager for stream initiation requests send to us.
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
*
|
||||
* 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.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -16,6 +16,8 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.pubsub;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -34,6 +36,7 @@ import org.jivesoftware.smack.packet.EmptyResultIQ;
|
|||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Condition;
|
||||
import org.jivesoftware.smack.packet.Stanza;
|
||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
|
@ -246,12 +249,7 @@ public final class PubSubManager extends Manager {
|
|||
// 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 AssertionError(
|
||||
"PubSub service '"
|
||||
+ pubSubService
|
||||
+ "' returned disco info result for node '"
|
||||
+ id
|
||||
+ "', 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.");
|
||||
throw new PubSubAssertionError.DiscoInfoNodeAssertionError(pubSubService, id);
|
||||
}
|
||||
nodeMap.put(id, node);
|
||||
}
|
||||
|
@ -260,6 +258,71 @@ public final class PubSubManager extends Manager {
|
|||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get a leaf node and create one if it does not already exist.
|
||||
*
|
||||
* @param id The unique ID of the node.
|
||||
* @return the leaf node.
|
||||
* @throws NoResponseException
|
||||
* @throws NotConnectedException
|
||||
* @throws InterruptedException
|
||||
* @throws XMPPErrorException
|
||||
* @since 4.2.1
|
||||
*/
|
||||
public LeafNode getOrCreateLeafNode(final String id)
|
||||
throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException {
|
||||
try {
|
||||
return getNode(id);
|
||||
}
|
||||
catch (XMPPErrorException e1) {
|
||||
if (e1.getXMPPError().getCondition() == Condition.item_not_found) {
|
||||
try {
|
||||
return createNode(id);
|
||||
}
|
||||
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);
|
||||
}
|
||||
throw e2;
|
||||
}
|
||||
}
|
||||
throw e1;
|
||||
}
|
||||
catch (PubSubAssertionError.DiscoInfoNodeAssertionError e) {
|
||||
// This could be caused by Prosody bug #805 (see https://prosody.im/issues/issue/805). Prosody does not
|
||||
// answer to disco#info requests on the node ID, which makes it undecidable if a node is a leaf or
|
||||
// collection node.
|
||||
LOGGER.warning("The PubSub service " + pubSubService
|
||||
+ " threw an DiscoInfoNodeAssertionError, trying workaround for Prosody bug #805 (https://prosody.im/issues/issue/805)");
|
||||
return getOrCreateLeafNodeProsodyWorkaround(id);
|
||||
}
|
||||
}
|
||||
|
||||
private LeafNode getOrCreateLeafNodeProsodyWorkaround(final String id)
|
||||
throws XMPPErrorException, NoResponseException, NotConnectedException, InterruptedException {
|
||||
try {
|
||||
return createNode(id);
|
||||
}
|
||||
catch (XMPPErrorException e1) {
|
||||
if (e1.getXMPPError().getCondition() == Condition.conflict) {
|
||||
Constructor<?> constructor = LeafNode.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
LeafNode res;
|
||||
try {
|
||||
res = (LeafNode) constructor.newInstance(this, id);
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException e2) {
|
||||
throw new AssertionError(e2);
|
||||
}
|
||||
// TODO: How to verify that this is actually a leafe node and not a conflict with a collection node?
|
||||
return res;
|
||||
}
|
||||
throw e1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the nodes that currently exist as a child of the specified
|
||||
* collection node. If the service does not support collection nodes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue