My current situation is that I have a maven project which I'm deploying to Tomcat as a .war
file. Part of my project contents are actually third-party frontend pages (html/js/css) which result in two-way communication (I allow the user to navigate to these files using something like <a href="external/index.html">
, and as a result of user interaction these pages result in some kind of call back to my application via javascript).
The project structure resembles this:
- app/
|-- index.html
|-- js/
|-- css/
|-- external/
|---- index.html
|---- js/
|---- css/
What I would like to do is pull the external
contents out of my project/war file and instead link to them as some type of web resource. So far all of the solutions I've found seem to be focused on either externalizing the entire webapps directory, or setting single-page resources such as .pdf
files. Ideally these files wouldn't need to be hosted as a separate web application due to resource constraints and other concerns.
I've been trying to get something working that uses the answer posted in Simplest way to serve static data from outside the application server in a Java web application, but have been going in circles so wanted to verify this approach would work for what I was attempting.
EDIT:
Redirecting or forwarding won't work because this is data exists outside of my web application, but simply writing the html contents to the response will lose all the markup/scripts. Wondering if there might be something that could read this external
directory as a jsp, or if Spring has something for this, or if I'd need to flatten/minify the contents first?
EDIT2:
Something else I'm considering is placing these external files in ${tomcat}/webapps/external
alongside my main application, but that creates its own issue with security. These files contain no backend code, they are just js/html/css, so I'd have to find a way to restrict access unless the user is already authenticated.
EDIT3: An example of an ideal solution (that doesn't work) to illustrate what I would like to accomplish would be for the final structure to resemble below. What this would accomplish is allowing my app filters/security to still apply to requests made to the external resource, but I wouldn't need to worry about the external resources when building/packaging my war, and they could make edits to their external data without needing me to get involved.
- $CATALINA_HOME/webapp/
|-- app/
|---- index.html
|---- js/
|---- css/
|---- <symbolic link to external>
- external/
|-- index.html
|-- js/
|-- css/
I wanted to post my current working solution in case it helps anyone else, or so someone can tear it apart if I'm missing something. I was able to resolve with Spring MVC by defining an external resource handler and redirecting users to this:
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
@Value("${external.resource.location:file:///external/foo/}")
private String externalLocation;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/foo/**")
.addResourceLocations(externalResourcesLocation);
}
}
After adding this configuration and placing my external file content in /external/foo/[index.html|js/|css]
, I'm now able to access my externalized content when navigating to localhost:<port>/foo/index.html
without requiring it to be packaged or deployed within my .war
file.