Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sesemomo
spring-framework
提交
7de6cfa1
S
spring-framework
项目概览
sesemomo
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
7de6cfa1
编写于
6月 29, 2017
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor WebSession#getAttribute options
Issue: SPR-15718
上级
9253facf
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
54 addition
and
30 deletion
+54
-30
spring-test/src/test/java/org/springframework/test/web/reactive/server/MockServerIntegrationTests.java
.../test/web/reactive/server/MockServerIntegrationTests.java
+2
-2
spring-web/src/main/java/org/springframework/web/server/WebSession.java
.../main/java/org/springframework/web/server/WebSession.java
+35
-3
spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSession.java
...springframework/web/server/session/DefaultWebSession.java
+0
-6
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolver.java
...od/annotation/SessionAttributeMethodArgumentResolver.java
+2
-3
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java
...notation/SessionAttributeMethodArgumentResolverTests.java
+15
-16
未找到文件。
spring-test/src/test/java/org/springframework/test/web/reactive/server/MockServerIntegrationTests.java
浏览文件 @
7de6cfa1
...
@@ -43,9 +43,9 @@ public class MockServerIntegrationTests {
...
@@ -43,9 +43,9 @@ public class MockServerIntegrationTests {
}
}
else
{
else
{
return
exchange
.
getSession
()
return
exchange
.
getSession
()
.
map
(
session
->
session
.
getAttribute
(
"foo"
).
orElse
(
"none"
))
.
map
(
session
->
session
.
getAttribute
OrDefault
(
"foo"
,
"none"
))
.
flatMap
(
value
->
{
.
flatMap
(
value
->
{
byte
[]
bytes
=
value
.
toString
().
getBytes
(
UTF_8
);
byte
[]
bytes
=
value
.
getBytes
(
UTF_8
);
DataBuffer
buffer
=
new
DefaultDataBufferFactory
().
wrap
(
bytes
);
DataBuffer
buffer
=
new
DefaultDataBufferFactory
().
wrap
(
bytes
);
return
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
));
return
exchange
.
getResponse
().
writeWith
(
Mono
.
just
(
buffer
));
});
});
...
...
spring-web/src/main/java/org/springframework/web/server/WebSession.java
浏览文件 @
7de6cfa1
...
@@ -19,10 +19,12 @@ package org.springframework.web.server;
...
@@ -19,10 +19,12 @@ package org.springframework.web.server;
import
java.time.Duration
;
import
java.time.Duration
;
import
java.time.Instant
;
import
java.time.Instant
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Optional
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
/**
/**
* Main contract for using a server-side session that provides access to session
* Main contract for using a server-side session that provides access to session
* attributes across HTTP requests.
* attributes across HTTP requests.
...
@@ -48,12 +50,42 @@ public interface WebSession {
...
@@ -48,12 +50,42 @@ public interface WebSession {
Map
<
String
,
Object
>
getAttributes
();
Map
<
String
,
Object
>
getAttributes
();
/**
/**
* Return the attribute value if present.
* Return the session attribute value if present.
* @param name the attribute name
* @param <T> the attribute type
* @return the attribute value
*/
@SuppressWarnings
(
"unchecked"
)
@Nullable
default
<
T
>
T
getAttribute
(
String
name
)
{
return
(
T
)
getAttributes
().
get
(
name
);
}
/**
* Return the session attribute value or if not present raise an
* {@link IllegalArgumentException}.
* @param name the attribute name
* @param <T> the attribute type
* @return the attribute value
*/
@SuppressWarnings
(
"unchecked"
)
default
<
T
>
T
getRequiredAttribute
(
String
name
)
{
T
value
=
getAttribute
(
name
);
Assert
.
notNull
(
value
,
"Required attribute '"
+
name
+
"' is missing."
);
return
value
;
}
/**
* Return the session attribute value, or a default, fallback value.
* @param name the attribute name
* @param name the attribute name
* @param defaultValue a default value to return instead
* @param <T> the attribute type
* @param <T> the attribute type
* @return the attribute value
* @return the attribute value
*/
*/
<
T
>
Optional
<
T
>
getAttribute
(
String
name
);
@SuppressWarnings
(
"unchecked"
)
default
<
T
>
T
getAttributeOrDefault
(
String
name
,
T
defaultValue
)
{
return
(
T
)
getAttributes
().
getOrDefault
(
name
,
defaultValue
);
}
/**
/**
* Force the creation of a session causing the session id to be sent when
* Force the creation of a session causing the session id to be sent when
...
...
spring-web/src/main/java/org/springframework/web/server/session/DefaultWebSession.java
浏览文件 @
7de6cfa1
...
@@ -20,7 +20,6 @@ import java.time.Clock;
...
@@ -20,7 +20,6 @@ import java.time.Clock;
import
java.time.Duration
;
import
java.time.Duration
;
import
java.time.Instant
;
import
java.time.Instant
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicReference
;
import
java.util.concurrent.atomic.AtomicReference
;
import
java.util.function.Supplier
;
import
java.util.function.Supplier
;
...
@@ -108,11 +107,6 @@ public class DefaultWebSession implements ConfigurableWebSession, Serializable {
...
@@ -108,11 +107,6 @@ public class DefaultWebSession implements ConfigurableWebSession, Serializable {
return
this
.
attributes
;
return
this
.
attributes
;
}
}
@Override
@SuppressWarnings
(
"unchecked"
)
public
<
T
>
Optional
<
T
>
getAttribute
(
String
name
)
{
return
Optional
.
ofNullable
((
T
)
this
.
attributes
.
get
(
name
));
}
@Override
@Override
public
Instant
getCreationTime
()
{
public
Instant
getCreationTime
()
{
return
this
.
creationTime
;
return
this
.
creationTime
;
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolver.java
浏览文件 @
7de6cfa1
...
@@ -58,9 +58,8 @@ public class SessionAttributeMethodArgumentResolver extends AbstractNamedValueAr
...
@@ -58,9 +58,8 @@ public class SessionAttributeMethodArgumentResolver extends AbstractNamedValueAr
@Override
@Override
protected
Mono
<
Object
>
resolveName
(
String
name
,
MethodParameter
parameter
,
ServerWebExchange
exchange
)
{
protected
Mono
<
Object
>
resolveName
(
String
name
,
MethodParameter
parameter
,
ServerWebExchange
exchange
)
{
return
exchange
.
getSession
()
return
exchange
.
getSession
()
.
map
(
session
->
session
.
getAttribute
(
name
))
.
filter
(
session
->
session
.
getAttribute
(
name
)
!=
null
)
.
filter
(
Optional:
:
isPresent
)
.
map
(
session
->
session
.
getAttribute
(
name
));
.
map
(
Optional:
:
get
);
}
}
@Override
@Override
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributeMethodArgumentResolverTests.java
浏览文件 @
7de6cfa1
...
@@ -47,7 +47,6 @@ import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
...
@@ -47,7 +47,6 @@ import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
import
org.springframework.web.server.session.MockWebSessionManager
;
import
org.springframework.web.server.session.MockWebSessionManager
;
import
org.springframework.web.server.session.WebSessionManager
;
import
org.springframework.web.server.session.WebSessionManager
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
...
@@ -103,7 +102,7 @@ public class SessionAttributeMethodArgumentResolverTests {
...
@@ -103,7 +102,7 @@ public class SessionAttributeMethodArgumentResolverTests {
StepVerifier
.
create
(
mono
).
expectError
(
ServerWebInputException
.
class
).
verify
();
StepVerifier
.
create
(
mono
).
expectError
(
ServerWebInputException
.
class
).
verify
();
Foo
foo
=
new
Foo
();
Foo
foo
=
new
Foo
();
when
(
this
.
session
.
getAttribute
(
"foo"
)).
thenReturn
(
Optional
.
of
(
foo
)
);
when
(
this
.
session
.
getAttribute
(
"foo"
)).
thenReturn
(
foo
);
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
assertSame
(
foo
,
mono
.
block
());
assertSame
(
foo
,
mono
.
block
());
}
}
...
@@ -112,7 +111,7 @@ public class SessionAttributeMethodArgumentResolverTests {
...
@@ -112,7 +111,7 @@ public class SessionAttributeMethodArgumentResolverTests {
public
void
resolveWithName
()
throws
Exception
{
public
void
resolveWithName
()
throws
Exception
{
MethodParameter
param
=
initMethodParameter
(
1
);
MethodParameter
param
=
initMethodParameter
(
1
);
Foo
foo
=
new
Foo
();
Foo
foo
=
new
Foo
();
when
(
this
.
session
.
getAttribute
(
"specialFoo"
)).
thenReturn
(
Optional
.
of
(
foo
)
);
when
(
this
.
session
.
getAttribute
(
"specialFoo"
)).
thenReturn
(
foo
);
Mono
<
Object
>
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
Mono
<
Object
>
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
assertSame
(
foo
,
mono
.
block
());
assertSame
(
foo
,
mono
.
block
());
}
}
...
@@ -124,32 +123,32 @@ public class SessionAttributeMethodArgumentResolverTests {
...
@@ -124,32 +123,32 @@ public class SessionAttributeMethodArgumentResolverTests {
assertNull
(
mono
.
block
());
assertNull
(
mono
.
block
());
Foo
foo
=
new
Foo
();
Foo
foo
=
new
Foo
();
when
(
this
.
session
.
getAttribute
(
"foo"
)).
thenReturn
(
Optional
.
of
(
foo
)
);
when
(
this
.
session
.
getAttribute
(
"foo"
)).
thenReturn
(
foo
);
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
assertSame
(
foo
,
mono
.
block
());
assertSame
(
foo
,
mono
.
block
());
}
}
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
resolveOptional
()
throws
Exception
{
public
void
resolveOptional
()
throws
Exception
{
MethodParameter
param
=
initMethodParameter
(
3
);
MethodParameter
param
=
initMethodParameter
(
3
);
Mono
<
Object
>
mono
=
this
.
resolver
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
);
Optional
<
Object
>
actual
=
(
Optional
<
Object
>)
this
.
resolver
assertNotNull
(
mono
.
block
());
.
resolveArgument
(
param
,
new
BindingContext
(),
this
.
exchange
).
block
();
assertEquals
(
Optional
.
class
,
mono
.
block
().
getClass
());
assertFalse
(((
Optional
<?>)
mono
.
block
()).
isPresent
());
assertNotNull
(
actual
);
assertFalse
(
actual
.
isPresent
());
ConfigurableWebBindingInitializer
initializer
=
new
ConfigurableWebBindingInitializer
();
ConfigurableWebBindingInitializer
initializer
=
new
ConfigurableWebBindingInitializer
();
initializer
.
setConversionService
(
new
DefaultFormattingConversionService
());
initializer
.
setConversionService
(
new
DefaultFormattingConversionService
());
BindingContext
bindingContext
=
new
BindingContext
(
initializer
);
BindingContext
bindingContext
=
new
BindingContext
(
initializer
);
Foo
foo
=
new
Foo
();
Foo
foo
=
new
Foo
();
when
(
this
.
session
.
getAttribute
(
"foo"
)).
thenReturn
(
Optional
.
of
(
foo
));
when
(
this
.
session
.
getAttribute
(
"foo"
)).
thenReturn
(
foo
);
mono
=
this
.
resolver
.
resolveArgument
(
param
,
bindingContext
,
this
.
exchange
);
actual
=
(
Optional
<
Object
>)
this
.
resolver
.
resolveArgument
(
param
,
bindingContext
,
this
.
exchange
).
block
();
assertNotNull
(
mono
.
block
());
assertNotNull
(
actual
);
assertEquals
(
Optional
.
class
,
mono
.
block
().
getClass
());
assertTrue
(
actual
.
isPresent
());
Optional
<?>
optional
=
(
Optional
<?>)
mono
.
block
();
assertSame
(
foo
,
actual
.
get
());
assertTrue
(
optional
.
isPresent
());
assertSame
(
foo
,
optional
.
get
());
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录