I'm working on an Java applet that prints a file.
The applet is "self-signed".
The print function is:
//argFilePath : path to file (http://localhost/Teste/pdf1.pdf)
//argPrintService : something like PrintServiceLookup.lookupDefaultPrintService()
private int print(String argFilePath, PrintService argPrintService){
try
{
DocPrintJob printJob = argPrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(argFilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
I get this exception when trying to open the file:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)
HTML/JavaScrip
<input onclick="alert(document.getElementById('xpto').print('http://localhost/Teste/pdf1.pdf'));" type="button"/>
<applet width="180" height="120" code="printers.class" id="xpto" archive="printerAPI.jar"></applet>
is correct to use:
DocFlavor.INPUT_STREAM.AUTOSENSE
The idea seems to be to print as many file type as possible - pdf, docx, jpg, etc.
How can you fix the exception?
Found the answer (on stackoverflow lol :D)!
It looks like the problem was:
"javascript does not have file access permissions"
so the applet is blocked. we have to use
AccessController.doPrivileged()
doPrivileged
Here is my implementation:
private int print(String argFilePath, PrintService argPrintService){
cPrint cP = new cPrint(argFilePath, argPrintService);
return (Integer) AccessController.doPrivileged(cP);
}
class cPrint implements PrivilegedAction<Object> {
String FilePath;
PrintService PrintService;
public cPrint(String argFilePath, PrintService argPrintService) {
this.FilePath = argFilePath;
this.PrintService = argPrintService;
};
public Object run() {
// privileged code goes here
try
{
DocPrintJob printJob = PrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(FilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
}
You probably got this:
java.security.AccessControlException: access denied (java.net.SocketPermission
127.0.0.1:80 connect,resolve)
because applets can't make connections to websites, other than the one they came from. Now, this is terribly silly because one would think localhost
is not another website, but the Java SecurityManager must only look at IP address. Therefore, if the browser is connected to 74.125.224.224
then the Java applet must connect to that address—which is different from localhost
, whose address is 127.0.0.1
.
This will just take care of the Socket Permission error. But, you'll probably run into something else if you're trying to access the user's hardware. In which case, you'll need to make a certificate and the user will choose whether or not to run your applet.
If you just want to run this on your home computer then, you need a plain-text java.policy
file in your home directory. (~/.java.policy for Unix people.) In that file you'll type:
grant{
permission java.security.AllPermission;
};
After you save this file in your home directory, all java applets will be given full permission to run—anything. It'll be like the SecurityManager doesn't exist, so try to be a bit careful. After you're done with testing, I'd recommend to delete this file.