Search in sources :

Example 26 with RemoteEndpoint

use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project knox by apache.

the class ProxyWebSocketAdapter method onWebSocketConnect.

@Override
public void onWebSocketConnect(final Session frontEndSession) {
    /*
     * Let's connect to the backend, this is where the Backend-to-frontend
     * plumbing takes place
     */
    container = ContainerProvider.getWebSocketContainer();
    container.setDefaultMaxTextMessageBufferSize(frontEndSession.getPolicy().getMaxTextMessageBufferSize());
    container.setDefaultMaxBinaryMessageBufferSize(frontEndSession.getPolicy().getMaxBinaryMessageBufferSize());
    container.setAsyncSendTimeout(frontEndSession.getPolicy().getAsyncWriteTimeout());
    container.setDefaultMaxSessionIdleTimeout(frontEndSession.getPolicy().getIdleTimeout());
    KeyStore ks = null;
    if (clientConfig != null) {
        ks = (KeyStore) clientConfig.getUserProperties().get("org.apache.knox.gateway.websockets.truststore");
    }
    /*
       Currently javax.websocket API has no provisions to configure SSL
       https://github.com/eclipse-ee4j/websocket-api/issues/210
       Until that gets fixed we'll have to resort to this.
    */
    if (container instanceof org.eclipse.jetty.websocket.jsr356.ClientContainer && ((org.eclipse.jetty.websocket.jsr356.ClientContainer) container).getClient() != null && ((org.eclipse.jetty.websocket.jsr356.ClientContainer) container).getClient().getSslContextFactory() != null) {
        ((org.eclipse.jetty.websocket.jsr356.ClientContainer) container).getClient().getHttpClient().getSslContextFactory().setTrustStore(ks);
        LOG.logMessage("Truststore for websocket setup");
    }
    final ProxyInboundClient backendSocket = new ProxyInboundClient(getMessageCallback());
    /* Attempt Connect */
    try {
        backendSession = container.connectToServer(backendSocket, clientConfig, backend);
        LOG.onConnectionOpen(backend.toString());
    } catch (DeploymentException e) {
        LOG.connectionFailed(e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOG.connectionFailed(e);
        throw new RuntimeIOException(e);
    }
    remoteLock.lock();
    super.onWebSocketConnect(frontEndSession);
    this.frontendSession = frontEndSession;
    final RemoteEndpoint remote = frontEndSession.getRemote();
    try {
        if (!messageBuffer.isEmpty()) {
            flushBufferedMessages(remote);
            if (remote.getBatchMode() == BatchMode.ON) {
                remote.flush();
            }
        } else {
            LOG.debugLog("Message buffer is empty");
        }
    } catch (IOException e) {
        LOG.connectionFailed(e);
        throw new RuntimeIOException(e);
    } finally {
        remoteLock.unlock();
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) KeyStore(java.security.KeyStore) DeploymentException(javax.websocket.DeploymentException)

Example 27 with RemoteEndpoint

use of org.eclipse.jetty.websocket.api.RemoteEndpoint in project knox by apache.

the class EchoSocket method onWebSocketBinary.

@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
    if (isNotConnected()) {
        return;
    }
    try {
        RemoteEndpoint remote = getRemote();
        remote.sendBytes(BufferUtil.toBuffer(payload, offset, len), null);
        if (remote.getBatchMode() == BatchMode.ON) {
            remote.flush();
        }
    } catch (IOException x) {
        throw new RuntimeIOException(x);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Aggregations

RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)27 IOException (java.io.IOException)11 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)8 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)7 URI (java.net.URI)6 Session (org.eclipse.jetty.websocket.api.Session)6 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)4 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)3 CaptureSocket (org.eclipse.jetty.websocket.server.helper.CaptureSocket)3 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)2 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)2 KeyStore (java.security.KeyStore)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CloseReason (javax.websocket.CloseReason)1 DeploymentException (javax.websocket.DeploymentException)1 WebSocketAdapter (org.eclipse.jetty.websocket.api.WebSocketAdapter)1 OnWebSocketFrame (org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)1 Ignore (org.junit.Ignore)1