Package org.springframework.web.servlet.handler

Examples of org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter


  }

  public void testOpenEntityManagerInterceptorInView() throws Exception {
    OpenEntityManagerInViewInterceptor rawInterceptor = new OpenEntityManagerInViewInterceptor();
    rawInterceptor.setEntityManagerFactory(factory);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
   
    managerControl.replay();
    factoryControl.replay();
   
    interceptor.preHandle(request, response, "handler");
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
   
    // check that further invocations simply participate
    interceptor.preHandle(request, response, "handler");

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);
   
    factoryControl.verify();
    managerControl.verify();
   
    managerControl.reset();
    factoryControl.reset();
    managerControl.replay();
    factoryControl.replay();
   
    interceptor.postHandle(request, response, "handler", null);
    assertTrue(TransactionSynchronizationManager.hasResource(factory));
   
    factoryControl.verify();
    managerControl.verify();
   
    managerControl.reset();
    factoryControl.reset();
   
    manager.close();
   
    managerControl.replay();
    factoryControl.replay();
   
    interceptor.afterCompletion(request, response, "handler", null);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
   
    factoryControl.verify();
    managerControl.verify();
  }
View Full Code Here


   * @param interceptor the interceptor to add
   * @return An {@link InterceptorRegistration} that allows you optionally configure the
   * registered interceptor further for example adding URL patterns it should apply to.
   */
  public InterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor interceptor) {
    WebRequestHandlerInterceptorAdapter adapted = new WebRequestHandlerInterceptorAdapter(interceptor);
    InterceptorRegistration registration = new InterceptorRegistration(adapted);
    registrations.add(registration);
    return registration;
  }
View Full Code Here

   * @param interceptor the interceptor to add
   * @return An {@link InterceptorRegistration} that allows you optionally configure the
   * registered interceptor further for example adding URL patterns it should apply to.
   */
  public InterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor interceptor) {
    WebRequestHandlerInterceptorAdapter adapted = new WebRequestHandlerInterceptorAdapter(interceptor);
    InterceptorRegistration registration = new InterceptorRegistration(adapted);
    registrations.add(registration);
    return registration;
  }
View Full Code Here

    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();

    OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
    rawInterceptor.setSessionFactory(sf);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();

    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 2);
    session.isOpen();
    sessionControl.setReturnValue(true, 1);
    session.setFlushMode(FlushMode.NEVER);
    sessionControl.setVoidCallable(1);
    sfControl.replay();
    sessionControl.replay();
    interceptor.preHandle(request, response, "handler");
    assertTrue(TransactionSynchronizationManager.hasResource(sf));

    // check that further invocations simply participate
    interceptor.preHandle(request, response, "handler");

    assertEquals(session, SessionFactoryUtils.getSession(sf, false));

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    sfControl.verify();
    sessionControl.verify();

    sfControl.reset();
    sessionControl.reset();
    sfControl.replay();
    sessionControl.replay();
    interceptor.postHandle(request, response, "handler", null);
    assertTrue(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();

    sfControl.reset();
    sessionControl.reset();
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();
    interceptor.afterCompletion(request, response, "handler", null);
    assertFalse(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();
  }
View Full Code Here

    tm.getTransaction();
    tmControl.setReturnValue(null, 2);

    OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
    rawInterceptor.setSessionFactory(sf);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();

    sf.getTransactionManager();
    sfControl.setReturnValue(tm, 2);
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.isOpen();
    sessionControl.setReturnValue(true, 1);
    session.setFlushMode(FlushMode.NEVER);
    sessionControl.setVoidCallable(1);

    tmControl.replay();
    sfControl.replay();
    sessionControl.replay();

    interceptor.preHandle(request, response, "handler");
    assertTrue(TransactionSynchronizationManager.hasResource(sf));

    // check that further invocations simply participate
    interceptor.preHandle(request, response, "handler");

    assertEquals(session, SessionFactoryUtils.getSession(sf, false));

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    sfControl.verify();
    sessionControl.verify();

    sfControl.reset();
    sessionControl.reset();
    sfControl.replay();
    sessionControl.replay();
    interceptor.postHandle(request, response, "handler", null);
    assertTrue(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();

    sfControl.reset();
    sessionControl.reset();
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();
    interceptor.afterCompletion(request, response, "handler", null);
    assertFalse(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();
  }
View Full Code Here

    Session session = (Session) sessionControl.getMock();

    OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
    rawInterceptor.setSessionFactory(sf);
    rawInterceptor.setFlushMode(HibernateAccessor.FLUSH_AUTO);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();

    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf);
    sfControl.replay();
    sessionControl.replay();
    interceptor.preHandle(request, response, "handler");
    assertTrue(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();

    sfControl.reset();
    sessionControl.reset();
    session.flush();
    sessionControl.setVoidCallable(1);
    sfControl.replay();
    sessionControl.replay();
    interceptor.postHandle(request, response, "handler", null);
    assertTrue(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();

    sfControl.reset();
    sessionControl.reset();
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();
    interceptor.afterCompletion(request, response, "handler", null);
    assertFalse(TransactionSynchronizationManager.hasResource(sf));
    sfControl.verify();
    sessionControl.verify();
  }
View Full Code Here

    Session session = (Session) sessionControl.getMock();

    OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
    rawInterceptor.setSessionFactory(sf);
    rawInterceptor.setSingleSession(false);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();

    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.setFlushMode(FlushMode.NEVER);
    sessionControl.setVoidCallable(1);
    sfControl.replay();
    sessionControl.replay();

    interceptor.preHandle(request, response, "handler");
    org.hibernate.Session sess = SessionFactoryUtils.getSession(sf, true);
    SessionFactoryUtils.releaseSession(sess, sf);

    // check that further invocations simply participate
    interceptor.preHandle(request, response, "handler");

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    sfControl.verify();
    sessionControl.verify();
    sfControl.reset();
    sessionControl.reset();

    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);
    sfControl.verify();
    sessionControl.verify();
  }
View Full Code Here

    MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
    filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");

    OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
    rawInterceptor.setSessionFactory(sf);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    interceptor.preHandle(request, response, "handler");

    final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
    filter.init(filterConfig);

    final FilterChain filterChain = new FilterChain() {
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
        assertTrue(TransactionSynchronizationManager.hasResource(sf));
        servletRequest.setAttribute("invoked", Boolean.TRUE);
      }
    };

    assertTrue(TransactionSynchronizationManager.hasResource(sf));
    filter.doFilter(request, response, filterChain);
    assertTrue(TransactionSynchronizationManager.hasResource(sf));
    assertNotNull(request.getAttribute("invoked"));

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    sfControl.verify();
    sessionControl.verify();

    wac.close();
View Full Code Here

    filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");

    OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
    rawInterceptor.setSessionFactory(sf);
    rawInterceptor.setSingleSession(false);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    interceptor.preHandle(request, response, "handler");

    final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
    filter.init(filterConfig);
    final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter();
    filter2.init(filterConfig2);

    final FilterChain filterChain = new FilterChain() {
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
        HibernateTransactionManager tm = new HibernateTransactionManager(sf);
        TransactionStatus ts = tm.getTransaction(
            new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
        org.hibernate.Session sess = SessionFactoryUtils.getSession(sf, true);
        SessionFactoryUtils.releaseSession(sess, sf);
        tm.commit(ts);

        sessionControl.verify();
        sessionControl.reset();
        try {
          session.close();
        }
        catch (HibernateException ex) {
        }
        sessionControl.setReturnValue(null, 1);
        sessionControl.replay();

        servletRequest.setAttribute("invoked", Boolean.TRUE);
      }
    };

    FilterChain filterChain2 = new FilterChain() {
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
          throws IOException, ServletException {
        filter.doFilter(servletRequest, servletResponse, filterChain);
      }
    };

    filter.doFilter(request, response, filterChain2);
    assertNotNull(request.getAttribute("invoked"));

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    sfControl.verify();
    sessionControl.verify();

    wac.close();
View Full Code Here

    MockControl pmControl = MockControl.createControl(PersistenceManager.class);
    PersistenceManager pm = (PersistenceManager) pmControl.getMock();

    OpenPersistenceManagerInViewInterceptor rawInterceptor = new OpenPersistenceManagerInViewInterceptor();
    rawInterceptor.setPersistenceManagerFactory(pmf);
    HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);

    MockServletContext sc = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();

    pmf.getPersistenceManager();
    pmfControl.setReturnValue(pm, 1);
    pmfControl.replay();
    pmControl.replay();
    interceptor.preHandle(request, response, "handler");
    assertTrue(TransactionSynchronizationManager.hasResource(pmf));

    // check that further invocations simply participate
    interceptor.preHandle(request, response, "handler");

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    interceptor.preHandle(request, response, "handler");
    interceptor.postHandle(request, response, "handler", null);
    interceptor.afterCompletion(request, response, "handler", null);

    pmfControl.verify();
    pmControl.verify();

    pmfControl.reset();
    pmControl.reset();
    pmfControl.replay();
    pmControl.replay();
    interceptor.postHandle(request, response, "handler", null);
    assertTrue(TransactionSynchronizationManager.hasResource(pmf));
    pmfControl.verify();
    pmControl.verify();

    pmfControl.reset();
    pmControl.reset();
    pm.close();
    pmControl.setVoidCallable(1);
    pmfControl.replay();
    pmControl.replay();
    interceptor.afterCompletion(request, response, "handler", null);
    assertFalse(TransactionSynchronizationManager.hasResource(pmf));
    pmfControl.verify();
    pmControl.verify();
  }
View Full Code Here

TOP

Related Classes of org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.