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);
}
}
}
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;
}
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)));
}
}
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;
}
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();
}
}
Aggregations