greengold
greengold

Reputation: 1333

Add war to spring boot embedded tomcat

I have a spring-boot 2.1.2.RELEASE application that uses embedded tomcat webserver and uses OpenKM via it's SDK.

Now, I have some integration tests that use restassured lib to make REST calls and verify response structure. My idea is to integrate OpenKM.war into this wmbedded tomcat and be able to run this tests without need to have openkm application running on some different server.

this is how I make embedded tomcat to read and deploy openkm.war:

@Configuration
public class TomcatConfig {
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }

            @Override
            protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                new File(tomcat.getServer().getCatalinaBase(), "webapp").mkdirs();
                try {
                    tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
                } catch (Exception ex) {
                    throw new IllegalStateException("Failed to add okm", ex);
                }
                return super.getTomcatWebServer(tomcat);
            }
        };

        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
}

deploy takes way to longer than on the standalone tomcat webserver that is shipped with openkm, but it deploys.

However deployment process fails with:

IOException parsing XML document from URL [file:/tmp/tomcat.2352216859213135410.8080/openkm.xml]; nested exception is java.io.FileNotFoundException: /tmp/tomcat.2352216859213135410.8080/openkm.xml (No such file or directory)

I have this file (plus server.xml, context.xml) alongside openkm.war file, but it seems like embedded tomcat doesn't know about it.

All these files are located in src/main/resources/webapp.

So, I would like to know what configuration piece am I missing or whether there is whole other better to achieve what I want?

Upvotes: 8

Views: 1358

Answers (1)

buræquete
buræquete

Reputation: 14698

You can try doing something like;

StandardContext ctx = (StandardContext) tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
WebResourceRoot resources = new StandardRoot(ctx);
resources.addPreResources(new DirResourceSet(resources, "{target mount path}",
        new File("src/main/resources/webapp").getAbsolutePath(), "/"));
ctx.setResources(resources);

So that you can have your src/main/resources/webapp/ folder mounted on {target mount path} in tomcat deployment. Though I am unsure on this path? Can you try with "/", and maybe "/WEB-INF/" ? I have no way to test in my local currently but I hope this will help somehow.

You can mount individual files with FileResourceSet as well, if you need different files in different folders in tomcat.

Upvotes: 1

Related Questions