Skip to content

Allow simpler extension of the SpringServlet / structuring of SpringBootAutoConfiguration #19994

@pgould-taskize

Description

@pgould-taskize

Describe your motivation

We have certain ThreadLocal values that need to be set before access tasks are run.

The com.vaadin.flow.server.VaadinService provides a hook to allow overriding the accessSession, and in order to do this, one needs to extend the VaadinServlet class and override the createServletService(), which used to be advertised as a
https://vaadin.com/docs/v14/flow/advanced/tutorial-application-lifecycle.

    /**
     * Implementation for {@link VaadinSession#access(Command)}. This method is
     * implemented here instead of in {@link VaadinSession} to enable overriding
     * the implementation without using a custom subclass of VaadinSession.
     *
     * @param session
     *            the vaadin session to access
     * @param command
     *            the command to run with the session locked
     * @return a future that can be used to check for task completion and to
     *         cancel the task
     * @see VaadinSession#access(Command)
     */
    public Future<Void> accessSession(VaadinSession session, Command command) 
        ...
    }

This documentation has been removed in the V24 documentation, yet no other strategy for achieving the same process has been given, although it seems that this is still possible but requires requires cut + pasting of thecom.vaadin.flow.spring.SpringBootAutoConfiguration#servletRegistrationBean.

Describe the solution you'd like

Could the auto-configuration be modified in such a way that cut and pasting the entire servletRegistrationBean is not necessary, for example, the only piece of code that I'd like to have to supply is the springServletBean definition in the following configuration:

@Configuration
public class OverriddenServletConfiguration {

    @Autowired
    private WebApplicationContext context;

    @Bean
    public SpringServlet springServletBean(VaadinConfigurationProperties configurationProperties) {
        String mapping = configurationProperties.getUrlMapping();
        boolean rootMapping = RootMappedCondition.isRootMapping(mapping);
        return new OverriddenSpringServlet(context, rootMapping);
    }

    @Bean
    public ServletRegistrationBean<SpringServlet> servletRegistrationBean(
            ObjectProvider<MultipartConfigElement> multipartConfig,
            VaadinConfigurationProperties configurationProperties,
            SpringServlet springServlet) {
        String mapping = configurationProperties.getUrlMapping();
        Map<String, String> initParameters = new HashMap<>();
        boolean rootMapping = RootMappedCondition.isRootMapping(mapping);

        if (rootMapping) {
            mapping = VaadinServletConfiguration.VAADIN_SERVLET_MAPPING;
            initParameters.put(
                    VaadinServlet.INTERNAL_VAADIN_SERVLET_VITE_DEV_MODE_FRONTEND_PATH,
                    "");
        }

        String pushUrl = rootMapping ? "" : mapping.replace("/*", "");
        pushUrl += "/" + Constants.PUSH_MAPPING;

        initParameters.put(ApplicationConfig.JSR356_MAPPING_PATH, pushUrl);

        ServletRegistrationBean<SpringServlet> registration = new ServletRegistrationBean<>(springServlet, mapping);
        registration.setInitParameters(initParameters);
        registration
                .setAsyncSupported(configurationProperties.isAsyncSupported());
        registration.setName(
                ClassUtils.getShortNameAsProperty(SpringServlet.class));
        // Setup multi part form processing for non root servlet mapping to be
        // able to process Hilla login out of the box
        if (!rootMapping) {
            multipartConfig.ifAvailable(registration::setMultipartConfig);
        }
        registration.setLoadOnStartup(
                configurationProperties.isLoadOnStartup() ? 1 : -1);
        return registration;
    }

}

Describe alternatives you've considered

Any other hooks to execute code prior to access tasks being run and cleaned up afterwards would be perfectly acceptable.

Additional context

Is there anything else you can add about the proposal?

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Status

Done / Pending Release

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions