org.springframework.web.socket.server.support.WebSocketHttpRequestHandler类的使用及代码示例

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

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

WebSocketHttpRequestHandler介绍

[英]A HttpRequestHandler for processing WebSocket handshake requests.

This is the main class to use when configuring a server WebSocket at a specific URL. It is a very thin wrapper around a WebSocketHandler and a HandshakeHandler, also adapting the HttpServletRequest and HttpServletResponse to ServerHttpRequest and ServerHttpResponse, respectively.
[中]用于处理WebSocket握手请求的HttpRequestHandler。
这是在特定URL上配置服务器WebSocket时要使用的主要类。它是围绕WebSocketHandler和握手Handler的一个非常薄的包装,还分别将HttpServletRequest和HttpServletResponse改编为ServerHttpRequest和ServerHttpResponse。

代码示例

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

@Override
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings,
    WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler,
    HandshakeInterceptor[] interceptors, String path) {
  WebSocketHttpRequestHandler httpHandler =
      new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler);
  if (!ObjectUtils.isEmpty(interceptors)) {
    httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
  }
  mappings.add(httpHandler, path);
}

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

@Test
public void handshakeHandlerAndInterceptor() {
  WebMvcStompWebSocketEndpointRegistration registration =
      new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
  DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);
  MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
  assertEquals(1, mappings.size());
  Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
  assertEquals(Arrays.asList("/foo"), entry.getValue());
  WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
  assertNotNull(requestHandler.getWebSocketHandler());
  assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
  assertEquals(2, requestHandler.getHandshakeInterceptors().size());
  assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
  assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}

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

@Override
public void start() {
  if (!isRunning()) {
    this.running = true;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).start();
    }
  }
}

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

@Test
public void allowedOrigins() {
  WebMvcStompWebSocketEndpointRegistration registration =
      new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
  registration.setAllowedOrigins();
  MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
  assertEquals(1, mappings.size());
  HttpRequestHandler handler = mappings.entrySet().iterator().next().getKey();
  WebSocketHttpRequestHandler wsHandler = (WebSocketHttpRequestHandler) handler;
  assertNotNull(wsHandler.getWebSocketHandler());
  assertEquals(1, wsHandler.getHandshakeInterceptors().size());
  assertEquals(OriginHandshakeInterceptor.class, wsHandler.getHandshakeInterceptors().get(0).getClass());
}

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

WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) object;
List<HandshakeInterceptor> handshakeInterceptors = handler
    .getHandshakeInterceptors();
List<HandshakeInterceptor> interceptorsToSet = new ArrayList<>(
    handshakeInterceptors.size() + 1);
interceptorsToSet.addAll(handshakeInterceptors);
handler.setHandshakeInterceptors(interceptorsToSet);

代码示例来源:origin: com.github.tianjing/tgtools.web.develop

@Override
  public void onMessage(Message message) {
    if (StringUtil.isNullOrEmpty(mServletName) || StringUtil.isNullOrEmpty(mUrl)) {
      return;
    }
    try {
      if ("addDispatcherServlet".equals(message.getEvent())) {
        System.out.println("Message Content :" + message.getContent());
        JSONObject json = new JSONObject(message.getContent());
        if (mServletName.equals(json.getString("ServletName"))) {
          PlatformDispatcherServletFactory.getDispatcher(mServletName).addWebsocket(mUrl, new WebSocketHttpRequestHandler(AbstractWebSocketHandler.this));
          tgtools.message.MessageFactory.unRegisterListening(getName());
        }
      }
    } catch (APPErrorException e) {
      tgtools.util.LogHelper.error("", "region mywebsocket Error", getName(), e);
    }
  }
}

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

WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) object;
List<HandshakeInterceptor> handshakeInterceptors = handler
    .getHandshakeInterceptors();
List<HandshakeInterceptor> interceptorsToSet = new ArrayList<>(
    handshakeInterceptors.size() + 1);
interceptorsToSet.addAll(handshakeInterceptors);
handler.setHandshakeInterceptors(interceptorsToSet);

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

@Test
public void minimalRegistration() {
  WebMvcStompWebSocketEndpointRegistration registration =
      new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
  MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
  assertEquals(1, mappings.size());
  Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
  assertNotNull(((WebSocketHttpRequestHandler) entry.getKey()).getWebSocketHandler());
  assertEquals(1, ((WebSocketHttpRequestHandler) entry.getKey()).getHandshakeInterceptors().size());
  assertEquals(Arrays.asList("/foo"), entry.getValue());
}

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

public final MultiValueMap<HttpRequestHandler, String> getMappings() {
  MultiValueMap<HttpRequestHandler, String> mappings = new LinkedMultiValueMap<>();
  if (this.registration != null) {
    SockJsService sockJsService = this.registration.getSockJsService();
    for (String path : this.paths) {
      String pattern = (path.endsWith("/") ? path + "**" : path + "/**");
      SockJsHttpRequestHandler handler = new SockJsHttpRequestHandler(sockJsService, this.webSocketHandler);
      mappings.add(handler, pattern);
    }
  }
  else {
    for (String path : this.paths) {
      WebSocketHttpRequestHandler handler;
      if (this.handshakeHandler != null) {
        handler = new WebSocketHttpRequestHandler(this.webSocketHandler, this.handshakeHandler);
      }
      else {
        handler = new WebSocketHttpRequestHandler(this.webSocketHandler);
      }
      HandshakeInterceptor[] interceptors = getInterceptors();
      if (interceptors.length > 0) {
        handler.setHandshakeInterceptors(Arrays.asList(interceptors));
      }
      mappings.add(handler, path);
    }
  }
  return mappings;
}

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

WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) object;
List<HandshakeInterceptor> handshakeInterceptors = handler
    .getHandshakeInterceptors();
List<HandshakeInterceptor> interceptorsToSet = new ArrayList<>(
    handshakeInterceptors.size() + 1);
interceptorsToSet.addAll(handshakeInterceptors);
handler.setHandshakeInterceptors(interceptorsToSet);

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

@Test
public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
  WebMvcStompWebSocketEndpointRegistration registration =
      new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
  DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  String origin = "http://mydomain.com";
  registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin);
  MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
  assertEquals(1, mappings.size());
  Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
  assertEquals(Arrays.asList("/foo"), entry.getValue());
  WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
  assertNotNull(requestHandler.getWebSocketHandler());
  assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
  assertEquals(2, requestHandler.getHandshakeInterceptors().size());
  assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
  assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}

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

@Test
public void sameOrigin() {
  WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
      new String[] {"/foo"}, this.handler, this.scheduler);
  registration.setAllowedOrigins();
  MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
  assertEquals(1, mappings.size());
  HttpRequestHandler handler = mappings.entrySet().iterator().next().getKey();
  WebSocketHttpRequestHandler wsHandler = (WebSocketHttpRequestHandler) handler;
  assertNotNull(wsHandler.getWebSocketHandler());
  assertEquals(1, wsHandler.getHandshakeInterceptors().size());
  assertEquals(OriginHandshakeInterceptor.class, wsHandler.getHandshakeInterceptors().get(0).getClass());
}

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

@Override
public void stop() {
  if (isRunning()) {
    this.running = false;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).stop();
    }
  }
}

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

@Override
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings,
    WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler,
    HandshakeInterceptor[] interceptors, String path) {
  WebSocketHttpRequestHandler httpHandler =
      new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler);
  if (!ObjectUtils.isEmpty(interceptors)) {
    httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
  }
  mappings.add(httpHandler, path);
}

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

@Test
@SuppressWarnings("unchecked")
public void webSocketHandlersAttributes() {
  loadBeanDefinitions("websocket-config-handlers-attributes.xml");
  HandlerMapping handlerMapping = this.appContext.getBean(HandlerMapping.class);
  assertNotNull(handlerMapping);
  assertTrue(handlerMapping instanceof SimpleUrlHandlerMapping);
  SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;
  assertEquals(2, urlHandlerMapping.getOrder());
  WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/foo");
  assertNotNull(handler);
  unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
  HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
  assertNotNull(handshakeHandler);
  assertTrue(handshakeHandler instanceof TestHandshakeHandler);
  List<HandshakeInterceptor> interceptors = handler.getHandshakeInterceptors();
  assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class),
      instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
  handler = (WebSocketHttpRequestHandler) urlHandlerMapping.getUrlMap().get("/test");
  assertNotNull(handler);
  unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
  handshakeHandler = handler.getHandshakeHandler();
  assertNotNull(handshakeHandler);
  assertTrue(handshakeHandler instanceof TestHandshakeHandler);
  interceptors = handler.getHandshakeInterceptors();
  assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class),
      instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
}

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

@Override
public void start() {
  if (!isRunning()) {
    this.running = true;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).start();
    }
  }
}

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

@Override
protected void addWebSocketHandlerMapping(MultiValueMap<HttpRequestHandler, String> mappings,
    WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler,
    HandshakeInterceptor[] interceptors, String path) {
  WebSocketHttpRequestHandler httpHandler =
      new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler);
  if (!ObjectUtils.isEmpty(interceptors)) {
    httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors));
  }
  mappings.add(httpHandler, path);
}

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

WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) shm.getUrlMap().get("/foo");
assertNotNull(handler);
unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), FooWebSocketHandler.class);
HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
assertNotNull(handshakeHandler);
assertTrue(handshakeHandler instanceof DefaultHandshakeHandler);
assertFalse(handler.getHandshakeInterceptors().isEmpty());
assertTrue(handler.getHandshakeInterceptors().get(0) instanceof OriginHandshakeInterceptor);
WebSocketHttpRequestHandler handler = (WebSocketHttpRequestHandler) shm.getUrlMap().get("/test");
assertNotNull(handler);
unwrapAndCheckDecoratedHandlerType(handler.getWebSocketHandler(), TestWebSocketHandler.class);
HandshakeHandler handshakeHandler = handler.getHandshakeHandler();
assertNotNull(handshakeHandler);
assertTrue(handshakeHandler instanceof DefaultHandshakeHandler);
assertFalse(handler.getHandshakeInterceptors().isEmpty());
assertTrue(handler.getHandshakeInterceptors().get(0) instanceof OriginHandshakeInterceptor);

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

@Override
public void stop() {
  if (isRunning()) {
    this.running = false;
    if (this.handshakeHandler instanceof Lifecycle) {
      ((Lifecycle) this.handshakeHandler).stop();
    }
  }
}

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

public final MultiValueMap<HttpRequestHandler, String> getMappings() {
  MultiValueMap<HttpRequestHandler, String> mappings = new LinkedMultiValueMap<>();
  if (this.registration != null) {
    SockJsService sockJsService = this.registration.getSockJsService();
    for (String path : this.paths) {
      String pattern = (path.endsWith("/") ? path + "**" : path + "/**");
      SockJsHttpRequestHandler handler = new SockJsHttpRequestHandler(sockJsService, this.webSocketHandler);
      mappings.add(handler, pattern);
    }
  }
  else {
    for (String path : this.paths) {
      WebSocketHttpRequestHandler handler;
      if (this.handshakeHandler != null) {
        handler = new WebSocketHttpRequestHandler(this.webSocketHandler, this.handshakeHandler);
      }
      else {
        handler = new WebSocketHttpRequestHandler(this.webSocketHandler);
      }
      HandshakeInterceptor[] interceptors = getInterceptors();
      if (interceptors.length > 0) {
        handler.setHandshakeInterceptors(Arrays.asList(interceptors));
      }
      mappings.add(handler, path);
    }
  }
  return mappings;
}

相关文章