Java Code Examples for javax.servlet.ServletRequestEvent

The following are top voted examples for showing how to use javax.servlet.ServletRequestEvent. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: lams   File: RequestContextListener.java   View source code 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 2
Project: spring4-understanding   File: RequestContextListener.java   View source code 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 3
Project: spring4-understanding   File: RequestContextListenerTests.java   View source code 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 4
Project: spring4-understanding   File: RequestContextListenerTests.java   View source code 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 5
Project: MBaaS   File: ServletRequestListener.java   View source code 6 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
    ServletContext servletContext = sre.getServletContext();
    ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    GroovyClassLoader classLoader = applicationContext.getBean(GroovyClassLoader.class);
    if (Thread.currentThread().getContextClassLoader() instanceof GroovyClassLoader) {
    } else {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
    DataSource dataSource = applicationContext.getBean(DataSource.class);
    try {
        this.connection = dataSource.getConnection();
        this.connection.setAutoCommit(true);
    } catch (SQLException e) {
    }
}
 
Example 6
Project: domui   File: WebApplicationListener.java   View source code 6 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent e) {
	LOG.debug("Request entered");

	HttpServletRequest req = (HttpServletRequest) e.getServletRequest();
	WebConfiguration wc = Iocular.getConfiguration(e.getServletContext());
	Container sc = createSessionContainer(req.getSession(true));
	if(sc == null)
		throw new IllegalStateException("No session container found!?");

	//-- Create a request container,
	BasicContainer bc = new BasicContainer(wc.getRequestDefinition(), sc);
	bc.start();
	req.setAttribute(Keys.REQUEST_CONTAINER, bc);
	Iocular._setRequest((HttpServletRequest) e.getServletRequest(), bc);
}
 
Example 7
Project: opengse   File: WebAppImpl.java   View source code 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void doFilter(ServletRequest request, ServletResponse response)
    throws IOException, ServletException {
  // set our context classloader
  Thread.currentThread().setContextClassLoader(classLoader);
  // send a requestInititalized event
  ServletRequestEvent sre = new ServletRequestEvent(context, request);
  srListeners.requestInitialized(sre);
  // if (!sraListeners.isEmpty()) {
  // if we have at least one request attribute listener, wrap the
  // request so we can intercept calls to setAttribute/removeAttribute
  request = wrapRequestForServletRequestAttributeListeners(request);
  // }
  RegularExpressionRequestHandler handler = regexHandler.getRequestHandler();
  handler.doFilter(request, response);
}
 
Example 8
Project: bootique-jetty   File: DefaultServletEnvironmentTest.java   View source code 6 votes vote down vote up
@Test
public void testRequest() {
	DefaultServletEnvironment o = new DefaultServletEnvironment();

	assertFalse(o.request().isPresent());

	ServletContext mockContext = mock(ServletContext.class);
	ServletRequest mockRequest = mock(HttpServletRequest.class);
	o.requestInitialized(new ServletRequestEvent(mockContext, mockRequest));
	assertSame(mockRequest, o.request().get());
	
	o.requestDestroyed(new ServletRequestEvent(mockContext, mockRequest));
	assertFalse(o.request().isPresent());
	
	ServletRequest mockRequest2 = mock(HttpServletRequest.class);
	o.requestInitialized(new ServletRequestEvent(mockContext, mockRequest2));
	assertSame(mockRequest2, o.request().get());
	
	o.requestDestroyed(new ServletRequestEvent(mockContext, mockRequest2));
	assertFalse(o.request().isPresent());
	
	// TODO: test multithreaded scenario..
}
 
Example 9
Project: BookStore   File: StoreRequestListener.java   View source code 6 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
	//System.out.println("Requset 初始化了:" + sre.getServletRequest());
	
	ServletContext ctx = sre.getServletContext();
	exector.execute(new Runnable() {
		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName());
			Integer count = (Integer) ctx.getAttribute(BookStoreConstants.REQUEST_COUNT); // 获得list
			if (count == null) {
				count = 0;
			}
			ctx.setAttribute(BookStoreConstants.REQUEST_COUNT, count+1);
			
		}
	});
	

}
 
Example 10
Project: iss   File: DBInitializer.java   View source code 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent arg0) {
	logger.entering("DBInitializer","requestDestroyed");
	ServletRequest req = arg0.getServletRequest();
	
	req.removeAttribute(StringConstants.REQUEST_ATTR_USER_DAO);
	req.removeAttribute(StringConstants.REQUEST_ATTR_TAG_DAO);
	req.removeAttribute(StringConstants.REQUEST_ATTR_SCREEN_DAO );
	req.removeAttribute(StringConstants.REQUEST_ATTR_CONTENT_DAO );
	req.removeAttribute(StringConstants.REQUEST_ATTR_SETTING_DAO );
	req.removeAttribute(StringConstants.REQUEST_ATTR_ENTITY_MANAGER);

	EntityManager em = (EntityManager) req.getAttribute(StringConstants.REQUEST_ATTR_ENTITY_MANAGER);
	if(em!=null && em.isOpen()){
		em.close();
	}
	req.removeAttribute(StringConstants.REQUEST_ATTR_ENTITY_MANAGER);
	
	logger.exiting("DBInitializer","requestDestroyed");
}
 
Example 11
Project: iss   File: DBInitializer.java   View source code 6 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent arg0) {
	logger.entering("DBInitializer","requestInitialized");
	EntityManager em = DBConfig.getEntityManager();
	UserDao userDAO = new UserDao(em);
	TagDao tagDAO = new TagDao(em);
	ScreenDao screenDAO = new ScreenDao(em);
	ContentDao contentDAO = new ContentDao(em);
	SettingDao settingDAO = new SettingDao(em);

	ServletRequest req = arg0.getServletRequest();
	req.setAttribute(StringConstants.REQUEST_ATTR_USER_DAO, userDAO);
	req.setAttribute(StringConstants.REQUEST_ATTR_TAG_DAO, tagDAO);
	req.setAttribute(StringConstants.REQUEST_ATTR_SCREEN_DAO, screenDAO);
	req.setAttribute(StringConstants.REQUEST_ATTR_CONTENT_DAO, contentDAO);
	req.setAttribute(StringConstants.REQUEST_ATTR_SETTING_DAO, settingDAO);
	req.setAttribute(StringConstants.REQUEST_ATTR_ENTITY_MANAGER, em);
	logger.exiting("DBInitializer","requestInitialized");
}
 
Example 12
Project: class-guard   File: RequestContextListener.java   View source code 6 votes vote down vote up
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 13
Project: class-guard   File: RequestContextListenerTests.java   View source code 6 votes vote down vote up
public void testRequestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 14
Project: class-guard   File: RequestContextListenerTests.java   View source code 6 votes vote down vote up
public void testRequestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 15
Project: lutece-core   File: SearchAppTest.java   View source code 6 votes vote down vote up
/**
 * Test of getPage method, of class fr.paris.lutece.portal.web.search.SearchApp.
 * 
 * @throws SiteMessageException
 */
public void testGetPage( ) throws SiteMessageException
{
    System.out.println( "getPage" );

    MockHttpServletRequest request = new MockHttpServletRequest( );
    request.addParameter( "query", "lutece" );
    request.addParameter( "items_per_page", "5" );

    RequestContextListener listener = new RequestContextListener( );
    ServletContext context = new MockServletContext( );
    listener.requestInitialized( new ServletRequestEvent( context, request ) );

    int nMode = 0;
    Plugin plugin = null;
    SearchApp instance = SpringContextService.getBean( "core.xpage.search" );

    XPage result = instance.getPage( request, nMode, plugin );

    listener.requestDestroyed( new ServletRequestEvent( context, request ) );
}
 
Example 16
Project: opennmszh   File: DispatcherServlet.java   View source code 6 votes vote down vote up
@Override
public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
{
    final ServletRequestEvent sre = new ServletRequestEvent(getServletContext(), req);
    this.controller.getRequestListener().requestInitialized(sre);
    try
    {
        req = new AttributeEventRequest(getServletContext(), this.controller.getRequestAttributeListener(), req);
        this.controller.getDispatcher().dispatch(req, res, new NotFoundFilterChain());
    }
    finally
    {
        this.controller.getRequestListener().requestDestroyed(sre);
    }
}
 
Example 17
Project: tomee   File: BeginWebBeansListener.java   View source code 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void requestInitialized(final ServletRequestEvent event) {
    final Object oldContext = ThreadSingletonServiceImpl.enter(this.webBeansContext);
    if (event != null) {
        event.getServletRequest().setAttribute(contextKey, oldContext);
    }

    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Starting a new request : [{0}]", event == null ? "null" : event.getServletRequest().getRemoteAddr());
        }

        if (webBeansContext instanceof WebappWebBeansContext) { // start before child
            ((WebappWebBeansContext) webBeansContext).getParent().getContextsService().startContext(RequestScoped.class, event);
        }
        contextsService.startContext(RequestScoped.class, event);

        // we don't initialise the Session here but do it lazily if it gets requested
        // the first time. See OWB-457

    } catch (final Exception e) {
        logger.error(OWBLogConst.ERROR_0019, event == null ? "null" : event.getServletRequest());
        WebBeansUtil.throwRuntimeExceptions(e);
    }
}
 
Example 18
Project: tomee   File: HttpRequestImpl.java   View source code 6 votes vote down vote up
public void destroy() {
    final boolean openejbRequestDestroyed = getAttribute("openejb_requestDestroyed") == null;
    if (listeners != null && !listeners.isEmpty()) {
        if (begin != null && end != null && openejbRequestDestroyed) {
            end.requestDestroyed(new ServletRequestEvent(getServletContext(), this));
        }
        final ServletRequestEvent event = new ServletRequestEvent(getServletContext(), this);
        for (final ServletRequestListener listener : listeners) {
            listener.requestDestroyed(event);
        }
    }
    if (begin != null && openejbRequestDestroyed) {
        setAttribute("openejb_requestDestroyed", "ok");
        begin.requestDestroyed(new ServletRequestEvent(getServletContext(), this));
    }
}
 
Example 19
Project: bugsnag-java   File: ServletCallbackTest.java   View source code 6 votes vote down vote up
/**
 * Generate a new request instance which will be read by the servlet
 * context and callback
 */
@Before
public void setUp() {
    HttpServletRequest request = mock(HttpServletRequest.class);

    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("account", new String[]{"Acme Co"});
    params.put("name", new String[]{"Bill"});
    when(request.getParameterMap()).thenReturn(params);

    when(request.getMethod()).thenReturn("PATCH");
    when(request.getRequestURL()).thenReturn(new StringBuffer("/foo/bar"));
    when(request.getRequestURI()).thenReturn("/foo/bar");
    when(request.getRemoteAddr()).thenReturn("12.0.4.57");

    Enumeration<String> headers = new Vector<String>(
            Arrays.asList("Content-Type", "Content-Length")).elements();
    when(request.getHeaderNames()).thenReturn(headers);
    when(request.getHeader("Content-Type")).thenReturn("application/json");
    when(request.getHeader("Content-Length")).thenReturn("54");

    ServletContext context = mock(ServletContext.class);
    BugsnagServletRequestListener listener = new BugsnagServletRequestListener();
    listener.requestInitialized(new ServletRequestEvent(context, request));
}
 
Example 20
Project: geomajas-project-server   File: GeomajasContextListener.java   View source code 6 votes vote down vote up
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
	ServletRequestAttributes attributes = (ServletRequestAttributes) servletRequestEvent.getServletRequest()
			.getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	ServletRequestAttributes threadAttributes =
			(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		if (attributes == null) {
			attributes = threadAttributes;
		}
		RequestContextHolder.setRequestAttributes(null);
		LocaleContextHolder.setLocale(null);
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 21
Project: lucee-websocket   File: HttpSessionInitializer.java   View source code 5 votes vote down vote up
/**
 * Receives notification that a ServletRequest is about to come into scope of the web application.
 *
 * @param sre
 *            the ServletRequestEvent containing the ServletRequest and the ServletContext representing the web
 *            application
 */
@Override
public void requestInitialized(ServletRequestEvent sre) {
	// TODO: log debug level only
	// System.out.println(HttpSessionInitializer.class.getSimpleName() + ": requestInitialized");

	HttpServletRequest servletRequest = (HttpServletRequest) sre.getServletRequest();

	// call getSession() so that the Servlet Container will initialize the HttpSession
	servletRequest.getSession();
}
 
Example 22
Project: dubbo-transaction   File: DefaultServletListener.java   View source code 5 votes vote down vote up
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
    HttpServletRequest request=(HttpServletRequest)servletRequestEvent.getServletRequest();
    long now=System.currentTimeMillis();
    try {
        HOOK.get().postHook();
    } catch (RpcTransactionRollbackException e) {
        e.printStackTrace();
    }
    log.debug(request.getRequestURL()+" spend:"+(System.currentTimeMillis()-now));
    HOOK.set(null);
}
 
Example 23
Project: tomcat7   File: StandardContext.java   View source code 5 votes vote down vote up
@Override
public boolean fireRequestInitEvent(ServletRequest request) {

    Object instances[] = getApplicationEventListeners();

    if ((instances != null) && (instances.length > 0)) {

        ServletRequestEvent event = 
                new ServletRequestEvent(getServletContext(), request);

        for (int i = 0; i < instances.length; i++) {
            if (instances[i] == null)
                continue;
            if (!(instances[i] instanceof ServletRequestListener))
                continue;
            ServletRequestListener listener =
                (ServletRequestListener) instances[i];
            
            try {
                listener.requestInitialized(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString(
                        "standardContext.requestListener.requestInit",
                        instances[i].getClass().getName()), t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
                return false;
            }
        }
    }
    return true;
}
 
Example 24
Project: tomcat7   File: StandardContext.java   View source code 5 votes vote down vote up
@Override
public boolean fireRequestDestroyEvent(ServletRequest request) {
    Object instances[] = getApplicationEventListeners();

    if ((instances != null) && (instances.length > 0)) {

        ServletRequestEvent event = 
            new ServletRequestEvent(getServletContext(), request);

        for (int i = 0; i < instances.length; i++) {
            int j = (instances.length -1) -i;
            if (instances[j] == null)
                continue;
            if (!(instances[j] instanceof ServletRequestListener))
                continue;
            ServletRequestListener listener =
                (ServletRequestListener) instances[j];
            
            try {
                listener.requestDestroyed(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString(
                        "standardContext.requestListener.requestInit",
                        instances[j].getClass().getName()), t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
                return false;
            }
        }
    }
    return true;
}
 
Example 25
Project: lams   File: RequestContextListener.java   View source code 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 26
Project: lams   File: ApplicationListeners.java   View source code 5 votes vote down vote up
public void requestDestroyed(final ServletRequest request) {
    final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
    for (int i = servletRequestListeners.length - 1; i >= 0; --i) {
        ManagedListener listener = servletRequestListeners[i];
        try {
            this.<ServletRequestListener>get(listener).requestDestroyed(sre);
        } catch (Exception e) {
            UndertowServletLogger.REQUEST_LOGGER.errorInvokingListener("requestDestroyed", listener.getListenerInfo().getListenerClass(), e);
        }
    }
}
 
Example 27
Project: apache-tomcat-7.0.73-with-comment   File: StandardContext.java   View source code 5 votes vote down vote up
@Override
public boolean fireRequestInitEvent(ServletRequest request) {

    Object instances[] = getApplicationEventListeners();

    if ((instances != null) && (instances.length > 0)) {

        ServletRequestEvent event = 
                new ServletRequestEvent(getServletContext(), request);

        for (int i = 0; i < instances.length; i++) {
            if (instances[i] == null)
                continue;
            if (!(instances[i] instanceof ServletRequestListener))
                continue;
            ServletRequestListener listener =
                (ServletRequestListener) instances[i];
            
            try {
                listener.requestInitialized(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString(
                        "standardContext.requestListener.requestInit",
                        instances[i].getClass().getName()), t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
                return false;
            }
        }
    }
    return true;
}
 
Example 28
Project: apache-tomcat-7.0.73-with-comment   File: StandardContext.java   View source code 5 votes vote down vote up
@Override
public boolean fireRequestDestroyEvent(ServletRequest request) {
    Object instances[] = getApplicationEventListeners();

    if ((instances != null) && (instances.length > 0)) {

        ServletRequestEvent event = 
            new ServletRequestEvent(getServletContext(), request);

        for (int i = 0; i < instances.length; i++) {
            int j = (instances.length -1) -i;
            if (instances[j] == null)
                continue;
            if (!(instances[j] instanceof ServletRequestListener))
                continue;
            ServletRequestListener listener =
                (ServletRequestListener) instances[j];
            
            try {
                listener.requestDestroyed(event);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLogger().error(sm.getString(
                        "standardContext.requestListener.requestInit",
                        instances[j].getClass().getName()), t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
                return false;
            }
        }
    }
    return true;
}
 
Example 29
Project: lazycat   File: StandardContext.java   View source code 5 votes vote down vote up
@Override
public boolean fireRequestInitEvent(ServletRequest request) {

	Object instances[] = getApplicationEventListeners();

	if ((instances != null) && (instances.length > 0)) {

		ServletRequestEvent event = new ServletRequestEvent(getServletContext(), request);

		for (int i = 0; i < instances.length; i++) {
			if (instances[i] == null)
				continue;
			if (!(instances[i] instanceof ServletRequestListener))
				continue;
			ServletRequestListener listener = (ServletRequestListener) instances[i];

			try {
				listener.requestInitialized(event);
			} catch (Throwable t) {
				ExceptionUtils.handleThrowable(t);
				getLogger().error(sm.getString("standardContext.requestListener.requestInit",
						instances[i].getClass().getName()), t);
				request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
				return false;
			}
		}
	}
	return true;
}
 
Example 30
Project: lazycat   File: StandardContext.java   View source code 5 votes vote down vote up
@Override
public boolean fireRequestDestroyEvent(ServletRequest request) {
	Object instances[] = getApplicationEventListeners();

	if ((instances != null) && (instances.length > 0)) {

		ServletRequestEvent event = new ServletRequestEvent(getServletContext(), request);

		for (int i = 0; i < instances.length; i++) {
			int j = (instances.length - 1) - i;
			if (instances[j] == null)
				continue;
			if (!(instances[j] instanceof ServletRequestListener))
				continue;
			ServletRequestListener listener = (ServletRequestListener) instances[j];

			try {
				listener.requestDestroyed(event);
			} catch (Throwable t) {
				ExceptionUtils.handleThrowable(t);
				getLogger().error(sm.getString("standardContext.requestListener.requestInit",
						instances[j].getClass().getName()), t);
				request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
				return false;
			}
		}
	}
	return true;
}
 
Example 31
Project: xiaov   File: XiaoVServletListener.java   View source code 5 votes vote down vote up
@Override
public void requestDestroyed(final ServletRequestEvent servletRequestEvent) {
    try {
        super.requestDestroyed(servletRequestEvent);
    } finally {
        Stopwatchs.release();
    }
}
 
Example 32
Project: spring4-understanding   File: RequestContextListener.java   View source code 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
	if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
		throw new IllegalArgumentException(
				"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
	}
	HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
	ServletRequestAttributes attributes = new ServletRequestAttributes(request);
	request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
	LocaleContextHolder.setLocale(request.getLocale());
	RequestContextHolder.setRequestAttributes(attributes);
}
 
Example 33
Project: spring4-understanding   File: RequestContextListenerTests.java   View source code 5 votes vote down vote up
@Test
public void requestContextListenerWithDifferentThread() {
	final RequestContextListener listener = new RequestContextListener();
	final MockServletContext context = new MockServletContext();
	final MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	// Execute requestDestroyed callback in different thread.
	Thread thread = new Thread() {
		@Override
		public void run() {
			listener.requestDestroyed(new ServletRequestEvent(context, request));
		}
	};
	thread.start();
	try {
		thread.join();
	}
	catch (InterruptedException ex) {
	}
	// Still bound to original thread, but at least completed.
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());

	// Check that a repeated execution in the same thread works and performs cleanup.
	listener.requestInitialized(new ServletRequestEvent(context, request));
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
}
 
Example 34
Project: leopard   File: TopnbRequestServletRequestListener.java   View source code 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
	// System.err.println("requestInitialized:" + sre);
	START.set(System.nanoTime());

	// methodtime
	HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
	RequestHolder.setRequest(request);
	String uri = request.getRequestURI();
	EntryService.add(uri);
}
 
Example 35
Project: leopard   File: TopnbRequestServletRequestListener.java   View source code 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent sre) {
	long start = START.get();
	long time = System.nanoTime() - start;

	HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
	String url = request.getRequestURI();
	// System.out.println("add:" + url + " time:" + time);
	topService.add(url, time);
}
 
Example 36
Project: sumk   File: RequestListener.java   View source code 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent sre) {
	if (sre == null) {
		return;
	}
	HttpHeadersHolder.remove();
}
 
Example 37
Project: sumk   File: RequestListener.java   View source code 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent sre) {
	if (sre == null) {
		return;
	}
	ServletRequest request = sre.getServletRequest();
	if (HttpServletRequest.class.isInstance(request)) {
		HttpHeadersHolder.setHttpRequest((HttpServletRequest) request);
	}

}
 
Example 38
Project: tomcat-8-wffweb-demo-apps   File: WSServerForIndexPage.java   View source code 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent sre) {
    httpSession = ((HttpServletRequest) sre.getServletRequest())
            .getSession();
    LOGGER.info("requestDestroyed httpSession " + httpSession);

}
 
Example 39
Project: domui   File: WebApplicationListener.java   View source code 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent e) {
	LOG.debug("Request destroyed");
	Container c = Iocular.findRequestContainer((HttpServletRequest) e.getServletRequest());
	if(c != null)
		c.destroy();
}
 
Example 40
Project: symphonyx   File: SymphonyServletListener.java   View source code 5 votes vote down vote up
@Override
public void requestInitialized(final ServletRequestEvent servletRequestEvent) {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequestEvent.getServletRequest();

    httpServletRequest.setAttribute(Keys.TEMAPLTE_DIR_NAME, Symphonys.get("skinDirName"));

    if (Requests.searchEngineBotRequest(httpServletRequest)) {
        LOGGER.log(Level.DEBUG, "Request made from a search engine[User-Agent={0}]", httpServletRequest.getHeader("User-Agent"));
        httpServletRequest.setAttribute(Keys.HttpRequest.IS_SEARCH_ENGINE_BOT, true);

        return;
    }

    httpServletRequest.setAttribute(Keys.HttpRequest.IS_SEARCH_ENGINE_BOT, false);

    if (StaticResources.isStatic(httpServletRequest)) {
        return;
    }

    // Gets the session of this request
    final HttpSession session = httpServletRequest.getSession();
    LOGGER.log(Level.TRACE, "Gets a session[id={0}, remoteAddr={1}, User-Agent={2}, isNew={3}]",
            new Object[]{session.getId(), httpServletRequest.getRemoteAddr(), httpServletRequest.getHeader("User-Agent"),
                session.isNew()});

    resolveSkinDir(httpServletRequest);
}