Set a path to an outside-the-app file in web.xml - java

I'm working in a java webapp that uses Picketlink package. This package needs a config file ( picketlink.xml ) that is located in WEB-INF directory by default. This app will be hosted in 3 different linux jboss servers/environments (development, testing and production).
This configuration file is different for each env.
It's a project requirement that I have only one single build (single artifact) for all envs, so env-specific stuff must be in the server (can't use profiles). So, I need to store this file in the server itself and reference it from inside the app.
The documentation recommends that I use a context-param inside my web.xml to point to this external file:
<context-param>
<param-name>CONFIG_FILE</param-name>
<param-value>/path/to/picketlink.xml</param-value>
</context-param>
I just can't make it work as the app does not find the file.
What would be the right way to write this path? I wanted something like this:
<context-param>
<param-name>CONFIG_FILE</param-name>
<param-value>home/user/picketlink_config/picketlink.xml</param-value>
</context-param>

Related

How to specify a file path in web.xml file

i am developing an application in java and i am new to this java ee platform.I have a dataset file called "51Degrees-EnterpriseV3_2.dat" in my resource folder. how do i specify the path of this in my web.xml file and how do i retrieve the file in code?
In web.xml , you can add
<context-param>
<param-name>you can mention here localtion variable name</param-name>
<param-value>location here </param-value>
</context-param>
The other answer shows the correct way to add the parameter to your web.xml. Which for the 51Degrees V3.2 data file would look like this:
<context-param>
<description>The name of the device database.</description>
<param-name>BINARY_FILE_PATH</param-name>
<param-value>51Degrees-EnterpriseV3_2.dat</param-value>
</context-param>
As far as I know, there is not a way to get a resource which is located in the resources path (it's an XML file so the .getResource(name) method cannot be used). However, as you have a web.xml file, I assume this is a web project (i.e. .war rather than .jar)? If this is the case, then the WEB-INF directory is what you need. Files in here are also packaged up like resources, but can be more easily consumed by a .war package.
If you put your data file in src/main/webapp/WEB-INF/, which is used as the root by the web project. So the above XML example will work if the path to your data file is:
src/main/webapp/WEB-INF/51Degrees-EnterpriseV3_2.dat
As a sidenote, there is more documentation on configuring 51Degrees in a Java web app here

How to pass the -D parameter when multiple WAR files are running?

When multiple WAR files are running under tomcat,
each WAR is expecting a
-Dconfig-path=/path/app.conf.ini
Is it possible to pass a unique -D Parameter value to each of the running applications?
tomcat
webapps
APPLICATION_1.war -Dconfig-path=/path/app.conf1.ini
APPLICATION_2.war -Dconfig-path=/path/app.conf2.ini
APPLICATION_3.war -Dconfig-path=/path/app.conf3.ini
You can add all the configuration properties in one file and pass it as command line parameter while starting tomcat. All those properties will be available to all .war files.
But if all the war files are using same property name then you have to modify the property name in config file and your code.
For example: If you are using app.version=1.1 for 1st war and 2.1 for 2nd war then you have to add them like
Firstwarname.app.version=1.1
SecondwarName.app.version=2.1
Accordingly, your code needs to be modified to access properties.
I found a way
In the web.xml added this:
<context-param>
<param-name>config-path</param-name>
<param-value>/path/app.conf1.ini</param-value>
</context-param>

servlet filter not working over virtual directories in tomcat

I had configured virtual directories in glassfish3.x over which I could write filters.
For an example I could access files at c:/web from http://localhost/TestApp/web over which I could also place a filter at my web app's web/xml file with
<filter-mapping>
<filter-name>dir_filter</filter-name>
<url-pattern>/web/*</url-pattern>
</filter-mapping>
Unfortunately Tomcat 8.0 is not allowing me to write a filter above that. It simply ignores the filters and shows the content in the web directory.
The problem is anybody can access all of the files in the "web" folder.
Any how can we place filter over the virtual directories.
FYI - i have made the web app named "TestApp" and the virtual config is located at "$tomcat_dir/conf/Catalina/localhost" directory with the file name "TestApp#web.xml" file and having the content
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="C:/web" debug="0" privileged="true"></Context>
Regards
The context XML file TestApp#web.xml is mapping requests to /TestApp/web/... to the webapp. In the webapp, paths are relative to that, so /TestApp/web/x.txt is path /x.txt to the webapp, and will serve file C:/web/x.txt.
Change the filter to /*, so all requests are filtered, incl. the request for /x.txt.

How to eliminate hardcoded values in the web.xml

Let's say that I have something like this in my web.xml file.
<filter name="foo">
<init-param>
<param-name>fooBarUrl</param-name>
<param-value>http://foo.bar.com</param-value>
</init-param>
</filter>
Say there are different url values (for the param-value above) for dev/test/production. Is there a way that you can use filters and/or profiles in the pom to eliminate the need for changing this every time the application moves to a different stage? I.E. for dev it would be http://localfoo.com, and for test it would be http://testserver.com, etc.
You can use Maven's resource filtering ro replace properties with values at build time, e.g. by using different Maven profiles to set the properties values.
On the other hand you could use servlet parameters and move them to the context configuration, so your web.xml references them and they are actually configured in the application server where the .war file is deployed. That way, the application server administrator can reuse the same war file for each environment by just configuring it at server level.
In Tomcat for example, you can set the values in the context.xml file:
<Context>
...
<Parameter name="targetURL" value="http://testserver.com"
override="true"/>
...
</Context>
maven-replacer-plugin is what we use in our project for doing such text, pattern and version replacements during build time.

Maven copying applicationContext.xml from src/main/resources to target/myproject/WEB-INF

At the moment, the default I think, it copies to
target/myproject/WEB-INF/classes
so when deploying it does not pick up the context.
Also, i want to reference a server specific config file database.properties, I want to put it in tomcat/conf and then reference it in applicationContext.xml, how can I do this ?
Also(2), I am under the impression that this is a fairly standard and decent way to set things up - please correct me if I am wrong.
edit
for the server specific config file I user this
<context:property-placeholder
location="file:${catalina.home}/conf/database.properties"
ignore-unresolvable="true"
/>
If you need to keep applicationContext.xml as a classpath resource, you can configure ContextLoaderListener to pick it from the classpath by adding the following lines to web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
It's much easier than configuring Maven to copy it to WEB-INF.
Regarding the second question, you can configure PropertyPlaceholderConfigurer or <context:property-placeholder> to load .properties file from a file system.
For your title question: Often in a .war maven module, you'll put web related resources under src/main/webapp instead of src/main/resources. Then the maven plugin will pick them up automatically because it matches convention. So, move your applicationContext.xml to src/main/webapp/WEB-INF
Another option is to configure the webResources as described in the documentation
For the second question you can look at a PropertyPlaceHolderConfigurer. You'll just have to get the path correct.

Categories

Resources