mirror of https://codeberg.org/Mercury-IM/Smack
Compare commits
11 Commits
a4e4fbeee1
...
b7fe56fb9b
Author | SHA1 | Date |
---|---|---|
Florian Schmaus | b7fe56fb9b | |
Florian Schmaus | 3e8666cd91 | |
Florian Schmaus | 89e39fadd2 | |
Florian Schmaus | b1c0f3fa26 | |
Florian Schmaus | c80afbbce7 | |
Florian Schmaus | ed02bcf0d4 | |
Florian Schmaus | 049d687778 | |
Aditya Borikar | 45f75d5ce0 | |
Florian Schmaus | 0eeb89409a | |
Florian Schmaus | bc503c7475 | |
Aditya Borikar | dcb66eef59 |
|
@ -259,6 +259,13 @@ allprojects {
|
||||||
if (JavaVersion.current().isJava9Compatible()) {
|
if (JavaVersion.current().isJava9Compatible()) {
|
||||||
tasks.withType(Javadoc) {
|
tasks.withType(Javadoc) {
|
||||||
options.addStringOption('-release', javaMajor)
|
options.addStringOption('-release', javaMajor)
|
||||||
|
|
||||||
|
// Fix for javadoc search. If not set, the search result would direct to
|
||||||
|
// javadoc/undefined/org/jivesoftware/smack/altconnections/HttpLookupMethod.html
|
||||||
|
// instead of
|
||||||
|
// javadoc/org/jivesoftware/smack/altconnections/HttpLookupMethod.html
|
||||||
|
// https://stackoverflow.com/a/53732633/194894
|
||||||
|
options.addBooleanOption("-no-module-directories", true)
|
||||||
}
|
}
|
||||||
tasks.withType(JavaCompile) {
|
tasks.withType(JavaCompile) {
|
||||||
options.compilerArgs.addAll([
|
options.compilerArgs.addAll([
|
||||||
|
|
|
@ -14,6 +14,7 @@ Currently supported XEPs of Smack (all sub-projects)
|
||||||
|
|
||||||
| Name | XEP | Version | Description |
|
| Name | XEP | Version | Description |
|
||||||
|---------------------------------------------|--------------------------------------------------------|-----------|----------------------------------------------------------------------------------------------------------|
|
|---------------------------------------------|--------------------------------------------------------|-----------|----------------------------------------------------------------------------------------------------------|
|
||||||
|
| Discovering Alternative XMPP Connection Methods | [XEP-0156](https://xmpp.org/extensions/xep-0156.html) | 1.3.0 | Defines ways to discover alternative connection methods. |
|
||||||
| Nonzas | [XEP-0360](https://xmpp.org/extensions/xep-0360.html) | n/a | Defines the term "Nonza", describing every top level stream element that is not a Stanza. |
|
| Nonzas | [XEP-0360](https://xmpp.org/extensions/xep-0360.html) | n/a | Defines the term "Nonza", describing every top level stream element that is not a Stanza. |
|
||||||
|
|
||||||
Currently supported XEPs of smack-tcp
|
Currently supported XEPs of smack-tcp
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2020 Aditya Borikar.
|
||||||
|
*
|
||||||
|
* 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.smack.altconnections;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
||||||
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
||||||
|
|
||||||
|
import org.jxmpp.jid.DomainBareJid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HTTP lookup method uses web host metadata to list the URIs of alternative connection methods.
|
||||||
|
*
|
||||||
|
* <p>In order to obtain host-meta XRD element from the host in the form of an <i>InputStream</i>,
|
||||||
|
* use {@link #getXrdStream(DomainBareJid)} method. To obtain endpoints for Bosh or Websocket
|
||||||
|
* connection endpoints from host, use {@link LinkRelation#BOSH} and {@link LinkRelation#WEBSOCKET}
|
||||||
|
* respectively with the {@link #lookup(DomainBareJid, LinkRelation)} method. In case one is looking
|
||||||
|
* for endpoints described by other than BOSH or Websocket LinkRelation, use the more flexible
|
||||||
|
* {@link #lookup(DomainBareJid, String)} method.</p>
|
||||||
|
* Example:<br>
|
||||||
|
* <pre>
|
||||||
|
* {@code
|
||||||
|
* DomainBareJid xmppServerAddress = JidCreate.domainBareFrom("example.org");
|
||||||
|
* List<URI> endpoints = HttpLookupMethod.lookup(xmppServiceAddress, LinkRelation.WEBSOCKET);
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
* @see <a href="https://xmpp.org/extensions/xep-0156.html#http">
|
||||||
|
* HTTP Lookup Method from XEP-0156.
|
||||||
|
* </a>
|
||||||
|
*/
|
||||||
|
public final class HttpLookupMethod {
|
||||||
|
private static final String XRD_NAMESPACE = "http://docs.oasis-open.org/ns/xri/xrd-1.0";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies a link relation for the selected type of connection.
|
||||||
|
*/
|
||||||
|
public enum LinkRelation {
|
||||||
|
/**
|
||||||
|
* Selects link relation attribute as "urn:xmpp:alt-connections:xbosh".
|
||||||
|
*/
|
||||||
|
BOSH("urn:xmpp:alt-connections:xbosh"),
|
||||||
|
/**
|
||||||
|
* Selects link relation attribute as "urn:xmpp:alt-connections:websocket".
|
||||||
|
*/
|
||||||
|
WEBSOCKET("urn:xmpp:alt-connections:websocket");
|
||||||
|
private final String attribute;
|
||||||
|
LinkRelation(String relAttribute) {
|
||||||
|
this.attribute = relAttribute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get remote endpoints for the given LinkRelation from host.
|
||||||
|
*
|
||||||
|
* @param xmppServiceAddress address of host
|
||||||
|
* @param relation LinkRelation as a string specifying type of connection
|
||||||
|
* @return list of endpoints
|
||||||
|
* @throws IOException exception due to input/output operations
|
||||||
|
* @throws XmlPullParserException exception encountered during XML parsing
|
||||||
|
* @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference
|
||||||
|
*/
|
||||||
|
public static List<URI> lookup(DomainBareJid xmppServiceAddress, String relation) throws IOException, XmlPullParserException, URISyntaxException {
|
||||||
|
try (InputStream inputStream = getXrdStream(xmppServiceAddress)) {
|
||||||
|
XmlPullParser xmlPullParser = PacketParserUtils.getParserFor(inputStream);
|
||||||
|
List<URI> endpoints = parseXrdLinkReferencesFor(xmlPullParser, relation);
|
||||||
|
return endpoints;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get remote endpoints for the given LinkRelation from host.
|
||||||
|
*
|
||||||
|
* @param xmppServiceAddress address of host
|
||||||
|
* @param relation {@link LinkRelation} specifying type of connection
|
||||||
|
* @return list of endpoints
|
||||||
|
* @throws IOException exception due to input/output operations
|
||||||
|
* @throws XmlPullParserException exception encountered during XML parsing
|
||||||
|
* @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference
|
||||||
|
*/
|
||||||
|
public static List<URI> lookup(DomainBareJid xmppServiceAddress, LinkRelation relation) throws IOException, XmlPullParserException, URISyntaxException {
|
||||||
|
return lookup(xmppServiceAddress, relation.attribute);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a HTTP connection with the host specified by the DomainBareJid
|
||||||
|
* and retrieves XRD element in the form of an InputStream. The method will
|
||||||
|
* throw a {@link FileNotFoundException} if host-meta isn't published.
|
||||||
|
*
|
||||||
|
* @param xmppServiceAddress address of host
|
||||||
|
* @return InputStream containing XRD element
|
||||||
|
* @throws IOException exception due to input/output operations
|
||||||
|
*/
|
||||||
|
public static InputStream getXrdStream(DomainBareJid xmppServiceAddress) throws IOException {
|
||||||
|
final String metadataUrl = "https://" + xmppServiceAddress + "/.well-known/host-meta";
|
||||||
|
final URL putUrl = new URL(metadataUrl);
|
||||||
|
final URLConnection urlConnection = putUrl.openConnection();
|
||||||
|
return urlConnection.getInputStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get remote endpoints for the provided LinkRelation from provided XmlPullParser.
|
||||||
|
*
|
||||||
|
* @param parser XmlPullParser that contains LinkRelations
|
||||||
|
* @param relation type of endpoints specified by the given LinkRelation
|
||||||
|
* @return list of endpoints
|
||||||
|
* @throws IOException exception due to input/output operations
|
||||||
|
* @throws XmlPullParserException exception encountered during XML parsing
|
||||||
|
* @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference
|
||||||
|
*/
|
||||||
|
public static List<URI> parseXrdLinkReferencesFor(XmlPullParser parser, String relation) throws IOException, XmlPullParserException, URISyntaxException {
|
||||||
|
List<URI> uriList = new ArrayList<>();
|
||||||
|
int initialDepth = parser.getDepth();
|
||||||
|
|
||||||
|
loop: while (true) {
|
||||||
|
XmlPullParser.TagEvent tag = parser.nextTag();
|
||||||
|
switch (tag) {
|
||||||
|
case START_ELEMENT:
|
||||||
|
String name = parser.getName();
|
||||||
|
String namespace = parser.getNamespace();
|
||||||
|
String rel = parser.getAttributeValue("rel");
|
||||||
|
|
||||||
|
if (!namespace.equals(XRD_NAMESPACE) || !name.equals("Link") || !rel.equals(relation)) {
|
||||||
|
continue loop;
|
||||||
|
}
|
||||||
|
String endpointUri = parser.getAttributeValue("href");
|
||||||
|
URI uri = new URI(endpointUri);
|
||||||
|
uriList.add(uri);
|
||||||
|
break;
|
||||||
|
case END_ELEMENT:
|
||||||
|
if (parser.getDepth() == initialDepth) {
|
||||||
|
break loop;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return uriList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get remote endpoints for the provided LinkRelation from provided XmlPullParser.
|
||||||
|
*
|
||||||
|
* @param parser XmlPullParser that contains LinkRelations
|
||||||
|
* @param relation type of endpoints specified by the given LinkRelation
|
||||||
|
* @return list of endpoints
|
||||||
|
* @throws IOException exception due to input/output operations
|
||||||
|
* @throws XmlPullParserException exception encountered during XML parsing
|
||||||
|
* @throws URISyntaxException exception to indicate that a string could not be parsed as a URI reference
|
||||||
|
*/
|
||||||
|
public static List<URI> parseXrdLinkReferencesFor(XmlPullParser parser, LinkRelation relation) throws IOException, XmlPullParserException, URISyntaxException {
|
||||||
|
return parseXrdLinkReferencesFor(parser, relation.attribute);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2020 Aditya Borikar.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Smack's API for <a href="https://xmpp.org/extensions/xep-0156.html"> XEP-0156: Discovering Alternative XMPP Connection Methods</a>.
|
||||||
|
* <br>
|
||||||
|
* XEP is partially supported as HTTP lookup is supported but DNS lookup isn't.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.altconnections;
|
|
@ -0,0 +1,57 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2020 Aditya Borikar.
|
||||||
|
*
|
||||||
|
* 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.smack.altconnections;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.altconnections.HttpLookupMethod.LinkRelation;
|
||||||
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.jxmpp.stringprep.XmppStringprepException;
|
||||||
|
|
||||||
|
public class HttpLookupMethodTest {
|
||||||
|
private static final String XRD_XML = "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>" +
|
||||||
|
"<Link rel='urn:xmpp:alt-connections:xbosh' href='https://igniterealtime.org:443/http-bind/'/>" +
|
||||||
|
"<Link rel='urn:xmpp:alt-connections:websocket' href='wss://xmpp.igniterealtime.org:7483/ws/'/>" +
|
||||||
|
"<Link rel='urn:xmpp:alt-connections:websocket' href='ws://xmpp.igniterealtime.org:7070/ws/'/>" +
|
||||||
|
"</XRD>";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseXrdLinkReferencesForWebsockets() throws XmppStringprepException, IOException, XmlPullParserException, URISyntaxException {
|
||||||
|
List<URI> endpoints = new ArrayList<>();
|
||||||
|
endpoints.add(new URI("wss://xmpp.igniterealtime.org:7483/ws/"));
|
||||||
|
endpoints.add(new URI("ws://xmpp.igniterealtime.org:7070/ws/"));
|
||||||
|
List<URI> expectedEndpoints = HttpLookupMethod.parseXrdLinkReferencesFor(PacketParserUtils.getParserFor(XRD_XML), LinkRelation.WEBSOCKET);
|
||||||
|
assertEquals(expectedEndpoints, endpoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseXrdLinkReferencesForBosh() throws URISyntaxException, IOException, XmlPullParserException {
|
||||||
|
List<URI> endpoints = new ArrayList<>();
|
||||||
|
endpoints.add(new URI("https://igniterealtime.org:443/http-bind/"));
|
||||||
|
List<URI> expectedEndpoints = HttpLookupMethod.parseXrdLinkReferencesFor(PacketParserUtils.getParserFor(XRD_XML), LinkRelation.BOSH);
|
||||||
|
assertEquals(expectedEndpoints, endpoints);
|
||||||
|
}
|
||||||
|
}
|
|
@ -110,22 +110,6 @@ 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -206,6 +190,15 @@ 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);
|
||||||
|
@ -453,19 +446,6 @@ 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.
|
||||||
|
@ -539,30 +519,4 @@ 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,15 +16,9 @@
|
||||||
*/
|
*/
|
||||||
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.Stanza;
|
import org.jivesoftware.smack.packet.StanzaError;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -44,14 +38,10 @@ 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.
|
||||||
*
|
*
|
||||||
|
@ -60,40 +50,29 @@ 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 packet) {
|
public IQ handleIQRequest(final IQ iqRequest) {
|
||||||
initiationListenerExecutor.execute(new Runnable() {
|
Open ibbRequest = (Open) iqRequest;
|
||||||
|
|
||||||
@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 (ibbRequest.getBlockSize() > this.manager.getMaximumBlockSize()) {
|
if (blockSize > maximumBlockSize) {
|
||||||
this.manager.replyResourceConstraintPacket(ibbRequest);
|
StanzaError error = StanzaError.getBuilder().setCondition(StanzaError.Condition.resource_constraint)
|
||||||
return;
|
.setDescriptiveEnText("Requests block size of " + blockSize + " exceeds maximum block size of "
|
||||||
|
+ 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;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// build bytestream request from packet
|
// build bytestream request from packet
|
||||||
InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);
|
InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);
|
||||||
|
@ -102,7 +81,6 @@ 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()) {
|
||||||
/*
|
/*
|
||||||
|
@ -111,21 +89,16 @@ 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()
|
||||||
* if there is no listener for this initiation request, reply with reject message
|
.setCondition(StanzaError.Condition.not_acceptable)
|
||||||
*/
|
.setDescriptiveEnText("No file-transfer listeners registered")
|
||||||
this.manager.replyRejectPacket(ibbRequest);
|
.build();
|
||||||
|
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_multi:
|
case list_single:
|
||||||
return FormField.listSingleBuilder(fieldName);
|
return FormField.listSingleBuilder(fieldName);
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException("Unsupported type: " + type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,23 +83,14 @@ 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
|
||||||
initiationListener.handleIQRequest(initBytestream);
|
IQ response = 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, argument.getValue().getTo());
|
assertEquals(initiatorJID, response.getTo());
|
||||||
assertEquals(IQ.Type.error, argument.getValue().getType());
|
assertEquals(IQ.Type.error, response.getType());
|
||||||
assertEquals(StanzaError.Condition.not_acceptable,
|
assertEquals(StanzaError.Condition.not_acceptable,
|
||||||
argument.getValue().getError().getCondition());
|
response.getError().getCondition());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,21 +104,13 @@ 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
|
||||||
initiationListener.handleIQRequest(initBytestream);
|
IQ response = 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, argument.getValue().getTo());
|
assertEquals(initiatorJID, response.getTo());
|
||||||
assertEquals(IQ.Type.error, argument.getValue().getType());
|
assertEquals(IQ.Type.error, response.getType());
|
||||||
assertEquals(StanzaError.Condition.resource_constraint,
|
assertEquals(StanzaError.Condition.resource_constraint,
|
||||||
argument.getValue().getError().getCondition());
|
response.getError().getCondition());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -199,24 +182,17 @@ 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
|
||||||
initiationListener.handleIQRequest(initBytestream);
|
IQ response = 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, argument.getValue().getTo());
|
assertEquals(initiatorJID, response.getTo());
|
||||||
assertEquals(IQ.Type.error, argument.getValue().getType());
|
assertEquals(IQ.Type.error, response.getType());
|
||||||
assertEquals(StanzaError.Condition.not_acceptable,
|
assertEquals(StanzaError.Condition.not_acceptable,
|
||||||
argument.getValue().getError().getCondition());
|
response.getError().getCondition());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -69,7 +69,6 @@ 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 New Issue