use of cn.taketoday.http.HttpCookie in project today-infrastructure by TAKETODAY.
the class CookieSessionIdResolver method setId.
@Override
public void setId(RequestContext context, String sessionId) {
if (!sessionId.equals(context.getAttribute(WRITTEN_SESSION_ID_ATTR))) {
HttpCookie cookie = createCookie(sessionId);
context.addCookie(cookie);
context.setAttribute(WRITTEN_SESSION_ID_ATTR, sessionId);
}
}
use of cn.taketoday.http.HttpCookie in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolver method parseLocaleCookieIfNecessary.
private void parseLocaleCookieIfNecessary(RequestContext request) {
if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
Locale locale = null;
TimeZone timeZone = null;
// Retrieve and parse cookie value.
String cookieName = getCookieName();
if (cookieName != null) {
HttpCookie cookie = WebUtils.getCookie(request, cookieName);
if (cookie != null) {
String value = cookie.getValue();
String localePart = value;
String timeZonePart = null;
int separatorIndex = localePart.indexOf('/');
if (separatorIndex == -1) {
// Leniently accept older cookies separated by a space...
separatorIndex = localePart.indexOf(' ');
}
if (separatorIndex >= 0) {
localePart = value.substring(0, separatorIndex);
timeZonePart = value.substring(separatorIndex + 1);
}
try {
locale = !"-".equals(localePart) ? parseLocaleValue(localePart) : null;
if (timeZonePart != null) {
timeZone = StringUtils.parseTimeZoneString(timeZonePart);
}
} catch (IllegalArgumentException ex) {
if (isRejectInvalidCookies() && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
throw new IllegalStateException("Encountered invalid locale cookie '" + cookieName + "': [" + value + "] due to: " + ex.getMessage());
} else {
// Lenient handling (e.g. error dispatch): ignore locale/timezone parse exceptions
if (logger.isDebugEnabled()) {
logger.debug("Ignoring invalid locale cookie '" + cookieName + "': [" + value + "] due to: " + ex.getMessage());
}
}
}
if (logger.isTraceEnabled()) {
logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale + "'" + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
}
}
}
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, (locale != null ? locale : determineDefaultLocale(request)));
request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME, (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
}
use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.
the class CookieTokenResolver method saveToken.
@Override
public void saveToken(RequestContext context, WebSession session) {
HttpCookie cookie = buildCookie(session.getId());
context.addCookie(cookie);
}
use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.
the class CookieIntegrationTests method basicTest.
@ParameterizedHttpServerTest
public void basicTest(HttpServer httpServer) throws Exception {
startServer(httpServer);
URI url = new URI("http://localhost:" + port);
String header = "SID=31d4d96e407aad42; lang=en-US";
ResponseEntity<Void> response = new RestTemplate().exchange(RequestEntity.get(url).header("Cookie", header).build(), Void.class);
Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
assertThat(requestCookies.size()).isEqualTo(2);
List<HttpCookie> list = requestCookies.get("SID");
assertThat(list.size()).isEqualTo(1);
assertThat(list.iterator().next().getValue()).isEqualTo("31d4d96e407aad42");
list = requestCookies.get("lang");
assertThat(list.size()).isEqualTo(1);
assertThat(list.iterator().next().getValue()).isEqualTo("en-US");
List<String> headerValues = response.getHeaders().get("Set-Cookie");
assertThat(headerValues.size()).isEqualTo(2);
List<String> cookie0 = splitCookie(headerValues.get(0));
assertThat(cookie0.remove("SID=31d4d96e407aad42")).as("SID").isTrue();
assertThat(cookie0.stream().map(String::toLowerCase)).containsExactlyInAnyOrder("path=/", "secure", "httponly");
List<String> cookie1 = splitCookie(headerValues.get(1));
assertThat(cookie1.remove("lang=en-US")).as("lang").isTrue();
assertThat(cookie1.stream().map(String::toLowerCase)).containsExactlyInAnyOrder("path=/", "domain=example.com");
}
use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.
the class MockServerHttpRequestTests method cookieHeaderSet.
@Test
void cookieHeaderSet() {
HttpCookie foo11 = new HttpCookie("foo1", "bar1");
HttpCookie foo12 = new HttpCookie("foo1", "bar2");
HttpCookie foo21 = new HttpCookie("foo2", "baz1");
HttpCookie foo22 = new HttpCookie("foo2", "baz2");
MockServerHttpRequest request = MockServerHttpRequest.get("/").cookie(foo11, foo12, foo21, foo22).build();
assertThat(request.getCookies().get("foo1")).isEqualTo(Arrays.asList(foo11, foo12));
assertThat(request.getCookies().get("foo2")).isEqualTo(Arrays.asList(foo21, foo22));
assertThat(request.getHeaders().get(HttpHeaders.COOKIE)).isEqualTo(Arrays.asList("foo1=bar1", "foo1=bar2", "foo2=baz1", "foo2=baz2"));
}
Aggregations