Search in sources :

Example 1 with WebSocketHttpHeaders

use of org.springframework.web.socket.WebSocketHttpHeaders in project portal by ixinportal.

the class KeySegmentationTask method buildTask.

public static void buildTask(final WebSocketSession session, final WebSocketMessage<?> message) throws UnsupportedEncodingException, NoSuchAlgorithmException, URISyntaxException {
    // header 获取云盾秘钥连接 // TODO: 2017/11/29
    final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    MobileSecretKeyConfigExample configExample = new MobileSecretKeyConfigExample();
    MobileSecretKeyConfig keyConfig = accesskeyLiensConfigService.selectByConfig(configExample);
    // keyConfig.getPasswordNumber()
    headers.add("appId", keyConfig.getPasswordNumber());
    headers.add("Content-Signature", Base64.encode(HMACSHA1.getHmacSHA1(/*keyConfig.getPasswordNumber(),keyConfig.getPassword()*/
    "1", "123"), false));
    final URI uri = new URI(/*keyConfig.getConnectAddress()*/
    "ws://192.168.102.237:8080/km/webSocketServer");
    Runnable task = new Runnable() {

        @Override
        public void run() {
            WebSocketClient webSocketClient = new StandardWebSocketClient();
            webSocketClient.doHandshake(new KeySegmentationWebSocketHandler(session, message), headers, uri);
        }
    };
    threadPool.execute(task);
}
Also used : MobileSecretKeyConfig(com.itrus.portal.db.MobileSecretKeyConfig) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) KeySegmentationWebSocketHandler(com.itrus.portal.mobile.controller.websocket.handler.KeySegmentationWebSocketHandler) MobileSecretKeyConfigExample(com.itrus.portal.db.MobileSecretKeyConfigExample) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) WebSocketClient(org.springframework.web.socket.client.WebSocketClient) StandardWebSocketClient(org.springframework.web.socket.client.standard.StandardWebSocketClient) URI(java.net.URI)

Example 2 with WebSocketHttpHeaders

use of org.springframework.web.socket.WebSocketHttpHeaders in project spring-framework by spring-projects.

the class WebSocketConnectionManagerTests method openConnection.

@Test
public void openConnection() throws Exception {
    List<String> subprotocols = Arrays.asList("abc");
    TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
    WebSocketHandler handler = new TextWebSocketHandler();
    WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler, "/path/{id}", "123");
    manager.setSubProtocols(subprotocols);
    manager.openConnection();
    WebSocketHttpHeaders expectedHeaders = new WebSocketHttpHeaders();
    expectedHeaders.setSecWebSocketProtocol(subprotocols);
    assertThat(client.headers).isEqualTo(expectedHeaders);
    assertThat(client.uri).isEqualTo(new URI("/path/123"));
    WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
    assertThat(loggingHandler.getClass()).isEqualTo(LoggingWebSocketHandlerDecorator.class);
    assertThat(loggingHandler.getDelegate()).isSameAs(handler);
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) URI(java.net.URI) WebSocketHandlerDecorator(org.springframework.web.socket.handler.WebSocketHandlerDecorator) LoggingWebSocketHandlerDecorator(org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator) Test(org.junit.jupiter.api.Test)

Example 3 with WebSocketHttpHeaders

use of org.springframework.web.socket.WebSocketHttpHeaders in project spring-framework by spring-projects.

the class SockJsClientTests method connectWithHandshakeHeaders.

// SPR-13254
@Test
public void connectWithHandshakeHeaders() throws Exception {
    ArgumentCaptor<HttpHeaders> headersCaptor = setupInfoRequest(false);
    this.xhrTransport.setStreamingDisabled(true);
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    headers.set("foo", "bar");
    headers.set("auth", "123");
    this.sockJsClient.doHandshake(handler, headers, new URI(URL)).addCallback(this.connectCallback);
    HttpHeaders httpHeaders = headersCaptor.getValue();
    assertThat(httpHeaders.size()).isEqualTo(2);
    assertThat(httpHeaders.getFirst("foo")).isEqualTo("bar");
    assertThat(httpHeaders.getFirst("auth")).isEqualTo("123");
    httpHeaders = this.xhrTransport.getRequest().getHttpRequestHeaders();
    assertThat(httpHeaders.size()).isEqualTo(2);
    assertThat(httpHeaders.getFirst("foo")).isEqualTo("bar");
    assertThat(httpHeaders.getFirst("auth")).isEqualTo("123");
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 4 with WebSocketHttpHeaders

use of org.springframework.web.socket.WebSocketHttpHeaders in project spring-framework by spring-projects.

the class JettyWebSocketClientTests method doHandshake.

@Test
public void doHandshake() throws Exception {
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    headers.setSecWebSocketProtocol(Arrays.asList("echo"));
    this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get();
    assertEquals(this.wsUrl, this.wsSession.getUri().toString());
    assertEquals("echo", this.wsSession.getAcceptedProtocol());
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) URI(java.net.URI) Test(org.junit.Test)

Example 5 with WebSocketHttpHeaders

use of org.springframework.web.socket.WebSocketHttpHeaders in project spring-framework by spring-projects.

the class JettyWebSocketClientTests method doHandshakeWithTaskExecutor.

@Test
public void doHandshakeWithTaskExecutor() throws Exception {
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
    headers.setSecWebSocketProtocol(Arrays.asList("echo"));
    this.client.setTaskExecutor(new SimpleAsyncTaskExecutor());
    this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get();
    assertEquals(this.wsUrl, this.wsSession.getUri().toString());
    assertEquals("echo", this.wsSession.getAcceptedProtocol());
}
Also used : WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) TextWebSocketHandler(org.springframework.web.socket.handler.TextWebSocketHandler) URI(java.net.URI) Test(org.junit.Test)

Aggregations

WebSocketHttpHeaders (org.springframework.web.socket.WebSocketHttpHeaders)14 URI (java.net.URI)8 Test (org.junit.jupiter.api.Test)4 HttpHeaders (org.springframework.http.HttpHeaders)4 Test (org.junit.Test)3 TextWebSocketHandler (org.springframework.web.socket.handler.TextWebSocketHandler)3 WebSocketExtension (org.springframework.web.socket.WebSocketExtension)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 MobileSecretKeyConfig (com.itrus.portal.db.MobileSecretKeyConfig)1 MobileSecretKeyConfigExample (com.itrus.portal.db.MobileSecretKeyConfigExample)1 KeySegmentationWebSocketHandler (com.itrus.portal.mobile.controller.websocket.handler.KeySegmentationWebSocketHandler)1 WebSocketContainer (jakarta.websocket.WebSocketContainer)1 IOException (java.io.IOException)1 Principal (java.security.Principal)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONException (org.json.JSONException)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1