io.undertow.websockets.core.protocol.Handshake Java Examples

The following examples show how to use io.undertow.websockets.core.protocol.Handshake. 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: UndertowRequestUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private Handshake getHandshakeToUse(ServletWebSocketHttpExchange exchange, ConfiguredServerEndpoint endpoint) {
	Handshake handshake = new JsrHybi13Handshake(endpoint);
	if (handshake.matches(exchange)) {
		return handshake;
	}
	handshake = new JsrHybi08Handshake(endpoint);
	if (handshake.matches(exchange)) {
		return handshake;
	}
	handshake = new JsrHybi07Handshake(endpoint);
	if (handshake.matches(exchange)) {
		return handshake;
	}
	// Should never occur
	throw new HandshakeFailureException("No matching Undertow Handshake found: " + exchange.getRequestHeaders());
}
 
Example #4
Source File: UndertowWebSocketFilter.java    From pippo with Apache License 2.0 6 votes vote down vote up
private void processWebSocketRequest(Request request, Response response) {
        HttpServletRequest servletRequest = request.getHttpServletRequest();
        HttpServletResponse servletResponse = response.getHttpServletResponse();

        WebSocketHttpExchange exchange = new ServletWebSocketHttpExchange(servletRequest, servletResponse, peerConnections);
//        exchange.putAttachment(HandshakeUtil.PATH_PARAMS, Collections.<String, String>emptyMap());

        Handshake handshake = getHandshake(exchange);

        exchange.upgradeChannel((connection, serverExchange) -> {
            WebSocketChannel channel = handshake.createChannel(exchange, connection, exchange.getBufferPool());
            peerConnections.add(channel);
            createWebSocketAdapter(request).onConnect(exchange, channel);
        });

        handshake.handshake(exchange);
    }
 
Example #5
Source File: WebSocketServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

    final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(req, resp, peerConnections);
    Handshake handshaker = null;
    for (Handshake method : handshakes) {
        if (method.matches(facade)) {
            handshaker = method;
            break;
        }
    }

    if (handshaker == null) {
        UndertowLogger.REQUEST_LOGGER.debug("Could not find hand shaker for web socket request");
        resp.sendError(StatusCodes.BAD_REQUEST);
        return;
    }
    final Handshake selected = handshaker;
    facade.upgradeChannel(new HttpUpgradeListener() {
        @Override
        public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
            WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
            peerConnections.add(channel);
            callback.onConnect(facade, channel);
        }
    });
    handshaker.handshake(facade);
}
 
Example #6
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param callback The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                 established
 */
public WebSocketProtocolHandshakeHandler(final WebSocketConnectionCallback callback, final HttpHandler next) {
    this.callback = callback;
    Set<Handshake> handshakes = new HashSet<>();
    handshakes.add(new Hybi13Handshake());
    handshakes.add(new Hybi08Handshake());
    handshakes.add(new Hybi07Handshake());
    this.handshakes = handshakes;
    this.next = next;
    this.upgradeListener = null;
}
 
Example #7
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param callback The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                 established
 */
public WebSocketProtocolHandshakeHandler(final HttpUpgradeListener callback, final HttpHandler next) {
    this.callback = null;
    Set<Handshake> handshakes = new HashSet<>();
    handshakes.add(new Hybi13Handshake());
    handshakes.add(new Hybi08Handshake());
    handshakes.add(new Hybi07Handshake());
    this.handshakes = handshakes;
    this.next = next;
    this.upgradeListener = callback;
}
 
Example #8
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a new WebSocket Extension into the handshakes defined in this handler.
 *
 * @param extension a new {@code ExtensionHandshake} instance
 * @return          current handler
 */
public WebSocketProtocolHandshakeHandler addExtension(ExtensionHandshake extension) {
    if (extension != null) {
        for (Handshake handshake : handshakes) {
            handshake.addExtension(extension);
        }
    }
    return this;
}
 
Example #9
Source File: UndertowRequestUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
		String selectedProtocol, List<Extension> selectedExtensions, final Endpoint endpoint)
		throws HandshakeFailureException {

	HttpServletRequest servletRequest = getHttpServletRequest(request);
	HttpServletResponse servletResponse = getHttpServletResponse(response);

	final ServletWebSocketHttpExchange exchange = createHttpExchange(servletRequest, servletResponse);
	exchange.putAttachment(HandshakeUtil.PATH_PARAMS, Collections.<String, String>emptyMap());

	ServerWebSocketContainer wsContainer = (ServerWebSocketContainer) getContainer(servletRequest);
	final EndpointSessionHandler endpointSessionHandler = new EndpointSessionHandler(wsContainer);

	final ConfiguredServerEndpoint configuredServerEndpoint = createConfiguredServerEndpoint(
			selectedProtocol, selectedExtensions, endpoint, servletRequest);

	final Handshake handshake = getHandshakeToUse(exchange, configuredServerEndpoint);

	exchange.upgradeChannel(new HttpUpgradeListener() {
		@Override
		public void handleUpgrade(StreamConnection connection, HttpServerExchange serverExchange) {
			Object bufferPool = ReflectionUtils.invokeMethod(getBufferPoolMethod, exchange);
			WebSocketChannel channel = (WebSocketChannel) ReflectionUtils.invokeMethod(
					createChannelMethod, handshake, exchange, connection, bufferPool);
			if (peerConnections != null) {
				peerConnections.add(channel);
			}
			endpointSessionHandler.onConnect(exchange, channel);
		}
	});

	handshake.handshake(exchange);
}
 
Example #10
Source File: UndertowWebSocketFilter.java    From pippo with Apache License 2.0 5 votes vote down vote up
private Handshake getHandshake(WebSocketHttpExchange exchange) {
    for (Handshake handshake : handshakes) {
        if (handshake.matches(exchange)) {
            return handshake;
        }
    }

    throw new PippoRuntimeException("No matching Undertow Handshake found: {}", exchange.getRequestHeaders());
}
 
Example #11
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param handshakes The supported handshake methods
 * @param callback   The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                   established
 */
public WebSocketProtocolHandshakeHandler(Collection<Handshake> handshakes, final WebSocketConnectionCallback callback, final HttpHandler next) {
    this.callback = callback;
    this.handshakes = new HashSet<>(handshakes);
    this.next = next;
    this.upgradeListener = null;
}
 
Example #12
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param handshakes The supported handshake methods
 * @param callback   The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                   established
 */
public WebSocketProtocolHandshakeHandler(Collection<Handshake> handshakes, final HttpUpgradeListener callback, final HttpHandler next) {
    this.callback = null;
    this.handshakes = new HashSet<>(handshakes);
    this.next = next;
    this.upgradeListener = callback;
}
 
Example #13
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param handshakes The supported handshake methods
 * @param callback   The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                   established
 */
public WebSocketProtocolHandshakeHandler(Collection<Handshake> handshakes, final WebSocketConnectionCallback callback) {
    this(handshakes, callback, ResponseCodeHandler.HANDLE_404);
}
 
Example #14
Source File: WebSocketProtocolHandshakeHandler.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@link WebSocketProtocolHandshakeHandler}
 *
 * @param handshakes The supported handshake methods
 * @param callback   The {@link WebSocketConnectionCallback} which will be executed once the handshake was
 *                   established
 */
public WebSocketProtocolHandshakeHandler(Collection<Handshake> handshakes, final HttpUpgradeListener callback) {
    this(handshakes, callback, ResponseCodeHandler.HANDLE_404);
}