io.undertow.websockets.WebSocketProtocolHandshakeHandler Java Examples

The following examples show how to use io.undertow.websockets.WebSocketProtocolHandshakeHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UndertowRequestUpgradeStrategy.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
		@Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {

	ServerHttpRequest request = exchange.getRequest();
	Assert.isInstanceOf(AbstractServerHttpRequest.class, request);
	HttpServerExchange httpExchange = ((AbstractServerHttpRequest) request).getNativeRequest();

	Set<String> protocols = (subProtocol != null ? Collections.singleton(subProtocol) : Collections.emptySet());
	Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
	List<Handshake> handshakes = Collections.singletonList(handshake);

	HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
	DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();

	try {
		DefaultCallback callback = new DefaultCallback(handshakeInfo, handler, bufferFactory);
		new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
	}
	catch (Exception ex) {
		return Mono.error(ex);
	}

	return Mono.empty();
}
 
Example #2
Source File: UndertowRequestUpgradeStrategy.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
		@Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {

	ServerHttpRequest request = exchange.getRequest();
	Assert.isInstanceOf(AbstractServerHttpRequest.class, request);
	HttpServerExchange httpExchange = ((AbstractServerHttpRequest) request).getNativeRequest();

	Set<String> protocols = (subProtocol != null ? Collections.singleton(subProtocol) : Collections.emptySet());
	Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
	List<Handshake> handshakes = Collections.singletonList(handshake);

	HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
	DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();

	try {
		DefaultCallback callback = new DefaultCallback(handshakeInfo, handler, bufferFactory);
		new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
	}
	catch (Exception ex) {
		return Mono.error(ex);
	}

	return Mono.empty();
}
 
Example #3
Source File: EventBusToWebSocket.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Override
public void customize(DeploymentInfo deploymentInfo) {
    deploymentInfo.addInitialHandlerChainWrapper(handler -> {
            return Handlers.path()
                .addPrefixPath("/", handler)
                .addPrefixPath(path, new WebSocketProtocolHandshakeHandler(new WSHandler()) {
                    @Override
                    @SuppressWarnings("PMD.SignatureDeclareThrowsException")
                    public void handleRequest(HttpServerExchange exchange) throws Exception {
                        if (reservationCheck(exchange)) {
                            super.handleRequest(exchange);
                        }
                    }
                });
        }
    );
}
 
Example #4
Source File: Term.java    From termd with Apache License 2.0 6 votes vote down vote up
synchronized HttpHandler getWebSocketHandler() {
  WebSocketConnectionCallback onWebSocketConnected = (exchange, webSocketChannel) -> {
    if (webSocketTtyConnection == null) {
      webSocketTtyConnection = new WebSocketTtyConnection(webSocketChannel, executor);
      webSocketChannel.addCloseTask((task) -> {webSocketTtyConnection.removeWebSocketChannel(); destroyIfInactiveAndDisconnected();});
      TtyBridge ttyBridge = new TtyBridge(webSocketTtyConnection);
      ttyBridge
          .setProcessListener(onTaskCreated())
          .readline();
    } else {
      if (webSocketTtyConnection.isOpen()) {
        webSocketTtyConnection.addReadonlyChannel(webSocketChannel);
        webSocketChannel.addCloseTask((task) -> {webSocketTtyConnection.removeReadonlyChannel(webSocketChannel); destroyIfInactiveAndDisconnected();});
      } else {
        webSocketTtyConnection.setWebSocketChannel(webSocketChannel);
        webSocketChannel.addCloseTask((task) -> {webSocketTtyConnection.removeWebSocketChannel(); destroyIfInactiveAndDisconnected();});
      }
    }
  };
  return new WebSocketProtocolHandshakeHandler(onWebSocketConnected);
}
 
Example #5
Source File: Term.java    From termd with Apache License 2.0 6 votes vote down vote up
HttpHandler webSocketStatusUpdateHandler() {
  WebSocketConnectionCallback webSocketConnectionCallback = (exchange, webSocketChannel) -> {
    Consumer<TaskStatusUpdateEvent> statusUpdateListener = event -> {
      Map<String, Object> statusUpdate = new HashMap<>();
      statusUpdate.put("action", "status-update");
      statusUpdate.put("event", event);

      ObjectMapper objectMapper = new ObjectMapper();
      try {
        String message = objectMapper.writeValueAsString(statusUpdate);
        WebSockets.sendText(message, webSocketChannel, null);
      } catch (JsonProcessingException e) {
        log.error("Cannot write object to JSON", e);
        String errorMessage = "Cannot write object to JSON: " + e.getMessage();
        WebSockets.sendClose(CloseMessage.UNEXPECTED_ERROR, errorMessage, webSocketChannel, null);
      }
    };
    log.debug("Registering new status update listener {}.", statusUpdateListener);
    addStatusUpdateListener(statusUpdateListener);
    webSocketChannel.addCloseTask((task) -> removeStatusUpdateListener(statusUpdateListener));
  };

  return new WebSocketProtocolHandshakeHandler(webSocketConnectionCallback);
}
 
Example #6
Source File: Handlers.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @param sessionHandler The web socket session handler
 * @return The web socket handler
 */
public static WebSocketProtocolHandshakeHandler websocket(final WebSocketConnectionCallback sessionHandler) {
    return new WebSocketProtocolHandshakeHandler(sessionHandler);
}
 
Example #7
Source File: Handlers.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @param sessionHandler The web socket session handler
 * @param next           The handler to invoke if the web socket connection fails
 * @return The web socket handler
 */
public static WebSocketProtocolHandshakeHandler websocket(final WebSocketConnectionCallback sessionHandler, final HttpHandler next) {
    return new WebSocketProtocolHandshakeHandler(sessionHandler, next);
}