Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
d03fb895
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,体验更适合开发者的 AI 搜索 >>
提交
d03fb895
编写于
1月 21, 2014
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Encode user names in user destanations
Issue: SPR-11302
上级
d96b91a5
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
104 addition
and
9 deletion
+104
-9
spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessagingTemplate.java
...springframework/messaging/simp/SimpMessagingTemplate.java
+15
-9
spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java
...k/messaging/simp/user/DefaultUserDestinationResolver.java
+2
-0
spring-messaging/src/test/java/org/springframework/messaging/simp/SimpMessagingTemplateTests.java
...gframework/messaging/simp/SimpMessagingTemplateTests.java
+72
-0
spring-messaging/src/test/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolverTests.java
...saging/simp/user/DefaultUserDestinationResolverTests.java
+15
-0
未找到文件。
spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessagingTemplate.java
浏览文件 @
d03fb895
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -26,10 +26,17 @@ import org.springframework.messaging.core.AbstractMessageSendingTemplate;
import
org.springframework.messaging.core.MessagePostProcessor
;
import
org.springframework.messaging.support.MessageBuilder
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
/**
* A specialization of {@link AbstractMessageSendingTemplate} that adds String-based
* destinations as a message header.
* A specialization of {@link AbstractMessageSendingTemplate} that interprets a
* String-based destination as the
* {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor#DESTINATION_HEADER DESTINATION_HEADER}
* to be added to the headers of sent messages.
* <p>
* Also provides methods for sending messages to a user. See
* {@link org.springframework.messaging.simp.user.UserDestinationResolver UserDestinationResolver}
* for more on user destinations.
*
* @author Rossen Stoyanchev
* @since 4.0
...
...
@@ -127,30 +134,29 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
@Override
public
void
convertAndSendToUser
(
String
user
,
String
destination
,
Object
payload
)
throws
MessagingException
{
MessagePostProcessor
postProcessor
=
null
;
this
.
convertAndSendToUser
(
user
,
destination
,
payload
,
postProcessor
);
this
.
convertAndSendToUser
(
user
,
destination
,
payload
,
(
MessagePostProcessor
)
null
);
}
@Override
public
void
convertAndSendToUser
(
String
user
,
String
destination
,
Object
payload
,
Map
<
String
,
Object
>
headers
)
throws
MessagingException
{
MessagePostProcessor
postProcessor
=
null
;
this
.
convertAndSendToUser
(
user
,
destination
,
payload
,
headers
,
postProcessor
);
this
.
convertAndSendToUser
(
user
,
destination
,
payload
,
headers
,
null
);
}
@Override
public
void
convertAndSendToUser
(
String
user
,
String
destination
,
Object
payload
,
MessagePostProcessor
postProcessor
)
throws
MessagingException
{
Map
<
String
,
Object
>
headers
=
null
;
this
.
convertAndSendToUser
(
user
,
destination
,
payload
,
headers
,
postProcessor
);
this
.
convertAndSendToUser
(
user
,
destination
,
payload
,
null
,
postProcessor
);
}
@Override
public
void
convertAndSendToUser
(
String
user
,
String
destination
,
Object
payload
,
Map
<
String
,
Object
>
headers
,
MessagePostProcessor
postProcessor
)
throws
MessagingException
{
Assert
.
notNull
(
user
,
"User must not be null"
);
user
=
StringUtils
.
replace
(
user
,
"/"
,
"%2F"
);
super
.
convertAndSend
(
this
.
userDestinationPrefix
+
user
+
destination
,
payload
,
headers
,
postProcessor
);
}
...
...
spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java
浏览文件 @
d03fb895
...
...
@@ -22,6 +22,7 @@ import org.springframework.messaging.Message;
import
org.springframework.messaging.simp.SimpMessageHeaderAccessor
;
import
org.springframework.messaging.simp.SimpMessageType
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
import
java.security.Principal
;
import
java.util.Collections
;
...
...
@@ -138,6 +139,7 @@ public class DefaultUserDestinationResolver implements UserDestinationResolver {
int
endIndex
=
destination
.
indexOf
(
'/'
,
startIndex
);
Assert
.
isTrue
(
endIndex
>
0
,
"Expected destination pattern \"/principal/{userId}/**\""
);
targetUser
=
destination
.
substring
(
startIndex
,
endIndex
);
targetUser
=
StringUtils
.
replace
(
targetUser
,
"%2F"
,
"/"
);
targetDestination
=
destination
.
substring
(
endIndex
);
targetSessionIds
=
this
.
userSessionRegistry
.
getSessionIds
(
targetUser
);
}
...
...
spring-messaging/src/test/java/org/springframework/messaging/simp/SimpMessagingTemplateTests.java
0 → 100644
浏览文件 @
d03fb895
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.messaging.simp
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.messaging.Message
;
import
org.springframework.messaging.StubMessageChannel
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
/**
* Unit tests for {@link org.springframework.messaging.simp.SimpMessagingTemplate}.
*
* @author Rossen Stoyanchev
*/
public
class
SimpMessagingTemplateTests
{
private
SimpMessagingTemplate
messagingTemplate
;
private
StubMessageChannel
messageChannel
;
@Before
public
void
setup
()
{
this
.
messageChannel
=
new
StubMessageChannel
();
this
.
messagingTemplate
=
new
SimpMessagingTemplate
(
messageChannel
);
}
@Test
public
void
convertAndSendToUser
()
{
this
.
messagingTemplate
.
convertAndSendToUser
(
"joe"
,
"/queue/foo"
,
"data"
);
List
<
Message
<
byte
[]>>
messages
=
this
.
messageChannel
.
getMessages
();
assertEquals
(
1
,
messages
.
size
());
Message
<
byte
[]>
message
=
messages
.
get
(
0
);
SimpMessageHeaderAccessor
headers
=
SimpMessageHeaderAccessor
.
wrap
(
message
);
assertEquals
(
SimpMessageType
.
MESSAGE
,
headers
.
getMessageType
());
assertEquals
(
"/user/joe/queue/foo"
,
headers
.
getDestination
());
}
@Test
public
void
convertAndSendToUserWithEncoding
()
{
this
.
messagingTemplate
.
convertAndSendToUser
(
"http://joe.openid.example.org/"
,
"/queue/foo"
,
"data"
);
List
<
Message
<
byte
[]>>
messages
=
this
.
messageChannel
.
getMessages
();
assertEquals
(
1
,
messages
.
size
());
Message
<
byte
[]>
message
=
messages
.
get
(
0
);
SimpMessageHeaderAccessor
headers
=
SimpMessageHeaderAccessor
.
wrap
(
message
);
assertEquals
(
"/user/http:%2F%2Fjoe.openid.example.org%2F/queue/foo"
,
headers
.
getDestination
());
}
}
spring-messaging/src/test/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolverTests.java
浏览文件 @
d03fb895
...
...
@@ -23,6 +23,7 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import
org.springframework.messaging.simp.SimpMessageType
;
import
org.springframework.messaging.simp.TestPrincipal
;
import
org.springframework.messaging.support.MessageBuilder
;
import
org.springframework.util.StringUtils
;
import
java.util.Set
;
...
...
@@ -30,6 +31,8 @@ import static org.junit.Assert.assertEquals;
/**
* Unit tests for {@link org.springframework.messaging.simp.user.DefaultUserDestinationResolver}.
*
* @author Rossen Stoyanchev
*/
public
class
DefaultUserDestinationResolverTests
{
...
...
@@ -93,6 +96,18 @@ public class DefaultUserDestinationResolverTests {
assertEquals
(
"/queue/foo-user123"
,
actual
.
iterator
().
next
());
}
@Test
public
void
handleMessageEncodedUserName
()
{
String
userName
=
"http://joe.openid.example.org/"
;
this
.
registry
.
registerSessionId
(
userName
,
"openid123"
);
String
destination
=
"/user/"
+
StringUtils
.
replace
(
userName
,
"/"
,
"%2F"
)
+
"/queue/foo"
;
Message
<?>
message
=
createMessage
(
SimpMessageType
.
MESSAGE
,
this
.
user
,
SESSION_ID
,
destination
);
Set
<
String
>
actual
=
this
.
resolver
.
resolveDestination
(
message
);
assertEquals
(
1
,
actual
.
size
());
assertEquals
(
"/queue/foo-useropenid123"
,
actual
.
iterator
().
next
());
}
@Test
public
void
ignoreMessage
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录