Problem granting read FilePermission to an applet - Page 2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27

Thread: Problem granting read FilePermission to an applet

  1. #16
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299

    Solution for simple applet

    Sorry, I forgot to post my working code. I'll attach it to this message. The zip file is a jar file renamed by adding .zip to its filename.

    For the example, I have the class files in D:\Testing\ReadFile\
    and the files to be read in E:\Testing\

    The policy file was setup to allow files in D:\Testing\ and subfolders to read files from E:\Testing\ and subfolders.

    The java.policy samples are in the program's source file as comments at the end.

    I don't believe that Sun has changed the basic Permission granting logic. There is something about my program that Sun is NOT allowing in version 1.4.2 that it did allow in 1.3.1. I need to find that and fix it.
    There is nothing wrong with my design. Your solution requires too many separate environments/programs as a solution. If you are using a browser to read HTML pages on your disk (for example the Java Tutorial) and you want to search the tutorial files for something, why exit the browser, find another tool, start the tool, find the folders for the tutorial and THEN be able to issue the search command. I prefer to issue the search command immediately from the browser while I'm looking at the files.

    The AppletContext is a way for Java programs to communicate with a browser. I don't think it has to be only on the internet. Local use is fine. I think most computer users are used to using a browser, so putting an application in a browser environment seems to me an easy way to present an interface to a lot of none programming users.

    Norm
    Attached Files Attached Files

  2. #17
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Ok, but you don't need a browser to look at local (or remote) html files, you
    can use a an application that contains a JEditorPane and implement a
    HyperlinkListener for it.

    As for struggling with the security management, I just don't have the time
    for it.

    Why not trying to contact tech. support at SUN ?
    Last edited by sjalle; 08-04-2005 at 11:13 AM.
    eschew obfuscation

  3. #18
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Sounds like the design should include writing a new browser in Java. Too much work. The ones I have are fine.

    This thread is getting too long. I'm leaving it until I find the answer.

    Norm

  4. #19
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I don't mean to bug you....

    But making a basic browser for displaying local (or remote) html files in java,
    including images is no big deal.
    This one is not perfect, it is not very good with fancy styling and it should
    have a proper back button and perhaps some other nice stuff too.
    A textfield for URL entry,a getURL-button and code for url-create and load
    into the pane is all it needs to go on the web.



    Btw, it reads/displays txt and rtf files also.

    I agree this thread is long. Bye for now.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.*;
    import java.net.*;
    import javax.swing.event.*;
    /**
     * Basic code for java browswr
     */
    public class Browser extends JFrame implements ActionListener, HyperlinkListener {
      BorderLayout borderLayout1 = new BorderLayout();
      JPanel jPanel1 = new JPanel();
      JButton getFileBtn = new JButton();
      JButton closeBtn = new JButton();
      JScrollPane jScrollPane1 = new JScrollPane();
      JEditorPane edHTML = new JEditorPane();
    
      public Browser() {
        try {
          jbInit();
          getFileBtn.addActionListener(this);
          closeBtn.addActionListener(this);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public void loadFile(String fp) throws Exception {
        if (fp==null) return;
        String filePath=fp;
        File f=new File(fp);
        long av=f.length();
        if (av==0) {
          System.out.println("no data");
          return;
        }
        URL url=new URL("file:"+fp);
        edHTML.setPage(url);
        this.validate();
      }
      public static void main(String[] args) {
        Browser ed = new Browser();
        ed.setBounds(20,20,800,600);
        ed.setVisible(true);
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        getFileBtn.setText("Get FIle");
        closeBtn.setText("Close");
        edHTML.setEditable(false);
        edHTML.addHyperlinkListener(this);
        this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
        jPanel1.add(getFileBtn, null);
        this.getContentPane().add(jScrollPane1,  BorderLayout.CENTER);
        jScrollPane1.getViewport().add(edHTML, null);
        jPanel1.add(closeBtn, null);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.closeBtn)
          System.exit(0);
        if (e.getSource() == this.getFileBtn) {
          FileDialog fd=new FileDialog(this,"Select File",FileDialog.LOAD);
          fd.setVisible(true);
          String fName=fd.getFile();
          if (fName==null) return;
          String fPath=fd.getDirectory()+fName;
          try {
            loadFile(fPath);
          }
          catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
          }
        }
      }
      public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          URL url = e.getURL();
          try {
            edHTML.setPage(url);
          }
          catch (IOException ex) {
            ex.printStackTrace();
            return;
          }
        }
      }
    }
    Last edited by sjalle; 08-04-2005 at 07:14 PM.
    eschew obfuscation

  5. #20
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299

    Progress on Security Policy

    Thanks for the program. It works for simple HTML but not with Javascript.

    I narrowed the Security problem to being when I called the Applet from Javascript within an HTML page in IE running jre 1.4.2
    For example: document.myApplet.fromJS(str);

    The applet in IE with 1.4.2 worked OK when called directly.

    I tried several codeBase= entries in the .java.policy file and could not find any that would work. So I gave up and added the FilePermissions to the global entry at the beginning of the file.
    For example: The bottom 4 entries allowed IE to work.

    grant {
    permission java.lang.RuntimePermission "modifyThread";
    permission java.lang.RuntimePermission "loadLibrary.HelloNative";
    permission java.util.PropertyPermission "*", "read";
    permission java.lang.RuntimePermission "modifyThreadGroup";
    permission java.io.FilePermission "E:/Testing/", "read";
    permission java.io.FilePermission "E:\\Testing\\-", "read";
    permission java.io.FilePermission "E:/Miscellaneous/SailJazz/", "read";
    permission java.io.FilePermission "E:\\Miscellaneous\\SailJazz\\-", "read";
    };

    The following entries allowed NS with jre 1.3.1 to work:

    grant codeBase "file:/E:/Java/Tutorial/-" {
    permission java.io.FilePermission "E:/", "read";
    permission java.io.FilePermission "E:/Java/Tutorial/", "read";
    permission java.io.FilePermission "E:\\Java\\Tutorial\\-", "read";
    };

    grant codeBase "file:/D:/Testing/-" {
    permission java.io.FilePermission "E:\\Testing\\-", "read";
    permission java.io.FilePermission "E:/Testing/", "read";
    };

    Someday when I get inspired again, I'll try other codeBases to see if I can narrow it down.

    Norm

  6. #21
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    What do you need javascript for, when you already have the urls ?
    Isn't it your java program that does the search ?
    eschew obfuscation

  7. #22
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    I'm not sure what javascript your talking about. Is it to do with the simple browser that you gave me? The javascript exists in the HTML pages that I'm searching thru.

    What are the urls you mention? All the files are on my harddrive.

    The javascript in my application calls the applet which has a MAYSCRIPT tag.
    document.MyApplet.startSearch(str); // where str has the search parms
    How else would you pass parameters from an HTML form to an applet? Then applet then uses a JSObject to write to a new Browser window with the results of the search.

  8. #23
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Cool Nothing complicated works...

    If you load a local html file, like the javadoc index.html, then the urls returned in the hyperlink event are the local files on your harddrive.

    If you have an html page w javascript that delivers a search parameter string to the
    applet, then you should be able to create a near identically looking java
    "clone" of that html page and its javascript logic and implement it as a JPanel
    extension.

    You then have a java application with three panels:

    Browser: displays html content with working hyperlinks, or just plaint text/rtf
    depending on what the url points to.

    Search panel:handles serach parameter entry.

    Search results panel:Generated during the search process by appending to
    a StriingBuffer, not by using document.write() in javascript. This buffer is loaded into a new JEditorPane (html style), not by using
    and clicking the links is this page opens the link in the browser panel.
    The StingBuffers generated can be saved in a combobox as previous search
    results.

    With this setup you can search the contents two ways:

    1; Search of all the hyperlinks on a displayed htmlPage , and the contents of all hyperlinks under each of those hyperlinks ... etc. to the level you set.
    For your use you could restrict this follow-hyperlinks search to only
    local files (but it would work for the web also).
    Note: you dont have to code any logic for getting all the urls (links)
    contained in an HTML fle (or any other type of content you want). If you use the HTMLEditorKit.ParserCallback technique you get all the hyperlink urls
    for free.


    2: Use a user directory selection and get the files using standard File
    methods with optional subdirectory search. If this method also uses the
    "follow-hyperlinks" strategy it would be a broader search

    The way I see it this is a far better solution. It has browser behaviour and
    look and is coded in java only, not java + javascript + html. Maintenance
    and enhancement is much easier
    .

    If the javascript code && html are too complicated to stranslate to java, then
    it would be very interesting to have a look
    Last edited by sjalle; 08-06-2005 at 04:13 PM.
    eschew obfuscation

  9. #24
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Thanks again for the ideas. I'll study them and get back with some questions later.

    The basic question is: Can the process of converting an HTML page with Javascript and styles, forms etc be automated?

    Norm
    Last edited by Norm; 08-06-2005 at 10:01 PM.

  10. #25
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    That depends... what do you want the javascript for ? Are you not searching for
    content ?
    Is is not enough to be able to extract all the urls and all the content ?
    eschew obfuscation

  11. #26
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    It's not what "I" want the javascript for. Some of the html pages I've downloaded contain javascript that build/control the gui for the page. The pages also have <STYLE tags. The simple browser example you posted doesn't seem to handle styles.
    Sorry, I don't know how to extract the "content" from an html page. I haven't been keeping up with the latest releases of Java. I'm a couple of years behind. Could you give me the class(es) and method(s) that would do that?
    Thanks,
    Norm

  12. #27
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You can parse for the text that falls between <body> and </body>, then strip out any text between script tags, then delete your other tags (anything between < and >) ... this will leave the "text" of the page? Take a look at the Scanner class and the ability to change your delimiter or to use the "findInLine" or "findWithinHorizon" methods to scan for the regex pattern ...

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. Replies: 3
    Last Post: 03-21-2007, 03:28 PM
  3. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  4. Replies: 0
    Last Post: 11-14-2001, 07:02 AM
  5. X & Y Coordinates of Mouse over an Applet
    By Alan Amos in forum Architecture and Design
    Replies: 0
    Last Post: 02-21-2001, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center