Skip to content

Commit 698f923

Browse files
committed
Add @SessionAttribute with Servlet-based support
Issue: SPR-13894
1 parent 7df3a32 commit 698f923

File tree

9 files changed

+394
-42
lines changed

9 files changed

+394
-42
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,6 +126,10 @@
126126
* {@link org.springframework.validation.Errors} argument.
127127
* Instead a {@link org.springframework.web.bind.MethodArgumentNotValidException}
128128
* exception is raised.
129+
* <li>{@link SessionAttribute @SessionAttribute} annotated parameters for access
130+
* to existing, permanent session attributes (e.g. user authentication object)
131+
* as opposed to model attributes temporarily stored in the session as part of
132+
* a controller workflow via {@link SessionAttributes}.
129133
* <li>{@link org.springframework.http.HttpEntity HttpEntity&lt;?&gt;} parameters
130134
* (Servlet-only) for access to the Servlet request HTTP headers and contents.
131135
* The request stream will be converted to the entity body using
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.bind.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
/**
28+
* Annotation to bind a method parameter to a session attribute.
29+
*
30+
* <p>The main motivation is to provide convenient access to existing, permanent
31+
* session attributes (e.g. user authentication object) with an optional/required
32+
* check and a cast to the target method parameter type.
33+
*
34+
* <p>For use cases that require adding or removing session attributes consider
35+
* injecting {@code org.springframework.web.context.request.WebRequest} or
36+
* {@code javax.servlet.http.HttpSession} into the controller method.
37+
*
38+
* <p>For temporary storage of model attributes in the session as part of the
39+
* workflow for a controller, consider using {@link SessionAttributes} instead.
40+
*
41+
* @author Rossen Stoyanchev
42+
* @since 4.3
43+
* @see RequestMapping
44+
* @see SessionAttributes
45+
*/
46+
@Target(ElementType.PARAMETER)
47+
@Retention(RetentionPolicy.RUNTIME)
48+
@Documented
49+
public @interface SessionAttribute {
50+
51+
/**
52+
* Alias for {@link #name}.
53+
*/
54+
@AliasFor("name")
55+
String value() default "";
56+
57+
/**
58+
* The name of the session attribute to bind to.
59+
* <p>The default name is inferred from the method parameter name.
60+
*/
61+
@AliasFor("value")
62+
String name() default "";
63+
64+
/**
65+
* Whether the session attribute is required.
66+
* <p>Defaults to {@code true}, leading to an exception being thrown
67+
* if the attribute is missing in the session or there is no session.
68+
* Switch this to {@code false} if you prefer a {@code null} or Java 1.8+
69+
* {@code java.util.Optional} if the attribute doesn't exist.
70+
*/
71+
boolean required() default true;
72+
73+
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -290,6 +290,9 @@ public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> getExceptionHan
290290
protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
291291
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<HandlerMethodArgumentResolver>();
292292

293+
// Annotation-based argument resolution
294+
resolvers.add(new SessionAttributeMethodArgumentResolver());
295+
293296
// Type-based argument resolution
294297
resolvers.add(new ServletRequestMethodArgumentResolver());
295298
resolvers.add(new ServletResponseMethodArgumentResolver());

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ private List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
600600
resolvers.add(new RequestHeaderMapMethodArgumentResolver());
601601
resolvers.add(new ServletCookieValueMethodArgumentResolver(getBeanFactory()));
602602
resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory()));
603+
resolvers.add(new SessionAttributeMethodArgumentResolver());
603604

604605
// Type-based argument resolution
605606
resolvers.add(new ServletRequestMethodArgumentResolver());
@@ -639,6 +640,7 @@ private List<HandlerMethodArgumentResolver> getDefaultInitBinderArgumentResolver
639640
resolvers.add(new MatrixVariableMethodArgumentResolver());
640641
resolvers.add(new MatrixVariableMapMethodArgumentResolver());
641642
resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory()));
643+
resolvers.add(new SessionAttributeMethodArgumentResolver());
642644

643645
// Type-based argument resolution
644646
resolvers.add(new ServletRequestMethodArgumentResolver());
@@ -788,46 +790,50 @@ protected ModelAndView invokeHandlerMethod(HttpServletRequest request,
788790
HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
789791

790792
ServletWebRequest webRequest = new ServletWebRequest(request, response);
793+
try {
794+
WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
795+
ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
796+
797+
ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
798+
invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
799+
invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
800+
invocableMethod.setDataBinderFactory(binderFactory);
801+
invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
802+
803+
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
804+
mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
805+
modelFactory.initModel(webRequest, mavContainer, invocableMethod);
806+
mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
807+
808+
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
809+
asyncWebRequest.setTimeout(this.asyncRequestTimeout);
810+
811+
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
812+
asyncManager.setTaskExecutor(this.taskExecutor);
813+
asyncManager.setAsyncWebRequest(asyncWebRequest);
814+
asyncManager.registerCallableInterceptors(this.callableInterceptors);
815+
asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);
816+
817+
if (asyncManager.hasConcurrentResult()) {
818+
Object result = asyncManager.getConcurrentResult();
819+
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
820+
asyncManager.clearConcurrentResult();
821+
if (logger.isDebugEnabled()) {
822+
logger.debug("Found concurrent result value [" + result + "]");
823+
}
824+
invocableMethod = invocableMethod.wrapConcurrentResult(result);
825+
}
791826

792-
WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
793-
ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
794-
795-
ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
796-
invocableMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
797-
invocableMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
798-
invocableMethod.setDataBinderFactory(binderFactory);
799-
invocableMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
800-
801-
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
802-
mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
803-
modelFactory.initModel(webRequest, mavContainer, invocableMethod);
804-
mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
805-
806-
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
807-
asyncWebRequest.setTimeout(this.asyncRequestTimeout);
808-
809-
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
810-
asyncManager.setTaskExecutor(this.taskExecutor);
811-
asyncManager.setAsyncWebRequest(asyncWebRequest);
812-
asyncManager.registerCallableInterceptors(this.callableInterceptors);
813-
asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);
814-
815-
if (asyncManager.hasConcurrentResult()) {
816-
Object result = asyncManager.getConcurrentResult();
817-
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
818-
asyncManager.clearConcurrentResult();
819-
if (logger.isDebugEnabled()) {
820-
logger.debug("Found concurrent result value [" + result + "]");
827+
invocableMethod.invokeAndHandle(webRequest, mavContainer);
828+
if (asyncManager.isConcurrentHandlingStarted()) {
829+
return null;
821830
}
822-
invocableMethod = invocableMethod.wrapConcurrentResult(result);
823-
}
824831

825-
invocableMethod.invokeAndHandle(webRequest, mavContainer);
826-
if (asyncManager.isConcurrentHandlingStarted()) {
827-
return null;
832+
return getModelAndView(mavContainer, modelFactory, webRequest);
833+
}
834+
finally {
835+
webRequest.requestCompleted();
828836
}
829-
830-
return getModelAndView(mavContainer, modelFactory, webRequest);
831837
}
832838

833839
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.servlet.mvc.method.annotation;
17+
18+
import javax.servlet.ServletException;
19+
20+
import org.springframework.core.MethodParameter;
21+
import org.springframework.web.bind.ServletRequestBindingException;
22+
import org.springframework.web.bind.annotation.SessionAttribute;
23+
import org.springframework.web.bind.annotation.ValueConstants;
24+
import org.springframework.web.context.request.NativeWebRequest;
25+
import org.springframework.web.context.request.RequestAttributes;
26+
import org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver;
27+
28+
/**
29+
* Resolves method arguments annotated with an @{@link SessionAttribute}.
30+
*
31+
* @author Rossen Stoyanchev
32+
* @since 4.3
33+
*/
34+
public class SessionAttributeMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
35+
36+
37+
@Override
38+
public boolean supportsParameter(MethodParameter parameter) {
39+
return parameter.hasParameterAnnotation(SessionAttribute.class);
40+
}
41+
42+
43+
@Override
44+
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
45+
SessionAttribute annot = parameter.getParameterAnnotation(SessionAttribute.class);
46+
return new NamedValueInfo(annot.name(), annot.required(), ValueConstants.DEFAULT_NONE);
47+
}
48+
49+
@Override
50+
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request){
51+
return request.getAttribute(name, RequestAttributes.SCOPE_SESSION);
52+
}
53+
54+
@Override
55+
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException {
56+
throw new ServletRequestBindingException("Missing session attribute '" + name +
57+
"' of type " + parameter.getNestedParameterType().getSimpleName());
58+
}
59+
60+
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterIntegrationTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.springframework.web.bind.annotation.RequestPart;
6969
import org.springframework.web.bind.annotation.ResponseBody;
7070
import org.springframework.web.bind.annotation.ResponseStatus;
71+
import org.springframework.web.bind.annotation.SessionAttribute;
7172
import org.springframework.web.bind.annotation.SessionAttributes;
7273
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
7374
import org.springframework.web.bind.support.SessionStatus;
@@ -148,12 +149,13 @@ public void teardown() {
148149
public void handle() throws Exception {
149150
Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, String.class, String.class, Map.class,
150151
Date.class, Map.class, String.class, String.class, TestBean.class, Errors.class, TestBean.class,
151-
Color.class, HttpServletRequest.class, HttpServletResponse.class, User.class, OtherUser.class,
152-
Model.class, UriComponentsBuilder.class };
152+
Color.class, HttpServletRequest.class, HttpServletResponse.class, TestBean.class,
153+
User.class, OtherUser.class, Model.class, UriComponentsBuilder.class };
153154

154155
String datePattern = "yyyy.MM.dd";
155156
String formattedDate = "2011.03.16";
156157
Date date = new GregorianCalendar(2011, Calendar.MARCH, 16).getTime();
158+
TestBean sessionAttribute = new TestBean();
157159

158160
request.addHeader("Content-Type", "text/plain; charset=utf-8");
159161
request.addHeader("header", "headerValue");
@@ -171,6 +173,7 @@ public void handle() throws Exception {
171173
Map<String, String> uriTemplateVars = new HashMap<String, String>();
172174
uriTemplateVars.put("pathvar", "pathvarValue");
173175
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
176+
request.getSession().setAttribute("sessionAttribute", sessionAttribute);
174177

175178
HandlerMethod handlerMethod = handlerMethod("handle", parameterTypes);
176179
ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
@@ -215,6 +218,8 @@ public void handle() throws Exception {
215218
assertEquals(User.class, model.get("user").getClass());
216219
assertEquals(OtherUser.class, model.get("otherUser").getClass());
217220

221+
assertSame(sessionAttribute, model.get("sessionAttribute"));
222+
218223
assertEquals(new URI("http://localhost/contextPath/main/path"), model.get("url"));
219224
}
220225

@@ -363,6 +368,7 @@ public String handle(
363368
Color customArg,
364369
HttpServletRequest request,
365370
HttpServletResponse response,
371+
@SessionAttribute TestBean sessionAttribute,
366372
User user,
367373
@ModelAttribute OtherUser otherUser,
368374
Model model,
@@ -373,6 +379,7 @@ public String handle(
373379
.addAttribute("dateParam", dateParam).addAttribute("paramMap", paramMap)
374380
.addAttribute("paramByConvention", paramByConvention).addAttribute("value", value)
375381
.addAttribute("customArg", customArg).addAttribute(user)
382+
.addAttribute("sessionAttribute", sessionAttribute)
376383
.addAttribute("url", builder.path("/path").build().toUri());
377384

378385
assertNotNull(request);

0 commit comments

Comments
 (0)