mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-29 23:42:06 +01:00
Merge pull request #430 from Flowdalic/websocket
Introduce smack-websocket-okhttp
This commit is contained in:
commit
0a6c21982b
37 changed files with 404 additions and 341 deletions
|
@ -31,6 +31,7 @@ include 'smack-core',
|
|||
'smack-repl',
|
||||
'smack-openpgp',
|
||||
'smack-websocket',
|
||||
'smack-websocket-okhttp',
|
||||
'smack-xmlparser',
|
||||
'smack-xmlparser-stax',
|
||||
'smack-xmlparser-xpp3'
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
<className>org.jivesoftware.smack.android.AndroidSmackInitializer</className>
|
||||
<className>org.jivesoftware.smack.java7.Java7SmackInitializer</className>
|
||||
<className>org.jivesoftware.smack.im.SmackImInitializer</className>
|
||||
<className>org.jivesoftware.smack.websocket.WebsocketInitializer</className>
|
||||
<className>org.jivesoftware.smack.websocket.WebSocketInitializer</className>
|
||||
<className>org.jivesoftware.smackx.omemo.OmemoInitializer</className>
|
||||
<className>org.jivesoftware.smackx.ox.util.OpenPgpInitializer</className>
|
||||
</optionalStartupClasses>
|
||||
|
|
|
@ -44,7 +44,7 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
|||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.jivesoftware.smack.util.MultiMap;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModuleDescriptor;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor;
|
||||
|
||||
import org.jivesoftware.smackx.admin.ServiceAdministrationManager;
|
||||
import org.jivesoftware.smackx.iqregister.AccountManager;
|
||||
|
@ -92,7 +92,7 @@ public class XmppConnectionManager {
|
|||
.withNickname("modular-websocket")
|
||||
.applyExtraConfguration(cb -> {
|
||||
cb.removeAllModules();
|
||||
cb.addModule(XmppWebsocketTransportModuleDescriptor.class);
|
||||
cb.addModule(XmppWebSocketTransportModuleDescriptor.class);
|
||||
})
|
||||
.build()
|
||||
);
|
||||
|
|
|
@ -32,7 +32,7 @@ digraph {
|
|||
"ConnectedButUnauthenticated" -> "InstantShutdown" [xlabel="5"];
|
||||
"ConnectedButUnauthenticated" [ style=filled ]
|
||||
"EstablishingTcpConnection" -> "ConnectedButUnauthenticated" [xlabel="2"];
|
||||
"LookupRemoteConnectionEndpoints" -> "EstablishingWebsocketConnection" [xlabel="2"];
|
||||
"EstablishingWebsocketConnection" -> "ConnectedButUnauthenticated";
|
||||
"LookupRemoteConnectionEndpoints" -> "EstablishingWebSocketConnection" [xlabel="2"];
|
||||
"EstablishingWebSocketConnection" -> "ConnectedButUnauthenticated";
|
||||
"Disconnected" [ style=filled ]
|
||||
}
|
||||
|
|
|
@ -29,9 +29,9 @@ import org.jivesoftware.smack.XMPPException;
|
|||
|
||||
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
|
||||
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModuleDescriptor;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor;
|
||||
|
||||
public class WebsocketConnection {
|
||||
public class WebSocketConnection {
|
||||
|
||||
public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException, URISyntaxException {
|
||||
ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
|
||||
|
@ -39,8 +39,8 @@ public class WebsocketConnection {
|
|||
builder.setXmppAddressAndPassword(args[0], args[1]);
|
||||
|
||||
// Set a fallback uri into websocket transport descriptor and add this descriptor into connection builder.
|
||||
XmppWebsocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebsocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebsocketEndpointAndDiscovery(new URI(args[2]), false);
|
||||
XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(new URI(args[2]), false);
|
||||
builder.addModule(websocketBuilder.build());
|
||||
|
||||
ModularXmppClientToServerConnectionConfiguration config = builder.build();
|
10
smack-websocket-okhttp/build.gradle
Normal file
10
smack-websocket-okhttp/build.gradle
Normal file
|
@ -0,0 +1,10 @@
|
|||
description = """\
|
||||
Smack for XMPP connections over WebSocket (RFC 7395) using OkHttp."""
|
||||
|
||||
dependencies {
|
||||
api project(':smack-websocket')
|
||||
|
||||
testFixturesApi(testFixtures(project(':smack-websocket')))
|
||||
|
||||
implementation("com.squareup.okhttp3:okhttp:4.6.0")
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket.implementations.okhttp;
|
||||
package org.jivesoftware.smack.websocket.okhttp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket.implementations.okhttp;
|
||||
package org.jivesoftware.smack.websocket.okhttp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
|
@ -27,11 +27,10 @@ import org.jivesoftware.smack.XMPPException;
|
|||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smack.websocket.WebsocketException;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
import org.jivesoftware.smack.websocket.elements.WebsocketOpenElement;
|
||||
import org.jivesoftware.smack.websocket.implementations.AbstractWebsocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.WebSocketException;
|
||||
import org.jivesoftware.smack.websocket.elements.WebSocketOpenElement;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.xml.XmlPullParserException;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
@ -40,9 +39,9 @@ import okhttp3.Response;
|
|||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
|
||||
public final class OkHttpWebsocket extends AbstractWebsocket {
|
||||
public final class OkHttpWebSocket extends AbstractWebSocket {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(OkHttpWebsocket.class.getName());
|
||||
private static final Logger LOGGER = Logger.getLogger(OkHttpWebSocket.class.getName());
|
||||
|
||||
private static OkHttpClient okHttpClient = null;
|
||||
|
||||
|
@ -50,12 +49,11 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
private final LoggingInterceptor interceptor;
|
||||
|
||||
private String openStreamHeader;
|
||||
private WebSocket currentWebsocket;
|
||||
private WebsocketConnectionPhase phase;
|
||||
private WebsocketRemoteConnectionEndpoint connectedEndpoint;
|
||||
private WebSocket currentWebSocket;
|
||||
private WebSocketConnectionPhase phase;
|
||||
private WebSocketRemoteConnectionEndpoint connectedEndpoint;
|
||||
|
||||
public OkHttpWebsocket(ModularXmppClientToServerConnectionInternal connectionInternal,
|
||||
DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints) {
|
||||
public OkHttpWebSocket(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
this.connectionInternal = connectionInternal;
|
||||
|
||||
if (okHttpClient == null) {
|
||||
|
@ -72,8 +70,8 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void connect(WebsocketRemoteConnectionEndpoint endpoint) throws InterruptedException, SmackException, XMPPException {
|
||||
final String currentUri = endpoint.getWebsocketEndpoint().toString();
|
||||
public void connect(WebSocketRemoteConnectionEndpoint endpoint) throws InterruptedException, SmackException, XMPPException {
|
||||
final String currentUri = endpoint.getWebSocketEndpoint().toString();
|
||||
Request request = new Request.Builder()
|
||||
.url(currentUri)
|
||||
.header("Sec-WebSocket-Protocol", "xmpp")
|
||||
|
@ -83,12 +81,12 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
|
||||
@Override
|
||||
public void onOpen(WebSocket webSocket, Response response) {
|
||||
LOGGER.log(Level.FINER, "Websocket is open");
|
||||
phase = WebsocketConnectionPhase.openFrameSent;
|
||||
LOGGER.log(Level.FINER, "WebSocket is open");
|
||||
phase = WebSocketConnectionPhase.openFrameSent;
|
||||
if (interceptor != null) {
|
||||
interceptor.interceptOpenResponse(response);
|
||||
}
|
||||
send(new WebsocketOpenElement(connectionInternal.connection.getXMPPServiceDomain()));
|
||||
send(new WebSocketOpenElement(connectionInternal.connection.getXMPPServiceDomain()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -107,7 +105,7 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
if (isOpenElement(text)) {
|
||||
// Converts the <open> element received into <stream> element.
|
||||
openStreamHeader = getStreamFromOpenElement(text);
|
||||
phase = WebsocketConnectionPhase.exchangingTopLevelStreamElements;
|
||||
phase = WebSocketConnectionPhase.exchangingTopLevelStreamElements;
|
||||
|
||||
try {
|
||||
connectionInternal.onStreamOpen(PacketParserUtils.getParserFor(openStreamHeader));
|
||||
|
@ -129,7 +127,7 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
@Override
|
||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||
LOGGER.log(Level.INFO, "Exception caught", t);
|
||||
WebsocketException websocketException = new WebsocketException(t);
|
||||
WebSocketException websocketException = new WebSocketException(t);
|
||||
if (connectionInternal.connection.isConnected()) {
|
||||
connectionInternal.notifyConnectionError(websocketException);
|
||||
} else {
|
||||
|
@ -139,7 +137,7 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
};
|
||||
|
||||
// Creates an instance of websocket through okHttpClient.
|
||||
currentWebsocket = okHttpClient.newWebSocket(request, listener);
|
||||
currentWebSocket = okHttpClient.newWebSocket(request, listener);
|
||||
|
||||
// Open a new stream and wait until features are received.
|
||||
connectionInternal.waitForFeaturesReceived("Waiting to receive features");
|
||||
|
@ -153,13 +151,13 @@ public final class OkHttpWebsocket extends AbstractWebsocket {
|
|||
if (interceptor != null) {
|
||||
interceptor.interceptSentText(textToBeSent);
|
||||
}
|
||||
currentWebsocket.send(textToBeSent);
|
||||
currentWebSocket.send(textToBeSent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(int code, String message) {
|
||||
currentWebsocket.close(code, message);
|
||||
LOGGER.log(Level.INFO, "Websocket has been closed with message: " + message);
|
||||
currentWebSocket.close(code, message);
|
||||
LOGGER.log(Level.INFO, "WebSocket has been closed with message: " + message);
|
||||
}
|
||||
|
||||
@Override
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 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.smack.websocket.okhttp;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
|
||||
import org.jivesoftware.smack.websocket.impl.WebSocketFactory;
|
||||
|
||||
public class OkHttpWebSocketFactory implements WebSocketFactory {
|
||||
|
||||
@Override
|
||||
public AbstractWebSocket create(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
return new OkHttpWebSocket(connectionInternal);
|
||||
}
|
||||
|
||||
}
|
|
@ -14,4 +14,4 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket.implementations.okhttp;
|
||||
package org.jivesoftware.smack.websocket.okhttp;
|
|
@ -0,0 +1 @@
|
|||
org.jivesoftware.smack.websocket.okhttp.OkHttpWebSocketFactory
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 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.smack.websocket.okhttp;
|
||||
|
||||
import org.jivesoftware.smack.websocket.test.WebSocketFactoryServiceTestUtil;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OkHttpWebSocketFactoryServiceTest {
|
||||
|
||||
@Test
|
||||
public void createWebSocketTest() {
|
||||
WebSocketFactoryServiceTestUtil.createWebSocketTest(OkHttpWebSocket.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
description = """\
|
||||
Smack for standard XMPP connections over Websockets."""
|
||||
Smack for XMPP connections over WebSocket (RFC 7395)."""
|
||||
|
||||
dependencies {
|
||||
compile project(':smack-core')
|
||||
api project(':smack-core')
|
||||
|
||||
testFixturesApi(testFixtures(project(":smack-core")))
|
||||
|
||||
implementation("com.squareup.okhttp3:okhttp:4.6.0")
|
||||
testFixturesApi(testFixtures(project(':smack-core')))
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Aditya Borikar
|
||||
* Copyright 2020 Aditya Borikar, Florian Schmaus.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,58 +16,48 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.websocket;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.EstablishingWebsocketConnectionState;
|
||||
import org.jivesoftware.smack.websocket.implementations.AbstractWebsocket;
|
||||
import org.jivesoftware.smack.websocket.implementations.WebsocketImplProvider;
|
||||
import org.jivesoftware.smack.websocket.implementations.okhttp.OkHttpWebsocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.EstablishingWebSocketConnectionState;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
|
||||
import org.jivesoftware.smack.websocket.impl.WebSocketFactoryService;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
|
||||
public final class WebsocketConnectionAttemptState {
|
||||
public final class WebSocketConnectionAttemptState {
|
||||
private final ModularXmppClientToServerConnectionInternal connectionInternal;
|
||||
private final XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints discoveredEndpoints;
|
||||
private final XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints discoveredEndpoints;
|
||||
|
||||
private WebsocketRemoteConnectionEndpoint connectedEndpoint;
|
||||
private WebSocketRemoteConnectionEndpoint connectedEndpoint;
|
||||
|
||||
WebsocketConnectionAttemptState(ModularXmppClientToServerConnectionInternal connectionInternal,
|
||||
XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints,
|
||||
EstablishingWebsocketConnectionState establishingWebsocketConnectionState) {
|
||||
assert discoveredWebsocketEndpoints != null;
|
||||
WebSocketConnectionAttemptState(ModularXmppClientToServerConnectionInternal connectionInternal,
|
||||
XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints discoveredWebSocketEndpoints,
|
||||
EstablishingWebSocketConnectionState establishingWebSocketConnectionState) {
|
||||
assert discoveredWebSocketEndpoints != null;
|
||||
this.connectionInternal = connectionInternal;
|
||||
this.discoveredEndpoints = discoveredWebsocketEndpoints;
|
||||
this.discoveredEndpoints = discoveredWebSocketEndpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish a websocket connection with one of the discoveredRemoteConnectionEndpoints.<br>
|
||||
*
|
||||
* @return {@link AbstractWebsocket} with which connection is establised
|
||||
* @return {@link AbstractWebSocket} with which connection is establised
|
||||
* @throws InterruptedException if the calling thread was interrupted
|
||||
* @throws WebsocketException if encounters a websocket exception
|
||||
* @throws WebSocketException if encounters a websocket exception
|
||||
*/
|
||||
AbstractWebsocket establishWebsocketConnection() throws InterruptedException, WebsocketException {
|
||||
List<WebsocketRemoteConnectionEndpoint> endpoints = discoveredEndpoints.result.discoveredRemoteConnectionEndpoints;
|
||||
AbstractWebSocket establishWebSocketConnection() throws InterruptedException, WebSocketException {
|
||||
List<WebSocketRemoteConnectionEndpoint> endpoints = discoveredEndpoints.result.discoveredRemoteConnectionEndpoints;
|
||||
|
||||
if (endpoints.isEmpty()) {
|
||||
throw new WebsocketException(new Throwable("No Endpoints discovered to establish connection"));
|
||||
throw new WebSocketException(new Throwable("No Endpoints discovered to establish connection"));
|
||||
}
|
||||
|
||||
List<Throwable> connectionFailureList = new ArrayList<>();
|
||||
AbstractWebsocket websocket;
|
||||
|
||||
try {
|
||||
// Obtain desired websocket implementation by using WebsocketImplProvider
|
||||
websocket = WebsocketImplProvider.getWebsocketImpl(OkHttpWebsocket.class, connectionInternal, discoveredEndpoints);
|
||||
} catch (NoSuchMethodException | SecurityException | InstantiationException |
|
||||
IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
|
||||
throw new WebsocketException(exception);
|
||||
}
|
||||
AbstractWebSocket websocket = WebSocketFactoryService.createWebSocket(connectionInternal);
|
||||
|
||||
// Keep iterating over available endpoints until a connection is establised or all endpoints are tried to create a connection with.
|
||||
for (WebsocketRemoteConnectionEndpoint endpoint : endpoints) {
|
||||
for (WebSocketRemoteConnectionEndpoint endpoint : endpoints) {
|
||||
try {
|
||||
websocket.connect(endpoint);
|
||||
connectedEndpoint = endpoint;
|
||||
|
@ -78,8 +68,8 @@ public final class WebsocketConnectionAttemptState {
|
|||
// If the number of entries in connectionFailureList is equal to the number of endpoints,
|
||||
// it means that all endpoints have been tried and have been unsuccessful.
|
||||
if (connectionFailureList.size() == endpoints.size()) {
|
||||
WebsocketException websocketException = new WebsocketException(connectionFailureList);
|
||||
throw new WebsocketException(websocketException);
|
||||
WebSocketException websocketException = new WebSocketException(connectionFailureList);
|
||||
throw new WebSocketException(websocketException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -95,7 +85,7 @@ public final class WebsocketConnectionAttemptState {
|
|||
*
|
||||
* @return connected websocket endpoint
|
||||
*/
|
||||
public WebsocketRemoteConnectionEndpoint getConnectedEndpoint() {
|
||||
public WebSocketRemoteConnectionEndpoint getConnectedEndpoint() {
|
||||
return connectedEndpoint;
|
||||
}
|
||||
}
|
|
@ -19,16 +19,16 @@ package org.jivesoftware.smack.websocket;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class WebsocketException extends Exception {
|
||||
public final class WebSocketException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final List<Throwable> throwableList;
|
||||
|
||||
public WebsocketException(List<Throwable> throwableList) {
|
||||
public WebSocketException(List<Throwable> throwableList) {
|
||||
this.throwableList = throwableList;
|
||||
}
|
||||
|
||||
public WebsocketException(Throwable throwable) {
|
||||
public WebSocketException(Throwable throwable) {
|
||||
this.throwableList = Collections.singletonList(throwable);
|
||||
}
|
||||
|
|
@ -19,10 +19,10 @@ package org.jivesoftware.smack.websocket;
|
|||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.initializer.UrlInitializer;
|
||||
|
||||
public final class WebsocketInitializer extends UrlInitializer {
|
||||
public final class WebSocketInitializer extends UrlInitializer {
|
||||
|
||||
static {
|
||||
SmackConfiguration.addModule(XmppWebsocketTransportModuleDescriptor.class);
|
||||
SmackConfiguration.addModule(XmppWebSocketTransportModuleDescriptor.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -50,13 +50,13 @@ import org.jivesoftware.smack.packet.AbstractStreamOpen;
|
|||
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
import org.jivesoftware.smack.websocket.elements.WebsocketCloseElement;
|
||||
import org.jivesoftware.smack.websocket.elements.WebsocketOpenElement;
|
||||
import org.jivesoftware.smack.websocket.implementations.AbstractWebsocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpointLookup;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpointLookup.Result;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints;
|
||||
import org.jivesoftware.smack.websocket.elements.WebSocketCloseElement;
|
||||
import org.jivesoftware.smack.websocket.elements.WebSocketOpenElement;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpointLookup;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpointLookup.Result;
|
||||
|
||||
import org.jxmpp.jid.DomainBareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
|
@ -65,44 +65,44 @@ import org.jxmpp.stringprep.XmppStringprepException;
|
|||
/**
|
||||
* The websocket transport module that goes with Smack's modular architecture.
|
||||
*/
|
||||
public final class XmppWebsocketTransportModule
|
||||
extends ModularXmppClientToServerConnectionModule<XmppWebsocketTransportModuleDescriptor> {
|
||||
private final XmppWebsocketTransport websocketTransport;
|
||||
public final class XmppWebSocketTransportModule
|
||||
extends ModularXmppClientToServerConnectionModule<XmppWebSocketTransportModuleDescriptor> {
|
||||
private final XmppWebSocketTransport websocketTransport;
|
||||
|
||||
private AbstractWebsocket websocket;
|
||||
private AbstractWebSocket websocket;
|
||||
|
||||
protected XmppWebsocketTransportModule(XmppWebsocketTransportModuleDescriptor moduleDescriptor,
|
||||
protected XmppWebSocketTransportModule(XmppWebSocketTransportModuleDescriptor moduleDescriptor,
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
super(moduleDescriptor, connectionInternal);
|
||||
|
||||
websocketTransport = new XmppWebsocketTransport(connectionInternal);
|
||||
websocketTransport = new XmppWebSocketTransport(connectionInternal);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XmppWebsocketTransport getTransport() {
|
||||
protected XmppWebSocketTransport getTransport() {
|
||||
return websocketTransport;
|
||||
}
|
||||
|
||||
static final class EstablishingWebsocketConnectionStateDescriptor extends StateDescriptor {
|
||||
private EstablishingWebsocketConnectionStateDescriptor() {
|
||||
super(XmppWebsocketTransportModule.EstablishingWebsocketConnectionState.class);
|
||||
static final class EstablishingWebSocketConnectionStateDescriptor extends StateDescriptor {
|
||||
private EstablishingWebSocketConnectionStateDescriptor() {
|
||||
super(XmppWebSocketTransportModule.EstablishingWebSocketConnectionState.class);
|
||||
addPredeccessor(LookupRemoteConnectionEndpointsStateDescriptor.class);
|
||||
addSuccessor(ConnectedButUnauthenticatedStateDescriptor.class);
|
||||
|
||||
// This states preference to TCP transports over this Websocket transport implementation.
|
||||
// This states preference to TCP transports over this WebSocket transport implementation.
|
||||
declareInferiorityTo("org.jivesoftware.smack.tcp.XmppTcpTransportModule$EstablishingTcpConnectionStateDescriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected State constructState(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
XmppWebsocketTransportModule websocketTransportModule = connectionInternal.connection.getConnectionModuleFor(
|
||||
XmppWebsocketTransportModuleDescriptor.class);
|
||||
return websocketTransportModule.constructEstablishingWebsocketConnectionState(this, connectionInternal);
|
||||
XmppWebSocketTransportModule websocketTransportModule = connectionInternal.connection.getConnectionModuleFor(
|
||||
XmppWebSocketTransportModuleDescriptor.class);
|
||||
return websocketTransportModule.constructEstablishingWebSocketConnectionState(this, connectionInternal);
|
||||
}
|
||||
}
|
||||
|
||||
final class EstablishingWebsocketConnectionState extends State {
|
||||
protected EstablishingWebsocketConnectionState(StateDescriptor stateDescriptor,
|
||||
final class EstablishingWebSocketConnectionState extends State {
|
||||
protected EstablishingWebSocketConnectionState(StateDescriptor stateDescriptor,
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
super(stateDescriptor, connectionInternal);
|
||||
}
|
||||
|
@ -110,78 +110,78 @@ public final class XmppWebsocketTransportModule
|
|||
@Override
|
||||
public AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext)
|
||||
throws IOException, SmackException, InterruptedException, XMPPException {
|
||||
WebsocketConnectionAttemptState connectionAttemptState = new WebsocketConnectionAttemptState(
|
||||
connectionInternal, discoveredWebsocketEndpoints, this);
|
||||
WebSocketConnectionAttemptState connectionAttemptState = new WebSocketConnectionAttemptState(
|
||||
connectionInternal, discoveredWebSocketEndpoints, this);
|
||||
|
||||
try {
|
||||
websocket = connectionAttemptState.establishWebsocketConnection();
|
||||
} catch (InterruptedException | WebsocketException e) {
|
||||
websocket = connectionAttemptState.establishWebSocketConnection();
|
||||
} catch (InterruptedException | WebSocketException e) {
|
||||
StateTransitionResult.Failure failure = new StateTransitionResult.FailureCausedByException<Exception>(e);
|
||||
return failure;
|
||||
}
|
||||
|
||||
connectionInternal.setTransport(websocketTransport);
|
||||
|
||||
WebsocketRemoteConnectionEndpoint connectedEndpoint = connectionAttemptState.getConnectedEndpoint();
|
||||
WebSocketRemoteConnectionEndpoint connectedEndpoint = connectionAttemptState.getConnectedEndpoint();
|
||||
|
||||
// Construct a WebsocketConnectedResult using the connected endpoint.
|
||||
return new WebsocketConnectedResult(connectedEndpoint);
|
||||
// Construct a WebSocketConnectedResult using the connected endpoint.
|
||||
return new WebSocketConnectedResult(connectedEndpoint);
|
||||
}
|
||||
}
|
||||
|
||||
public EstablishingWebsocketConnectionState constructEstablishingWebsocketConnectionState(
|
||||
EstablishingWebsocketConnectionStateDescriptor establishingWebsocketConnectionStateDescriptor,
|
||||
public EstablishingWebSocketConnectionState constructEstablishingWebSocketConnectionState(
|
||||
EstablishingWebSocketConnectionStateDescriptor establishingWebSocketConnectionStateDescriptor,
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
return new EstablishingWebsocketConnectionState(establishingWebsocketConnectionStateDescriptor,
|
||||
return new EstablishingWebSocketConnectionState(establishingWebSocketConnectionStateDescriptor,
|
||||
connectionInternal);
|
||||
}
|
||||
|
||||
public static final class WebsocketConnectedResult extends StateTransitionResult.Success {
|
||||
final WebsocketRemoteConnectionEndpoint connectedEndpoint;
|
||||
public static final class WebSocketConnectedResult extends StateTransitionResult.Success {
|
||||
final WebSocketRemoteConnectionEndpoint connectedEndpoint;
|
||||
|
||||
public WebsocketConnectedResult(WebsocketRemoteConnectionEndpoint connectedEndpoint) {
|
||||
super("Websocket connection establised with endpoint: " + connectedEndpoint.getWebsocketEndpoint());
|
||||
public WebSocketConnectedResult(WebSocketRemoteConnectionEndpoint connectedEndpoint) {
|
||||
super("WebSocket connection establised with endpoint: " + connectedEndpoint.getWebSocketEndpoint());
|
||||
this.connectedEndpoint = connectedEndpoint;
|
||||
}
|
||||
}
|
||||
|
||||
private DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints;
|
||||
private DiscoveredWebSocketEndpoints discoveredWebSocketEndpoints;
|
||||
|
||||
/**
|
||||
* Transport class for {@link ModularXmppClientToServerConnectionModule}'s websocket implementation.
|
||||
*/
|
||||
public final class XmppWebsocketTransport extends XmppClientToServerTransport {
|
||||
public final class XmppWebSocketTransport extends XmppClientToServerTransport {
|
||||
|
||||
AsyncButOrdered<Queue<TopLevelStreamElement>> asyncButOrderedOutgoingElementsQueue;
|
||||
|
||||
protected XmppWebsocketTransport(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
protected XmppWebSocketTransport(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
super(connectionInternal);
|
||||
asyncButOrderedOutgoingElementsQueue = new AsyncButOrdered<Queue<TopLevelStreamElement>>();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetDiscoveredConnectionEndpoints() {
|
||||
discoveredWebsocketEndpoints = null;
|
||||
discoveredWebSocketEndpoints = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SmackFuture<LookupConnectionEndpointsResult, Exception>> lookupConnectionEndpoints() {
|
||||
// Assert that there are no stale discovered endpoints prior performing the lookup.
|
||||
assert discoveredWebsocketEndpoints == null;
|
||||
assert discoveredWebSocketEndpoints == null;
|
||||
|
||||
InternalSmackFuture<LookupConnectionEndpointsResult, Exception> websocketEndpointsLookupFuture = new InternalSmackFuture<>();
|
||||
|
||||
connectionInternal.asyncGo(() -> {
|
||||
|
||||
WebsocketRemoteConnectionEndpoint providedEndpoint = null;
|
||||
WebSocketRemoteConnectionEndpoint providedEndpoint = null;
|
||||
|
||||
// Check if there is a websocket endpoint already configured.
|
||||
URI uri = moduleDescriptor.getExplicitlyProvidedUri();
|
||||
if (uri != null) {
|
||||
providedEndpoint = new WebsocketRemoteConnectionEndpoint(uri);
|
||||
providedEndpoint = new WebSocketRemoteConnectionEndpoint(uri);
|
||||
}
|
||||
|
||||
if (!moduleDescriptor.isWebsocketEndpointDiscoveryEnabled()) {
|
||||
if (!moduleDescriptor.isWebSocketEndpointDiscoveryEnabled()) {
|
||||
// If discovery is disabled, assert that the provided endpoint isn't null.
|
||||
assert providedEndpoint != null;
|
||||
|
||||
|
@ -190,14 +190,14 @@ public final class XmppWebsocketTransportModule
|
|||
mode.equals(SecurityMode.disabled))
|
||||
|| (!providedEndpoint.isSecureEndpoint() &&
|
||||
mode.equals(SecurityMode.required))) {
|
||||
throw new IllegalStateException("Explicitly configured uri: " + providedEndpoint.getWebsocketEndpoint().toString()
|
||||
throw new IllegalStateException("Explicitly configured uri: " + providedEndpoint.getWebSocketEndpoint().toString()
|
||||
+ " does not comply with the configured security mode: " + mode);
|
||||
}
|
||||
|
||||
// Generate Result for explicitly configured endpoint.
|
||||
Result manualResult = new Result(Arrays.asList(providedEndpoint), null);
|
||||
|
||||
LookupConnectionEndpointsResult endpointsResult = new DiscoveredWebsocketEndpoints(manualResult);
|
||||
LookupConnectionEndpointsResult endpointsResult = new DiscoveredWebSocketEndpoints(manualResult);
|
||||
|
||||
websocketEndpointsLookupFuture.setResult(endpointsResult);
|
||||
} else {
|
||||
|
@ -206,14 +206,14 @@ public final class XmppWebsocketTransportModule
|
|||
SecurityMode mode = configuration.getSecurityMode();
|
||||
|
||||
// Fetch remote endpoints.
|
||||
Result xep0156result = WebsocketRemoteConnectionEndpointLookup.lookup(host, mode);
|
||||
Result xep0156result = WebSocketRemoteConnectionEndpointLookup.lookup(host, mode);
|
||||
|
||||
List<WebsocketRemoteConnectionEndpoint> discoveredEndpoints = xep0156result.discoveredRemoteConnectionEndpoints;
|
||||
List<WebSocketRemoteConnectionEndpoint> discoveredEndpoints = xep0156result.discoveredRemoteConnectionEndpoints;
|
||||
|
||||
// Generate result considering both manual and fetched endpoints.
|
||||
Result finalResult = new Result(discoveredEndpoints, xep0156result.getLookupFailures());
|
||||
|
||||
LookupConnectionEndpointsResult endpointsResult = new DiscoveredWebsocketEndpoints(finalResult);
|
||||
LookupConnectionEndpointsResult endpointsResult = new DiscoveredWebSocketEndpoints(finalResult);
|
||||
|
||||
websocketEndpointsLookupFuture.setResult(endpointsResult);
|
||||
}
|
||||
|
@ -224,7 +224,7 @@ public final class XmppWebsocketTransportModule
|
|||
|
||||
@Override
|
||||
protected void loadConnectionEndpoints(LookupConnectionEndpointsSuccess lookupConnectionEndpointsSuccess) {
|
||||
discoveredWebsocketEndpoints = (DiscoveredWebsocketEndpoints) lookupConnectionEndpointsSuccess;
|
||||
discoveredWebSocketEndpoints = (DiscoveredWebSocketEndpoints) lookupConnectionEndpointsSuccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -233,7 +233,7 @@ public final class XmppWebsocketTransportModule
|
|||
|
||||
@Override
|
||||
protected void disconnect() {
|
||||
websocket.disconnect(1000, "Websocket closed normally");
|
||||
websocket.disconnect(1000, "WebSocket closed normally");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -272,7 +272,7 @@ public final class XmppWebsocketTransportModule
|
|||
@Override
|
||||
public AbstractStreamOpen createStreamOpen(CharSequence to, CharSequence from, String id, String lang) {
|
||||
try {
|
||||
return new WebsocketOpenElement(JidCreate.domainBareFrom(to));
|
||||
return new WebSocketOpenElement(JidCreate.domainBareFrom(to));
|
||||
} catch (XmppStringprepException e) {
|
||||
Logger.getAnonymousLogger().log(Level.WARNING, "Couldn't create OpenElement", e);
|
||||
return null;
|
||||
|
@ -280,7 +280,7 @@ public final class XmppWebsocketTransportModule
|
|||
}
|
||||
@Override
|
||||
public AbstractStreamClose createStreamClose() {
|
||||
return new WebsocketCloseElement();
|
||||
return new WebSocketCloseElement();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -288,15 +288,15 @@ public final class XmppWebsocketTransportModule
|
|||
/**
|
||||
* Contains {@link Result} for successfully discovered endpoints.
|
||||
*/
|
||||
public final class DiscoveredWebsocketEndpoints implements LookupConnectionEndpointsSuccess {
|
||||
final WebsocketRemoteConnectionEndpointLookup.Result result;
|
||||
public final class DiscoveredWebSocketEndpoints implements LookupConnectionEndpointsSuccess {
|
||||
final WebSocketRemoteConnectionEndpointLookup.Result result;
|
||||
|
||||
DiscoveredWebsocketEndpoints(Result result) {
|
||||
DiscoveredWebSocketEndpoints(Result result) {
|
||||
assert result != null;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public WebsocketRemoteConnectionEndpointLookup.Result getResult() {
|
||||
public WebSocketRemoteConnectionEndpointLookup.Result getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -305,11 +305,11 @@ public final class XmppWebsocketTransportModule
|
|||
* Contains list of {@link RemoteConnectionEndpointLookupFailure} when no endpoint
|
||||
* could be found during http lookup.
|
||||
*/
|
||||
final class WebsocketEndpointsDiscoveryFailed implements LookupConnectionEndpointsFailed {
|
||||
final class WebSocketEndpointsDiscoveryFailed implements LookupConnectionEndpointsFailed {
|
||||
final List<RemoteConnectionEndpointLookupFailure> lookupFailures;
|
||||
|
||||
WebsocketEndpointsDiscoveryFailed(
|
||||
WebsocketRemoteConnectionEndpointLookup.Result result) {
|
||||
WebSocketEndpointsDiscoveryFailed(
|
||||
WebSocketRemoteConnectionEndpointLookup.Result result) {
|
||||
assert result != null;
|
||||
lookupFailures = Collections.unmodifiableList(result.lookupFailures);
|
||||
}
|
|
@ -28,20 +28,20 @@ import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescr
|
|||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.fsm.StateDescriptor;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.EstablishingWebsocketConnectionStateDescriptor;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.EstablishingWebSocketConnectionStateDescriptor;
|
||||
|
||||
/**
|
||||
* The descriptor class for {@link XmppWebsocketTransportModule}.
|
||||
* The descriptor class for {@link XmppWebSocketTransportModule}.
|
||||
* <br>
|
||||
* To add {@link XmppWebsocketTransportModule} to {@link ModularXmppClientToServerConnection},
|
||||
* To add {@link XmppWebSocketTransportModule} to {@link ModularXmppClientToServerConnection},
|
||||
* use {@link ModularXmppClientToServerConnectionConfiguration.Builder#addModule(ModularXmppClientToServerConnectionModuleDescriptor)}.
|
||||
*/
|
||||
public final class XmppWebsocketTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
|
||||
private boolean performWebsocketEndpointDiscovery;
|
||||
public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
|
||||
private boolean performWebSocketEndpointDiscovery;
|
||||
private URI uri;
|
||||
|
||||
public XmppWebsocketTransportModuleDescriptor(Builder builder) {
|
||||
this.performWebsocketEndpointDiscovery = builder.performWebsocketEndpointDiscovery;
|
||||
public XmppWebSocketTransportModuleDescriptor(Builder builder) {
|
||||
this.performWebSocketEndpointDiscovery = builder.performWebSocketEndpointDiscovery;
|
||||
this.uri = builder.uri;
|
||||
}
|
||||
|
||||
|
@ -49,8 +49,8 @@ public final class XmppWebsocketTransportModuleDescriptor extends ModularXmppCli
|
|||
* Returns true if websocket endpoint discovery is true, returns false otherwise.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isWebsocketEndpointDiscoveryEnabled() {
|
||||
return performWebsocketEndpointDiscovery;
|
||||
public boolean isWebSocketEndpointDiscoveryEnabled() {
|
||||
return performWebSocketEndpointDiscovery;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,14 +64,14 @@ public final class XmppWebsocketTransportModuleDescriptor extends ModularXmppCli
|
|||
@Override
|
||||
protected Set<Class<? extends StateDescriptor>> getStateDescriptors() {
|
||||
Set<Class<? extends StateDescriptor>> res = new HashSet<>();
|
||||
res.add(EstablishingWebsocketConnectionStateDescriptor.class);
|
||||
res.add(EstablishingWebSocketConnectionStateDescriptor.class);
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ModularXmppClientToServerConnectionModule<? extends ModularXmppClientToServerConnectionModuleDescriptor> constructXmppConnectionModule(
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
return new XmppWebsocketTransportModule(this, connectionInternal);
|
||||
return new XmppWebSocketTransportModule(this, connectionInternal);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,19 +86,19 @@ public final class XmppWebsocketTransportModuleDescriptor extends ModularXmppCli
|
|||
}
|
||||
|
||||
/**
|
||||
* Builder class for {@link XmppWebsocketTransportModuleDescriptor}.
|
||||
* Builder class for {@link XmppWebSocketTransportModuleDescriptor}.
|
||||
* <br>
|
||||
* To obtain an instance of {@link XmppWebsocketTransportModuleDescriptor.Builder}, use {@link XmppWebsocketTransportModuleDescriptor#getBuilder(ModularXmppClientToServerConnectionConfiguration.Builder)} method.
|
||||
* To obtain an instance of {@link XmppWebSocketTransportModuleDescriptor.Builder}, use {@link XmppWebSocketTransportModuleDescriptor#getBuilder(ModularXmppClientToServerConnectionConfiguration.Builder)} method.
|
||||
* <br>
|
||||
* Use {@link Builder#explicitlySetWebsocketEndpoint(URI)} to configure the URI of an endpoint as a backup in case connection couldn't be established with endpoints through http lookup.
|
||||
* Use {@link Builder#explicitlySetWebSocketEndpoint(URI)} to configure the URI of an endpoint as a backup in case connection couldn't be established with endpoints through http lookup.
|
||||
* <br>
|
||||
* Use {@link Builder#explicitlySetWebsocketEndpointAndDiscovery(URI, boolean)} to configure endpoint and disallow websocket endpoint discovery through http lookup.
|
||||
* By default, {@link Builder#performWebsocketEndpointDiscovery} is set to true.
|
||||
* Use {@link Builder#explicitlySetWebSocketEndpointAndDiscovery(URI, boolean)} to configure endpoint and disallow websocket endpoint discovery through http lookup.
|
||||
* By default, {@link Builder#performWebSocketEndpointDiscovery} is set to true.
|
||||
* <br>
|
||||
* Use {@link Builder#build()} to obtain {@link XmppWebsocketTransportModuleDescriptor}.
|
||||
* Use {@link Builder#build()} to obtain {@link XmppWebSocketTransportModuleDescriptor}.
|
||||
*/
|
||||
public static final class Builder extends ModularXmppClientToServerConnectionModuleDescriptor.Builder {
|
||||
private boolean performWebsocketEndpointDiscovery = true;
|
||||
private boolean performWebSocketEndpointDiscovery = true;
|
||||
private URI uri;
|
||||
|
||||
private Builder(
|
||||
|
@ -106,31 +106,31 @@ public final class XmppWebsocketTransportModuleDescriptor extends ModularXmppCli
|
|||
super(connectionConfigurationBuilder);
|
||||
}
|
||||
|
||||
public Builder explicitlySetWebsocketEndpoint(URI endpoint) {
|
||||
return explicitlySetWebsocketEndpointAndDiscovery(endpoint, true);
|
||||
public Builder explicitlySetWebSocketEndpoint(URI endpoint) {
|
||||
return explicitlySetWebSocketEndpointAndDiscovery(endpoint, true);
|
||||
}
|
||||
|
||||
public Builder explicitlySetWebsocketEndpointAndDiscovery(URI endpoint, boolean performWebsocketEndpointDiscovery) {
|
||||
public Builder explicitlySetWebSocketEndpointAndDiscovery(URI endpoint, boolean performWebSocketEndpointDiscovery) {
|
||||
Objects.requireNonNull(endpoint, "Provided endpoint URI must not be null");
|
||||
this.uri = endpoint;
|
||||
this.performWebsocketEndpointDiscovery = performWebsocketEndpointDiscovery;
|
||||
this.performWebSocketEndpointDiscovery = performWebSocketEndpointDiscovery;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder explicitlySetWebsocketEndpoint(CharSequence endpoint) throws URISyntaxException {
|
||||
public Builder explicitlySetWebSocketEndpoint(CharSequence endpoint) throws URISyntaxException {
|
||||
URI endpointUri = new URI(endpoint.toString());
|
||||
return explicitlySetWebsocketEndpointAndDiscovery(endpointUri, true);
|
||||
return explicitlySetWebSocketEndpointAndDiscovery(endpointUri, true);
|
||||
}
|
||||
|
||||
public Builder explicitlySetWebsocketEndpoint(CharSequence endpoint, boolean performWebsocketEndpointDiscovery)
|
||||
public Builder explicitlySetWebSocketEndpoint(CharSequence endpoint, boolean performWebSocketEndpointDiscovery)
|
||||
throws URISyntaxException {
|
||||
URI endpointUri = new URI(endpoint.toString());
|
||||
return explicitlySetWebsocketEndpointAndDiscovery(endpointUri, performWebsocketEndpointDiscovery);
|
||||
return explicitlySetWebSocketEndpointAndDiscovery(endpointUri, performWebSocketEndpointDiscovery);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModularXmppClientToServerConnectionModuleDescriptor build() {
|
||||
return new XmppWebsocketTransportModuleDescriptor(this);
|
||||
return new XmppWebSocketTransportModuleDescriptor(this);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,12 +22,12 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
|
|||
|
||||
import org.jxmpp.jid.DomainBareJid;
|
||||
|
||||
public abstract class AbstractWebsocketNonza implements Nonza {
|
||||
public abstract class AbstractWebSocketNonza implements Nonza {
|
||||
public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-framing";
|
||||
private static final String VERSION = "1.0";
|
||||
private final DomainBareJid to;
|
||||
|
||||
public AbstractWebsocketNonza(DomainBareJid jid) {
|
||||
public AbstractWebSocketNonza(DomainBareJid jid) {
|
||||
this.to = jid;
|
||||
}
|
||||
|
|
@ -22,12 +22,12 @@ import org.jivesoftware.smack.packet.AbstractStreamClose;
|
|||
import org.jivesoftware.smack.packet.XmlEnvironment;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
public final class WebsocketCloseElement extends AbstractStreamClose {
|
||||
public final class WebSocketCloseElement extends AbstractStreamClose {
|
||||
public static final String ELEMENT = "close";
|
||||
public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-framing";
|
||||
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
|
||||
|
||||
public WebsocketCloseElement() {
|
||||
public WebSocketCloseElement() {
|
||||
}
|
||||
|
||||
@Override
|
|
@ -25,12 +25,12 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
|
|||
|
||||
import org.jxmpp.jid.DomainBareJid;
|
||||
|
||||
public final class WebsocketOpenElement extends AbstractStreamOpen {
|
||||
public final class WebSocketOpenElement extends AbstractStreamOpen {
|
||||
public static final String ELEMENT = "open";
|
||||
public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-framing";
|
||||
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
|
||||
|
||||
public WebsocketOpenElement(DomainBareJid to) {
|
||||
public WebSocketOpenElement(DomainBareJid to) {
|
||||
super(to, null, null, null, StreamContentNamespace.client);
|
||||
}
|
||||
|
|
@ -14,16 +14,16 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket.implementations;
|
||||
package org.jivesoftware.smack.websocket.impl;
|
||||
|
||||
import javax.net.ssl.SSLSession;
|
||||
|
||||
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
|
||||
public abstract class AbstractWebsocket {
|
||||
public abstract class AbstractWebSocket {
|
||||
|
||||
protected enum WebsocketConnectionPhase {
|
||||
protected enum WebSocketConnectionPhase {
|
||||
openFrameSent,
|
||||
exchangingTopLevelStreamElements
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public abstract class AbstractWebsocket {
|
|||
return false;
|
||||
}
|
||||
|
||||
public abstract void connect(WebsocketRemoteConnectionEndpoint endpoint) throws Throwable;
|
||||
public abstract void connect(WebSocketRemoteConnectionEndpoint endpoint) throws Throwable;
|
||||
|
||||
public abstract void send(TopLevelStreamElement element);
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 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.smack.websocket.impl;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
|
||||
public interface WebSocketFactory {
|
||||
|
||||
AbstractWebSocket create(ModularXmppClientToServerConnectionInternal connectionInternal);
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 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.smack.websocket.impl;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
|
||||
public final class WebSocketFactoryService {
|
||||
|
||||
private static final ServiceLoader<WebSocketFactory> SERVICE_LOADER = ServiceLoader.load(WebSocketFactory.class);
|
||||
|
||||
public static AbstractWebSocket createWebSocket(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
assert connectionInternal != null;
|
||||
|
||||
Iterator<WebSocketFactory> websocketFactories = SERVICE_LOADER.iterator();
|
||||
if (!websocketFactories.hasNext()) {
|
||||
throw new IllegalStateException("No smack websocket service configured");
|
||||
}
|
||||
|
||||
WebSocketFactory websocketFactory = websocketFactories.next();
|
||||
return websocketFactory.create(connectionInternal);
|
||||
}
|
||||
|
||||
}
|
|
@ -17,4 +17,4 @@
|
|||
/**
|
||||
* This package contains websocket implementations to be plugged inside websocket transport.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket.implementations;
|
||||
package org.jivesoftware.smack.websocket.impl;
|
|
@ -1,35 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* 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.websocket.implementations;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
|
||||
public final class WebsocketImplProvider {
|
||||
|
||||
public static AbstractWebsocket getWebsocketImpl(Class<? extends AbstractWebsocket> websocketImpl, ModularXmppClientToServerConnectionInternal connectionInternal, DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||
Objects.requireNonNull(connectionInternal, "ConnectionInternal cannot be null");
|
||||
|
||||
// Creates an instance of the constructor for the desired websocket implementation.
|
||||
Constructor<? extends AbstractWebsocket> constructor = websocketImpl.getConstructor(ModularXmppClientToServerConnectionInternal.class, DiscoveredWebsocketEndpoints.class);
|
||||
return (AbstractWebsocket) constructor.newInstance(connectionInternal, discoveredWebsocketEndpoints);
|
||||
}
|
||||
}
|
|
@ -15,6 +15,6 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* Websocket related classes for Smack.
|
||||
* WebSocket related classes for Smack.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket;
|
||||
|
|
|
@ -28,17 +28,17 @@ import java.util.logging.Logger;
|
|||
import org.jivesoftware.smack.datatypes.UInt16;
|
||||
import org.jivesoftware.smack.util.rce.RemoteConnectionEndpoint;
|
||||
|
||||
public final class WebsocketRemoteConnectionEndpoint implements RemoteConnectionEndpoint {
|
||||
public final class WebSocketRemoteConnectionEndpoint implements RemoteConnectionEndpoint {
|
||||
|
||||
private static final Logger LOGGER = Logger.getAnonymousLogger();
|
||||
|
||||
private final URI uri;
|
||||
|
||||
public WebsocketRemoteConnectionEndpoint(String uri) throws URISyntaxException {
|
||||
public WebSocketRemoteConnectionEndpoint(String uri) throws URISyntaxException {
|
||||
this(new URI(uri));
|
||||
}
|
||||
|
||||
public WebsocketRemoteConnectionEndpoint(URI uri) {
|
||||
public WebSocketRemoteConnectionEndpoint(URI uri) {
|
||||
this.uri = uri;
|
||||
String scheme = uri.getScheme();
|
||||
if (!(scheme.equals("ws") || scheme.equals("wss"))) {
|
||||
|
@ -46,7 +46,7 @@ public final class WebsocketRemoteConnectionEndpoint implements RemoteConnection
|
|||
}
|
||||
}
|
||||
|
||||
public URI getWebsocketEndpoint() {
|
||||
public URI getWebSocketEndpoint() {
|
||||
return uri;
|
||||
}
|
||||
|
|
@ -31,11 +31,11 @@ import org.jivesoftware.smack.xml.XmlPullParserException;
|
|||
|
||||
import org.jxmpp.jid.DomainBareJid;
|
||||
|
||||
public final class WebsocketRemoteConnectionEndpointLookup {
|
||||
public final class WebSocketRemoteConnectionEndpointLookup {
|
||||
|
||||
public static Result lookup(DomainBareJid domainBareJid, SecurityMode securityMode) {
|
||||
List<RemoteConnectionEndpointLookupFailure> lookupFailures = new ArrayList<>(1);
|
||||
List<WebsocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints = new ArrayList<>();
|
||||
List<WebSocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints = new ArrayList<>();
|
||||
|
||||
List<URI> rcUriList = null;
|
||||
try {
|
||||
|
@ -52,11 +52,11 @@ public final class WebsocketRemoteConnectionEndpointLookup {
|
|||
throw new IllegalStateException("No endpoints were found inside host-meta");
|
||||
}
|
||||
|
||||
// Convert rcUriList to List<WebsocketRemoteConnectionEndpoint>
|
||||
// Convert rcUriList to List<WebSocketRemoteConnectionEndpoint>
|
||||
Iterator<URI> iterator = rcUriList.iterator();
|
||||
List<WebsocketRemoteConnectionEndpoint> rceList = new ArrayList<>();
|
||||
List<WebSocketRemoteConnectionEndpoint> rceList = new ArrayList<>();
|
||||
while (iterator.hasNext()) {
|
||||
rceList.add(new WebsocketRemoteConnectionEndpoint(iterator.next()));
|
||||
rceList.add(new WebSocketRemoteConnectionEndpoint(iterator.next()));
|
||||
}
|
||||
|
||||
switch (securityMode) {
|
||||
|
@ -64,9 +64,9 @@ public final class WebsocketRemoteConnectionEndpointLookup {
|
|||
// If security mode equals `if-possible`, give priority to secure endpoints over insecure endpoints.
|
||||
|
||||
// Seprate secure and unsecure endpoints.
|
||||
List<WebsocketRemoteConnectionEndpoint> secureEndpointsForSecurityModeIfPossible = new ArrayList<>();
|
||||
List<WebsocketRemoteConnectionEndpoint> insecureEndpointsForSecurityModeIfPossible = new ArrayList<>();
|
||||
for (WebsocketRemoteConnectionEndpoint uri : rceList) {
|
||||
List<WebSocketRemoteConnectionEndpoint> secureEndpointsForSecurityModeIfPossible = new ArrayList<>();
|
||||
List<WebSocketRemoteConnectionEndpoint> insecureEndpointsForSecurityModeIfPossible = new ArrayList<>();
|
||||
for (WebSocketRemoteConnectionEndpoint uri : rceList) {
|
||||
if (uri.isSecureEndpoint()) {
|
||||
secureEndpointsForSecurityModeIfPossible.add(uri);
|
||||
} else {
|
||||
|
@ -82,7 +82,7 @@ public final class WebsocketRemoteConnectionEndpointLookup {
|
|||
* If, SecurityMode equals to required, accept wss endpoints (secure endpoints) only or,
|
||||
* if SecurityMode equals to disabled, accept ws endpoints (unsecure endpoints) only.
|
||||
*/
|
||||
for (WebsocketRemoteConnectionEndpoint uri : rceList) {
|
||||
for (WebSocketRemoteConnectionEndpoint uri : rceList) {
|
||||
if ((securityMode.equals(SecurityMode.disabled) && !uri.isSecureEndpoint())
|
||||
|| (securityMode.equals(SecurityMode.required) && uri.isSecureEndpoint())) {
|
||||
discoveredRemoteConnectionEndpoints.add(uri);
|
||||
|
@ -95,16 +95,16 @@ public final class WebsocketRemoteConnectionEndpointLookup {
|
|||
}
|
||||
|
||||
public static final class Result {
|
||||
public final List<WebsocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints;
|
||||
public final List<WebSocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints;
|
||||
public final List<RemoteConnectionEndpointLookupFailure> lookupFailures;
|
||||
|
||||
public Result(List<WebsocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints,
|
||||
public Result(List<WebSocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints,
|
||||
List<RemoteConnectionEndpointLookupFailure> lookupFailures) {
|
||||
this.discoveredRemoteConnectionEndpoints = discoveredRemoteConnectionEndpoints;
|
||||
this.lookupFailures = lookupFailures;
|
||||
}
|
||||
|
||||
public List<WebsocketRemoteConnectionEndpoint> getDiscoveredRemoteConnectionEndpoints() {
|
||||
public List<WebSocketRemoteConnectionEndpoint> getDiscoveredRemoteConnectionEndpoints() {
|
||||
return discoveredRemoteConnectionEndpoints;
|
||||
}
|
||||
|
|
@ -20,9 +20,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class WebsocketConnectionAttemptStateTest {
|
||||
public class WebSocketConnectionAttemptStateTest {
|
||||
@Test
|
||||
public void constructorTest() {
|
||||
assertThrows(AssertionError.class, () -> new WebsocketConnectionAttemptState(null, null, null));
|
||||
assertThrows(AssertionError.class, () -> new WebSocketConnectionAttemptState(null, null, null));
|
||||
}
|
||||
}
|
|
@ -22,10 +22,10 @@ import java.util.List;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class WebsocketInitializerTest {
|
||||
public class WebSocketInitializerTest {
|
||||
@Test
|
||||
public void testExtensionInitializer() {
|
||||
WebsocketInitializer initializer = new WebsocketInitializer();
|
||||
WebSocketInitializer initializer = new WebSocketInitializer();
|
||||
List<Exception> exceptions = initializer.initialize();
|
||||
assertTrue(exceptions.size() == 0);
|
||||
}
|
|
@ -31,27 +31,27 @@ import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfigurati
|
|||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure;
|
||||
import org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure.HttpLookupFailure;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.WebsocketEndpointsDiscoveryFailed;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpointLookup.Result;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.XmppWebSocketTransport.WebSocketEndpointsDiscoveryFailed;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpointLookup.Result;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class XmppWebsocketTransportModuleTest {
|
||||
public class XmppWebSocketTransportModuleTest {
|
||||
@Test
|
||||
public void createWebsocketModuleConnectionInstanceTest() throws URISyntaxException, XmppStringprepException {
|
||||
public void createWebSocketModuleConnectionInstanceTest() throws URISyntaxException, XmppStringprepException {
|
||||
ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration
|
||||
.builder();
|
||||
|
||||
builder.removeAllModules();
|
||||
builder.addModule(XmppWebsocketTransportModuleDescriptor.class);
|
||||
builder.addModule(XmppWebSocketTransportModuleDescriptor.class);
|
||||
builder.setXmppAddressAndPassword("user5@localhost.org", "user5");
|
||||
builder.setHost("localhost.org");
|
||||
|
||||
XmppWebsocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebsocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebsocketEndpointAndDiscovery(new URI("wss://localhost.org:7443/ws/"), false);
|
||||
XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(new URI("wss://localhost.org:7443/ws/"), false);
|
||||
|
||||
ModularXmppClientToServerConnectionConfiguration config = builder.build();
|
||||
ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
|
||||
|
@ -60,26 +60,26 @@ public class XmppWebsocketTransportModuleTest {
|
|||
|
||||
@Test
|
||||
public void createDescriptorTest() throws URISyntaxException, XmppStringprepException {
|
||||
XmppWebsocketTransportModuleDescriptor websocketTransportModuleDescriptor = getWebsocketDescriptor();
|
||||
XmppWebSocketTransportModuleDescriptor websocketTransportModuleDescriptor = getWebSocketDescriptor();
|
||||
assertNotNull(websocketTransportModuleDescriptor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketEndpointDiscoveryTest() throws URISyntaxException {
|
||||
XmppWebsocketTransportModuleDescriptor websocketTransportModuleDescriptor = getWebsocketDescriptor();
|
||||
XmppWebSocketTransportModuleDescriptor websocketTransportModuleDescriptor = getWebSocketDescriptor();
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
|
||||
|
||||
XmppWebsocketTransportModule transportModule
|
||||
= new XmppWebsocketTransportModule(websocketTransportModuleDescriptor, connectionInternal);
|
||||
XmppWebSocketTransportModule transportModule
|
||||
= new XmppWebSocketTransportModule(websocketTransportModuleDescriptor, connectionInternal);
|
||||
|
||||
XmppWebsocketTransportModule.XmppWebsocketTransport transport = transportModule.getTransport();
|
||||
XmppWebSocketTransportModule.XmppWebSocketTransport transport = transportModule.getTransport();
|
||||
|
||||
assertThrows(AssertionError.class, () -> transport.new DiscoveredWebsocketEndpoints(null));
|
||||
assertThrows(AssertionError.class, () -> transport.new WebsocketEndpointsDiscoveryFailed(null));
|
||||
assertThrows(AssertionError.class, () -> transport.new DiscoveredWebSocketEndpoints(null));
|
||||
assertThrows(AssertionError.class, () -> transport.new WebSocketEndpointsDiscoveryFailed(null));
|
||||
|
||||
WebsocketRemoteConnectionEndpoint endpoint = new WebsocketRemoteConnectionEndpoint("wss://localhost.org:7443/ws/");
|
||||
WebSocketRemoteConnectionEndpoint endpoint = new WebSocketRemoteConnectionEndpoint("wss://localhost.org:7443/ws/");
|
||||
|
||||
List<WebsocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints = new ArrayList<>();
|
||||
List<WebSocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints = new ArrayList<>();
|
||||
discoveredRemoteConnectionEndpoints.add(endpoint);
|
||||
|
||||
HttpLookupFailure httpLookupFailure = new RemoteConnectionEndpointLookupFailure.HttpLookupFailure(null, null);
|
||||
|
@ -87,38 +87,38 @@ public class XmppWebsocketTransportModuleTest {
|
|||
failureList.add(httpLookupFailure);
|
||||
Result result = new Result(discoveredRemoteConnectionEndpoints, failureList);
|
||||
|
||||
DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints = transport.new DiscoveredWebsocketEndpoints(result);
|
||||
assertNotNull(discoveredWebsocketEndpoints.getResult());
|
||||
DiscoveredWebSocketEndpoints discoveredWebSocketEndpoints = transport.new DiscoveredWebSocketEndpoints(result);
|
||||
assertNotNull(discoveredWebSocketEndpoints.getResult());
|
||||
|
||||
WebsocketEndpointsDiscoveryFailed endpointsDiscoveryFailed = transport.new WebsocketEndpointsDiscoveryFailed(result);
|
||||
WebSocketEndpointsDiscoveryFailed endpointsDiscoveryFailed = transport.new WebSocketEndpointsDiscoveryFailed(result);
|
||||
assertNotNull(endpointsDiscoveryFailed.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketConnectedResultTest() throws URISyntaxException {
|
||||
WebsocketRemoteConnectionEndpoint connectedEndpoint = new WebsocketRemoteConnectionEndpoint("wss://localhost.org:7443/ws/");
|
||||
assertNotNull(new XmppWebsocketTransportModule.WebsocketConnectedResult(connectedEndpoint));
|
||||
WebSocketRemoteConnectionEndpoint connectedEndpoint = new WebSocketRemoteConnectionEndpoint("wss://localhost.org:7443/ws/");
|
||||
assertNotNull(new XmppWebSocketTransportModule.WebSocketConnectedResult(connectedEndpoint));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lookupConnectionEndpointsTest() throws URISyntaxException {
|
||||
XmppWebsocketTransportModuleDescriptor websocketTransportModuleDescriptor = getWebsocketDescriptor();
|
||||
XmppWebSocketTransportModuleDescriptor websocketTransportModuleDescriptor = getWebSocketDescriptor();
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
|
||||
|
||||
XmppWebsocketTransportModule transportModule
|
||||
= new XmppWebsocketTransportModule(websocketTransportModuleDescriptor, connectionInternal);
|
||||
XmppWebSocketTransportModule transportModule
|
||||
= new XmppWebSocketTransportModule(websocketTransportModuleDescriptor, connectionInternal);
|
||||
|
||||
XmppWebsocketTransportModule.XmppWebsocketTransport transport = transportModule.getTransport();
|
||||
XmppWebSocketTransportModule.XmppWebSocketTransport transport = transportModule.getTransport();
|
||||
assertNotNull(transport.lookupConnectionEndpoints());
|
||||
|
||||
}
|
||||
|
||||
private static XmppWebsocketTransportModuleDescriptor getWebsocketDescriptor() throws URISyntaxException {
|
||||
private static XmppWebSocketTransportModuleDescriptor getWebSocketDescriptor() throws URISyntaxException {
|
||||
ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration
|
||||
.builder();
|
||||
|
||||
XmppWebsocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebsocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebsocketEndpointAndDiscovery(new URI("wss://localhost.org:7443/ws/"), false);
|
||||
return (XmppWebsocketTransportModuleDescriptor) websocketBuilder.build();
|
||||
XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(new URI("wss://localhost.org:7443/ws/"), false);
|
||||
return (XmppWebSocketTransportModuleDescriptor) websocketBuilder.build();
|
||||
}
|
||||
}
|
|
@ -23,20 +23,20 @@ import org.junit.jupiter.api.Test;
|
|||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
public class WebsocketElementTest {
|
||||
public class WebSocketElementTest {
|
||||
private static final String OPEN_ELEMENT = "<open xmlns='urn:ietf:params:xml:ns:xmpp-framing' to='foodomain.foo' version='1.0'/>";
|
||||
private static final String CLOSE_ELEMENT = "<close xmlns='urn:ietf:params:xml:ns:xmpp-framing'/>";
|
||||
|
||||
@Test
|
||||
public void websocketOpenElementTest() throws XmppStringprepException {
|
||||
String openElementXml = new WebsocketOpenElement(JidCreate.domainBareFrom("foodomain.foo")).toXML().toString();
|
||||
String openElementXml = new WebSocketOpenElement(JidCreate.domainBareFrom("foodomain.foo")).toXML().toString();
|
||||
assertXmlSimilar(OPEN_ELEMENT, openElementXml);
|
||||
assertXmlNotSimilar(CLOSE_ELEMENT, new WebsocketOpenElement(JidCreate.domainBareFrom("foodomain.foo")).toXML());
|
||||
assertXmlNotSimilar(CLOSE_ELEMENT, new WebSocketOpenElement(JidCreate.domainBareFrom("foodomain.foo")).toXML());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void websocketCloseElementTest() throws XmppStringprepException {
|
||||
String closeElementXml = new WebsocketCloseElement().toXML().toString();
|
||||
String closeElementXml = new WebSocketCloseElement().toXML().toString();
|
||||
assertXmlSimilar(CLOSE_ELEMENT, closeElementXml);
|
||||
assertXmlNotSimilar(OPEN_ELEMENT, closeElementXml);
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.websocket.implementations;
|
||||
package org.jivesoftware.smack.websocket.impl;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
@ -22,26 +22,26 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public final class AbstractWebsocketTest {
|
||||
public final class AbstractWebSocketTest {
|
||||
private static final String OPEN_ELEMENT = "<open from='localhost.org' id='aov9ihhmmn' xmlns='urn:ietf:params:xml:ns:xmpp-framing' xml:lang='en' version='1.0'/>";
|
||||
private static final String OPEN_STREAM = "<stream from='localhost.org' id='aov9ihhmmn' xmlns='jabber:client' xml:lang='en' version='1.0'>";
|
||||
private static final String CLOSE_ELEMENT = "<close xmlns='urn:ietf:params:xml:ns:xmpp-framing'/>";
|
||||
|
||||
@Test
|
||||
public void getStreamFromOpenElementTest() {
|
||||
String generatedOpenStream = AbstractWebsocket.getStreamFromOpenElement(OPEN_ELEMENT);
|
||||
String generatedOpenStream = AbstractWebSocket.getStreamFromOpenElement(OPEN_ELEMENT);
|
||||
assertEquals(generatedOpenStream, OPEN_STREAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isOpenElementTest() {
|
||||
assertTrue(AbstractWebsocket.isOpenElement(OPEN_ELEMENT));
|
||||
assertFalse(AbstractWebsocket.isOpenElement(OPEN_STREAM));
|
||||
assertTrue(AbstractWebSocket.isOpenElement(OPEN_ELEMENT));
|
||||
assertFalse(AbstractWebSocket.isOpenElement(OPEN_STREAM));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCloseElementTest() {
|
||||
assertTrue(AbstractWebsocket.isCloseElement(CLOSE_ELEMENT));
|
||||
assertFalse(AbstractWebsocket.isCloseElement(OPEN_STREAM));
|
||||
assertTrue(AbstractWebSocket.isCloseElement(CLOSE_ELEMENT));
|
||||
assertFalse(AbstractWebSocket.isCloseElement(OPEN_STREAM));
|
||||
}
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* 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.websocket.implementations;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
|
||||
import org.jivesoftware.smack.websocket.implementations.okhttp.OkHttpWebsocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpointLookup.Result;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ProviderTest {
|
||||
@Test
|
||||
public void providerTest() {
|
||||
assertThrows(IllegalArgumentException.class, () -> WebsocketImplProvider.getWebsocketImpl(OkHttpWebsocket.class, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getImplTest() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, URISyntaxException {
|
||||
WebsocketRemoteConnectionEndpoint endpoint = new WebsocketRemoteConnectionEndpoint("wss://localhost.org:7443/ws/");
|
||||
|
||||
List<WebsocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints = new ArrayList<>();
|
||||
discoveredRemoteConnectionEndpoints.add(endpoint);
|
||||
|
||||
Result result = new Result(discoveredRemoteConnectionEndpoints, null);
|
||||
|
||||
DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints = mock(DiscoveredWebsocketEndpoints.class);
|
||||
when(discoveredWebsocketEndpoints.getResult()).thenReturn(result);
|
||||
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
|
||||
|
||||
assertNotNull(WebsocketImplProvider.getWebsocketImpl(OkHttpWebsocket.class, connectionInternal, discoveredWebsocketEndpoints));
|
||||
}
|
||||
}
|
|
@ -25,21 +25,21 @@ import org.jivesoftware.smack.datatypes.UInt16;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class WebsocketRemoteConnectionEndpointTest {
|
||||
public class WebSocketRemoteConnectionEndpointTest {
|
||||
@Test
|
||||
public void endpointTest() throws URISyntaxException {
|
||||
String endpointString = "ws://fooDomain.org:7070/ws/";
|
||||
WebsocketRemoteConnectionEndpoint endpoint = new WebsocketRemoteConnectionEndpoint(endpointString);
|
||||
WebSocketRemoteConnectionEndpoint endpoint = new WebSocketRemoteConnectionEndpoint(endpointString);
|
||||
assertEquals("fooDomain.org", endpoint.getHost());
|
||||
assertEquals(UInt16.from(7070), endpoint.getPort());
|
||||
assertEquals(endpointString, endpoint.getWebsocketEndpoint().toString());
|
||||
assertEquals(endpointString, endpoint.getWebSocketEndpoint().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void faultyEndpointTest() {
|
||||
String faultyProtocolString = "wst://fooDomain.org:7070/ws/";
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new WebsocketRemoteConnectionEndpoint(faultyProtocolString);
|
||||
new WebSocketRemoteConnectionEndpoint(faultyProtocolString);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 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.smack.websocket.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
|
||||
import org.jivesoftware.smack.websocket.impl.WebSocketFactoryService;
|
||||
|
||||
public class WebSocketFactoryServiceTestUtil {
|
||||
|
||||
public static void createWebSocketTest(Class<? extends AbstractWebSocket> expected) {
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
|
||||
|
||||
AbstractWebSocket websocket = WebSocketFactoryService.createWebSocket(connectionInternal);
|
||||
assertEquals(expected, websocket.getClass());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue