Solved

Help with java.net.SocketPermission Error

Posted on 2002-03-21
10
372 Views
Last Modified: 2007-12-19

I have an applet that's been working for a long time in Netscape 4.7x and IE, but it will not work in Netscape 6.2.1. This applet reads in an XML data file as a URL from the same machine that the applet is on. The line that is giving me the problem is:

    BufferedReader br = new BufferedReader(new InputStreamReader(tocUrl.openStream()));

Where tocUrl is a valid URL to my XML document, underneath document root.

The error message I get with Netscape 6.2.1 is:

    IO Error in Toc.readData: java.security.AccessControlException: access denied (java.net.SocketPermission cypci654 resolve)
    java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException: java.lang.NullPointerException
     at Toc.getPramVal(Toc.java:190)
     at TocData.getData(Toc.java:529)
     at Toc.loadData(Toc.java:143)
     at java.lang.reflect.Method.invoke(Native Method)
     at sun.plugin.liveconnect.PrivilegedCallMethodAction.run(Unknown Source)
     at java.security.AccessController.doPrivileged(Native Method)
     at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)

I understand that an applet can not read an external file unless that file is on the same server as the applet, which is the case here. Can someone tell me how to make this work!!

Thanks!

dv
0
Comment
Question by:vanfleet
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:m_onkey_boy
Comment Utility
You might check and make sure that when you open the URL back to the server, you use getCodeBase() instead of passing the url any other way.  Different security managers can be fickle about this.
0
 

Author Comment

by:vanfleet
Comment Utility
Thanks for your input. Here is what I've learned so far about this problem.

I setup a separate test area to look at this, and I was able to read in the data from the XML file without any problems using NS 6. So, I started looking at the difference between my test area  (where I got this to work), and my original applet (where I can't get it to work).

I discovered that the only difference between the two, is that in my original applet, the Java method that is reading the XML file is called from the browser with a line of JavaScript. When I tried this same thing in my test area I was able to recreate the original error message.

Here is all the code from my test area so you can try this yourself:

This is the test.html file that loads the applet and called the loadData method
in the applet.

<HTML>
<HEAD>
  <SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
    function callApplet() {
      // Send a text file name to the loadData method
      document.Toc.loadData('filename.txt');

    }

  </SCRIPT>

</HEAD>
<BODY onload="callApplet()" MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0">
<APPLET
  CODE = "Test.class"
  WIDTH = "300"
  HEIGHT = "300"
  NAME = "Toc"
  ALIGN = "left"
  MAYSCRIPT = true>
</APPLET>
</BODY></HTML>

This is the Test.java file for the applet:

import java.awt.*;
import java.net.*;
import java.io.*;
import java.applet.Applet;

/////////////////////////////////////////////////////////

public class Test extends Applet {
  static URL codeBase;

  public void init() {
    System.out.println("Code Base: " + getCodeBase());
    codeBase=getCodeBase();
 
    // If readData is called from here with a valid file name it reads that file
    // with no problems
    //readData("file.txt");
  }

  //////////////////////////////////////////////

  public static void readData(String dataFile) {

    URL tocUrl=null;
    String str;


    try {
      tocUrl = new URL(codeBase + dataFile);

    } catch (MalformedURLException e) {
      System.out.println("URL Exception :" + e);

    }

    System.out.println("tocUrl: " + tocUrl);

    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(tocUrl.openStream()));
      while((str=br.readLine())!=null) {
        System.out.println("str: " + str);
      }

      br.close();

    } catch (Exception e) {
      System.out.println("Exception: " + e);

    }

  }

  // When loadData is called from JavaScript access
  // a denied error is displayed.
 
  public void loadData(String datFile) {
    System.out.println("loadData method called: " + datFile);
    readData(datFile);

  }

}

Any Help that anyone can give me on this would be greatly appreciated.

Thanks;

dv
 
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Just a stab in the dark, but try in the JavaScript function calling not document.Toc.loadData('filename.txt'); but document.Toc.readData('filename.txt');

I assume the path to the file is not showing as mangled in any of your debugging println() calls?
0
 

Author Comment

by:vanfleet
Comment Utility
CEHU;

If I try calling the readData method rather than loadData I have a problem because readData is a static method and can't be called from JavaScript. However, when I changed readData from a static method to a regular method, then called it from the JavaScript, I got the same error message in NS 6.2.1:

Exception: java.security.AccessControlException: access denied (java.net.SocketPermission cypci654 resolve)

Any other ideas?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Not really I'm afraid, other than looking into possible NS bugs in newsgroups etc.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Accepted Solution

by:
absong earned 200 total points
Comment Utility
i know a way to solve this problem, but it involves callback. according to http://www.kfki.hu/~cspeter/jvm-bugs/ns6jsace.html :

Netscape does not let applets open http and file connection in methods called from JavaScript. The I/O connections should be opened in a separate thread that is created before the JavaScript calls, in applet init().

the site also provides sample code to avoid this problem. hope this solves your problem. i have a similar problem, but callback is not an option.
0
 

Author Comment

by:vanfleet
Comment Utility
Absong, this is a good workaround, and I think it will work for me. I didn't quite understand what you meant when you said that it involves "callback". Could you explain this to me, I'm not that experienced with using threads!

Thanks,

dv  
0
 
LVL 3

Expert Comment

by:absong
Comment Utility
vanfleet,

first of all, i reread your problem again today, and i am not so sure if the solution i posted may help you. your problem sounds very familiar, but i don't really know if it is the same as mine. having said that ...

when I mentioned callback I was actually referring to a similar problem I am having. I can explain briefly:

like you, I am trying to call a Java function from within a JavaScript function, but I need to wait for the Java function to return to process the result. Like the code below:

// jsfun() is a javascript function
function jsfun() {
   // javaapplet is a Java applet
   var str = document.javaapplet.callURL();
   // upon return, str needs to contain the requested data
   // for me to process
   process(str);
}

the above code would fail, because callURL() will not return with requested data until jsfun() has completed execution, but jsfun() will not complete execution until callURL() returns data.
the way around this problem is, don't process the requested data within jsfun(), instead, make callURL() call another javascript jsfun2() function to process the data after the data is retrieved:

function jsfun() {
   document.javaapplet.callURL();
   // callURL() immediately returns without waiting for
   // the request to complete. this allows jsfun() to
   // immediately return too.
}

// after callURL() obtains the data from server, it calls
// another javascript function jsfun2(), that processes
// this data
function jsfun2(p_str) {
    process(p_str);
}
0
 

Author Comment

by:vanfleet
Comment Utility
You are doing someing similar to me, I think, except you're having the Java method call back to a JavaScript function when the processing is done. I'm haveing the Java thread call back to the applet when it's done.

I think the solution you posted will work well for me. Thanks for your help,

dv
0
 
LVL 3

Expert Comment

by:absong
Comment Utility
yeah, sticking to applet is definitely the best idea. if nothing else, it's much faster than javascript in terms of processing.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now