OriginNotAllowedException when calling Applet from JavaScript using AppletLoader

Started by blainebell1, April 18, 2011, 21:36:31

Previous topic - Next topic

blainebell1

Hello,

I am getting this error when trying to call a function from JavaScript to my Applet on a Mac using org.lwjgl.util.applet.AppletLoader:

Ignored exception: sun.plugin.liveconnect.OriginNotAllowedException: JavaScript is not from the same origin as the Java code, caller=https://www.schrodinger.com/internal/web/users/nyc/bell/jymol/lwjgl/demonew.html, callee=file:/Users/bell/Library/Caches/Java/cache/lwjglcache/www.schrodinger.com/JyMOL LWJGL Test/jymol-1.1.jar

But the javascript/html is in the same directory on my server.  It looks like the AppletLoader caches the jar locally (I have tried this without caching, but it doesn't work).  Does anyone know how I can fix this?  Is there some sort of security setting that I need to set?

Thanks in advance,

Blaine

jediTofu

cool story, bro

Matzon

that looks a bit  silly...

kappa do we have a general purpose liveconnect test somewhere? - should work on all platforms

kappa

Quote from: Matzon on April 19, 2011, 06:08:56
that looks a bit  silly...

kappa do we have a general purpose liveconnect test somewhere? - should work on all platforms

Don't think I've seen a general purpose liveconnect test but essentially the steps for javascript to java communication when using the appletloader are:

1) Give your applet tag an id (in html).

<applet code="org.lwjgl.util.applet.AppletLoader"
       id = "myApplet"
       archive="lwjgl_util_applet.jar"
       codebase="."
       width="640" height="480">

2) The main applet in this case is the appletloader and you'll need to get the LWJGL Applet loaded by it, this is done as follows (in javascript).

var appletloader = document.getElementById('myApplet');
var applet = appletloader.getApplet();


3) That is it, once you have the main applet object (lwjgl applet instance) you can call any public method therein using applet.x()

Java to Javascript communication is the same, there is no difference to the normal way to do it.

@blainebell1 do you still get the same error when you run without using separate_vm parameter (or without out of process applets)? think i read somewhere that there was a liveconnect bug with the apple plugin which happens when running applets in a separate process.

pjohnsen

I had this problem a while back, but only on Mac with Safari, Firefox worked fine .... hmmmm or was it the other way around  ???

I found that I only got the security error when calling JS > Java, not the other way around, so I ended up implementing a simple JS command queue, which the applet would then poll.

Unfortunately my Mac Book Pro is now R.I.P, so I can't really check anymore :(

eczkchtl

I'd like to note that I've got the same issue on Mac OS X 10.4.11 with Safari 3. It would be nice to create a list of quirks for different OS/Browser combinations. Have anyone tried with Safari 4 and 5? And is this an issue on any other browser? Maybe it's about time to drop support for Safari 3 anyway :)

Quote from: pjohnsen on May 03, 2011, 15:27:25
I found that I only got the security error when calling JS > Java, not the other way around, so I ended up implementing a simple JS command queue, which the applet would then poll.

Creating a command queue would seem like the easiest way to fix this. Have anyone come up with some other solution?

eczkchtl

I had another go at this recently, and it's actually simple to circumvent the OriginNotAllowedException by adding a method like this to the AppletLoader:

public String pushCommand(final String cmd) {
		try {
			return AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
				public String run() throws Exception {
					Class<?> c = lwjglApplet.getClass();
					Method m = c.getDeclaredMethod("pushCommand", String.class);
					return (String) m.invoke(lwjglApplet, cmd);
				}
			});
		} catch (PrivilegedActionException e) {
			e.printStackTrace();
		}
		return null;
	}


Your applet will need to implement a pushCommand(String) method and you'll call the applet from javascript with:

var cmd = "Hello World";
var appletloader = document.getElementById('myApplet');
var result = appletloader.pushCommand(cmd);


We convert all our command objects to json strings which we then feed the applet.

The problem with polling was that it would quite often freeze the entire browser. I'm not sure why that happened for us.

kappa

does the above technique also work if that method is in your main class (as opposed to the appletloader) ?

jediTofu

It looks like the domain of the java code is not the same as the domain as the javascript code for whatever reason.  And the server and browser think that there's a cross-site scripting thing going on.  It's be best not to use a "hack" and figure out how to make your domain be the same on both java and javascript, but not sure what would be causing this.
cool story, bro