XSL import causing FileNotFoundException in web application - java

I have a simple web application under websphere5. Under appDir\WEB-INF\classes\ I have these files:
main.xslt
templates.xslt
main.xslt contains the instruction
<xsl:import href="templates.xsl" />
but the application fails when main.xslt is used in Java code. How should I specify the path to imported XSL files if they all are in the same folder (WEB-INF\classes\)?
Text of exception:
java.io.FileNotFoundException: d:\Program Files\WebSphere\AppServer1\templates.xsl (The system cannot find the file specified.
)

You need to provide a custom uri-resolver to process the includes. In a web application, there's no guarantee that a filesystem is accessible, as you could be running out of a WAR file. Take a look at the javax.xml.transform.URIResolver interface and Transformer.setURIResolver()

Depending on how you loaded the main.xsl, you may need to set the SystemID property, so that it can resolve the relative path.

Related

where should I locate a common ESAPI.properties file used by multiple wars inside an ear?

I have two modules that will use ESAPI with the same properties files (ESAPI and validation.properties).
These modules output to wars that are contained in an ear.
I have the properties files inside one of the war files, where they are found at server start. The other war file seems to work fine and does not complain that it can't find the properties files in the log.
I am using ESAPI to sanitize html and url parameters - I wonder if I even need these property files to be accessible to the second module, or either one since there is no configuration and everything is being done with defaults.
First, let me describe how ESAPI 2.x goes about finding its ESAPI.properties file.
The reference implementation class for ESAPI's SecurityConfiguration interface is
org.owasp.esapi.reference.DefaultSecurityConfiguration
With this default implementation, resources like ESAPI.properties and
Validation.properties can be put in several locations, which are searched in the following order:
1) Inside a directory set with a call to SecurityConfiguration.setResourceDirectory(). E.g.,
ESAPI.securityConfiguration().setResourceDirectory("C:\myApp\resources");
Of course, if you use this technique, it must be done before any other ESAPI calls are made that use ESAPI.properties (which are most of them).
2) Inside the directory defined by the System property "org.owasp.esapi.resources". You can set this on the java command line as follows (for example):
java -Dorg.owasp.esapi.resources="C:\temp\resources" ...
You may have to add this to the start-up script that starts your web server. For example, for Tomcat, in the "catalina" script that starts Tomcat, you can set the JAVA_OPTS variable to the '-D' string above.
3) Inside the
System.getProperty( "user.home" ) + "/.esapi"
directory (supported for backward compatibility) or inside the
System.getProperty( "user.home" ) + "/esapi"
4) The first ".esapi" or "esapi" directory encountered on the classpath. Note this may be complicated by the fact that Java uses multiple class loaders and if you are have multiple applications in a given application server, they may be using different classpaths. For this reason, this option is not generally recommended, but is offered for reasons of backward compatibility with earlier ESAPI 1.4.x versions.
Once ESAPI finds a valid property file (e.g., ESAPI.properties) that it can read, it stops searching for others.
Now, that said, if you want to share a single ESAPI.properties file across all of your .war files, I would recommend going with option #2 and set the System property "org.owasp.esapi.resources" to some common secured directory that both of them can access. Also, you should use a full path name.
The answer was to place the esapi directory containing the properties files in
src/main/application
in one of the modules. This path puts it's contents at the root of the ear.
I'm running ESAPI on a maven project with java 1.8.0_71. I've put ESAPI.properties and validation.properties in
src/main/resources
This worked for me:
Attempting to load ESAPI.properties via the classpath.
SUCCESSFULLY LOADED ESAPI.properties via the CLASSPATH from '/ (root)' using current thread context class loader!
Attempting to load validation.properties via the classpath.
SUCCESSFULLY LOADED validation.properties via the CLASSPATH from '/ (root)' using current thread context class loader!

Error in getting path of file on server

I've a service class in src.service.ABCservice
In ABCService class, I need to write in a xml file.
The xml file is in resource folder, which is at same level of src folder
When i run it as a single java class though main method, I can write in the xml using path ./resource/aaa.xml but when I run it on my tomcat server, it fails to read the file.
ERROR : java.io.FileNotFoundException: .\resources\LocationOfOperation.xml (The system cannot find the path specified)
Please tell me how to reach the xml file and write in it.
When you run as single class, you can read project's file path, but when you package and deploy as web project you miss project path reference.
You must create a parameter (context param, file properties, ...) to point to a path that existing into web part, thus you can use the files contained in that path to your porpouses.

How can I programmatically find the path of JNLP file? I am using Java Web Start to generate the JNLP file

How can I programmatically find the path of JNLP file?I am using Java Web Start to generate the JNLP file.
I know that manually you can find the JNLP file in the Java Cache Viewer in Resources with the name launch.jnlp, but I really need to know if there exists a Java class that can programmatically find the jnlp file by searching the in-memory cache.
How can I programatically find the path of JNLP file?
It is hidden deliberately. If you can find it from within code (shorting of asking the user to browse to it), the JRE has a security bug.
..for launching the installer I need the application jar files path
No you don't.
Put the installer inside a Jar.
Add the Jar to the resources of the app.
Get an URL using getResource(String).
Read the byte[] and write it to a temporary file at a known location (e.g. java.io.tmpdir).
Launch the installer from the known location.
String jnlpPath = System.getProperty("jnlpx.origFilenameArg");

How to load resources in Tomcat via Java File api

I have to use a library that accepts path to a directory and loads it via File(path);. What should be the path in tomcat webapp context ?
I always work with classpaths, but this API is really not thought through...
I don't have experience with java execution context in tomcat, but it seems to me a bad idea doing something like this
System.getProperty("catalina.base") + "webapps/app/WEB-INF/classes/profiles"
Does tomcat provides java execution (AKA $PWD) path for resources in application context ?
Use ServletContext#getRealPath() to convert a webapp-relative path to an absolute disk file system path. Given your example, the following should do:
String path = getServletContext().getRealPath("/WEB-INF/classes/profiles");
File file = new File(path);
// ...
Note that this requires that the WAR is expanded, otherwise it will return null. Tomcat by default expands the WAR, but some other servletcontainers/configs don't. Keep this in mind with regard to portability.
Application resources do NOT necessary exist on file system, therefore the search from context path approach is simply wrong.
You should provide an external configuration to your application (a property file or DB), where you should define the path depending on environment.
Suggested order of searching for configuration:
System.properties: -Dmy.configuration=/home/tomcat/my.properties
System environment: MY_CONFIGURATION=/home/tomcat/my.properties
Default value: ./my.properties (for Tomcat it is usually bin folder, but not guaranteed)
Fail and complain.

What is the proper way to use ResourceBundle.getBundle() in an Android application?

I want to know how I can use ResourceBundle.getBundle() in an Android application given that I use it in my Java applications. For example:
I have a properties file, "MyOrg.properties", which I've included in a JAR file named "MyOrg.jar". The path information in the JAR file associated with "MyOrg.jar" is "myorg\" (this is on a Windows system). I build my Java application using "myorg.jar" and note that the following line of code works as expected, which is that it loads the file "MyOrg.properties" from "MyOrg.jar" as a java.util.ResourceBundle:
ResourceBundle resources = ResourceBundle.getBundle( "myorg.MyOrg" );
Next, I place a modified copy of the file "MyOrg.properties" on the file system in the directory "c:\myorg", which is on my Java application's class path. I now rerun my Java application and note that the Resource.getBundle() returns, as expected, a bundle for the modified copy of "MyOrg.properties" (the one that is on the file system).
The behavior I've just described is what I would like to be able to accomplish with an Android application. However, ResourceBundle.getBundle() throws a MissingResourceException; it fails to find "MyOrg.properties" in either the JAR file or as a stand-alone file.
So I have two questions:
1) - Is it possible for an Android application to retrieve a resource from a JAR file using ResourceBundle.getBundle() (or any other way for that matter)?
2) - Is it possible for an Android application to retrieve a resource from a file using ResourceBundle.getBundle()?
Regarding 2): I'm using the nbandroid plugin with NetBeans 6.7.1 and I've placed copies of "MyOrg.properties" on the file system as follows prior to building my Android application:
MyProject
-- build
-- classes
myorg (directory contains "MyOrg.properties")
...
src
myorg (directory contains "MyOrg.properties")
you need to make sure the properties file makes it to the .apk file. your best bet is probably in res/raw/ or assets/. see also PropertyResourceBundle's constructors (since it's easy to get hold of an InputStream).

Categories