I'm trying to create an Applet that saves a file in a determined folder. Because I want a specific folder according to the OS, I use the System.getenv and System.getProperty lines.
My applet works perfectly in debug and test modes; however, I need to send the Applet certain values, which I'm trying to send via JavaScript, and this is when it fails, giving me the following error: java.security.AccessControlException: access denied ("java.io.FilePermission")
So far, my Applet is self signed and verified, and I'm using AccessController.doPrivileged in order to get the System properties.
Here's my code:
import java.io.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JApplet;
public class SaveSymmetricKey extends JApplet
{
@SuppressWarnings("unchecked")
public String returnOSUniversalPath()
{
String osName = System.getProperty("os.name").toLowerCase();
String universalOSPath = null;
String osPath = null;
if (osName.indexOf("windows") > -1) {
{
osPath = (String)AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
try
{
return System.getenv("APPDATA");
}
catch(Exception e)
{
return "No se pudo llevar a cabo el acceso al SO.";
}
}
});
universalOSPath = osPath;
}
}
else if (osName.indexOf("mac") > -1 ) {
osPath = (String)AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
try
{
return System.getenv("?");
}
catch(Exception e)
{
return "No se pudo llevar a cabo el acceso al SO.";
}
}
});
universalOSPath = osPath;
}
else {
osPath = (String)AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
try
{
return System.getProperty("user.home");
}
catch(Exception e)
{
return "No se pudo llevar a cabo el acceso al SO.";
}
}
});
}
return universalOSPath;
}
//This is the method I call via JavaScript.
public String getKeyFromDotNET(String keyText, String userName) {
FileOutputStream fos;
BufferedWriter out;
String keyPath = returnOSUniversalPath();
String successReturn = null;
try
{
File keyFile = new File(keyPath + "\\" + userName + ".pem");
if (!keyFile.exists()) {
FileWriter fstream = new FileWriter(keyFile);
out = new BufferedWriter(fstream);
out.write(keyText);
out.close();
successReturn = "Llave guardada exitosamente.";
}
else {
successReturn = "Llave ya existe.";
}
return successReturn;
}
catch (IOException e)
{
e.printStackTrace();
successReturn = "No se pudo guardar llave.";
return successReturn;
}
}
What am I doing wrong? Thanks in advance. :)