Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
03e3ef53
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,发现更多精彩内容 >>
提交
03e3ef53
编写于
1月 29, 2016
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
@SendToUser supported on the class level
Issue: SPR-12047
上级
bedf1a9b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
101 addition
and
11 deletion
+101
-11
spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/SendToUser.java
...springframework/messaging/simp/annotation/SendToUser.java
+5
-2
spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java
...mp/annotation/support/SendToMethodReturnValueHandler.java
+15
-2
spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java
...notation/support/SendToMethodReturnValueHandlerTests.java
+78
-5
src/asciidoc/web-websocket.adoc
src/asciidoc/web-websocket.adoc
+2
-1
src/asciidoc/whats-new.adoc
src/asciidoc/whats-new.adoc
+1
-1
未找到文件。
spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/SendToUser.java
浏览文件 @
03e3ef53
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
6
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.
...
...
@@ -30,6 +30,9 @@ import org.springframework.core.annotation.AliasFor;
* destination(s) prepended with <code>"/user/{username}"</code> where the user name
* is extracted from the headers of the input message being handled.
*
* <p>The annotation may also be placed at class-level in which case all methods
* in the class where the annotation applies will inherit it.
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0
...
...
@@ -37,7 +40,7 @@ import org.springframework.core.annotation.AliasFor;
* @see org.springframework.messaging.simp.user.UserDestinationMessageHandler
* @see org.springframework.messaging.simp.SimpMessageHeaderAccessor#getUser()
*/
@Target
(
ElementType
.
METHOD
)
@Target
(
{
ElementType
.
METHOD
,
ElementType
.
TYPE
}
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
SendToUser
{
...
...
spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java
浏览文件 @
03e3ef53
...
...
@@ -134,7 +134,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
public
boolean
supportsReturnType
(
MethodParameter
returnType
)
{
if
(
returnType
.
getMethodAnnotation
(
SendTo
.
class
)
!=
null
||
AnnotationUtils
.
getAnnotation
(
returnType
.
getDeclaringClass
(),
SendTo
.
class
)
!=
null
||
returnType
.
getMethodAnnotation
(
SendToUser
.
class
)
!=
null
)
{
returnType
.
getMethodAnnotation
(
SendToUser
.
class
)
!=
null
||
AnnotationUtils
.
getAnnotation
(
returnType
.
getDeclaringClass
(),
SendToUser
.
class
)
!=
null
)
{
return
true
;
}
return
(!
this
.
annotationRequired
);
...
...
@@ -149,7 +150,7 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
MessageHeaders
headers
=
message
.
getHeaders
();
String
sessionId
=
SimpMessageHeaderAccessor
.
getSessionId
(
headers
);
PlaceholderResolver
varResolver
=
initVarResolver
(
headers
);
SendToUser
sendToUser
=
returnType
.
getMethodAnnotation
(
SendToUser
.
class
);
SendToUser
sendToUser
=
getSendToUser
(
returnType
);
if
(
sendToUser
!=
null
)
{
boolean
broadcast
=
sendToUser
.
broadcast
();
...
...
@@ -184,6 +185,18 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
}
}
private
SendToUser
getSendToUser
(
MethodParameter
returnType
)
{
SendToUser
annot
=
returnType
.
getMethodAnnotation
(
SendToUser
.
class
);
if
(
annot
!=
null
&&
!
ObjectUtils
.
isEmpty
((
annot
.
value
())))
{
return
annot
;
}
SendToUser
typeAnnot
=
AnnotationUtils
.
getAnnotation
(
returnType
.
getDeclaringClass
(),
SendToUser
.
class
);
if
(
typeAnnot
!=
null
&&
!
ObjectUtils
.
isEmpty
((
typeAnnot
.
value
())))
{
return
typeAnnot
;
}
return
(
annot
!=
null
?
annot
:
typeAnnot
);
}
private
SendTo
getSendTo
(
MethodParameter
returnType
)
{
SendTo
sendTo
=
returnType
.
getMethodAnnotation
(
SendTo
.
class
);
if
(
sendTo
!=
null
&&
!
ObjectUtils
.
isEmpty
((
sendTo
.
value
())))
{
...
...
spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java
浏览文件 @
03e3ef53
...
...
@@ -91,6 +91,9 @@ public class SendToMethodReturnValueHandlerTests {
private
MethodParameter
defaultNoAnnotation
;
private
MethodParameter
defaultEmptyAnnotation
;
private
MethodParameter
defaultOverrideAnnotation
;
private
MethodParameter
userDefaultNoAnnotation
;
private
MethodParameter
userDefaultEmptyAnnotation
;
private
MethodParameter
userDefaultOverrideAnnotation
;
@Before
...
...
@@ -133,14 +136,23 @@ public class SendToMethodReturnValueHandlerTests {
method
=
this
.
getClass
().
getDeclaredMethod
(
"handleAndSendToJsonView"
);
this
.
jsonViewReturnType
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
method
=
TestBean
.
class
.
getDeclaredMethod
(
"handleNoAnnotation"
);
method
=
SendTo
TestBean
.
class
.
getDeclaredMethod
(
"handleNoAnnotation"
);
this
.
defaultNoAnnotation
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
method
=
TestBean
.
class
.
getDeclaredMethod
(
"handleAndSendToDefaultDestination"
);
method
=
SendTo
TestBean
.
class
.
getDeclaredMethod
(
"handleAndSendToDefaultDestination"
);
this
.
defaultEmptyAnnotation
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
method
=
TestBean
.
class
.
getDeclaredMethod
(
"handleAndSendToOverride"
);
method
=
SendTo
TestBean
.
class
.
getDeclaredMethod
(
"handleAndSendToOverride"
);
this
.
defaultOverrideAnnotation
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
method
=
SendToUserTestBean
.
class
.
getDeclaredMethod
(
"handleNoAnnotation"
);
this
.
userDefaultNoAnnotation
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
method
=
SendToUserTestBean
.
class
.
getDeclaredMethod
(
"handleAndSendToDefaultDestination"
);
this
.
userDefaultEmptyAnnotation
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
method
=
SendToUserTestBean
.
class
.
getDeclaredMethod
(
"handleAndSendToOverride"
);
this
.
userDefaultOverrideAnnotation
=
new
SynthesizingMethodParameter
(
method
,
-
1
);
}
...
...
@@ -154,6 +166,10 @@ public class SendToMethodReturnValueHandlerTests {
assertTrue
(
this
.
handler
.
supportsReturnType
(
this
.
defaultNoAnnotation
));
assertTrue
(
this
.
handler
.
supportsReturnType
(
this
.
defaultEmptyAnnotation
));
assertTrue
(
this
.
handler
.
supportsReturnType
(
this
.
defaultOverrideAnnotation
));
assertTrue
(
this
.
handler
.
supportsReturnType
(
this
.
userDefaultNoAnnotation
));
assertTrue
(
this
.
handler
.
supportsReturnType
(
this
.
userDefaultEmptyAnnotation
));
assertTrue
(
this
.
handler
.
supportsReturnType
(
this
.
userDefaultOverrideAnnotation
));
}
@Test
...
...
@@ -230,6 +246,44 @@ public class SendToMethodReturnValueHandlerTests {
assertResponse
(
this
.
defaultOverrideAnnotation
,
sessionId
,
1
,
"/dest4"
);
}
@Test
public
void
sendToUserClassDefaultNoAnnotation
()
throws
Exception
{
given
(
this
.
messageChannel
.
send
(
any
(
Message
.
class
))).
willReturn
(
true
);
String
sessionId
=
"sess1"
;
Message
<?>
inputMessage
=
createInputMessage
(
sessionId
,
"sub1"
,
null
,
null
,
null
);
this
.
handler
.
handleReturnValue
(
PAYLOAD
,
this
.
userDefaultNoAnnotation
,
inputMessage
);
verify
(
this
.
messageChannel
,
times
(
1
)).
send
(
this
.
messageCaptor
.
capture
());
assertResponse
(
this
.
userDefaultNoAnnotation
,
sessionId
,
0
,
"/user/sess1/dest-default"
);
}
@Test
public
void
sendToUserClassDefaultEmptyAnnotation
()
throws
Exception
{
given
(
this
.
messageChannel
.
send
(
any
(
Message
.
class
))).
willReturn
(
true
);
String
sessionId
=
"sess1"
;
Message
<?>
inputMessage
=
createInputMessage
(
sessionId
,
"sub1"
,
null
,
null
,
null
);
this
.
handler
.
handleReturnValue
(
PAYLOAD
,
this
.
userDefaultEmptyAnnotation
,
inputMessage
);
verify
(
this
.
messageChannel
,
times
(
1
)).
send
(
this
.
messageCaptor
.
capture
());
assertResponse
(
this
.
userDefaultEmptyAnnotation
,
sessionId
,
0
,
"/user/sess1/dest-default"
);
}
@Test
public
void
sendToUserClassDefaultOverride
()
throws
Exception
{
given
(
this
.
messageChannel
.
send
(
any
(
Message
.
class
))).
willReturn
(
true
);
String
sessionId
=
"sess1"
;
Message
<?>
inputMessage
=
createInputMessage
(
sessionId
,
"sub1"
,
null
,
null
,
null
);
this
.
handler
.
handleReturnValue
(
PAYLOAD
,
this
.
userDefaultOverrideAnnotation
,
inputMessage
);
verify
(
this
.
messageChannel
,
times
(
2
)).
send
(
this
.
messageCaptor
.
capture
());
assertResponse
(
this
.
userDefaultOverrideAnnotation
,
sessionId
,
0
,
"/user/sess1/dest3"
);
assertResponse
(
this
.
userDefaultOverrideAnnotation
,
sessionId
,
1
,
"/user/sess1/dest4"
);
}
private
void
assertResponse
(
MethodParameter
methodParameter
,
String
sessionId
,
int
index
,
String
destination
)
{
SimpMessageHeaderAccessor
accessor
=
getCapturedAccessor
(
index
);
...
...
@@ -537,8 +591,8 @@ public class SendToMethodReturnValueHandlerTests {
return
payload
;
}
@SendTo
(
"/dest-default"
)
private
static
class
TestBean
{
@SendTo
(
"/dest-default"
)
@SuppressWarnings
(
"unused"
)
private
static
class
SendTo
TestBean
{
public
String
handleNoAnnotation
()
{
return
PAYLOAD
;
...
...
@@ -556,6 +610,25 @@ public class SendToMethodReturnValueHandlerTests {
}
@SendToUser
(
"/dest-default"
)
@SuppressWarnings
(
"unused"
)
private
static
class
SendToUserTestBean
{
public
String
handleNoAnnotation
()
{
return
PAYLOAD
;
}
@SendToUser
public
String
handleAndSendToDefaultDestination
()
{
return
PAYLOAD
;
}
@SendToUser
({
"/dest3"
,
"/dest4"
})
public
String
handleAndSendToOverride
()
{
return
PAYLOAD
;
}
}
private
interface
MyJacksonView1
{}
private
interface
MyJacksonView2
{}
...
...
src/asciidoc/web-websocket.adoc
浏览文件 @
03e3ef53
...
...
@@ -1700,7 +1700,8 @@ than their name and the generic destination. This is also supported through an
annotation
as
well
as
a
messaging
template
.
For
example
,
a
message
-
handling
method
can
send
messages
to
the
user
associated
with
the
message
being
handled
through
the
`@
SendToUser
`
annotation
:
the
message
being
handled
through
the
`@
SendToUser
`
annotation
(
also
supported
on
the
class
-
level
to
share
a
common
destination
):
[
source
,
java
,
indent
=
0
]
[
subs
=
"verbatim,quotes"
]
...
...
src/asciidoc/whats-new.adoc
浏览文件 @
03e3ef53
...
...
@@ -671,7 +671,7 @@ Spring 4.3 also improves the caching abstraction as follows:
=== WebSocket Messaging Improvements
* `@SendTo` can now be specified at class-level to share a common destination.
* `@SendTo`
and `@SendToUser`
can now be specified at class-level to share a common destination.
=== Testing Improvements
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录