Setting VaadinServlet Init-Parameters when using vaadin-spring (and no web.

Hey guys,

I am using the new vaadin-spring / vaadin-spring-boot addon and wonder how I can set some custom Init-parameters to Vaadin-Servlet when not using a web.xml (because old and ugly).

Specifically I want to set what I would have done with web.xml the following way:

org.atmosphere.cpr.AtmosphereInterceptor acolson.atmosphere.RecoverSecurityContextAtmosphereInterceptor

If I add it to application.properties it is ignored.

You should be able to give it as an annotation in you Servlet that extends VaadinServlet using the

@WebServlet(value = “/*”, asyncSupported = true, initParams = {
@WebInitParam(name = “org.atmosphere.cpr.AtmosphereInterceptor”, value = “acolson.atmosphere.RecoverSecurityContextAtmosphereInterceptor”)})

For more information on this see:
https://vaadin.com/wiki/-/wiki/Main/Creating+a+servlet+3.0+application

Thanks for the hint - I tried it but it seems to be not working the way I’m doing it:

[code]
@EnableAutoConfiguration
@ComponentScan
@Configuration
@EnableJpaRepositories
public class Application
{
public static void main(String args) throws Throwable
{
SpringApplication.run(Application.class, args);
}

@Bean
public SpringVaadinServlet createMyVaadinServlet()
{
    return new MyVaadinSpringServlet();
}


@WebServlet(value = "/ui", asyncSupported = true, initParams = {
@WebInitParam(name = "org.atmosphere.cpr.AtmosphereInterceptor", value = "acolson.atmosphere.RecoverSecurityContextAtmosphereInterceptor")})
public static class MyVaadinSpringServlet extends SpringVaadinServlet
{
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
    }
}

}
[/code]I only overrride the servletInitialized to track if the right servlet is instantiated (it is) and to see which parameters are stored in ServletConfig of the VaadinServlet init-method.
Unfortunately I can’t find any parameter entries “org.atmosphere.cpr.AtmosphereInterceptor” or something similar in the parameters map of the StandardWrapperFacade.

Shouldn’t I see new WebInitParams there?

Also no method of RecoverSecurityContextAtmosphereInterceptor is ever called.

Any hints what I’m doing wrong?

Hi.
I don’t know how I missed the fact that you are using the spring-vaadin add-on.

The only thing I can come up with on short notice would be to override the init method in
your VaadinSpringServlet like:

[code]
public static class MyVaadinSpringServlet extends SpringVaadinServlet {
@Override
public init(ServletConfig config) throws ServletException {
final ServletContext context = servletConfig.getServletContext();
// Set own initParameters here.
context.setInitParameter(“org.atmosphere.cpr.AtmosphereInterceptor”,
“acolson.atmosphere.RecoverSecurityContextAtmosphereInterceptor”);

        super.init(config);
    }
 
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
    }
}

[/code]This is probably as much as I can help with in this case as I’m not familiar with the add-on.
Hope this helps.

I already tried that - unfortunately it leads to an “Allocate exception”:

2015-05-18 10:31:17.270 ERROR 13708 --- [nio-8080-exec-5]
 o.a.c.c.C.[.[.[.[myVaadinSpringServlet]
  : Allocate exception for servlet myVaadinSpringServlet

java.lang.IllegalStateException: Initialization parameters can not be set after the context has been initialized
    at org.apache.catalina.core.ApplicationContext.setInitParameter(ApplicationContext.java:1228)
    at org.apache.catalina.core.ApplicationContextFacade.setInitParameter(ApplicationContextFacade.java:624)
    at com.fumm.Application$MyVaadinSpringServlet.init(Application.java:45)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:837)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

I guess it has to be set in a different way - just can’t figure out how so far - but thanks for your help anyway!

Perhaps you could override the createDeploymentConfiguration?

@Override protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
    initParameters.setProperty("org.atmosphere.cpr.AtmosphereInterceptor",
                "acolson.atmosphere.RecoverSecurityContextAtmosphereInterceptor");
    return super.createDeploymentConfiguration(initParameters);
}

Hm, it seems to do the trick (at least I can log in now again with Spring Security while Push is enabled).
Though I have the impression this is more a hack than a clean solution. I’m playing around with ServletContainerInitializer at the moment - will post if I find a different solution.
Many thanks for your help, though!

Overriding DeploymentConfiguration didn’t do the trick in the end.
Though I have a feeling about being not very clean the following approach worked for me:

[code]
@Configuration
@EnableConfigurationProperties(VaadinServletConfigurationProperties.class)
@Import(VaadinServletConfiguration.class)
public class MyVaadinServletConfiguration extends VaadinServletConfiguration
{
@Bean
ServletRegistrationBean vaadinServletRegistration()
{
return createServletRegistrationBean();
}

@Override
protected void addInitParameters(
        ServletRegistrationBean servletRegistrationBean)
{
    super.addInitParameters(servletRegistrationBean);
    
    addInitParameter(servletRegistrationBean, "org.atmosphere.cpr.AtmosphereInterceptor", "acolson.atmosphere.RecoverSecurityContextAtmosphereInterceptor");    
    addInitParameter(servletRegistrationBean, ApplicationConfig.JSR356_MAPPING_PATH, "/VAADIN");
}

private void addInitParameter(
        ServletRegistrationBean servletRegistrationBean, String paramName,
        String propertyValue) {
    if (propertyValue != null) {
        getLogger().info("Set servlet init parameter [{}]

= [{}]
",
paramName, propertyValue);
servletRegistrationBean.addInitParameter(paramName, propertyValue);
}
}
}
[/code]It would be absolutely great if one of the guys working on vaadin-spring / vaadin-spring-boot addon could give feedback on that solution.
Is it intended to add Init-Parameters that way to SpringVaadinServlet?