javax.servlet.ServletRequestEvent Java Examples

The following examples show how to use javax.servlet.ServletRequestEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchAppTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 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" );

    assertNotNull( instance.getPage( request, nMode, plugin ) );

    listener.requestDestroyed( new ServletRequestEvent( context, request ) );
}
 
Example #2
Source File: RequestContextListener.java    From lams with GNU General Public License v2.0 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
Source File: RequestContextListenerTests.java    From spring4-understanding with Apache License 2.0 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
Source File: GeomajasContextListener.java    From geomajas-project-server with GNU Affero General Public License v3.0 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 #5
Source File: RequestContextListener.java    From spring4-understanding with Apache License 2.0 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 #6
Source File: RequestContextListener.java    From spring-analysis-note with MIT License 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 #7
Source File: RequestContextListenerTests.java    From spring-analysis-note with MIT License 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 #8
Source File: RequestContextListener.java    From java-technology-stack with MIT License 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 #9
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
public void requestDestroyed(final ServletRequest request) {
    if(!started) {
        return;
    }
    if(servletRequestListeners.length > 0) {
        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 #10
Source File: RequestDisposalTest.java    From sling-org-apache-sling-models-impl with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithUnitializedRequest() {
    // destroy a wrapper of the root request
    SlingHttpServletRequest destroyedRequest = new SlingHttpServletRequestWrapper(request);

    // but adapt from a wrapper of a wrapper of that wrapper
    SlingHttpServletRequest adaptableRequest = new SlingHttpServletRequestWrapper(new SlingHttpServletRequestWrapper(destroyedRequest));

    TestModel model = factory.getAdapter(adaptableRequest, TestModel.class);
    assertEquals("teststring", model.testString);

    assertNoneDisposed();

    factory.requestDestroyed(new ServletRequestEvent(servletContext, destroyedRequest));

    assertNoneDisposed();
}
 
Example #11
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 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 #12
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 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 #13
Source File: CdiServletRequestListener.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent)
{
    LOG.log(Level.FINER,"Request done.");
    ContextControl contextControl = (ContextControl)servletRequestEvent.getServletRequest()
            .getAttribute(CDI_REQ_CONTEXT);
    contextControl.stopContext(RequestScoped.class);
}
 
Example #14
Source File: SessionsCounterServiceTest.java    From live-chat-engine with Apache License 2.0 5 votes vote down vote up
@Test
public void test_work(){
	
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest req1 = new MockHttpServletRequest();
	MockHttpServletRequest req2 = new MockHttpServletRequest();
	
	SessionsCounterService service = new SessionsCounterService();
	
	assertFalse(service.isInited());
	service.init(context);
	assertTrue(service.isInited());
	assertEquals(0, service.getSessionsCount(req1));
	assertEquals(0, service.getSessionsCount(req2));
	
	service.requestInitialized(new ServletRequestEvent(context, req1));
	service.sessionCreated(new HttpSessionEvent(req1.getSession()));
	service.requestDestroyed(new ServletRequestEvent(context, req1));
	assertEquals(1, service.getSessionsCount(req1));
	assertEquals(1, service.getSessionsCount(req2));
	
	
	service.requestInitialized(new ServletRequestEvent(context, req2));
	service.sessionCreated(new HttpSessionEvent(req2.getSession()));
	assertEquals(2, service.getSessionsCount(req1));
	assertEquals(2, service.getSessionsCount(req2));
	
	service.sessionDestroyed(new HttpSessionEvent(req1.getSession()));
	assertEquals(1, service.getSessionsCount(req1));
	assertEquals(1, service.getSessionsCount(req2));
	
	service.sessionDestroyed(new HttpSessionEvent(req2.getSession()));
	assertEquals(0, service.getSessionsCount(req1));
	assertEquals(0, service.getSessionsCount(req2));
	
}
 
Example #15
Source File: RequestContextListener.java    From spring4-understanding with Apache License 2.0 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 #16
Source File: ThreadLocalCleanupListener.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(final ServletRequestEvent sre) {
    FormatUtils.clear();

    ThreadClassLoaderManager.clearInstance();
    CurrentLocale.clear();
}
 
Example #17
Source File: GeomajasContextListener.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
	if (servletRequestEvent.getServletRequest() instanceof HttpServletRequest) {
		HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
		ServletRequestAttributes attributes = new ServletRequestAttributes(request);
		request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
		LocaleContextHolder.setLocale(request.getLocale());
		RequestContextHolder.setRequestAttributes(attributes);
	}
}
 
Example #18
Source File: RequestContextListenerTests.java    From spring-analysis-note with MIT License 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 #19
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void requestInitialized(final ServletRequest request) {
    if(!started) {
        return;
    }
    if(servletRequestListeners.length > 0) {
        final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
        for (int i = 0; i < servletRequestListeners.length; ++i) {
            this.<ServletRequestListener>get(servletRequestListeners[i]).requestInitialized(sre);
        }
    }
}
 
Example #20
Source File: RequestResponseHolderListener.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent sre)
{
    if (activated)
    {
        RequestResponseHolder.REQUEST.release();
    }
}
 
Example #21
Source File: SHDRequestListener.java    From kardio with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
    HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
    String endPointName = request.getRequestURI();
    endPointName = endPointName.substring(1);
    endPointName = endPointName.replaceAll("/", "_");
    prometheusMetricService.initializeMetricData(request, endPointName);
}
 
Example #22
Source File: BeginWebBeansListener.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void requestDestroyed(final ServletRequestEvent event) {
    if (webBeansContext == null) {
        return;
    }

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

    final Object oldContext;
    if (event != null) {
        oldContext = event.getServletRequest().getAttribute(contextKey);
    } else {
        oldContext = null;
    }

    try {
        // clean up the EL caches after each request
        final ELContextStore elStore = ELContextStore.getInstance(false);
        if (elStore != null) {
            elStore.destroyELContextStore();
        }

        contextsService.endContext(RequestScoped.class, event);
        if (webBeansContext instanceof WebappWebBeansContext) { // end after child
            ((WebappWebBeansContext) webBeansContext).getParent().getContextsService().endContext(RequestScoped.class, event);
        }
    } finally {
        contextsService.removeThreadLocals();
        ThreadSingletonServiceImpl.enter((WebBeansContext) oldContext);
    }
}
 
Example #23
Source File: RequestContextListener.java    From java-technology-stack with MIT License 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 #24
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 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 #25
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Fire the request initialized event
 *
 * @param request the request
 */
private void requestInitialized(ServletRequest request) {
    if (!requestListeners.isEmpty()) {
        requestListeners.stream().forEach((servletRequestListener) -> {
            servletRequestListener.requestInitialized(new ServletRequestEvent(this, request));
        });
    }
}
 
Example #26
Source File: HttpRequestImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void init() {
    if (begin != null && getAttribute("openejb_requestInitialized") == null) {
        setAttribute("openejb_requestInitialized", "ok"); // if called again we loose the request scope
        begin.requestInitialized(new ServletRequestEvent(getServletContext(), this));
    }

    listeners = LightweightWebAppBuilderListenerExtractor.findByTypeForContext(contextPath, ServletRequestListener.class);
    if (!listeners.isEmpty()) {
        final ServletRequestEvent event = new ServletRequestEvent(getServletContext(), this);
        for (final ServletRequestListener listener : listeners) {
            listener.requestInitialized(event);
        }
    }
}
 
Example #27
Source File: RequestListener.java    From trimou with Apache License 2.0 5 votes vote down vote up
@Override
public void requestInitialized(ServletRequestEvent event) {
    if (event.getServletRequest() instanceof HttpServletRequest) {
        RequestHolder.requestInitialized((HttpServletRequest) event
                .getServletRequest());
    } else {
        throw new IllegalStateException(
                "This extension only supports HTTP Servlet lifecycle");
    }
}
 
Example #28
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void requestInitialized(final ServletRequest request) {
    if(!started) {
        return;
    }
    if(servletRequestListeners.length > 0) {
        final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
        for (int i = 0; i < servletRequestListeners.length; ++i) {
            this.<ServletRequestListener>get(servletRequestListeners[i]).requestInitialized(sre);
        }
    }
}
 
Example #29
Source File: ServletCallbackTest.java    From bugsnag-java with MIT License 5 votes vote down vote up
/**
 * Generate a new request instance which will be read by the servlet
 * context and callback
 */
@Before
public void setUp() {
    bugsnag = new Bugsnag("apikey", false);
    bugsnag.setDelivery(null);

    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");

    when(request.getHeaderNames()).thenReturn(
            stringsToEnumeration(
                    "Content-Type",
                    "Content-Length",
                    "X-Custom-Header",
                    "Authorization",
                    "Cookie"));
    when(request.getHeaders("Content-Type")).thenReturn(
            stringsToEnumeration("application/json"));
    when(request.getHeaders("Content-Length")).thenReturn(
            stringsToEnumeration("54"));
    when(request.getHeaders("X-Custom-Header")).thenReturn(
            stringsToEnumeration("some-data-1", "some-data-2"));
    when(request.getHeaders("Authorization")).thenReturn(
            stringsToEnumeration("Basic ABC123"));
    when(request.getHeaders("Cookie")).thenReturn(
            stringsToEnumeration("name1=val1; name2=val2"));

    ServletContext context = mock(ServletContext.class);
    BugsnagServletRequestListener listener = new BugsnagServletRequestListener();
    listener.requestInitialized(new ServletRequestEvent(context, request));
}
 
Example #30
Source File: RequestContextListener.java    From lams with GNU General Public License v2.0 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);
}