Search in sources :

Example 96 with HttpSession

use of javax.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;
}
Also used : HttpSession(javax.servlet.http.HttpSession) ModelAndView(org.springframework.web.servlet.ModelAndView)

Example 97 with HttpSession

use of javax.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);
    assertEquals(Locale.GERMAN, resolver.resolveLocale(request));
    HttpSession session = request.getSession();
    request = new MockHttpServletRequest();
    request.setSession(session);
    resolver = new SessionLocaleResolver();
    assertEquals(Locale.GERMAN, resolver.resolveLocale(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 98 with HttpSession

use of javax.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);
    assertNull(locale);
    HttpSession session = request.getSession();
    request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    request.setSession(session);
    resolver = new SessionLocaleResolver();
    assertEquals(Locale.TAIWAN, resolver.resolveLocale(request));
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 99 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterizedAnnotatedInterface.

@SuppressWarnings("rawtypes")
@Test
public void parameterizedAnnotatedInterface() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            context.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
        }
    }, MyParameterizedControllerImpl.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("page1", request.getAttribute("viewName"));
    HttpSession session = request.getSession();
    assertTrue(session.getAttribute("object1") != null);
    assertTrue(session.getAttribute("object2") != null);
    assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
    request = new MockHttpServletRequest("POST", "/myPage");
    request.setSession(session);
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("page2", request.getAttribute("viewName"));
    assertTrue(session.getAttribute("object1") != null);
    assertTrue(session.getAttribute("object2") != null);
    assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
    assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 100 with HttpSession

use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@Test
public void parameterDispatchingController() throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.setServletContext(servletContext);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
        }
    }, MyParameterDispatchingController.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    assertSame(request.getLocale(), request.getAttribute("locale"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertNotNull(deserialized.request);
    assertNotNull(deserialized.session);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Aggregations

HttpSession (javax.servlet.http.HttpSession)730 HttpServletRequest (javax.servlet.http.HttpServletRequest)151 Test (org.junit.Test)110 IOException (java.io.IOException)80 HttpServletResponse (javax.servlet.http.HttpServletResponse)80 ServletException (javax.servlet.ServletException)75 ArrayList (java.util.ArrayList)65 RequestDispatcher (javax.servlet.RequestDispatcher)59 HashMap (java.util.HashMap)48 Map (java.util.Map)44 Locale (java.util.Locale)39 Properties (java.util.Properties)39 PrintWriter (java.io.PrintWriter)38 Cookie (javax.servlet.http.Cookie)27 List (java.util.List)24 SQLException (java.sql.SQLException)23 WebUser (org.compiere.util.WebUser)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 ModelAndView (org.springframework.web.servlet.ModelAndView)20