org.apache.tomcat.websocket.server.WsServerContainer.doUpgrade()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(173)

本文整理了Java中org.apache.tomcat.websocket.server.WsServerContainer.doUpgrade()方法的一些代码示例,展示了WsServerContainer.doUpgrade()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WsServerContainer.doUpgrade()方法的具体详情如下:
包路径:org.apache.tomcat.websocket.server.WsServerContainer
类名称:WsServerContainer
方法名:doUpgrade

WsServerContainer.doUpgrade介绍

[英]Until the WebSocket specification provides such a mechanism, this Tomcat proprietary method is provided to enable applications to programmatically determine whether or not to upgrade an individual request to WebSocket.

Note: This method is not used by Tomcat but is used directly by third-party code and must not be removed.
[中]在WebSocket规范提供这样一种机制之前,Tomcat专有方法是用来让应用程序以编程方式确定是否将单个请求升级到WebSocket的。
注意:Tomcat不使用此方法,但第三方代码直接使用此方法,不得删除。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
    @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint)
    throws HandshakeFailureException {
  HttpServletRequest servletRequest = getHttpServletRequest(request);
  HttpServletResponse servletResponse = getHttpServletResponse(response);
  StringBuffer requestUrl = servletRequest.getRequestURL();
  String path = servletRequest.getRequestURI();  // shouldn't matter
  Map<String, String> pathParams = Collections.<String, String> emptyMap();
  ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
  endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
  endpointConfig.setExtensions(selectedExtensions);
  try {
    getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
  }
  catch (ServletException ex) {
    throw new HandshakeFailureException(
        "Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
  }
  catch (IOException ex) {
    throw new HandshakeFailureException(
        "Response update failed during upgrade to WebSocket: " + requestUrl, ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
    @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory){
  ServerHttpRequest request = exchange.getRequest();
  ServerHttpResponse response = exchange.getResponse();
  HttpServletRequest servletRequest = getHttpServletRequest(request);
  HttpServletResponse servletResponse = getHttpServletResponse(response);
  HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
  DataBufferFactory bufferFactory = response.bufferFactory();
  Endpoint endpoint = new StandardWebSocketHandlerAdapter(
      handler, session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
  String requestURI = servletRequest.getRequestURI();
  DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
  config.setSubprotocols(subProtocol != null ?
      Collections.singletonList(subProtocol) : Collections.emptyList());
  try {
    WsServerContainer container = getContainer(servletRequest);
    container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
  }
  catch (ServletException | IOException ex) {
    return Mono.error(ex);
  }
  return Mono.empty();
}

代码示例来源:origin: org.springframework/spring-websocket

@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
    @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint)
    throws HandshakeFailureException {
  HttpServletRequest servletRequest = getHttpServletRequest(request);
  HttpServletResponse servletResponse = getHttpServletResponse(response);
  StringBuffer requestUrl = servletRequest.getRequestURL();
  String path = servletRequest.getRequestURI();  // shouldn't matter
  Map<String, String> pathParams = Collections.<String, String> emptyMap();
  ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
  endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
  endpointConfig.setExtensions(selectedExtensions);
  try {
    getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
  }
  catch (ServletException ex) {
    throw new HandshakeFailureException(
        "Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
  }
  catch (IOException ex) {
    throw new HandshakeFailureException(
        "Response update failed during upgrade to WebSocket: " + requestUrl, ex);
  }
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
    @Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint)
    throws HandshakeFailureException {
  HttpServletRequest servletRequest = getHttpServletRequest(request);
  HttpServletResponse servletResponse = getHttpServletResponse(response);
  StringBuffer requestUrl = servletRequest.getRequestURL();
  String path = servletRequest.getRequestURI();  // shouldn't matter
  Map<String, String> pathParams = Collections.<String, String> emptyMap();
  ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
  endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
  endpointConfig.setExtensions(selectedExtensions);
  try {
    getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
  }
  catch (ServletException ex) {
    throw new HandshakeFailureException(
        "Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
  }
  catch (IOException ex) {
    throw new HandshakeFailureException(
        "Response update failed during upgrade to WebSocket: " + requestUrl, ex);
  }
}

代码示例来源:origin: org.springframework/spring-web-reactive

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
    Optional<String> subProtocol){
  ServerHttpRequest request = exchange.getRequest();
  ServerHttpResponse response = exchange.getResponse();
  HttpServletRequest servletRequest = getHttpServletRequest(request);
  HttpServletResponse servletResponse = getHttpServletResponse(response);
  Endpoint endpoint = new StandardWebSocketHandlerAdapter(handler,
      session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new StandardWebSocketSession(session, info, factory);
      });
  String requestURI = servletRequest.getRequestURI();
  DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
  config.setSubprotocols(subProtocol.map(Collections::singletonList).orElse(Collections.emptyList()));
  try {
    WsServerContainer container = getContainer(servletRequest);
    container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
  }
  catch (ServletException | IOException ex) {
    return Mono.error(ex);
  }
  return Mono.empty();
}

相关文章