Search in sources :

Example 6 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.

the class HttpComponentsClientHttpRequest method applyCookies.

@Override
protected void applyCookies() {
    if (getCookies().isEmpty()) {
        return;
    }
    CookieStore cookieStore = this.context.getCookieStore();
    for (Map.Entry<String, List<HttpCookie>> entry : getCookies().entrySet()) {
        for (HttpCookie cookie : entry.getValue()) {
            BasicClientCookie clientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
            clientCookie.setDomain(getURI().getHost());
            clientCookie.setPath(getURI().getPath());
            cookieStore.addCookie(clientCookie);
        }
    }
}
Also used : CookieStore(org.apache.hc.client5.http.cookie.CookieStore) List(java.util.List) BasicClientCookie(org.apache.hc.client5.http.impl.cookie.BasicClientCookie) Map(java.util.Map) HttpCookie(cn.taketoday.http.HttpCookie)

Example 7 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.

the class ReactorServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    DefaultMultiValueMap<String, HttpCookie> cookies = MultiValueMap.fromLinkedHashMap();
    for (Map.Entry<CharSequence, Set<Cookie>> entry : request.cookies().entrySet()) {
        CharSequence name = entry.getKey();
        for (Cookie cookie : entry.getValue()) {
            HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value());
            cookies.add(name.toString(), httpCookie);
        }
    }
    return cookies;
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpCookie(cn.taketoday.http.HttpCookie) Set(java.util.Set) HttpCookie(cn.taketoday.http.HttpCookie) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap)

Example 8 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework 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 9 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.

the class ServletServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> httpCookies = MultiValueMap.fromLinkedHashMap();
    Cookie[] cookies;
    synchronized (this.cookieLock) {
        cookies = this.request.getCookies();
    }
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            HttpCookie httpCookie = new HttpCookie(name, cookie.getValue());
            httpCookies.add(name, httpCookie);
        }
    }
    return httpCookies;
}
Also used : HttpCookie(cn.taketoday.http.HttpCookie) Cookie(jakarta.servlet.http.Cookie) HttpCookie(cn.taketoday.http.HttpCookie)

Example 10 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.

the class DefaultWebSessionManagerTests method testWebSession.

@Test
public void testWebSession() {
    try (StandardApplicationContext applicationContext = new StandardApplicationContext()) {
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();
        SessionManager sessionManager = applicationContext.getBean(SessionManager.class);
        MockRequestContext context = new MockRequestContext();
        WebSession noneExistingSession = sessionManager.getSession(context, false);
        assertThat(noneExistingSession).isNull();
        WebSession createdSession = sessionManager.getSession(context);
        assertThat(createdSession).isNotNull();
        // CookieTokenResolver
        CookieSessionIdResolver cookieTokenResolver = applicationContext.getBean(CookieSessionIdResolver.class);
        List<HttpCookie> responseCookies = context.responseCookies();
        String sessionId = createdSession.getId();
        HttpCookie sessionCookie = cookieTokenResolver.createCookie(sessionId);
        assertThat(responseCookies).hasSize(1);
        assertThat(responseCookies.get(0)).isEqualTo(sessionCookie);
        // WebSessionStorage
        SessionRepository sessionStorage = applicationContext.getBean(SessionRepository.class);
        WebSession webSession = sessionStorage.retrieveSession(sessionId);
        assertThat(webSession).isEqualTo(createdSession);
        assertThat(sessionStorage.contains(sessionId)).isTrue();
        sessionStorage.removeSession(sessionId);
        assertThat(sessionStorage.contains(sessionId)).isFalse();
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) EnableWebSession(cn.taketoday.framework.web.session.EnableWebSession) MockRequestContext(cn.taketoday.web.MockRequestContext) HttpCookie(cn.taketoday.http.HttpCookie) Test(org.junit.jupiter.api.Test)

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