Add war to spring boot embedded tomcat
Asked Answered
G

1

8

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?

Gefell answered 13/5, 2019 at 14:14 Comment(2)
I have been faced with tmp dir issue before, can you try manually creating tmp dir and running your server?Livengood
that directory exists. problem is that file is really not there so I think tomcat doesn't know about it and how to treat it.Gefell
H
1

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.

Hegel answered 16/10, 2019 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.