Reputation: 1101
I have been attempting to obtain the context root of an application which is being deployed by using the app base of the host and doc base of the context.
try {
if (context != null) {
// Value of the following variable depends on various conditions. Sometimes you get just the webapp
// directory name. Sometime you get absolute path the webapp directory or war file.
String webappFilePath;
Host host = (Host) context.getParent();
String appBase = host.getAppBase();
File canonicalAppBase = new File(appBase);
if (canonicalAppBase.isAbsolute()) {
canonicalAppBase = canonicalAppBase.getCanonicalFile();
} else {
canonicalAppBase = new File(PathUtils.getCatalinaBase().toString(), appBase).getCanonicalFile();
}
String docBase = context.getDocBase();
File webappFile = new File(docBase);
if (webappFile.isAbsolute()) {
webappFilePath = webappFile.getCanonicalPath();
} else {
webappFilePath = (new File(canonicalAppBase, docBase)).getPath();
}
return Paths.get(webappFilePath);
} else {
throw new ApplicationServerException("Context cannot be null");
}
} catch (IOException ex) {
throw new ApplicationServerException("Error while generating webapp file path", ex);
}
The purpose of accessing the context root was to access a custom configuration file I have added to /WEB-INF folder.
When researching further, I discovered the following simple ways of accessing the a resource located within the app's folder.
How to get file system path of the context root of any application
But when I use the method defined here, I am always getting a null value for the context root.
Path contextWebAppDescriptor = Paths.
get(context.getServletContext().getRealPath("/"), Constants.WEBAPP_RESOURCE_FOLDER,
Constants.WEBAPP_DESCRIPTOR);
Please consider the two constants:
public static final String WEBAPP_RESOURCE_FOLDER = "WEB-INF";
public static final String WEBAPP_DESCRIPTOR = "wso2as-web.xml";
Have I misunderstood the proper usage of this method? Can we only use the method
context.getServletContext().getRealPath("/");
only within the web app code in order to function properly?
Upvotes: 0
Views: 1644
Reputation: 1101
I managed to solve this issue by using the ServletContext.getResourceAsStream() which can be used to obtain an InputStream for the specified resource.
This is simply the way in which Tomcat itself performs the reading of its web.xml as far as I know. And it has to be noted that this method is effective at the LifeCycle event CONFIGURE_START_EVENT rather than BEFORE_START_EVENT.
Upvotes: 0
Reputation: 89169
A better approach, IMHO, will be to use ServletContext.getResourcePaths()
method.
This, essentially:
Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument.
So, to get all the content inside your WEB-INF
folder, you could do:
Set<String> paths = servletContext.getResourcePaths("/WEB-INF/");
Then iterate through paths
to get the relevant resource. Resource ending /
indicates a sub-directory.
Upvotes: 1