Simple ESAPI Directory Path Validation Example Not Working - java

I have build a ESAPITestValidator class as follows :
public class ESAPITestValidator {
Validator instance = ESAPI.validator();
ValidationErrorList errors = new ValidationErrorList();
File parent = null;
String path = new String("");
boolean status = false;
public boolean testGetValidDirectoryPath(String inputPath,String context)
{
try
{
parent = new File(inputPath).getParentFile().getCanonicalFile();
//String parent = ESAPI.securityConfiguration().getResourceFile("ESAPI.properties").getParentFile().getCanonicalPath();
path = instance.getValidDirectoryPath(context, inputPath, parent, true, errors);
status = true;
return status;
}catch(Exception exception)
{
exception.printStackTrace();
status= false;
}
return status;
}
}
Now I am Trying to invoke the testGetValidDirectoryPath() from a main class as follows :
public class HelloWorld {
public static void main(String[] args) {
ESAPITestValidator obj = new ESAPITestValidator();
if ( obj.testGetValidDirectoryPath("C:\\Users\\1730176\\Downloads","C:\\Users") )
System.out.println("Success");
else
System.out.println("Failure");
}
}
I have also Uploaded the ESAPI.properties and Validation.properties in the same Source folder as the main and validator class.
But I am getting the following exception :
System property [org.owasp.esapi.opsteam] is not set
System property [org.owasp.esapi.devteam] is not set
Attempting to load ESAPI.properties via file I/O.
Attempting to load ESAPI.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable:
D:\Arindam_Workspace\ESAPI_Workspace\ESAPIProofOfConcept\ESAPI.properties
Not found in SystemResource Directory/resourceDirectory:
.esapi\ESAPI.properties
Found in 'user.home' directory: C:\Users\1730176\esapi\ESAPI.properties
Loaded 'ESAPI.properties' properties file
SecurityConfiguration for Validator.ConfigurationFile.MultiValued not found
in ESAPI.properties. Using default: false
Attempting to load validation.properties via file I/O.
Attempting to load validation.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable:
D:\Arindam_Workspace\ESAPI_Workspace\ESAPIProofOfConcept\
validation.properties
Not found in SystemResource Directory/resourceDirectory:
.esapi\validation.properties
Found in 'user.home' directory: C:\Users\1730176\esapi\validation.properties
Loaded 'validation.properties' properties file
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/servlet/http/HttpServletRequest
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getMethod0(Class.java:2670)
at java.lang.Class.getMethod(Class.java:1603)
at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:77)
at org.owasp.esapi.ESAPI.validator(ESAPI.java:191)
at main.utility.ESAPITestValidator.<init>(ESAPITestValidator.java:20)
at main.core.HelloWorld.main(HelloWorld.java:16)
Caused by: java.lang.ClassNotFoundException:
javax.servlet.http.HttpServletRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 8 more
Can anyone sujjest What I have done worng or am I missing something ?

The JVM is looking for the javax/servlet/http/HttpServletRequest class, and you haven't loaded it into the classpath. You will need to include an implementation of the java servlet API on the classpath. Keep in mind that ESAPI is a web-based library and you will probably have an easier time testing if you clone the actual esapi-java-legacy project and work with the unit tests there.

Related

can not find files in jar build b Spring Boot

these logs and the Controller Class are self-explanatory:
logs:
java.io.FileNotFoundException: class path resource [/static/index.html] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/mehrdad/ocr/main/ocr-web/target/ocr-web-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/index.html
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:180)
at com.pouya.ocr.web.web.RouteController.defaultPath(RouteController.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Class:
#Controller
public class RouteController {
#RequestMapping(value = "/app/**", method = RequestMethod.GET)
public ResponseEntity<String> defaultPath() {
System.out.println("Unmapped request handling!");
try {
File index = ResourceUtils.getFile("classpath:/static/index.html");
FileInputStream inputStream = new FileInputStream(index);
String body = StreamUtils.copyToString(inputStream, Charset.defaultCharset());
return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(body);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error in redirecting to index");
}
}
}
the second ! seems to cause the problem:
../ocr-web-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/index.html
How to solve this?
I tried to use classpath:static/index.html instead of classpath:/static/index.html, nothing changed.
I solved the problem.
do not use ResourceUtils(sometimes Spring does not make Java simpler!)use old java way for loading resource from classPath:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/static/index.html");

unable to access outside xml config file in spring boot jar

am trying to use an xml file from outside jar.now its getting null pointer exception
private static String getRequestConfigurationLocation() throws UnsupportedEncodingException
{
URL resourceURL = ConfigurationFactory.class.getResource(Configuration.XML_CONF);
//Assert.notNull(resourceURL, "Resource url is null : ");
String urlFilePath = resourceURL.getFile();
String actualFilePath = java.net.URLDecoder.decode(urlFilePath, StandardCharsets.UTF_8.name());
return actualFilePath;
}
Configuration
public class ConfigurationFactory
{
public static final String XML_CONF = "D:/DEV//X/X1/Service/target/conf/rConfiguration.xml";
}
how can i use external file in an executable jar
error log
Caused by: java.lang.NullPointerException
You should use java.util.Properties to access external file,
refer to this answer to load xml properties file
In your property file try :
external.config=D:/DEV//X/X1/Service/target/conf/rConfiguration.xml
Or
annotate your configuration file with following
#configuration
#ImportResource("D:/DEV//X/X1/Service/target/conf/rConfiguration.xml")

java.io.FileNotFoundException: resourceedge-config.xml (The system cannot find the file specified)

I was using jboss server to deploy my applications and it works fine. My jboss server is corrupted, and I'm changing to tomcat 7.0.55 and its giving me error on my console
"java.io.FileNotFoundException: resourceedge-config.xml (The system cannot find the file specified)"
The resourceedge-config.xml is readingfrom this method:
public static Properties loadSystemConfiguration() throws FileNotFoundException, IOException{
return loadSystemConfiguration(ConfigReader.getFile_Prefix() + "-config.xml");
}
And it's also calling from my application filter class too which is:
//get the application context
ServletContext context = filterConfig.getServletContext();
String configFilePrefix = context.getInitParameter(ApplicationFilter.APPLICATION_CONFIG_FILE_PREFIX);
if(configFilePrefix != null){
ConfigReader.setFile_Prefix(configFilePrefix);
}
try{
configuration = ConfigReader.loadSystemConfiguration();
}catch(Exception e){
e.printStackTrace();
configuration = null;
}
The resourceedge-config.xml is place inside C:\apache-tomcat-7.0.55\bin
Please I need help on this to enable to read the file resourceedge-config.xml.
Thanks

reading config file from different folder java

I am trying to read a config file based on the code here:
http://www.opencodez.com/java/read-config-file-in-java.htm
So I found that if the config.cfg is in same directory as of where I am running the code, then everything is fine but if the config is at different directory
example: /path/to/config.cfg
I get this error:
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at packagename.conf.Config.<init>(Config.java:14)
at packagename.conf.Config.main(Config.java:30)
My guess is it is not able to find the file.
But how do I modify the above code to read config file from a different folder?
Thanks
Edit: Code from the link:
import java.util.*;
import java.util.Properties;
public class Config
{
Properties configFile;
public Config()
{
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("myapp/config.cfg"));
}catch(Exception eta){
eta.printStackTrace();
}
}
public String getProperty(String key)
{
String value = this.configFile.getProperty(key);
return value;
}
}
The code that you posted expects the configuration-file to be on the classpath (that is, in the same sorts of places that Java looks for your .class files). So, you can either include the directory containing the configuration-file in the classpath:
java -classpath .:/path/to packagename.conf.Config
Or else you can modify the code to expect the configuration-file to be a regular filesystem file:
final InputStream cfg = new FileInputStream("/path/to/config.cfg");
try
{ configFile.load(cfg); }
finally
{ cfg.close(); }

java.util.Properties$LineReader.readLine

I need to read a config file
I get this error while running the following code:
java.util.Properties$LineReader.readLine
The file config.cfg is present and has r/w permissions.
import java.util.*;
import java.util.Properties;
public class Config
{
Properties configFile;
public Config()
{
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("config.cfg"));
}catch(Exception eta){
eta.printStackTrace();
}
}
public String getProperty(String key)
{
String value = this.configFile.getProperty(key);
return value;
}
}
EDIT - Full Error
[java] java.lang.NullPointerException
[java] at java.util.Properties$LineReader.readLine(Properties.java:418)
[java] at java.util.Properties.load0(Properties.java:337)
[java] at java.util.Properties.load(Properties.java:325)
[java] at Config.<init>(Unknown Source)
[java] at ClosureBuilder.<clinit>(Unknown Source)
EDIT - Directory Structure
src
-> config.java
-> config.cfg
You have to put your config.cfg in the same folder where your .class file lies.
The resource stream is being returned as null. The resource isn't where you think it is in the classpath.
It seems your program is not able to find the config.cfg file.
this.getClass().getClassLoader().getResourceAsStream("config.cfg")
The above call is returning null.
Try the below:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.cfg")
if(is !=null){
configFile.load(is);
}
This change will not let your program fail. But also, if the file is not located, your property object configFile will not have any property from the file.
Check the path what you are getting for your config using this.getClass().getResource("/");

Categories