This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Applets and the fly likes Access denied error for midi file in same directory as applet! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Access denied error for midi file in same directory as applet!" Watch "Access denied error for midi file in same directory as applet!" New topic
Author

Access denied error for midi file in same directory as applet!

Sam Foletta
Greenhorn

Joined: Apr 28, 2009
Posts: 4
Hi! First time caller, long time listener.

I'm getting an access denied error trying to load a midi file on the server, in a subdirectory of the applet's folder. As in, the applet root/ffp.class, on webpage root/ffp.htm, is trying to access root/mid/wm0.mid, and is denied access.

It works fine when I run the applet on my hard disk, but run from the server it throws an AccessControlException (access denied). This is odd, because I can put the same midi file into an AudioClip and run it with no problem. However, I don't have much control over playback with AudioClip and prefer to use a midi sequencer. I'm trying to load the midi file as a FileInputStream which, again, works fine when I run it on my hard disk, but doesn't work once I upload it onto the server.

Shouldn't the applet already have access to files in subdirectories without modifying permissions? Especially if the applet can already access the same files as a different data type? I feel like I'm missing something here.

The code is basically the following:





The line that calls MidiSystem.getSequence throws the following error:





Would very much appreciate any help.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
You can't use file I/O in this situation. Those classes will try to access the client file system, and that's out of bounds for applets.

You'll need to use the MidiSystem.getSequence(InputStream) method like this:

URL fileURL = new URL( getCodeBase() , "mid/wm0.mid" );
InputStream is = fileURL.openStream();


Ping & DNS - my free Android networking tools app
Sam Foletta
Greenhorn

Joined: Apr 28, 2009
Posts: 4
So, I tried that, getCodeBase() threw a NullPointerException so I moved that line from the constructor to init(), but now I get this:



I tried signing the applet, no luck there. Do I need to use privileges? This seems so excessively problematic for loading a midi.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
It should not be necessary to sign the applet, nor to use privileges. "www.cwahi.net" is the server where the applet originates from?

By the way, you shouldn't use constructors in applets at all - that's what the init() method is for.
Sam Foletta
Greenhorn

Joined: Apr 28, 2009
Posts: 4
Yes, cwahi.net is the same server, the midi is in a subdirectory of the applet's directory.

I tried calling getSequence with InputStream, URL, and File inputs with the same results, even tried skipping the sequence altogether and calling setSequence with an InputStream, no luck. I placed the call in a PrivilegedAction and signed the script just to try everything. Always an access denied error.



UPDATE:
Picking through the console at a higher trace level, I noticed that other resources were being redirected to "stopit.php". When I opened it up in Firefox, it brought up a page that states direct/hot linking is not allowed. It looks like the host server is blocking the applet's connection to these files thinking that they're being linked to by a foreign web site. Yet image files still get through somehow. Hmm...

Any way to get around that?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
The server is *probably* looking for a REFERER header (which a web browser would send if the file was linked from a web page). If that's the case then it gets slightly more tricky, but something like this should work:

It could be something else about the request that the server doesn't like, though - the server admin should be able to tell you what.
Sam Foletta
Greenhorn

Joined: Apr 28, 2009
Posts: 4
You're absolutely right. Just added a "toString()" and it worked like a charm.

Final code looked something like this:



and later...




I ran into the same problem with AudioClips, actually, and since they only accept URLs and not InputStreams I assume this fix won't work and I'll have to use Clips and AudioSystem.

Anyways... thanks for the help!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Access denied error for midi file in same directory as applet!