Skip to content

Commit 6f05c84

Browse files
vpavicrwinch
authored andcommitted
Rename HttpSessionStrategy to HttpSessionIdResolver
This commit harmonizes `HttpSessionStrategy` with Spring Framework's `WebSessionIdResolver` by renaming it to `WebSessionIdResolver`.
1 parent cd394bb commit 6f05c84

File tree

12 files changed

+185
-201
lines changed

12 files changed

+185
-201
lines changed

samples/javaconfig/rest/src/integration-test/java/rest/RestMockMvcTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.springframework.security.test.context.support.WithMockUser;
3232
import org.springframework.session.Session;
3333
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
34-
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
35-
import org.springframework.session.web.http.HttpSessionStrategy;
34+
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
35+
import org.springframework.session.web.http.HttpSessionIdResolver;
3636
import org.springframework.session.web.http.SessionRepositoryFilter;
3737
import org.springframework.test.context.ContextConfiguration;
3838
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -104,8 +104,8 @@ public LettuceConnectionFactory redisConnectionFactory() {
104104
}
105105

106106
@Bean
107-
public HttpSessionStrategy httpSessionStrategy() {
108-
return new HeaderHttpSessionStrategy();
107+
public HttpSessionIdResolver httpSessionIdResolver() {
108+
return new HeaderHttpSessionIdResolver();
109109
}
110110

111111
}

samples/javaconfig/rest/src/main/java/sample/HttpSessionConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.springframework.context.annotation.Import;
2222
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
2323
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
24-
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
25-
import org.springframework.session.web.http.HttpSessionStrategy;
24+
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
25+
import org.springframework.session.web.http.HttpSessionIdResolver;
2626

2727
@Import(EmbeddedRedisConfig.class)
2828
// tag::class[]
@@ -36,8 +36,8 @@ public LettuceConnectionFactory connectionFactory() {
3636
}
3737

3838
@Bean
39-
public HttpSessionStrategy httpSessionStrategy() {
40-
return new HeaderHttpSessionStrategy(); // <3>
39+
public HttpSessionIdResolver httpSessionIdResolver() {
40+
return new HeaderHttpSessionIdResolver(); // <3>
4141
}
4242
}
4343
// end::class[]

spring-session-core/src/main/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import org.springframework.session.events.SessionCreatedEvent;
3939
import org.springframework.session.events.SessionDestroyedEvent;
4040
import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices;
41-
import org.springframework.session.web.http.CookieHttpSessionStrategy;
41+
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
4242
import org.springframework.session.web.http.CookieSerializer;
4343
import org.springframework.session.web.http.DefaultCookieSerializer;
44-
import org.springframework.session.web.http.HttpSessionStrategy;
44+
import org.springframework.session.web.http.HttpSessionIdResolver;
4545
import org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter;
4646
import org.springframework.session.web.http.SessionRepositoryFilter;
4747
import org.springframework.util.ClassUtils;
@@ -96,23 +96,23 @@ public class SpringHttpSessionConfiguration implements ApplicationContextAware {
9696

9797
private final Log logger = LogFactory.getLog(getClass());
9898

99-
private CookieHttpSessionStrategy defaultHttpSessionStrategy = new CookieHttpSessionStrategy();
99+
private CookieHttpSessionIdResolver defaultHttpSessionIdResolver = new CookieHttpSessionIdResolver();
100100

101101
private boolean usesSpringSessionRememberMeServices;
102102

103103
private ServletContext servletContext;
104104

105105
private CookieSerializer cookieSerializer;
106106

107-
private HttpSessionStrategy httpSessionStrategy = this.defaultHttpSessionStrategy;
107+
private HttpSessionIdResolver httpSessionIdResolver = this.defaultHttpSessionIdResolver;
108108

109109
private List<HttpSessionListener> httpSessionListeners = new ArrayList<>();
110110

111111
@PostConstruct
112112
public void init() {
113113
CookieSerializer cookieSerializer = this.cookieSerializer != null
114114
? this.cookieSerializer : createDefaultCookieSerializer();
115-
this.defaultHttpSessionStrategy.setCookieSerializer(cookieSerializer);
115+
this.defaultHttpSessionIdResolver.setCookieSerializer(cookieSerializer);
116116
}
117117

118118
@Bean
@@ -126,7 +126,7 @@ public <S extends Session> SessionRepositoryFilter<? extends Session> springSess
126126
SessionRepositoryFilter<S> sessionRepositoryFilter = new SessionRepositoryFilter<>(
127127
sessionRepository);
128128
sessionRepositoryFilter.setServletContext(this.servletContext);
129-
sessionRepositoryFilter.setHttpSessionStrategy(this.httpSessionStrategy);
129+
sessionRepositoryFilter.setHttpSessionIdResolver(this.httpSessionIdResolver);
130130
return sessionRepositoryFilter;
131131
}
132132

@@ -153,8 +153,8 @@ public void setCookieSerializer(CookieSerializer cookieSerializer) {
153153
}
154154

155155
@Autowired(required = false)
156-
public void setHttpSessionStrategy(HttpSessionStrategy httpSessionStrategy) {
157-
this.httpSessionStrategy = httpSessionStrategy;
156+
public void setHttpSessionIdResolver(HttpSessionIdResolver httpSessionIdResolver) {
157+
this.httpSessionIdResolver = httpSessionIdResolver;
158158
}
159159

160160
@Autowired(required = false)

spring-session-core/src/main/java/org/springframework/session/web/http/CookieHttpSessionStrategy.java renamed to spring-session-core/src/main/java/org/springframework/session/web/http/CookieHttpSessionIdResolver.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
import javax.servlet.http.HttpServletRequest;
2222
import javax.servlet.http.HttpServletResponse;
2323

24-
import org.springframework.session.Session;
2524
import org.springframework.session.web.http.CookieSerializer.CookieValue;
2625

2726
/**
28-
* A {@link HttpSessionStrategy} that uses a cookie to obtain the session from.
27+
* A {@link HttpSessionIdResolver} that uses a cookie to obtain the session from.
2928
* Specifically, this implementation will allow specifying a cookie serialization strategy
30-
* using {@link CookieHttpSessionStrategy#setCookieSerializer(CookieSerializer)}. The
29+
* using {@link CookieHttpSessionIdResolver#setCookieSerializer(CookieSerializer)}. The
3130
* default is cookie name is "SESSION".
3231
*
3332
* When a session is created, the HTTP response will have a cookie with the specified
@@ -62,22 +61,21 @@
6261
* @author Vedran Pavic
6362
* @since 1.0
6463
*/
65-
public final class CookieHttpSessionStrategy implements HttpSessionStrategy {
64+
public final class CookieHttpSessionIdResolver implements HttpSessionIdResolver {
6665

67-
private static final String WRITTEN_SESSION_ID_ATTR = CookieHttpSessionStrategy.class
66+
private static final String WRITTEN_SESSION_ID_ATTR = CookieHttpSessionIdResolver.class
6867
.getName().concat(".WRITTEN_SESSION_ID_ATTR");
6968

7069
private CookieSerializer cookieSerializer = new DefaultCookieSerializer();
7170

7271
@Override
73-
public List<String> getRequestedSessionIds(HttpServletRequest request) {
72+
public List<String> resolveSessionIds(HttpServletRequest request) {
7473
return this.cookieSerializer.readCookieValues(request);
7574
}
7675

7776
@Override
78-
public void onNewSession(Session session, HttpServletRequest request,
79-
HttpServletResponse response) {
80-
String sessionId = session.getId();
77+
public void setSessionId(HttpServletRequest request, HttpServletResponse response,
78+
String sessionId) {
8179
if (sessionId.equals(request.getAttribute(WRITTEN_SESSION_ID_ATTR))) {
8280
return;
8381
}
@@ -87,8 +85,7 @@ public void onNewSession(Session session, HttpServletRequest request,
8785
}
8886

8987
@Override
90-
public void onInvalidateSession(HttpServletRequest request,
91-
HttpServletResponse response) {
88+
public void expireSession(HttpServletRequest request, HttpServletResponse response) {
9289
this.cookieSerializer.writeCookieValue(new CookieValue(request, response, ""));
9390
}
9491

spring-session-core/src/main/java/org/springframework/session/web/http/HeaderHttpSessionStrategy.java renamed to spring-session-core/src/main/java/org/springframework/session/web/http/HeaderHttpSessionIdResolver.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25-
import org.springframework.session.Session;
26-
2725
/**
28-
* A {@link HttpSessionStrategy} that uses a header to obtain the session from.
26+
* A {@link HttpSessionIdResolver} that uses a header to obtain the session from.
2927
* Specifically, this implementation will allow specifying a header name using
30-
* {@link HeaderHttpSessionStrategy#setHeaderName(String)}. The default is "X-Auth-Token".
28+
* {@link HeaderHttpSessionIdResolver#setHeaderName(String)}. The default is
29+
* "X-Auth-Token".
3130
*
3231
* When a session is created, the HTTP response will have a response header of the
3332
* specified name and the value of the session id. For example:
@@ -58,26 +57,25 @@
5857
* @author Vedran Pavic
5958
* @since 1.0
6059
*/
61-
public class HeaderHttpSessionStrategy implements HttpSessionStrategy {
60+
public class HeaderHttpSessionIdResolver implements HttpSessionIdResolver {
6261

6362
private String headerName = "X-Auth-Token";
6463

6564
@Override
66-
public List<String> getRequestedSessionIds(HttpServletRequest request) {
65+
public List<String> resolveSessionIds(HttpServletRequest request) {
6766
String headerValue = request.getHeader(this.headerName);
6867
return headerValue != null ? Collections.singletonList(headerValue)
6968
: Collections.emptyList();
7069
}
7170

7271
@Override
73-
public void onNewSession(Session session, HttpServletRequest request,
74-
HttpServletResponse response) {
75-
response.setHeader(this.headerName, session.getId());
72+
public void setSessionId(HttpServletRequest request, HttpServletResponse response,
73+
String sessionId) {
74+
response.setHeader(this.headerName, sessionId);
7675
}
7776

7877
@Override
79-
public void onInvalidateSession(HttpServletRequest request,
80-
HttpServletResponse response) {
78+
public void expireSession(HttpServletRequest request, HttpServletResponse response) {
8179
response.setHeader(this.headerName, "");
8280
}
8381

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2014-2017 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.session.web.http;
18+
19+
import java.util.List;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
24+
/**
25+
* Contract for session id resolution strategies. Allows for session id resolution through
26+
* the request and for sending the session id or expiring the session through the
27+
* response.
28+
*
29+
* @author Rob Winch
30+
* @author Vedran Pavic
31+
* @since 1.0
32+
*/
33+
public interface HttpSessionIdResolver {
34+
35+
/**
36+
* Resolve the session ids associated with the provided {@link HttpServletRequest}.
37+
* For example, the session id might come from a cookie or a request header.
38+
* @param request the current request
39+
* @return the session ids
40+
*/
41+
List<String> resolveSessionIds(HttpServletRequest request);
42+
43+
/**
44+
* Send the given session id to the client. This method is invoked when a new session
45+
* is created and should inform a client what the new session id is. For example, it
46+
* might create a new cookie with the session id in it or set an HTTP response header
47+
* with the value of the new session id.
48+
* @param request the current request
49+
* @param response the current response
50+
* @param sessionId the session id
51+
*/
52+
void setSessionId(HttpServletRequest request, HttpServletResponse response,
53+
String sessionId);
54+
55+
/**
56+
* Instruct the client to end the current session. This method is invoked when a
57+
* session is invalidated and should inform a client that the session id is no longer
58+
* valid. For example, it might remove a cookie with the session id in it or set an
59+
* HTTP response header with an empty value indicating to the client to no longer
60+
* submit that session id.
61+
* @param request the current request
62+
* @param response the current response
63+
*/
64+
void expireSession(HttpServletRequest request, HttpServletResponse response);
65+
66+
}

spring-session-core/src/main/java/org/springframework/session/web/http/HttpSessionStrategy.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)