Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java applet java.security.AccessControlException

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?

like image 529
V1tOr Avatar asked Dec 19 '25 14:12

V1tOr


1 Answers

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;
    }
}
like image 53
V1tOr Avatar answered Dec 21 '25 03:12

V1tOr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!