Skip to content

Rename getRequestedSessionId -> getRequestedSessionIds #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -135,6 +136,7 @@
*
*
* @author Rob Winch
* @author Eddú Meléndez
* @since 1.0
*/
public final class CookieHttpSessionStrategy
Expand Down Expand Up @@ -168,10 +170,11 @@ public final class CookieHttpSessionStrategy
*/
private String serializationDelimiter = DEFAULT_DELIMITER;

public String getRequestedSessionId(HttpServletRequest request) {
public List<String> getRequestedSessionIds(HttpServletRequest request) {
Map<String, String> sessionIds = getSessionIds(request);
String sessionAlias = getCurrentSessionAlias(request);
return sessionIds.get(sessionAlias);
String sessionId = sessionIds.get(sessionAlias);
return sessionId == null ? Collections.<String>emptyList() : Collections.singletonList(sessionId);
}

public String getCurrentSessionAlias(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.session.web.http;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand Down Expand Up @@ -53,13 +56,15 @@
* </pre>
*
* @author Rob Winch
* @author Eddú Meléndez
* @since 1.0
*/
public class HeaderHttpSessionStrategy implements HttpSessionStrategy {
private String headerName = "x-auth-token";

public String getRequestedSessionId(HttpServletRequest request) {
return request.getHeader(this.headerName);
public List<String> getRequestedSessionIds(HttpServletRequest request) {
String headerValue = request.getHeader(this.headerName);
return headerValue == null ? Collections.<String>emptyList() : Collections.singletonList(headerValue);
}

public void onNewSession(Session session, HttpServletRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.session.web.http;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -25,21 +27,22 @@
* A strategy for mapping HTTP request and responses to a {@link Session}.
*
* @author Rob Winch
* @author Eddú Meléndez
* @since 1.0
*/
public interface HttpSessionStrategy {

/**
* Obtains the requested session id from the provided
* {@link javax.servlet.http.HttpServletRequest}. For example, the session id might
* Obtains the requested session ids from the provided
* {@link javax.servlet.http.HttpServletRequest}. For example, the session ids might
* come from a cookie or a request header.
*
* @param request the {@link javax.servlet.http.HttpServletRequest} to obtain the
* session id from. Cannot be null.
* session ids from. Cannot be null.
* @return the {@link javax.servlet.http.HttpServletRequest} to obtain the session id
* from.
*/
String getRequestedSessionId(HttpServletRequest request);
List<String> getRequestedSessionIds(HttpServletRequest request);

/**
* This method is invoked when a new session is created and should inform a client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.FilterChain;
Expand Down Expand Up @@ -53,8 +54,8 @@
* {@link org.springframework.session.Session} abstraction. Specifically:
*
* <ul>
* <li>The session id is looked up using
* {@link HttpSessionStrategy#getRequestedSessionId(javax.servlet.http.HttpServletRequest)}
* <li>The session ids is looked up using
* {@link HttpSessionStrategy#getRequestedSessionIds(javax.servlet.http.HttpServletRequest)}
* . The default is to look in a cookie named SESSION.</li>
* <li>The session id of newly created {@link org.springframework.session.ExpiringSession}
* is sent to the client using
Expand All @@ -72,6 +73,7 @@
* @param <S> the {@link ExpiringSession} type.
* @since 1.0
* @author Rob Winch
* @author Eddú Meléndez
*/
@Order(SessionRepositoryFilter.DEFAULT_ORDER)
public class SessionRepositoryFilter<S extends ExpiringSession>
Expand Down Expand Up @@ -391,8 +393,12 @@ public HttpSessionWrapper getSession() {

@Override
public String getRequestedSessionId() {
return SessionRepositoryFilter.this.httpSessionStrategy
.getRequestedSessionId(this);
List<String> sessionIds = SessionRepositoryFilter.this
.httpSessionStrategy.getRequestedSessionIds(this);
if (sessionIds.size() > 0) {
return sessionIds.get(0);
}
return null;
}

/**
Expand Down Expand Up @@ -431,8 +437,8 @@ static class MultiHttpSessionStrategyAdapter implements MultiHttpSessionStrategy
this.delegate = delegate;
}

public String getRequestedSessionId(HttpServletRequest request) {
return this.delegate.getRequestedSessionId(request);
public List<String> getRequestedSessionIds(HttpServletRequest request) {
return this.delegate.getRequestedSessionIds(request);
}

public void onNewSession(Session session, HttpServletRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ public void setup() throws Exception {

@Test
public void getRequestedSessionIdNull() throws Exception {
assertThat(this.strategy.getRequestedSessionId(this.request)).isNull();
assertThat(this.strategy.getRequestedSessionIds(this.request)).isEmpty();
}

@Test
public void getRequestedSessionIdNotNull() throws Exception {
setSessionCookie(this.session.getId());
assertThat(this.strategy.getRequestedSessionId(this.request))
.isEqualTo(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
.contains(this.session.getId());
}

@Test
public void getRequestedSessionIdNotNullCustomCookieName() throws Exception {
setCookieName("CUSTOM");
setSessionCookie(this.session.getId());
assertThat(this.strategy.getRequestedSessionId(this.request))
.isEqualTo(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
.contains(this.session.getId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ public void setup() throws Exception {

@Test
public void getRequestedSessionIdNull() throws Exception {
assertThat(this.strategy.getRequestedSessionId(this.request)).isNull();
assertThat(this.strategy.getRequestedSessionIds(this.request)).isEmpty();
}

@Test
public void getRequestedSessionIdNotNull() throws Exception {
setSessionId(this.session.getId());
assertThat(this.strategy.getRequestedSessionId(this.request))
.isEqualTo(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
.contains(this.session.getId());
}

@Test
public void getRequestedSessionIdNotNullCustomHeaderName() throws Exception {
setHeaderName("CUSTOM");
setSessionId(this.session.getId());
assertThat(this.strategy.getRequestedSessionId(this.request))
.isEqualTo(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
.contains(this.session.getId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1158,11 +1158,11 @@ public void doFilter(HttpServletRequest wrappedRequest,

@Test
public void doFilterAdapterGetRequestedSessionId() throws Exception {
this.sessionRepository.save(new MapSession("MultiHttpSessionStrategyAdapter-requested-id"));
this.filter.setHttpSessionStrategy(this.strategy);
final String expectedId = "MultiHttpSessionStrategyAdapter-requested-id";
given(this.strategy.getRequestedSessionId(any(HttpServletRequest.class)))
.willReturn(expectedId);

given(this.strategy.getRequestedSessionIds(any(HttpServletRequest.class)))
.willReturn(Collections.singletonList(expectedId));
doFilter(new DoInFilter() {
@Override
public void doFilter(HttpServletRequest wrappedRequest,
Expand Down Expand Up @@ -1205,8 +1205,8 @@ public void doFilter(HttpServletRequest wrappedRequest,

HttpServletRequest request = (HttpServletRequest) this.chain.getRequest();
String id = request.getSession().getId();
given(this.strategy.getRequestedSessionId(any(HttpServletRequest.class)))
.willReturn(id);
given(this.strategy.getRequestedSessionIds(any(HttpServletRequest.class)))
.willReturn(Collections.singletonList(id));
setupRequest();

doFilter(new DoInFilter() {
Expand Down Expand Up @@ -1237,8 +1237,8 @@ public void doFilter(HttpServletRequest wrappedRequest,

HttpServletRequest request = (HttpServletRequest) this.chain.getRequest();
String id = request.getSession().getId();
given(this.strategy.getRequestedSessionId(any(HttpServletRequest.class)))
.willReturn(id);
given(this.strategy.getRequestedSessionIds(any(HttpServletRequest.class)))
.willReturn(Collections.singletonList(id));

doFilter(new DoInFilter() {
@Override
Expand Down Expand Up @@ -1361,7 +1361,7 @@ public void doFilter(HttpServletRequest wrappedRequest) {
.getAttribute(SessionRepositoryFilter.INVALID_SESSION_ID_ATTR))
.isNull();

// First call should go all the way through to the sessioRepository (it
// First call should go all the way through to the sessionRepository (it
// will not find the session)
HttpSession session = wrappedRequest.getSession(false);
verify(SessionRepositoryFilterTests.this.sessionRepository, times(1))
Expand Down