mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-30 07:52:06 +01:00
Compare commits
No commits in common. "b7fe56fb9ba3bf4547999f0a18759e5ad1f8b16e" and "0eeb89409a0de7714b05655801978f9acea5d438" have entirely different histories.
b7fe56fb9b
...
0eeb89409a
5 changed files with 141 additions and 43 deletions
|
@ -110,6 +110,22 @@ public final class InBandBytestreamManager extends Manager implements Bytestream
|
||||||
public void connectionCreated(final XMPPConnection connection) {
|
public void connectionCreated(final XMPPConnection connection) {
|
||||||
// create the manager for this connection
|
// create the manager for this connection
|
||||||
InBandBytestreamManager.getByteStreamManager(connection);
|
InBandBytestreamManager.getByteStreamManager(connection);
|
||||||
|
|
||||||
|
// register shutdown listener
|
||||||
|
connection.addConnectionListener(new AbstractConnectionClosedListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connectionTerminated() {
|
||||||
|
InBandBytestreamManager.getByteStreamManager(connection).disableService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connected(XMPPConnection connection) {
|
||||||
|
InBandBytestreamManager.getByteStreamManager(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -190,15 +206,6 @@ public final class InBandBytestreamManager extends Manager implements Bytestream
|
||||||
private InBandBytestreamManager(XMPPConnection connection) {
|
private InBandBytestreamManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
|
|
||||||
connection.addConnectionListener(new AbstractConnectionClosedListener() {
|
|
||||||
@Override
|
|
||||||
public void connectionTerminated() {
|
|
||||||
// reset internal status
|
|
||||||
InBandBytestreamManager.this.sessions.clear();
|
|
||||||
InBandBytestreamManager.this.ignoredBytestreamRequests.clear();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// register bytestream open packet listener
|
// register bytestream open packet listener
|
||||||
this.initiationListener = new InitiationListener(this);
|
this.initiationListener = new InitiationListener(this);
|
||||||
connection.registerIQRequestHandler(initiationListener);
|
connection.registerIQRequestHandler(initiationListener);
|
||||||
|
@ -446,6 +453,19 @@ public final class InBandBytestreamManager extends Manager implements Bytestream
|
||||||
connection().sendStanza(error);
|
connection().sendStanza(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responses to the given IQ packet's sender with an XMPP error that an In-Band Bytestream open
|
||||||
|
* request is rejected because its block size is greater than the maximum allowed block size.
|
||||||
|
*
|
||||||
|
* @param request IQ stanza that should be answered with a resource-constraint error
|
||||||
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
||||||
|
* @throws InterruptedException if the calling thread was interrupted.
|
||||||
|
*/
|
||||||
|
protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException, InterruptedException {
|
||||||
|
IQ error = IQ.createErrorResponse(request, StanzaError.Condition.resource_constraint);
|
||||||
|
connection().sendStanza(error);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responses to the given IQ packet's sender with an XMPP error that an In-Band Bytestream
|
* Responses to the given IQ packet's sender with an XMPP error that an In-Band Bytestream
|
||||||
* session could not be found.
|
* session could not be found.
|
||||||
|
@ -519,4 +539,30 @@ public final class InBandBytestreamManager extends Manager implements Bytestream
|
||||||
return ignoredBytestreamRequests;
|
return ignoredBytestreamRequests;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables the InBandBytestreamManager by removing its stanza listeners and resetting its
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
// remove all listeners registered by this manager
|
||||||
|
connection.unregisterIQRequestHandler(initiationListener);
|
||||||
|
connection.unregisterIQRequestHandler(dataListener);
|
||||||
|
connection.unregisterIQRequestHandler(closeListener);
|
||||||
|
|
||||||
|
// shutdown threads
|
||||||
|
this.initiationListener.shutdown();
|
||||||
|
|
||||||
|
// reset internal status
|
||||||
|
this.userListeners.clear();
|
||||||
|
this.allRequestListeners.clear();
|
||||||
|
this.sessions.clear();
|
||||||
|
this.ignoredBytestreamRequests.clear();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,9 +16,15 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.bytestreams.ibb;
|
package org.jivesoftware.smackx.bytestreams.ibb;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||||
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smack.packet.StanzaError;
|
import org.jivesoftware.smack.packet.Stanza;
|
||||||
|
|
||||||
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
|
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
|
||||||
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
|
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
|
||||||
|
@ -38,10 +44,14 @@ import org.jivesoftware.smackx.filetransfer.StreamNegotiator;
|
||||||
* @author Henning Staib
|
* @author Henning Staib
|
||||||
*/
|
*/
|
||||||
class InitiationListener extends AbstractIqRequestHandler {
|
class InitiationListener extends AbstractIqRequestHandler {
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(InitiationListener.class.getName());
|
||||||
|
|
||||||
/* manager containing the listeners and the XMPP connection */
|
/* manager containing the listeners and the XMPP connection */
|
||||||
private final InBandBytestreamManager manager;
|
private final InBandBytestreamManager manager;
|
||||||
|
|
||||||
|
/* executor service to process incoming requests concurrently */
|
||||||
|
private final ExecutorService initiationListenerExecutor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
|
@ -50,29 +60,40 @@ class InitiationListener extends AbstractIqRequestHandler {
|
||||||
protected InitiationListener(InBandBytestreamManager manager) {
|
protected InitiationListener(InBandBytestreamManager manager) {
|
||||||
super(Open.ELEMENT, Open.NAMESPACE, IQ.Type.set, Mode.async);
|
super(Open.ELEMENT, Open.NAMESPACE, IQ.Type.set, Mode.async);
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
|
initiationListenerExecutor = Executors.newCachedThreadPool();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IQ handleIQRequest(final IQ iqRequest) {
|
public IQ handleIQRequest(final IQ packet) {
|
||||||
Open ibbRequest = (Open) iqRequest;
|
initiationListenerExecutor.execute(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
processRequest(packet);
|
||||||
|
}
|
||||||
|
catch (InterruptedException | NotConnectedException e) {
|
||||||
|
LOGGER.log(Level.WARNING, "proccessRequest", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processRequest(Stanza packet) throws NotConnectedException, InterruptedException {
|
||||||
|
Open ibbRequest = (Open) packet;
|
||||||
|
|
||||||
int blockSize = ibbRequest.getBlockSize();
|
|
||||||
int maximumBlockSize = manager.getMaximumBlockSize();
|
|
||||||
// validate that block size is within allowed range
|
// validate that block size is within allowed range
|
||||||
if (blockSize > maximumBlockSize) {
|
if (ibbRequest.getBlockSize() > this.manager.getMaximumBlockSize()) {
|
||||||
StanzaError error = StanzaError.getBuilder().setCondition(StanzaError.Condition.resource_constraint)
|
this.manager.replyResourceConstraintPacket(ibbRequest);
|
||||||
.setDescriptiveEnText("Requests block size of " + blockSize + " exceeds maximum block size of "
|
return;
|
||||||
+ maximumBlockSize)
|
|
||||||
.build();
|
|
||||||
return IQ.createErrorResponse(iqRequest, error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamNegotiator.signal(ibbRequest.getFrom().toString() + '\t' + ibbRequest.getSessionID(), ibbRequest);
|
StreamNegotiator.signal(ibbRequest.getFrom().toString() + '\t' + ibbRequest.getSessionID(), ibbRequest);
|
||||||
|
|
||||||
// ignore request if in ignore list
|
// ignore request if in ignore list
|
||||||
if (this.manager.getIgnoredBytestreamRequests().remove(ibbRequest.getSessionID())) {
|
if (this.manager.getIgnoredBytestreamRequests().remove(ibbRequest.getSessionID()))
|
||||||
return null;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
// build bytestream request from packet
|
// build bytestream request from packet
|
||||||
InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);
|
InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);
|
||||||
|
@ -81,6 +102,7 @@ class InitiationListener extends AbstractIqRequestHandler {
|
||||||
BytestreamListener userListener = this.manager.getUserListener(ibbRequest.getFrom());
|
BytestreamListener userListener = this.manager.getUserListener(ibbRequest.getFrom());
|
||||||
if (userListener != null) {
|
if (userListener != null) {
|
||||||
userListener.incomingBytestreamRequest(request);
|
userListener.incomingBytestreamRequest(request);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (!this.manager.getAllRequestListeners().isEmpty()) {
|
else if (!this.manager.getAllRequestListeners().isEmpty()) {
|
||||||
/*
|
/*
|
||||||
|
@ -89,16 +111,21 @@ class InitiationListener extends AbstractIqRequestHandler {
|
||||||
for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
|
for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
|
||||||
listener.incomingBytestreamRequest(request);
|
listener.incomingBytestreamRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
StanzaError error = StanzaError.getBuilder()
|
/*
|
||||||
.setCondition(StanzaError.Condition.not_acceptable)
|
* if there is no listener for this initiation request, reply with reject message
|
||||||
.setDescriptiveEnText("No file-transfer listeners registered")
|
*/
|
||||||
.build();
|
this.manager.replyRejectPacket(ibbRequest);
|
||||||
return IQ.createErrorResponse(iqRequest, error);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
/**
|
||||||
|
* Shuts down the listeners executor service.
|
||||||
|
*/
|
||||||
|
protected void shutdown() {
|
||||||
|
this.initiationListenerExecutor.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,10 +193,10 @@ public class FillableForm extends FilledForm {
|
||||||
return FormField.textSingleBuilder(fieldName);
|
return FormField.textSingleBuilder(fieldName);
|
||||||
case hidden:
|
case hidden:
|
||||||
return FormField.hiddenBuilder(fieldName);
|
return FormField.hiddenBuilder(fieldName);
|
||||||
case list_single:
|
case list_multi:
|
||||||
return FormField.listSingleBuilder(fieldName);
|
return FormField.listSingleBuilder(fieldName);
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unsupported type: " + type);
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,14 +83,23 @@ public class InitiationListenerTest extends SmackTestSuite {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldRespondWithError() throws Exception {
|
public void shouldRespondWithError() throws Exception {
|
||||||
|
|
||||||
// run the listener with the initiation packet
|
// run the listener with the initiation packet
|
||||||
IQ response = initiationListener.handleIQRequest(initBytestream);
|
initiationListener.handleIQRequest(initBytestream);
|
||||||
|
|
||||||
|
// wait because packet is processed in an extra thread
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
// capture reply to the In-Band Bytestream open request
|
||||||
|
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
|
||||||
|
verify(connection).sendStanza(argument.capture());
|
||||||
|
|
||||||
// assert that reply is the correct error packet
|
// assert that reply is the correct error packet
|
||||||
assertEquals(initiatorJID, response.getTo());
|
assertEquals(initiatorJID, argument.getValue().getTo());
|
||||||
assertEquals(IQ.Type.error, response.getType());
|
assertEquals(IQ.Type.error, argument.getValue().getType());
|
||||||
assertEquals(StanzaError.Condition.not_acceptable,
|
assertEquals(StanzaError.Condition.not_acceptable,
|
||||||
response.getError().getCondition());
|
argument.getValue().getError().getCondition());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,13 +113,21 @@ public class InitiationListenerTest extends SmackTestSuite {
|
||||||
byteStreamManager.setMaximumBlockSize(1024);
|
byteStreamManager.setMaximumBlockSize(1024);
|
||||||
|
|
||||||
// run the listener with the initiation packet
|
// run the listener with the initiation packet
|
||||||
IQ response = initiationListener.handleIQRequest(initBytestream);
|
initiationListener.handleIQRequest(initBytestream);
|
||||||
|
|
||||||
|
// wait because packet is processed in an extra thread
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
// capture reply to the In-Band Bytestream open request
|
||||||
|
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
|
||||||
|
verify(connection).sendStanza(argument.capture());
|
||||||
|
|
||||||
// assert that reply is the correct error packet
|
// assert that reply is the correct error packet
|
||||||
assertEquals(initiatorJID, response.getTo());
|
assertEquals(initiatorJID, argument.getValue().getTo());
|
||||||
assertEquals(IQ.Type.error, response.getType());
|
assertEquals(IQ.Type.error, argument.getValue().getType());
|
||||||
assertEquals(StanzaError.Condition.resource_constraint,
|
assertEquals(StanzaError.Condition.resource_constraint,
|
||||||
response.getError().getCondition());
|
argument.getValue().getError().getCondition());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,17 +199,24 @@ public class InitiationListenerTest extends SmackTestSuite {
|
||||||
byteStreamManager.addIncomingBytestreamListener(listener, JidCreate.from("other_" + initiatorJID));
|
byteStreamManager.addIncomingBytestreamListener(listener, JidCreate.from("other_" + initiatorJID));
|
||||||
|
|
||||||
// run the listener with the initiation packet
|
// run the listener with the initiation packet
|
||||||
IQ response = initiationListener.handleIQRequest(initBytestream);
|
initiationListener.handleIQRequest(initBytestream);
|
||||||
|
|
||||||
|
// wait because packet is processed in an extra thread
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
// assert listener is not called
|
// assert listener is not called
|
||||||
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
|
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
|
||||||
verify(listener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
|
verify(listener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
|
||||||
|
|
||||||
|
// capture reply to the In-Band Bytestream open request
|
||||||
|
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
|
||||||
|
verify(connection).sendStanza(argument.capture());
|
||||||
|
|
||||||
// assert that reply is the correct error packet
|
// assert that reply is the correct error packet
|
||||||
assertEquals(initiatorJID, response.getTo());
|
assertEquals(initiatorJID, argument.getValue().getTo());
|
||||||
assertEquals(IQ.Type.error, response.getType());
|
assertEquals(IQ.Type.error, argument.getValue().getType());
|
||||||
assertEquals(StanzaError.Condition.not_acceptable,
|
assertEquals(StanzaError.Condition.not_acceptable,
|
||||||
response.getError().getCondition());
|
argument.getValue().getError().getCondition());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -69,6 +69,7 @@ public final class ConnectionAttemptState {
|
||||||
|
|
||||||
List<Rfc6120TcpRemoteConnectionEndpoint> endpoints = discoveredEndpoints.result.discoveredRemoteConnectionEndpoints;
|
List<Rfc6120TcpRemoteConnectionEndpoint> endpoints = discoveredEndpoints.result.discoveredRemoteConnectionEndpoints;
|
||||||
connectionEndpointIterator = endpoints.iterator();
|
connectionEndpointIterator = endpoints.iterator();
|
||||||
|
connectionEndpoint = connectionEndpointIterator.next();
|
||||||
connectionExceptions = new ArrayList<>(endpoints.size());
|
connectionExceptions = new ArrayList<>(endpoints.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue