spring-native icon indicating copy to clipboard operation
spring-native copied to clipboard

Support directory listing in Tomcat

Open edigonzales opened this issue 2 years ago • 2 comments

I implement a WebServerFactoryCustomizer for having file and directory listing in Tomcat:


@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory>  {
        
    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        TomcatContextCustomizer tomcatContextCustomizer = new TomcatContextCustomizer() {
            @Override
            public void customize(Context context) {
                context.setDocBase("/tmp"); 
                Wrapper defServlet = (Wrapper) context.findChild("default");
                defServlet.addInitParameter("listings", "true");
                defServlet.addInitParameter("sortListings", "true");
                defServlet.addInitParameter("sortDirectoriesFirst", "true");
                defServlet.addInitParameter("readOnly", "true");
                defServlet.addInitParameter("contextXsltFile", "/listing.xsl");
                defServlet.addMapping("/*");                
            }
        };
        factory.addContextCustomizers(tomcatContextCustomizer);        
    }
}

./gradlew nativeCompile works but Tomcat does not start and throws errors:

Description:
Native reflection configuration for org.apache.catalina.servlets.DefaultServlet.<init>() is missing.

So I added TypeHints: https://github.com/edigonzales/default-servlet-native-demo/blob/main/src/main/java/com/example/demo/DemoApplication.java#L8

The class appears now in the reflect-config.json file but still the application does not start.

I'm not sure if this is the current intentional behaviour because of https://github.com/spring-projects-experimental/spring-native/blob/main/spring-native/src/main/java/org/springframework/nativex/substitutions/tomcat/Target_DefaultServlet.java#L25 and https://github.com/spring-projects-experimental/spring-native/issues/1426

Minimal example: https://github.com/edigonzales/default-servlet-native-demo

Using GraalVM 22.1, Spring Boot 2.7.0 and Spring Native 0.12.0.

edigonzales avatar Jun 06 '22 14:06 edigonzales

Yes, that seems to be unsupported at the moment. The Target_DefaultServlet substitution removes the DefaultServlet from the native image, that's why it can't be instantiated via reflection.

mhalbritter avatar Jun 09 '22 08:06 mhalbritter

Yeah we need to provide a more flexible support in Spring Boot 3, so a good fit for backlog.

sdeleuze avatar Jun 10 '22 12:06 sdeleuze

Spring Native is now superseded by Spring Boot 3 official native support, see the related reference documentation for more details.

As a consequence, I am closing this issue, and recommend trying your use case with latest Spring Boot 3 version. If you still experience the issue reported here, please open an issue directly on the related Spring project (Spring Framework, Data, Security, Boot, Cloud, etc.) with a reproducer.

Thanks for your contribution on the experimental Spring Native project, we hope you will enjoy the official native support introduced by Spring Boot 3.

sdeleuze avatar Jan 02 '23 11:01 sdeleuze