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();
}
}
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);
}
}
Aggregations