Polish Cookie abstraction in http packge of spring-web

A getCookies method is now available on ServerHttpRequest with one
ServletServerCookie implementation that wraps a Servlet cookie.

The SockJS service makes use of this to check for an existing session
cookie in the request.
This commit is contained in:
Rossen Stoyanchev
2013-08-02 12:23:48 -04:00
parent c26272cef6
commit 0d5901ffb6
26 changed files with 258 additions and 353 deletions

View File

@@ -78,7 +78,7 @@ public abstract class AbstractSockJsService implements SockJsService {
private int streamBytesLimit = 128 * 1024;
private boolean jsessionIdCookieRequired = true;
private boolean sessionCookieEnabled = false;
private long heartbeatTime = 25 * 1000;
@@ -187,23 +187,22 @@ public abstract class AbstractSockJsService implements SockJsService {
}
/**
* Some load balancers do sticky sessions, but only if there is a JSESSIONID
* Some load balancers do sticky sessions, but only if there is a "JSESSIONID"
* cookie. Even if it is set to a dummy value, it doesn't matter since
* session information is added by the load balancer.
*
* <p>Set this option to indicate if a JSESSIONID cookie should be created. The
* default value is "true".
* <p>The default value is "false" since Java servers set the session cookie.
*/
public void setJsessionIdCookieRequired(boolean jsessionIdCookieRequired) {
this.jsessionIdCookieRequired = jsessionIdCookieRequired;
public void setDummySessionCookieEnabled(boolean sessionCookieEnabled) {
this.sessionCookieEnabled = sessionCookieEnabled;
}
/**
* Whether setting JSESSIONID cookie is necessary.
* @see #setJsessionIdCookieRequired(boolean)
* @see #setDummySessionCookieEnabled(boolean)
*/
public boolean isJsessionIdCookieRequired() {
return this.jsessionIdCookieRequired;
public boolean isDummySessionCookieEnabled() {
return this.sessionCookieEnabled;
}
/**
@@ -476,7 +475,7 @@ public abstract class AbstractSockJsService implements SockJsService {
addCorsHeaders(request, response);
addNoCacheHeaders(response);
String content = String.format(INFO_CONTENT, random.nextInt(), isJsessionIdCookieRequired(), isWebSocketEnabled());
String content = String.format(INFO_CONTENT, random.nextInt(), isDummySessionCookieEnabled(), isWebSocketEnabled());
response.getBody().write(content.getBytes());
}
else if (HttpMethod.OPTIONS.equals(request.getMethod())) {

View File

@@ -91,7 +91,7 @@ public enum TransportType {
return this.headerHints.contains("cors");
}
public boolean setsJsessionId() {
public boolean sendsSessionCookie() {
return this.headerHints.contains("jsessionid");
}

View File

@@ -118,7 +118,7 @@ public class DefaultSockJsService extends AbstractSockJsService {
* @param transportHandlerOverrides zero or more overrides to the default transport
* handler types.
*/
public DefaultSockJsService(TaskScheduler taskScheduler, Set<TransportHandler> transportHandlers,
public DefaultSockJsService(TaskScheduler taskScheduler, Collection<TransportHandler> transportHandlers,
TransportHandler... transportHandlerOverrides) {
super(taskScheduler);
@@ -254,11 +254,10 @@ public class DefaultSockJsService extends AbstractSockJsService {
addNoCacheHeaders(response);
}
if (transportType.setsJsessionId() && isJsessionIdCookieRequired()) {
Cookie cookie = request.getCookies().getCookie("JSESSIONID");
String jsid = (cookie != null) ? cookie.getValue() : "dummy";
// TODO: bypass use of Cookie object (causes Jetty to set Expires header)
response.getHeaders().set("Set-Cookie", "JSESSIONID=" + jsid + ";path=/");
if (transportType.sendsSessionCookie() && isDummySessionCookieEnabled()) {
Cookie cookie = request.getCookies().get("JSESSIONID");
String value = (cookie != null) ? cookie.getValue() : "dummy";
response.getHeaders().set("Set-Cookie", "JSESSIONID=" + value + ";path=/");
}
if (transportType.supportsCors()) {