org.springframework.web.context.request.RequestContextListener Java Examples

The following examples show how to use org.springframework.web.context.request.RequestContextListener. 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: ApplicationInitializer.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {

	/* Setting the configuration classes */
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.setConfigLocation("qa.qcri.aidr.data.config");

	/*Configuring error handler filter for errors out isde the controllers
	FilterRegistration.Dynamic errorHandlerFilter = servletContext.addFilter("errorHandlerFilter", new ErrorHandlerFilter());
	errorHandlerFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
	*/
	FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new org.springframework.web.filter.CharacterEncodingFilter());
	encodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
	encodingFilter.setInitParameter("encoding", "UTF-8");
	encodingFilter.setInitParameter("forceEncoding", "true");
	
	/* Adding context listener */
	servletContext.addListener(new ContextLoaderListener(context));
	
	/* Adding request listener */
	servletContext.addListener(new RequestContextListener());

	/* Configuring dispatcher servlet for spring mvc */
	/*CustomDispatcherServlet servlet = new CustomDispatcherServlet(context); */
	ServletRegistration.Dynamic appServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
	appServlet.setLoadOnStartup(1);
	appServlet.addMapping("/*");
}
 
Example #3
Source File: WebServer.java    From metamodel-membrane with Apache License 2.0 5 votes vote down vote up
public static void startServer(int port, boolean enableCors) throws Exception {
    final DeploymentInfo deployment = Servlets.deployment().setClassLoader(WebServer.class.getClassLoader());
    deployment.setContextPath("");
    deployment.setDeploymentName("membrane");
    deployment.addInitParameter("contextConfigLocation", "classpath:context/application-context.xml");
    deployment.setResourceManager(new FileResourceManager(new File("."), 0));
    deployment.addListener(Servlets.listener(ContextLoaderListener.class));
    deployment.addListener(Servlets.listener(RequestContextListener.class));
    deployment.addServlet(Servlets.servlet("dispatcher", DispatcherServlet.class).addMapping("/*")
            .addInitParam("contextConfigLocation", "classpath:context/dispatcher-servlet.xml"));
    deployment.addFilter(Servlets.filter(CharacterEncodingFilter.class).addInitParam("forceEncoding", "true")
            .addInitParam("encoding", "UTF-8"));

    final DeploymentManager manager = Servlets.defaultContainer().addDeployment(deployment);
    manager.deploy();

    final HttpHandler handler;
    if (enableCors) {
        CorsHandlers corsHandlers = new CorsHandlers();
        handler = corsHandlers.allowOrigin(manager.start());
    } else {
        handler = manager.start();
    }

    final Undertow server = Undertow.builder().addHttpListener(port, "0.0.0.0").setHandler(handler).build();
    server.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            // graceful shutdown of everything
            server.stop();
            try {
                manager.stop();
            } catch (ServletException e) {
            }
            manager.undeploy();
        }
    });
}
 
Example #4
Source File: AbstractWebApplicationInitializer.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
protected void createWebApplicationContext(ServletContext servletContext, Class clazz) {
        log.info("Creating Web Application Context started");

        List<Class> configClasses = new ArrayList<>();
        configClasses.add(clazz);

        // let's determine if this is a cloud based server
        Cloud cloud = getCloud();

        String activeProfiles = System.getProperty(SPRING_PROFILES_ACTIVE);

        if (StringUtils.isEmpty(activeProfiles)) {
            if (cloud == null) {
                // if no active profiles are specified, we default to these profiles
                activeProfiles = String.format("%s,%s,%s,%s,%s", MONGODB_LOCAL,REDIS_LOCAL,RABBIT_LOCAL,ELASTICSEARCH_LOCAL,LOCAL);
            } else {
                activeProfiles = String.format("%s,%s,%s,%s,%s", MONGODB_CLOUD,REDIS_CLOUD,RABBIT_CLOUD,ELASTICSEARCH_CLOUD,CLOUD);
            }
        }

        log.info("Active spring profiles: " + activeProfiles);

        // load local or cloud based configs
        if (cloud != null) {

            // list available service - fail servlet initializing if we are missing one that we require below
            printAvailableCloudServices(cloud.getServiceInfos());

        }

        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(configClasses.toArray(new Class[configClasses.size()]));

        servletContext.addListener(new ContextLoaderListener(appContext));
        servletContext.addListener(new RequestContextListener());

//        log.info("Creating Web Application Context completed");
    }
 
Example #5
Source File: RegistryAppInitializer.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    super.onStartup(servletContext);
    servletContext.addListener(new RequestContextListener());

    // verifying configs for features activation
    Set<String> profiles = new HashSet<String>();
    if (isEmailFeaturesEnabled()) {
        profiles.add("email");
    }
    if (isCdnFeaturesEnabled()) {
        profiles.add("cdn");
    }
    if (isSslFeaturesEnabled()) {
        profiles.add("ssl");
    }

    servletContext.setInitParameter("spring.profiles.active", StringUtils.arrayToCommaDelimitedString(profiles.toArray()));
}
 
Example #6
Source File: JoinfacesAutoConfiguration.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
/**
 * This registers a {@link RequestContextFilter} in case {@link WebMvcAutoConfiguration} is not loaded.
 *
 * @see WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#requestContextFilter()
 * @return The {@link RequestContextFilter} Bean.
 */
@Bean
@ConditionalOnMissingBean({ RequestContextListener.class, RequestContextFilter.class })
@ConditionalOnMissingFilterBean(RequestContextFilter.class)
public static RequestContextFilter requestContextFilter() {
	return new OrderedRequestContextFilter();
}
 
Example #7
Source File: WebMVCConfig.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义,并注册   listener 演示
 * 直接用 Bean ,也可以用 ServletListenerRegistrationBean
 *
 * @return
 */
// 向系统注册一个 RequestContextListener Bean ,这样在其他组件中就可以使用了
//  CustomUserDetailsService 用到,用于截获 HttpServletRequest
//  @Autowired
//  private HttpServletRequest request;
@Bean
public RequestContextListener requestContextListener() {

    return new RequestContextListener();
}
 
Example #8
Source File: WebConfig.java    From wetech-admin with MIT License 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #9
Source File: ListenerConfiguration.java    From kob with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #10
Source File: WebConfig.java    From Guns with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * RequestContextListener注册
 */
@Bean
public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
    return new ServletListenerRegistrationBean<>(new RequestContextListener());
}
 
Example #11
Source File: SecurityConfig.java    From kafka-webview with MIT License 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #12
Source File: WebConfig.java    From sanshanblog with Apache License 2.0 4 votes vote down vote up
/**
  * 允许请求到外部的Listener
  * @return
  */
@Bean
public ServletListenerRegistrationBean<RequestContextListener> requestContextListener(){
    return new ServletListenerRegistrationBean<RequestContextListener>(new RequestContextListener());
}
 
Example #13
Source File: MvcConfig.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #14
Source File: WallRideServletConfiguration.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
	return new RequestContextListener();
}
 
Example #15
Source File: Application.java    From Vaadin4Spring-MVP-Sample-SpringSecurity with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #16
Source File: MolgenisWebAppInitializer.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** A Molgenis common web application initializer */
protected void onStartup(ServletContext servletContext, Class<?> appConfig, int maxFileSize) {
  // Create the 'root' Spring application context
  AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
  rootContext.registerShutdownHook();
  rootContext.setAllowBeanDefinitionOverriding(false);
  rootContext.register(appConfig);

  // Manage the lifecycle of the root application context
  servletContext.addListener(new ContextLoaderListener(rootContext));

  // Register and map the dispatcher servlet
  DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
  dispatcherServlet.setDispatchOptionsRequest(true);
  // instead of throwing a 404 when a handler is not found allow for custom handling
  dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

  ServletRegistration.Dynamic dispatcherServletRegistration =
      servletContext.addServlet("dispatcher", dispatcherServlet);
  if (dispatcherServletRegistration == null) {
    LOG.warn(
        "ServletContext already contains a complete ServletRegistration for servlet 'dispatcher'");
  } else {
    final long maxSize = (long) maxFileSize * MB;
    dispatcherServletRegistration.addMapping("/");
    dispatcherServletRegistration.setMultipartConfig(
        new MultipartConfigElement(null, maxSize, maxSize, FILE_SIZE_THRESHOLD));
    dispatcherServletRegistration.setAsyncSupported(true);
  }

  // Add filters
  Dynamic browserDetectionFiler =
      servletContext.addFilter("browserDetectionFilter", BrowserDetectionFilter.class);
  browserDetectionFiler.setAsyncSupported(true);
  browserDetectionFiler.addMappingForUrlPatterns(
      EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), false, "*");

  Dynamic etagFilter = servletContext.addFilter("etagFilter", ShallowEtagHeaderFilter.class);
  etagFilter.setAsyncSupported(true);
  etagFilter.addMappingForServletNames(
      EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), true, "dispatcher");

  // enable use of request scoped beans in FrontController
  servletContext.addListener(new RequestContextListener());

  servletContext.addListener(HttpSessionEventPublisher.class);
}
 
Example #17
Source File: UiApplication.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #18
Source File: UiApplication.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #19
Source File: Application.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onStartup(ServletContext sc) throws ServletException {
    // Manages the lifecycle of the root application context
    sc.addListener(new RequestContextListener());
}
 
Example #20
Source File: Application.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onStartup(ServletContext sc) throws ServletException {
    // Manages the lifecycle of the root application context
    sc.addListener(new RequestContextListener());
}
 
Example #21
Source File: WebConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #22
Source File: WebAppSecurityConfig.java    From camunda-bpm-identity-keycloak with Apache License 2.0 4 votes vote down vote up
@Bean
@Order(0)
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #23
Source File: WebConfig.java    From MeetingFilm with Apache License 2.0 4 votes vote down vote up
/**
 * RequestContextListener注册
 */
@Bean
public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
    return new ServletListenerRegistrationBean<>(new RequestContextListener());
}
 
Example #24
Source File: WebConfig.java    From WebStack-Guns with MIT License 4 votes vote down vote up
/**
 * RequestContextListener注册
 */
@Bean
public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
    return new ServletListenerRegistrationBean<>(new RequestContextListener());
}
 
Example #25
Source File: WebSecurityConfiguration.java    From personal_book_library_web_project with MIT License 4 votes vote down vote up
@Bean 
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #26
Source File: WebConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #27
Source File: WebConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #28
Source File: WebConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
	return new RequestContextListener();
}
 
Example #29
Source File: WebConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}
 
Example #30
Source File: WebConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public RequestContextListener requestContextListener() {
	return new RequestContextListener();
}