mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 09:09:38 +02:00
Add smack-websocket-java11
This also lifts a bunch of logic from smack-websocket-okhttp into smack-websocket. Furthermore, the following subprojects require now Java 11: - smack-integration-test - smack-omemo-signal-integration-test - smack-repl - smack-websocket-java11 Related tracking issue: SMACK-835
This commit is contained in:
parent
cd40455b62
commit
4aacdc5154
24 changed files with 442 additions and 162 deletions
|
@ -24,26 +24,28 @@ import org.jivesoftware.smack.SmackFuture;
|
|||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.fsm.StateTransitionResult;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
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.impl.WebSocketFactory;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpointLookup;
|
||||
|
||||
public final class WebSocketConnectionAttemptState {
|
||||
|
||||
private final ModularXmppClientToServerConnectionInternal connectionInternal;
|
||||
private final XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints discoveredEndpoints;
|
||||
private final WebSocketFactory webSocketFactory;
|
||||
|
||||
private AbstractWebSocket webSocket;
|
||||
|
||||
WebSocketConnectionAttemptState(ModularXmppClientToServerConnectionInternal connectionInternal,
|
||||
XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints discoveredWebSocketEndpoints,
|
||||
EstablishingWebSocketConnectionState establishingWebSocketConnectionState) {
|
||||
WebSocketFactory webSocketFactory) {
|
||||
assert discoveredWebSocketEndpoints != null;
|
||||
assert !discoveredWebSocketEndpoints.result.isEmpty();
|
||||
|
||||
this.connectionInternal = connectionInternal;
|
||||
this.discoveredEndpoints = discoveredWebSocketEndpoints;
|
||||
this.webSocketFactory = webSocketFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -87,7 +89,7 @@ public final class WebSocketConnectionAttemptState {
|
|||
List<AbstractWebSocket> webSockets = new ArrayList<>(endpointCount);
|
||||
// First only create the AbstractWebSocket instances, in case a constructor throws.
|
||||
for (WebSocketRemoteConnectionEndpoint endpoint : webSocketEndpoints) {
|
||||
AbstractWebSocket webSocket = WebSocketFactoryService.createWebSocket(endpoint, connectionInternal);
|
||||
AbstractWebSocket webSocket = webSocketFactory.create(endpoint, connectionInternal);
|
||||
webSockets.add(webSocket);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,8 @@ import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.XmppWebSock
|
|||
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.impl.WebSocketFactory;
|
||||
import org.jivesoftware.smack.websocket.impl.WebSocketFactoryService;
|
||||
import org.jivesoftware.smack.websocket.rce.InsecureWebSocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.SecureWebSocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
|
@ -63,6 +65,9 @@ import org.jxmpp.jid.DomainBareJid;
|
|||
*/
|
||||
public final class XmppWebSocketTransportModule
|
||||
extends ModularXmppClientToServerConnectionModule<XmppWebSocketTransportModuleDescriptor> {
|
||||
|
||||
private static final int WEBSOCKET_NORMAL_CLOSURE = 1000;
|
||||
|
||||
private final XmppWebSocketTransport websocketTransport;
|
||||
|
||||
private AbstractWebSocket websocket;
|
||||
|
@ -106,8 +111,15 @@ public final class XmppWebSocketTransportModule
|
|||
@Override
|
||||
public AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext) throws InterruptedException,
|
||||
NoResponseException, NotConnectedException, SmackException, XMPPException {
|
||||
final WebSocketFactory webSocketFactory;
|
||||
if (moduleDescriptor.webSocketFactory != null) {
|
||||
webSocketFactory = moduleDescriptor.webSocketFactory;
|
||||
} else {
|
||||
webSocketFactory = WebSocketFactoryService::createWebSocket;
|
||||
}
|
||||
|
||||
WebSocketConnectionAttemptState connectionAttemptState = new WebSocketConnectionAttemptState(
|
||||
connectionInternal, discoveredWebSocketEndpoints, this);
|
||||
connectionInternal, discoveredWebSocketEndpoints, webSocketFactory);
|
||||
|
||||
StateTransitionResult.Failure failure = connectionAttemptState.establishWebSocketConnection();
|
||||
if (failure != null) {
|
||||
|
@ -246,7 +258,7 @@ public final class XmppWebSocketTransportModule
|
|||
|
||||
@Override
|
||||
protected void disconnect() {
|
||||
websocket.disconnect(1000, "WebSocket closed normally");
|
||||
websocket.disconnect(WEBSOCKET_NORMAL_CLOSURE, "WebSocket closed normally");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -269,11 +281,6 @@ public final class XmppWebSocketTransportModule
|
|||
return websocket.isConnectionSecure();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return websocket.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stats getStats() {
|
||||
return null;
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionIn
|
|||
import org.jivesoftware.smack.fsm.StateDescriptor;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.EstablishingWebSocketConnectionStateDescriptor;
|
||||
import org.jivesoftware.smack.websocket.impl.WebSocketFactory;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
|
||||
/**
|
||||
|
@ -41,14 +42,16 @@ import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
|||
public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
|
||||
private final boolean performWebSocketEndpointDiscovery;
|
||||
private final boolean implicitWebSocketEndpoint;
|
||||
private final URI uri;
|
||||
private final WebSocketRemoteConnectionEndpoint wsRce;
|
||||
|
||||
final WebSocketFactory webSocketFactory;
|
||||
|
||||
public XmppWebSocketTransportModuleDescriptor(Builder builder) {
|
||||
this.performWebSocketEndpointDiscovery = builder.performWebSocketEndpointDiscovery;
|
||||
this.implicitWebSocketEndpoint = builder.implicitWebSocketEndpoint;
|
||||
this.webSocketFactory = builder.webSocketFactory;
|
||||
|
||||
this.uri = builder.uri;
|
||||
URI uri = builder.uri;
|
||||
if (uri != null) {
|
||||
wsRce = WebSocketRemoteConnectionEndpoint.from(uri);
|
||||
} else {
|
||||
|
@ -95,7 +98,7 @@ public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppCli
|
|||
* @return uri
|
||||
*/
|
||||
public URI getExplicitlyProvidedUri() {
|
||||
return uri;
|
||||
return wsRce.getUri();
|
||||
}
|
||||
|
||||
WebSocketRemoteConnectionEndpoint getExplicitlyProvidedEndpoint() {
|
||||
|
@ -142,6 +145,7 @@ public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppCli
|
|||
private boolean performWebSocketEndpointDiscovery = true;
|
||||
private boolean implicitWebSocketEndpoint = true;
|
||||
private URI uri;
|
||||
private WebSocketFactory webSocketFactory;
|
||||
|
||||
private Builder(
|
||||
ModularXmppClientToServerConnectionConfiguration.Builder connectionConfigurationBuilder) {
|
||||
|
@ -175,6 +179,12 @@ public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppCli
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder setWebSocketFactory(WebSocketFactory webSocketFactory) {
|
||||
Objects.requireNonNull(webSocketFactory);
|
||||
this.webSocketFactory = webSocketFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModularXmppClientToServerConnectionModuleDescriptor build() {
|
||||
return new XmppWebSocketTransportModuleDescriptor(this);
|
||||
|
|
|
@ -16,24 +16,44 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.websocket.impl;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.net.ssl.SSLSession;
|
||||
|
||||
import org.jivesoftware.smack.SmackFuture;
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.debugger.SmackDebugger;
|
||||
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
||||
import org.jivesoftware.smack.packet.XmlEnvironment;
|
||||
import org.jivesoftware.smack.websocket.WebSocketException;
|
||||
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
|
||||
|
||||
public abstract class AbstractWebSocket {
|
||||
|
||||
protected static final Logger LOGGER = Logger.getLogger(AbstractWebSocket.class.getName());
|
||||
|
||||
protected static final String SEC_WEBSOCKET_PROTOCOL_HEADER_FILED_NAME = "Sec-WebSocket-Protocol";
|
||||
protected static final String SEC_WEBSOCKET_PROTOCOL_HEADER_FILED_VALUE_XMPP = "xmpp";
|
||||
|
||||
protected final SmackFuture.InternalSmackFuture<AbstractWebSocket, Exception> future = new SmackFuture.InternalSmackFuture<>();
|
||||
|
||||
protected final ModularXmppClientToServerConnectionInternal connectionInternal;
|
||||
|
||||
protected final WebSocketRemoteConnectionEndpoint endpoint;
|
||||
|
||||
private final SmackWebSocketDebugger debugger;
|
||||
|
||||
protected AbstractWebSocket(WebSocketRemoteConnectionEndpoint endpoint,
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
this.endpoint = endpoint;
|
||||
this.connectionInternal = connectionInternal;
|
||||
|
||||
final SmackDebugger smackDebugger = connectionInternal.smackDebugger;
|
||||
if (smackDebugger != null) {
|
||||
debugger = new SmackWebSocketDebugger(smackDebugger);
|
||||
} else {
|
||||
debugger = null;
|
||||
}
|
||||
}
|
||||
|
||||
public final WebSocketRemoteConnectionEndpoint getEndpoint() {
|
||||
|
@ -44,6 +64,10 @@ public abstract class AbstractWebSocket {
|
|||
private String streamClose;
|
||||
|
||||
protected final void onIncomingWebSocketElement(String element) {
|
||||
if (debugger != null) {
|
||||
debugger.incoming(element);
|
||||
}
|
||||
|
||||
// TODO: Once smack-websocket-java15 is there, we have to re-evaluate if the async operation here is still
|
||||
// required, or if it should only be performed if OkHTTP is used.
|
||||
if (isOpenElement(element)) {
|
||||
|
@ -95,11 +119,31 @@ public abstract class AbstractWebSocket {
|
|||
return false;
|
||||
}
|
||||
|
||||
public abstract SmackFuture<AbstractWebSocket, Exception> getFuture();
|
||||
protected void onWebSocketFailure(Throwable throwable) {
|
||||
WebSocketException websocketException = new WebSocketException(throwable);
|
||||
|
||||
// If we are already connected, then we need to notify the connection that it got tear down. Otherwise we
|
||||
// need to notify the thread calling connect() that the connection failed.
|
||||
if (future.wasSuccessful()) {
|
||||
connectionInternal.notifyConnectionError(websocketException);
|
||||
} else {
|
||||
future.setException(websocketException);
|
||||
}
|
||||
}
|
||||
|
||||
public final SmackFuture<AbstractWebSocket, Exception> getFuture() {
|
||||
return future;
|
||||
}
|
||||
|
||||
public final void send(TopLevelStreamElement element) {
|
||||
XmlEnvironment outgoingStreamXmlEnvironment = connectionInternal.getOutgoingStreamXmlEnvironment();
|
||||
String elementString = element.toXML(outgoingStreamXmlEnvironment).toString();
|
||||
|
||||
// TODO: We could make use of Java 11's WebSocket (is)last feature when sending
|
||||
if (debugger != null) {
|
||||
debugger.outgoing(elementString);
|
||||
}
|
||||
|
||||
send(elementString);
|
||||
}
|
||||
|
||||
|
@ -107,9 +151,9 @@ public abstract class AbstractWebSocket {
|
|||
|
||||
public abstract void disconnect(int code, String message);
|
||||
|
||||
public abstract boolean isConnectionSecure();
|
||||
public boolean isConnectionSecure() {
|
||||
return endpoint.isSecureEndpoint();
|
||||
}
|
||||
|
||||
public abstract SSLSession getSSLSession();
|
||||
|
||||
public abstract boolean isConnected();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Aditya Borikar, 2021 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.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.debugger.SmackDebugger;
|
||||
|
||||
import org.jxmpp.xml.splitter.XmlPrettyPrinter;
|
||||
import org.jxmpp.xml.splitter.XmppXmlSplitter;
|
||||
|
||||
public class SmackWebSocketDebugger {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(SmackWebSocketDebugger.class.getName());
|
||||
|
||||
private final SmackDebugger debugger;
|
||||
private final XmppXmlSplitter incomingXmlSplitter;
|
||||
private final XmppXmlSplitter outgoingXmlSplitter;
|
||||
|
||||
SmackWebSocketDebugger(SmackDebugger smackDebugger) {
|
||||
this.debugger = smackDebugger;
|
||||
|
||||
XmlPrettyPrinter incomingTextPrinter = XmlPrettyPrinter.builder()
|
||||
.setPrettyWriter(sb -> debugger.incomingStreamSink(sb))
|
||||
.setTabWidth(4)
|
||||
.build();
|
||||
incomingXmlSplitter = new XmppXmlSplitter(incomingTextPrinter);
|
||||
|
||||
XmlPrettyPrinter outgoingTextPrinter = XmlPrettyPrinter.builder()
|
||||
.setPrettyWriter(sb -> debugger.outgoingStreamSink(sb))
|
||||
.setTabWidth(4)
|
||||
.build();
|
||||
outgoingXmlSplitter = new XmppXmlSplitter(outgoingTextPrinter);
|
||||
}
|
||||
|
||||
void incoming(String text) {
|
||||
try {
|
||||
incomingXmlSplitter.write(text);
|
||||
} catch (IOException e) {
|
||||
// Connections shouldn't be terminated due to exceptions encountered during debugging. hence only log them.
|
||||
LOGGER.log(Level.WARNING, "IOException encountered while parsing received text: " + text, e);
|
||||
}
|
||||
}
|
||||
|
||||
void outgoing(String text) {
|
||||
try {
|
||||
outgoingXmlSplitter.write(text);
|
||||
} catch (IOException e) {
|
||||
// Connections shouldn't be terminated due to exceptions encountered during debugging, hence only log them.
|
||||
LOGGER.log(Level.WARNING, "IOException encountered while parsing outgoing text: " + text, e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue