Search in sources :

Example 1 with HttpCookie

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);
    }
}
Also used : HttpCookie(cn.taketoday.http.HttpCookie)

Example 2 with HttpCookie

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)));
    }
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) HttpCookie(cn.taketoday.http.HttpCookie)

Example 3 with HttpCookie

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);
}
Also used : HttpCookie(cn.taketoday.http.HttpCookie)

Example 4 with HttpCookie

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");
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) HttpCookie(cn.taketoday.http.HttpCookie)

Example 5 with HttpCookie

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"));
}
Also used : MockServerHttpRequest(cn.taketoday.mock.http.server.reactive.MockServerHttpRequest) HttpCookie(cn.taketoday.http.HttpCookie) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpCookie (cn.taketoday.http.HttpCookie)19 List (java.util.List)4 Map (java.util.Map)4 Test (org.junit.jupiter.api.Test)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)2 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2 MockServerHttpRequest (cn.taketoday.mock.http.server.reactive.MockServerHttpRequest)2 MockRequestContext (cn.taketoday.web.MockRequestContext)2 RestTemplate (cn.taketoday.web.client.RestTemplate)2 Cookie (io.netty.handler.codec.http.cookie.Cookie)2 Cookie (jakarta.servlet.http.Cookie)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 Set (java.util.Set)2 TimeZone (java.util.TimeZone)2 CookieStore (org.apache.hc.client5.http.cookie.CookieStore)2 BasicClientCookie (org.apache.hc.client5.http.impl.cookie.BasicClientCookie)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2