Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
222f6998
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
222f6998
编写于
5月 03, 2015
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add userProperties to StandardWebSocketClient
Issue: SPR-12955
上级
bb1886c5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
92 addition
and
39 deletion
+92
-39
spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java
...k/web/socket/client/standard/StandardWebSocketClient.java
+34
-8
spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java
.../socket/client/standard/StandardWebSocketClientTests.java
+58
-31
未找到文件。
spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java
浏览文件 @
222f6998
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -21,10 +21,12 @@ import java.net.InetSocketAddress;
import
java.net.URI
;
import
java.net.UnknownHostException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.concurrent.Callable
;
import
javax.websocket.ClientEndpointConfig
;
import
javax.websocket.ClientEndpointConfig.Configurator
;
import
javax.websocket.ContainerProvider
;
...
...
@@ -49,9 +51,9 @@ import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import
org.springframework.web.socket.adapter.standard.WebSocketToStandardExtensionAdapter
;
import
org.springframework.web.socket.client.AbstractWebSocketClient
;
/**
* Initiates WebSocket requests to a WebSocket server programmatically
* through the standard Java WebSocket API.
* A WebSocketClient based on standard Java WebSocket API.
*
* @author Rossen Stoyanchev
* @since 4.0
...
...
@@ -60,6 +62,8 @@ public class StandardWebSocketClient extends AbstractWebSocketClient {
private
final
WebSocketContainer
webSocketContainer
;
private
final
Map
<
String
,
Object
>
userProperties
=
new
HashMap
<
String
,
Object
>();
private
AsyncListenableTaskExecutor
taskExecutor
=
new
SimpleAsyncTaskExecutor
();
...
...
@@ -84,6 +88,25 @@ public class StandardWebSocketClient extends AbstractWebSocketClient {
}
/**
* The standard Java WebSocket API allows passing "user properties" to the
* server via {@link ClientEndpointConfig#getUserProperties() userProperties}.
* Use this property to configure one or more properties to be passed on
* every handshake.
*/
public
void
setUserProperties
(
Map
<
String
,
Object
>
userProperties
)
{
if
(
userProperties
!=
null
)
{
this
.
userProperties
.
putAll
(
userProperties
);
}
}
/**
* The configured user properties, or {@code null}.
*/
public
Map
<
String
,
Object
>
getUserProperties
()
{
return
this
.
userProperties
;
}
/**
* Set an {@link AsyncListenableTaskExecutor} to use when opening connections.
* If this property is set to {@code null}, calls to any of the
...
...
@@ -114,16 +137,19 @@ public class StandardWebSocketClient extends AbstractWebSocketClient {
final
StandardWebSocketSession
session
=
new
StandardWebSocketSession
(
headers
,
attributes
,
localAddress
,
remoteAddress
);
final
ClientEndpointConfig
.
Builder
configBuilder
=
ClientEndpointConfig
.
Builder
.
create
();
configBuilder
.
configurator
(
new
StandardWebSocketClientConfigurator
(
headers
));
configBuilder
.
preferredSubprotocols
(
protocols
);
configBuilder
.
extensions
(
adaptExtensions
(
extensions
));
final
ClientEndpointConfig
endpointConfig
=
ClientEndpointConfig
.
Builder
.
create
()
.
configurator
(
new
StandardWebSocketClientConfigurator
(
headers
))
.
preferredSubprotocols
(
protocols
)
.
extensions
(
adaptExtensions
(
extensions
)).
build
();
endpointConfig
.
getUserProperties
().
putAll
(
getUserProperties
());
final
Endpoint
endpoint
=
new
StandardWebSocketHandlerAdapter
(
webSocketHandler
,
session
);
Callable
<
WebSocketSession
>
connectTask
=
new
Callable
<
WebSocketSession
>()
{
@Override
public
WebSocketSession
call
()
throws
Exception
{
webSocketContainer
.
connectToServer
(
endpoint
,
configBuilder
.
build
()
,
uri
);
webSocketContainer
.
connectToServer
(
endpoint
,
endpointConfig
,
uri
);
return
session
;
}
};
...
...
spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java
浏览文件 @
222f6998
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,12 +16,15 @@
package
org.springframework.web.socket.client.standard
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
import
java.net.URI
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
javax.websocket.ClientEndpointConfig
;
import
javax.websocket.Endpoint
;
import
javax.websocket.WebSocketContainer
;
...
...
@@ -36,9 +39,6 @@ import org.springframework.web.socket.WebSocketHttpHeaders;
import
org.springframework.web.socket.WebSocketSession
;
import
org.springframework.web.socket.handler.AbstractWebSocketHandler
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
/**
* Test fixture for {@link StandardWebSocketClient}.
*
...
...
@@ -66,8 +66,8 @@ public class StandardWebSocketClientTests {
@Test
public
void
l
ocalAddress
()
throws
Exception
{
URI
uri
=
new
URI
(
"ws://
example.com
/abc"
);
public
void
testGetL
ocalAddress
()
throws
Exception
{
URI
uri
=
new
URI
(
"ws://
localhost
/abc"
);
WebSocketSession
session
=
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
assertNotNull
(
session
.
getLocalAddress
());
...
...
@@ -75,8 +75,8 @@ public class StandardWebSocketClientTests {
}
@Test
public
void
l
ocalAddressWss
()
throws
Exception
{
URI
uri
=
new
URI
(
"wss://
example.com
/abc"
);
public
void
testGetL
ocalAddressWss
()
throws
Exception
{
URI
uri
=
new
URI
(
"wss://
localhost
/abc"
);
WebSocketSession
session
=
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
assertNotNull
(
session
.
getLocalAddress
());
...
...
@@ -84,61 +84,88 @@ public class StandardWebSocketClientTests {
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
l
ocalAddressNoScheme
()
throws
Exception
{
URI
uri
=
new
URI
(
"
example.com
/abc"
);
public
void
testGetL
ocalAddressNoScheme
()
throws
Exception
{
URI
uri
=
new
URI
(
"
localhost
/abc"
);
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
);
}
@Test
public
void
r
emoteAddress
()
throws
Exception
{
URI
uri
=
new
URI
(
"wss://
example.com
/abc"
);
public
void
testGetR
emoteAddress
()
throws
Exception
{
URI
uri
=
new
URI
(
"wss://
localhost
/abc"
);
WebSocketSession
session
=
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
assertNotNull
(
session
.
getRemoteAddress
());
assertEquals
(
"
example.com
"
,
session
.
getRemoteAddress
().
getHostName
());
assertEquals
(
"
localhost
"
,
session
.
getRemoteAddress
().
getHostName
());
assertEquals
(
443
,
session
.
getLocalAddress
().
getPort
());
}
@Test
public
void
h
eadersWebSocketSession
()
throws
Exception
{
public
void
h
andshakeHeaders
()
throws
Exception
{
URI
uri
=
new
URI
(
"ws://
example.com
/abc"
);
List
<
String
>
protocols
=
Arrays
.
as
List
(
"abc"
);
URI
uri
=
new
URI
(
"ws://
localhost
/abc"
);
List
<
String
>
protocols
=
Collections
.
singleton
List
(
"abc"
);
this
.
headers
.
setSecWebSocketProtocol
(
protocols
);
this
.
headers
.
add
(
"foo"
,
"bar"
);
WebSocketSession
session
=
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
assertEquals
(
Collections
.
singletonMap
(
"foo"
,
Arrays
.
asList
(
"bar"
)),
session
.
getHandshakeHeaders
());
assertEquals
(
1
,
session
.
getHandshakeHeaders
().
size
());
assertEquals
(
"bar"
,
session
.
getHandshakeHeaders
().
getFirst
(
"foo"
));
}
@Test
public
void
headersClientEndpointConfigurator
()
throws
Exception
{
public
void
clientEndpointConfig
()
throws
Exception
{
URI
uri
=
new
URI
(
"ws://
example.com
/abc"
);
List
<
String
>
protocols
=
Arrays
.
as
List
(
"abc"
);
URI
uri
=
new
URI
(
"ws://
localhost
/abc"
);
List
<
String
>
protocols
=
Collections
.
singleton
List
(
"abc"
);
this
.
headers
.
setSecWebSocketProtocol
(
protocols
);
this
.
headers
.
add
(
"foo"
,
"bar"
);
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
ArgumentCaptor
<
Endpoint
>
arg1
=
ArgumentCaptor
.
forClass
(
Endpoint
.
class
);
ArgumentCaptor
<
ClientEndpointConfig
>
arg2
=
ArgumentCaptor
.
forClass
(
ClientEndpointConfig
.
class
);
ArgumentCaptor
<
URI
>
arg3
=
ArgumentCaptor
.
forClass
(
URI
.
class
);
verify
(
this
.
wsContainer
).
connectToServer
(
arg1
.
capture
(),
arg2
.
capture
(),
arg3
.
capture
());
ArgumentCaptor
<
ClientEndpointConfig
>
captor
=
ArgumentCaptor
.
forClass
(
ClientEndpointConfig
.
class
);
verify
(
this
.
wsContainer
).
connectToServer
(
any
(
Endpoint
.
class
),
captor
.
capture
(),
any
(
URI
.
class
));
ClientEndpointConfig
endpointConfig
=
captor
.
getValue
();
ClientEndpointConfig
endpointConfig
=
arg2
.
getValue
();
assertEquals
(
protocols
,
endpointConfig
.
getPreferredSubprotocols
());
}
@Test
public
void
clientEndpointConfigWithUserProperties
()
throws
Exception
{
Map
<
String
,
Object
>
userProperties
=
Collections
.
singletonMap
(
"foo"
,
"bar"
);
URI
uri
=
new
URI
(
"ws://localhost/abc"
);
this
.
wsClient
.
setUserProperties
(
userProperties
);
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
ArgumentCaptor
<
ClientEndpointConfig
>
captor
=
ArgumentCaptor
.
forClass
(
ClientEndpointConfig
.
class
);
verify
(
this
.
wsContainer
).
connectToServer
(
any
(
Endpoint
.
class
),
captor
.
capture
(),
any
(
URI
.
class
));
ClientEndpointConfig
endpointConfig
=
captor
.
getValue
();
assertEquals
(
userProperties
,
endpointConfig
.
getUserProperties
());
}
@Test
public
void
standardWebSocketClientConfiguratorInsertsHandshakeHeaders
()
throws
Exception
{
URI
uri
=
new
URI
(
"ws://localhost/abc"
);
this
.
headers
.
add
(
"foo"
,
"bar"
);
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
ArgumentCaptor
<
ClientEndpointConfig
>
captor
=
ArgumentCaptor
.
forClass
(
ClientEndpointConfig
.
class
);
verify
(
this
.
wsContainer
).
connectToServer
(
any
(
Endpoint
.
class
),
captor
.
capture
(),
any
(
URI
.
class
));
ClientEndpointConfig
endpointConfig
=
captor
.
getValue
();
Map
<
String
,
List
<
String
>>
map
=
new
HashMap
<>();
endpointConfig
.
getConfigurator
().
beforeRequest
(
map
);
assertEquals
(
Collections
.
singletonMap
(
"foo"
,
Arrays
.
asList
(
"bar"
)),
map
);
Map
<
String
,
List
<
String
>>
headers
=
new
HashMap
<>();
endpointConfig
.
getConfigurator
().
beforeRequest
(
headers
);
assertEquals
(
1
,
headers
.
size
()
);
}
@Test
public
void
taskExecutor
()
throws
Exception
{
URI
uri
=
new
URI
(
"ws://
example.com
/abc"
);
URI
uri
=
new
URI
(
"ws://
localhost
/abc"
);
this
.
wsClient
.
setTaskExecutor
(
new
SimpleAsyncTaskExecutor
());
WebSocketSession
session
=
this
.
wsClient
.
doHandshake
(
this
.
wsHandler
,
this
.
headers
,
uri
).
get
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录