Loading Properties files in an applet - java

New to Applets, I have never dealt with having to export the resources to the jar.
The browser is failing to load properties files:
access denied ("java.io.FilePermission"
"config\en-us.properties""read")
Properties files are imported as so:
Code to load Properties file:
prop.load(new FileInputStream("config/en-us.properties"));

Obtain an URL to the properties file in the jar using:
URL urlToProps = this.getClass().getResource("/config/en-us.properties");
Use an URLConnection to set a read timeout.
// courtesy of MyTitle 'default timeout is infinity'
URLConnection connection = urlToProps.openConnection();
connection.setConnectTimeout(5000);
Get an InputStream.
InputStream is = connection.getInputStream();
Then use Properties.load(InputStream) to load it.
prop.load(is);

Related

Transfer file from SFTP to ADLS

We are currently in the process of exploring the sshj library to download a file from SFTP path into ADLS. We are using the example as reference.
We have already configured the ADLS Gen2 storage in Databricks to be accessed as an abfss URL.
We are using scala within Databricks.
How should we pass the abfss path as FileSystemFile object in the get step ?
sftp.get("test_file", new FileSystemFile("abfss://<container_name>#a<storage_account>.dfs.core.windows.net/<path>"));
Is the destination supposed to be a file path only or file path with file name?
Use streams. First obtain InputStream of the source SFTP file:
RemoteFile f = sftp.open(sftpPath);
InputStream is = f.new RemoteFileInputStream(0);
(How to read from the remote file into a Stream?)
Then obtain OutputStream of the destination file on ADLS:
OutputStream os = adlsStoreClient.createFile(adlsPath, IfExists.OVERWRITE);
(How to upload and download a file from my locale to azure adls using java sdk?)
And copy from the first to the other:
is.transferTo(os);
(Easy way to write contents of a Java InputStream to an OutputStream)

Getting error while loading properties file within the executable jar when running on linux server

I am loading the properties file within the jar, when i run this jar in the windows machine it is working fine. But when i run the same executable jar in linux server it is failing getting below error:
"Unable to execute command due to error: null/AutoTest/auto_env.properties (No such file or directory)"
Below is the code written to load the properties file:
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("/auto_env.properties");
Properties props = new Properties();
try {
if (in != null) {
props.load(in);
}
I tried below fixes to solve the issue but it did not work:
To get the properties file path: getClass()..getProtectionDomain().getCodeSource().getLocation().toURI().getPath() and passing it in the inputstream.
Instaed of Thread.currentThread().getContextClassLoader() used ClassLoader.class that also din't worked.
Please help to resolve this

java.io.FileNotFoundException: db.properties (The system cannot find the path specified)

I have written a basic jsp code for storing and retrieving the data from db.
Before that am checking validation of user.
When i click submit button it will redirect to my jsp page.
i have written a db.properties file separately.
When i gave complete path to read properties file., program is executing fine. (Which is not best way to hard code like below).
FileInputStream in = new FileInputStream("C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ServiceDisplay\db.properties");
But when i specify only "db.properties" like
(FileInputStream in = new FileInputStream("db.properties");) program is not executing i got file not found exception.
Please not this properties file is in current working dir only. (i.e., my db.properties file and my jsp file is under ServiceDisplay
I tried to changing the file name as "//db.properties", "/db.properties", "./db.properties", "\db.properties", .\db.properties, ../db.properties", "..\db.properties" .
But still i am getting java.io.FileNotFoundException: db.properties
The file 'ServiceDisplay\db.properties' is in your resources directory, to load this file is necessary to use the getResoursceAsStream. Code example below:
ServletContext context = request.getSession().getServletContext();
InputStream is = context.getResourceAsStream("/db.properties");

getCodeBase() gives nullpointer in Java Applet

I have created an applet to read some info from a file on the server. I try to access the file using the following code:
Properties Settings = new Properties();
settings.load(new URL(getDocumentBase(), "settings.ini")).openStream());
All of a sudden, the second line is giving me the error:
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)
My applet is signed and I access it through my localhost.Why can't I use getDocumentBase anymore?
Btw, I am using Netbeans Web Start option to create the necessary files (jars, html, jnlp) and then move them to my IIS local server.
SOLUTION
I'm loading the ini file from within the jar now:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
At first glance I would expect:
new URL(getCodeBase(), "settings.ini")
as getCodeBase gives the directory URL, getDocumentBase gives the HTML URL.
That it worked previously is astonishing. Maybe the HTML URL ended with ?... and you read the HTML page?
SOLUTION
I'm loading the ini file from within the jar now:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());

ClientCustomSSL with HTTP 4.1.1

Can anybody tell me whether we have to create my.keystore file or it will be created. And when I created this file in the same directory(webapp directory) where my jsp file is and I am using this code in my jsp file.
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("my.keystore"));
try {
trustStore.load(instream, "nopassword".toCharArray());
} finally {
instream.close();
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
And I got the error
java.io.FileNotFoundException: my.keystore (The system cannot find the file spec
ified)
So where should I put this file. this is the path for both of the file, that jsp file and my.keystore
C:\workspace\search-ui\search-ui\src\main\webapp
I would recommend adding the keystore to the classpath and then load it via
InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.keystore");
Putting the keystore under webapp but not under WEB-INF is insecure since anyone could just download it. Also, loading a file via new File() looks in the working directory for the Java process when a relative file path is used (see the JavaDoc). This is most likely somewhere under the directory hierarchy of the Servlet container you are using.
You don't need a keystore at all unless your server requires client authentication. Does it?

Categories