Loading textures problem in a web server context

Started by Tornado7, September 08, 2004, 12:35:27 PM

Previous topic - Next topic

Tornado7

Hi Egon,

I've to move my jPCT applet in a web server (EasyPHP) context; in my current version I load my textures giving to it a path in my local drive:


File dir_texture=new File("C:/Programmi/Java_SDK/bin/textures");        String[] files_texture=dir_texture.list();
      for (int i=0; i<files_texture.length; i++) {
         
           String name_texture=files_texture[i];          
           if (name_texture.endsWith(".jpg")) {          
               texMan.addTexture(name_texture, new Texture(getDocumentBase(),"textures/"+name_texture));             }
       
      }


Running the applet with the web server the Java Console obviously says:


java.security.AccessControlException: access denied (java.io.FilePermission textures read)

at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)

at java.security.AccessController.checkPermission(AccessController.java:401)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

at java.lang.SecurityManager.checkRead(SecurityManager.java:887)

at java.io.File.list(File.java:912)

at ThreeDSimApplet.init(ThreeDSimApplet.java:336)

at sun.applet.AppletPanel.run(AppletPanel.java:348)

at java.lang.Thread.run(Thread.java:536)



exist a way to load the textures in this new context considering that I've to load them automatically, I mean, loading them using a loop as in the current version?

Bye and thanks

EgonOlsen

I assume that you want to load the textures from the webserver too!? If so, you somehow have to get the list of (texture-)files on your webserver. One idea that comes into my mind is to write a php-script that collects the file names in a particular directory on the server and let the applet call this script so that it gets the list as response. Now parse this list and load the appropriate files.

Tornado7

Let's try to consider the problem from another point of view... Basically my problem is that now I express an absolute path that points to the textures directory:



File dir_texture=new File("c:/programs/java_sdk/bin/textures");



so, if I've to move my applet (and his related directory) to another working directory or I copy the applet to a pc that hasn't this path I've to change every the path in the code.... I'd solve my problem if I could express a relative path (i.e. something like /textures) instead of expressing an absolute one that I've to change every time I move the applet in another place.

Bye and thanks

EgonOlsen

I still don't fully understand your problem. The applet is stored on the webserver, right? And accessed via a browser!? And the textures? Are they local on the users machine or on the webserver too?

Tornado7

Do not consider for the moment the web server context, and let's think locally... in the current implementation, having an absolute path:



File dir_texture=new File("c:/programs/java_sdk/bin/textures");



my applet works only if I place my applet and the texture dir in c:/programs/java_sdk/bin/ .

If I'd move my applet and his dirs to another path (i.e. c:/myapplet/), I've to change my applet code in this way:



File dir_texture=new File("c:/myapplet/textures");



and so on, whenever I decide to move it in another place or another pc. If I could express in my code a relative path (i.e. /textures) I could move my applet and his dirs everywhere without changing my code everytime.

Hope I've been clear di time  :)

Bye and thanks again

EgonOlsen

If your applet is in "c:\plopp\applet\" for example and your textures are in "c:\plopp\applet\textures\" you should be able to access them via

File dir_texture=new File("textures");

You maybe have to set the codebase in the applet-tag, but i'm not sure.

Tornado7

Using simply:

File dir_texture=new File("textures");

it doesn't work  :( . What do you exacty mean with: "You maybe have to set the codebase in the applet-tag"?

EgonOlsen

You can set the codebase by using the codebase-attribute of the applet-tag. However, it's quite difficult to help you because i still don't fully understand the way in which you are accessing the applet. Maybe posting the corresponding html-page and how you call it would help? What exception do you get? Still a security exception or a simple file not found? Is the applet signed?

Tornado7

Here my html code where load the applet:


<table width="101%" height="100%">
 <tr>
   <td width="48%"> <br><br>
     <div align="center"><font size="5" face="arial">Area Incidente</font></div><br>
     <applet name="ThreeDSim"  code="ThreeDSimApplet" width="98%" height="72%"></applet>
.
.
.


and here the exception that I've got:


java.security.AccessControlException: access denied (java.io.FilePermission textures read)

at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)

at java.security.AccessController.checkPermission(AccessController.java:401)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

at java.lang.SecurityManager.checkRead(SecurityManager.java:887)

at java.io.File.list(File.java:912)

at ThreeDSimApplet.init(ThreeDSimApplet.java:336)

at sun.applet.AppletPanel.run(AppletPanel.java:348)

at java.lang.Thread.run(Thread.java:536)



that is still a security one.... what is pretty odd to me is why, when I give to it an absolute path to the dir, does it work fine, instead, when I give to it a relative path, does it produce an security exception?

EgonOlsen

An (unsigned) applet can only load resources from its origin, i.e. the server it came from. You usually can't access files on your local harddisc. That given, the behaviour you are getting is a bit strange...i would expect it to work with "textures" and to fail with "c:\....\textures" and not the other way round. It should work if you load the applet from the webserver and the textures too, but then you are having the problem to get the list of files from the server. That's what i tried to solve with my first reply.
Maybe signing the applet is a solution as it gives the applet full access to local (and remote) resources. The JDK contains everything you need to sign an applet. Your certifcate won't be a trusted one this way, but that's not a problem as long as your users are accepting it anyway.

Anonymous

No, it doesn't work.... I have decided to abandon the solution to try to access to the dir in local environment; so I access the dir using an url:


try {
           urlTexture = new URL(getDocumentBase(), "/www/textures");        }
      catch (MalformedURLException  e ) {

             stop();
      }


now, I've no more access problems. The problem now is to get the images in this directory. I've though the following. Looking at the html code of the page http://localhost/textures/:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <TITLE>Index of /textures</TITLE>
</HEAD>
<BODY>
<H1>Index of /textures</H1>
<PRE><IMG SRC="/icons/blank.gif" ALT="     "> <A HREF="?N=D">Name</A>                    <A HREF="?M=A">Last modified</A>       <A HREF="?S=A">Size</A>  <A HREF="?D=A">Description</A>
<HR>
<IMG SRC="/icons/folder.gif" ALT="[DIR]"> <A HREF="/">Parent Directory</A>        08-Sep-2004 10:11      -  
<IMG SRC="/icons/unknown.gif" ALT="[   ]"> <A HREF="Thumbs.db">Thumbs.db</A>               13-Jul-2004 17:12   124k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="barella.jpg">barella.jpg</A>             28-Jun-2004 14:24    12k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="car1.jpg">car1.jpg</A>                19-Apr-2004 10:48    45k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="car2.jpg">car2.jpg</A>                19-Apr-2004 11:27    45k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="casa.jpg">casa.jpg</A>                08-Apr-2004 12:43    21k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="cielo.jpg">cielo.jpg</A>               07-Apr-2004 10:43    13k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="ferito1.jpg">ferito1.jpg</A>             25-Jun-2004 16:43    37k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="ferito2.jpg">ferito2.jpg</A>             25-Jun-2004 16:45    37k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="ferito3.jpg">ferito3.jpg</A>             23-Apr-2004 10:26    36k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="giardino.jpg">giardino.jpg</A>            08-Apr-2004 12:30    10k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="palazzo1.jpg">palazzo1.jpg</A>            19-Apr-2004 09:36    29k  
<IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="palazzo2.jpg">palazzo2.jpg</A>            19-Apr-2004 09:37    27k  
</PRE><HR>
<ADDRESS>Apache/1.3.24 Server at <A HREF="mailto:admin@localhost">localhost</A> Port 80</ADDRESS>
</BODY></HTML>


if I could place this html rappresentation in to a file, I could parse it as I currently parse my configuration file and catching all .jpg images..... Exist a way in Java to do that???

Bye

EgonOlsen

To rely on this apache-behaviour is a bad idea because it's a matter of its configuration if it shows you the files in a specific directory if no index is present. It's better to let the applet call a php-script on your server that gets you the list of files in simple ASCII-format and parse this one. You can access it via the Loaders getTextFile()-method. The script should look somehow like this little one:

<?
header("Content-Type: text/plain");
$d = dir(".");
while($entry = $d->read()){
echo($entry."\n");
}
$d->close();
?>

Tornado7

Hi Egon,

firstable thanks for your code :) . I'm not an expert of php so I need some explanation... The script stands in a stand-alone file, sucha as script.php? The statement dir("."); opens the current dir (if it is so I've to place the php script in to the textures dir). Using which class and method I can run the script from my applet? At the end, I've to pass the "running script method" as input for the getTextFile()? i.e:

BufferedReader in = getTextFile(runScript(script.php));

bye and thanks a lot for your help and patience :oops:

EgonOlsen

Yes, create a file something.php with the code-snippet as its content. You can place the script wherever you want. Just use the correct path to your textures-dir instead of the ".", i.e. if you place it in a dir called "php" on the same level as "textures", your path will be something like "../textures". You can leave it in the textures-dir anyway...just don't try to load it as a texture... :wink:

You don't have to execute it on your own. Your webserver should if its configured correctly to run php-scripts. Just call it from the applet by loading it like a normal text-file. You can use Loader.loadTextFile(this.getDocumentBase(), "/textures/script.php"); (or wherever its located) for this or write your own method.

Tornado7

Hi Egon,

finally I did it  :D I solved all my problems, and I accomplished (I hope) all my tasks.... Thanks again for all your help, without I should still be in a swamp  :D