Skip to content

Commit

Permalink
Remove Cookie support from ServerHttpRequest
Browse files Browse the repository at this point in the history
Although ServletHttpRequest provides access to Cookies, other
implementations may not. At the moment this was only needed
for SockJS to check the value of the JSESSIONID cookie. This
is now down by parsing the raw cookie values locally.

If comprehensive cookie support is to be added, we should
probably consider HttpHeaders as a potential candidate.
  • Loading branch information
rstoyanchev committed Aug 14, 2013
1 parent b232dc9 commit 4c0490a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 182 deletions.
73 changes: 0 additions & 73 deletions spring-web/src/main/java/org/springframework/http/Cookie.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import java.net.InetSocketAddress;
import java.security.Principal;
import java.util.Map;

import org.springframework.http.Cookie;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpRequest;
import org.springframework.util.MultiValueMap;
Expand All @@ -39,11 +37,6 @@ public interface ServerHttpRequest extends HttpRequest, HttpInputMessage {
*/
MultiValueMap<String, String> getQueryParams();

/**
* Return the cookie values parsed from the "Cookie" request header.
*/
Map<String, Cookie> getCookies();

/**
* Return a {@link java.security.Principal} instance containing the name of the
* authenticated user. If the user has not been authenticated, the method returns
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.charset.Charset;
import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -40,7 +39,6 @@

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.Cookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -69,12 +67,11 @@ public class ServletServerHttpRequest implements ServerHttpRequest {

private HttpHeaders headers;

private Map<String, Cookie> cookies;

private MultiValueMap<String, String> queryParams;

private ServerHttpAsyncRequestControl asyncRequestControl;


/**
* Construct a new instance of the ServletServerHttpRequest based on the given {@link HttpServletRequest}.
* @param servletRequest the servlet request
Expand Down Expand Up @@ -157,20 +154,6 @@ public InetSocketAddress getRemoteAddress() {
return new InetSocketAddress(this.servletRequest.getRemoteHost(), this.servletRequest.getRemotePort());
}

@Override
public Map<String, Cookie> getCookies() {
if (this.cookies == null) {
this.cookies = new HashMap<String, Cookie>();
if (this.servletRequest.getCookies() != null) {
for (javax.servlet.http.Cookie cookie : this.servletRequest.getCookies()) {
this.cookies.put(cookie.getName(), new ServletServerCookie(cookie));
}
}
this.cookies = Collections.unmodifiableMap(this.cookies);
}
return this.cookies;
}

@Override
public MultiValueMap<String, String> getQueryParams() {
if (this.queryParams == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

import org.springframework.http.Cookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
Expand Down Expand Up @@ -307,9 +307,8 @@ protected void handleTransportRequest(ServerHttpRequest request, ServerHttpRespo
}

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=/");
String cookieValue = getJsessionIdCookieValue(request.getHeaders());
response.getHeaders().set("Set-Cookie", "JSESSIONID=" + cookieValue + ";path=/");
}

if (transportType.supportsCors()) {
Expand Down Expand Up @@ -387,6 +386,20 @@ public void run() {
}, getDisconnectDelay());
}

private String getJsessionIdCookieValue(HttpHeaders headers) {
List<String> rawCookies = headers.get("Cookie");
if (!CollectionUtils.isEmpty(rawCookies)) {
for (String rawCookie : rawCookies) {
if (rawCookie.startsWith("JSESSIONID=")) {
int start = "JSESSIONID=".length();
int end = rawCookie.indexOf(';');
return (end != -1) ? rawCookie.substring(start, end) : rawCookie.substring(start);
}
}
}
return "dummy";
}


private final SockJsServiceConfig sockJsServiceConfig = new SockJsServiceConfig() {

Expand Down

0 comments on commit 4c0490a

Please sign in to comment.