Smack/smack-websocket/src/main/java/org/jivesoftware/smack/websocket/WebSocketConnectionAttemptS...

149 lines
6.3 KiB
Java
Raw Normal View History

2020-05-14 14:35:37 +02:00
/**
*
* Copyright 2020 Aditya Borikar, 2020-2021 Florian Schmaus.
2020-05-14 14:35:37 +02:00
*
* 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;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackFuture;
2020-05-14 14:35:37 +02:00
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
import org.jivesoftware.smack.fsm.StateTransitionResult;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
import org.jivesoftware.smack.websocket.impl.WebSocketFactory;
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpointLookup;
2020-05-14 14:35:37 +02:00
public final class WebSocketConnectionAttemptState {
2020-05-14 14:35:37 +02:00
private final ModularXmppClientToServerConnectionInternal connectionInternal;
private final XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints discoveredEndpoints;
private final WebSocketFactory webSocketFactory;
2020-05-14 14:35:37 +02:00
private AbstractWebSocket webSocket;
2020-05-14 14:35:37 +02:00
WebSocketConnectionAttemptState(ModularXmppClientToServerConnectionInternal connectionInternal,
XmppWebSocketTransportModule.XmppWebSocketTransport.DiscoveredWebSocketEndpoints discoveredWebSocketEndpoints,
WebSocketFactory webSocketFactory) {
assert discoveredWebSocketEndpoints != null;
assert !discoveredWebSocketEndpoints.result.isEmpty();
2020-05-14 14:35:37 +02:00
this.connectionInternal = connectionInternal;
this.discoveredEndpoints = discoveredWebSocketEndpoints;
this.webSocketFactory = webSocketFactory;
2020-05-14 14:35:37 +02:00
}
/**
* Establish a websocket connection with one of the discoveredRemoteConnectionEndpoints.<br>
*
* @return {@link AbstractWebSocket} with which connection is establised
2020-05-14 14:35:37 +02:00
* @throws InterruptedException if the calling thread was interrupted
*/
@SuppressWarnings({"incomplete-switch", "MissingCasesInEnumSwitch"})
StateTransitionResult.Failure establishWebSocketConnection() throws InterruptedException {
final WebSocketRemoteConnectionEndpointLookup.Result endpointLookupResult = discoveredEndpoints.result;
final List<Exception> failures = new ArrayList<>(endpointLookupResult.discoveredEndpointCount());
webSocket = null;
2020-05-14 14:35:37 +02:00
SecurityMode securityMode = connectionInternal.connection.getConfiguration().getSecurityMode();
switch (securityMode) {
case required:
case ifpossible:
establishWebSocketConnection(endpointLookupResult.discoveredSecureEndpoints, failures);
if (webSocket != null) {
return null;
}
2020-05-14 14:35:37 +02:00
}
establishWebSocketConnection(endpointLookupResult.discoveredInsecureEndpoints, failures);
if (webSocket != null) {
return null;
}
StateTransitionResult.Failure failure = FailedToConnectToAnyWebSocketEndpoint.create(failures);
return failure;
}
private void establishWebSocketConnection(List<? extends WebSocketRemoteConnectionEndpoint> webSocketEndpoints,
List<Exception> failures) throws InterruptedException {
final int endpointCount = webSocketEndpoints.size();
List<SmackFuture<AbstractWebSocket, Exception>> futures = new ArrayList<>(endpointCount);
{
List<AbstractWebSocket> webSockets = new ArrayList<>(endpointCount);
// First only create the AbstractWebSocket instances, in case a constructor throws.
for (WebSocketRemoteConnectionEndpoint endpoint : webSocketEndpoints) {
AbstractWebSocket webSocket = webSocketFactory.create(endpoint, connectionInternal);
webSockets.add(webSocket);
}
for (AbstractWebSocket webSocket : webSockets) {
SmackFuture<AbstractWebSocket, Exception> future = webSocket.getFuture();
futures.add(future);
2020-05-14 14:35:37 +02:00
}
}
SmackFuture.await(futures, connectionInternal.connection.getReplyTimeout());
for (SmackFuture<AbstractWebSocket, Exception> future : futures) {
AbstractWebSocket connectedWebSocket = future.getIfAvailable();
if (connectedWebSocket == null) {
Exception exception = future.getExceptionIfAvailable();
assert exception != null;
failures.add(exception);
continue;
}
if (webSocket == null) {
webSocket = connectedWebSocket;
// Continue here since we still need to read out the failure exceptions from potential further remaining
// futures and close remaining successfully connected ones.
continue;
}
2020-05-14 14:35:37 +02:00
connectedWebSocket.disconnect(1000, "Using other connection endpoint at " + webSocket.getEndpoint());
}
2020-05-14 14:35:37 +02:00
}
public AbstractWebSocket getConnectedWebSocket() {
return webSocket;
}
public static final class FailedToConnectToAnyWebSocketEndpoint extends StateTransitionResult.Failure {
private final List<Exception> failures;
private FailedToConnectToAnyWebSocketEndpoint(String failureMessage, List<Exception> failures) {
super(failureMessage);
this.failures = failures;
}
public List<Exception> getFailures() {
return failures;
}
private static FailedToConnectToAnyWebSocketEndpoint create(List<Exception> failures) {
StringBuilder sb = new StringBuilder(256);
StringUtils.appendTo(failures, sb, e -> sb.append(e.getMessage()));
String message = sb.toString();
return new FailedToConnectToAnyWebSocketEndpoint(message, failures);
}
2020-05-14 14:35:37 +02:00
}
}