Smack/smack-websocket-okhttp/src/main/java/org/jivesoftware/smack/websocket/okhttp/OkHttpWebSocket.java

157 lines
5.5 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.okhttp;
2020-05-14 14:35:37 +02:00
import java.net.URI;
2020-05-14 14:35:37 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLSession;
import org.jivesoftware.smack.SmackFuture;
2020-05-14 14:35:37 +02:00
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
import org.jivesoftware.smack.websocket.WebSocketException;
import org.jivesoftware.smack.websocket.impl.AbstractWebSocket;
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
2020-05-14 14:35:37 +02:00
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
public final class OkHttpWebSocket extends AbstractWebSocket {
2020-05-14 14:35:37 +02:00
private static final Logger LOGGER = Logger.getLogger(OkHttpWebSocket.class.getName());
2020-05-14 14:35:37 +02:00
private static final OkHttpClient okHttpClient = new OkHttpClient();
// This is a potential candidate to be placed into AbstractWebSocket, but I keep it here until smack-websocket-java11
// arrives.
private final SmackFuture.InternalSmackFuture<AbstractWebSocket, Exception> future = new SmackFuture.InternalSmackFuture<>();
2020-05-14 14:35:37 +02:00
private final LoggingInterceptor interceptor;
private final WebSocket okHttpWebSocket;
2020-05-14 14:35:37 +02:00
public OkHttpWebSocket(WebSocketRemoteConnectionEndpoint endpoint,
ModularXmppClientToServerConnectionInternal connectionInternal) {
super(endpoint, connectionInternal);
2020-05-14 14:35:37 +02:00
if (connectionInternal.smackDebugger != null) {
interceptor = new LoggingInterceptor(connectionInternal.smackDebugger);
} else {
interceptor = null;
}
final URI uri = endpoint.getUri();
final String url = uri.toString();
2020-05-14 14:35:37 +02:00
Request request = new Request.Builder()
.url(url)
2020-05-14 14:35:37 +02:00
.header("Sec-WebSocket-Protocol", "xmpp")
.build();
okHttpWebSocket = okHttpClient.newWebSocket(request, listener);
}
private final WebSocketListener listener = new WebSocketListener() {
2020-05-14 14:35:37 +02:00
@Override
public void onOpen(WebSocket webSocket, Response response) {
LOGGER.log(Level.FINER, "OkHttp invoked onOpen() for {0}. Response: {1}",
new Object[] { webSocket, response });
if (interceptor != null) {
interceptor.interceptOpenResponse(response);
2020-05-14 14:35:37 +02:00
}
future.setResult(OkHttpWebSocket.this);
}
@Override
public void onMessage(WebSocket webSocket, String text) {
if (interceptor != null) {
interceptor.interceptReceivedText(text);
2020-05-14 14:35:37 +02:00
}
onIncomingWebSocketElement(text);
}
@Override
public void onFailure(WebSocket webSocket, Throwable throwable, Response response) {
LOGGER.log(Level.FINER, "OkHttp invoked onFailure() for " + webSocket + ". Response: " + response, 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);
2020-05-14 14:35:37 +02:00
}
}
2020-05-14 14:35:37 +02:00
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
LOGGER.log(Level.FINER, "OkHttp invoked onClosing() for " + webSocket + ". Code: " + code + ". Reason: " + reason);
}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
LOGGER.log(Level.FINER, "OkHttp invoked onClosed() for " + webSocket + ". Code: " + code + ". Reason: " + reason);
}
2020-05-14 14:35:37 +02:00
};
2020-05-14 14:35:37 +02:00
@Override
public SmackFuture<AbstractWebSocket, Exception> getFuture() {
return future;
2020-05-14 14:35:37 +02:00
}
@Override
public void send(String element) {
2020-05-14 14:35:37 +02:00
if (interceptor != null) {
interceptor.interceptSentText(element);
2020-05-14 14:35:37 +02:00
}
okHttpWebSocket.send(element);
2020-05-14 14:35:37 +02:00
}
@Override
public void disconnect(int code, String message) {
LOGGER.log(Level.INFO, "WebSocket closing with code: " + code + " and message: " + message);
okHttpWebSocket.close(code, message);
2020-05-14 14:35:37 +02:00
}
@Override
public boolean isConnectionSecure() {
return endpoint.isSecureEndpoint();
2020-05-14 14:35:37 +02:00
}
@Override
public boolean isConnected() {
// TODO: Do we need this method at all if we create an AbstractWebSocket object for every endpoint?
return true;
2020-05-14 14:35:37 +02:00
}
@Override
public SSLSession getSSLSession() {
// TODO: What shall we do about this method, as it appears that OkHttp does not provide access to the used SSLSession?
2020-05-14 14:35:37 +02:00
return null;
}
}