use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class SessionLocaleResolverTests method testSetAndResolveLocale.
@Test
public void testSetAndResolveLocale() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setLocale(request, response, Locale.GERMAN);
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
HttpSession session = request.getSession();
request = new MockHttpServletRequest();
request.setSession(session);
resolver = new SessionLocaleResolver();
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.GERMAN);
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class SessionLocaleResolverTests method testSetLocaleToNullLocale.
@Test
public void testSetLocaleToNullLocale() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMAN);
MockHttpServletResponse response = new MockHttpServletResponse();
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setLocale(request, response, null);
Locale locale = (Locale) request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
assertThat(locale).isNull();
HttpSession session = request.getSession();
request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
request.setSession(session);
resolver = new SessionLocaleResolver();
assertThat(resolver.resolveLocale(request)).isEqualTo(Locale.TAIWAN);
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class ServletRequestAttributesTests method updateAccessedAttributes.
@Test
public void updateAccessedAttributes() {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpSession session = mock(HttpSession.class);
given(request.getSession(anyBoolean())).willReturn(session);
given(session.getAttribute(KEY)).willReturn(VALUE);
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
assertThat(attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION)).isSameAs(VALUE);
attrs.requestCompleted();
verify(session, times(2)).getAttribute(KEY);
verify(session).setAttribute(KEY, VALUE);
verifyNoMoreInteractions(session);
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapter method handleInternal.
@Override
protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
ModelAndView mav;
checkRequest(request);
// Execute invokeHandlerMethod in synchronized block if required.
if (this.synchronizeOnSession) {
HttpSession session = request.getSession(false);
if (session != null) {
Object mutex = WebUtils.getSessionMutex(session);
synchronized (mutex) {
mav = invokeHandlerMethod(request, response, handlerMethod);
}
} else {
// No HttpSession available -> no mutex necessary
mav = invokeHandlerMethod(request, response, handlerMethod);
}
} else {
// No synchronization on session demanded at all...
mav = invokeHandlerMethod(request, response, handlerMethod);
}
if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
} else {
prepareResponse(response);
}
}
return mav;
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class ServletRequestAttributes method obtainSession.
private HttpSession obtainSession() {
HttpSession session = getSession(true);
Assert.state(session != null, "No HttpSession");
return session;
}
Aggregations