小さなデモについて触れました。HTMLに埋め込まれたアプレットとIcedTeaJREユーザーのポリシーの設定で、デモについてコメントしました 。彼らのために失敗しました。彼らはアプレットへの許可を拒否し(それによってそれをサンドボックスに制限しました)、緑色の「このアプレットはサンドボックス化されています」ページを見ることになっていました。代わりに、アプレットは完全に失敗し、アプレットがあるべき場所に「灰色のスペース」が表示されました。
私はそれがFile
違いであるオブジェクトをインスタンス化しようとしていたことをWAGしています。IE Sun / Oracle JREは問題なくそれを許可し、アプレットがを作成しようとしたときにのみセキュリティ例外をスローしますJFileChooser
。OTOH the Iced Tea JREでは、の作成は許可されていません
File
。
したがって、このコードはその問題を修正するはずです。最初の「他のすべてが失敗しました」メッセージの作成/追加
JEditorPane
とインストールを移動し、次に緑色の「サンドボックス」ページをnew File(..)
呼び出しの前に移動します。
私の質問はです。このコードは、Iced Tea JREを使用しているユーザーに対して「宣伝どおりに機能しますか?」
それをテストするには:
- pscode.org/test/docload/applet-latest.htmlでアプレットに アクセスします
- デジタル署名されたコードを拒否します。 これは、アプレットをテストするための適切な条件を作成するために非常に重要です。
- アプレットが緑色の sandbox.htmlをロードするかどうかを観察/報告します。サンドボックス化されたドキュメントは、バグ修正の「成功」を表します。
また、興味深い(ほとんどないかもしれませんが) 信頼できるアプレットの防御的ロードのデモのホームページは、アプレットページ、アプレットに表示される各HTMLファイル、およびソースを含むZIPアーカイブにリンクしています。コードとHTML、およびAnt build.xmlを使用して、「自宅でこれを実行できるようにします」。
これが新しいコードです。
package org.pscode.eg.docload;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JFileChooser;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.File;
import java.io.IOException;
import java.security.AccessControlException;
/** An applet to display documents that are JEditorPane compatible.
This applet loads in a defensive way in terms of the security environment,
in case the user has refused to accept the digitally signed code. */
public class DocumentLoader extends JApplet {
JEditorPane document;
@Override
public void init() {
System.out.println("init()");
JPanel main = new JPanel();
main.setLayout( new BorderLayout() );
getContentPane().add(main);
document = new JEditorPane("text/html",
"<html><body><h1>Testing</h1><p>Testing security environment..");
main.add( new JScrollPane(document), BorderLayout.CENTER );
System.out.println("init(): entering 'try'");
try {
// set up the green 'sandboxed URL', as a precaution..
URL sandboxed = new URL(getDocumentBase(), "sandbox.html");
document.setPage( sandboxed );
// It might seem odd that a sandboxed applet can /instantiate/
// a File object, but until it goes to do anything with it, the
// JVM considers it 'OK'. Until we go to do anything with a
// 'File' object, it is really just a filename.
System.out.println("init(): instantiate file");
File f = new File(".");
System.out.println("init(): file instantiated, create file chooser");
// Everything above here is possible for a sandboxed applet
// *test* if this applet is sandboxed
final JFileChooser jfc =
new JFileChooser(f); // invokes security check
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setMultiSelectionEnabled(false);
System.out.println(
"init(): file chooser created, " +
"create/add 'Load Document' button");
JButton button = new JButton("Load Document");
button.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int result = jfc.showOpenDialog(
DocumentLoader.this);
if ( result==JFileChooser.APPROVE_OPTION ) {
File temp = jfc.getSelectedFile();
try {
URL page = temp.toURI().toURL();
document.setPage( page );
} catch(Exception e) {
e.printStackTrace();
}
}
}
} );
main.add( button, BorderLayout.SOUTH );
// the applet is trusted, change to the red 'welcome page'
URL trusted = new URL(getDocumentBase(), "trusted.html");
document.setPage(trusted);
} catch (MalformedURLException murle) {
murle.printStackTrace();
document.setText( murle.toString() );
} catch (IOException ioe) {
ioe.printStackTrace();
document.setText( ioe.toString() );
} catch (AccessControlException ace) {
ace.printStackTrace();
// document should already be showing sandbox.html
}
}
@Override
public void start() {
System.out.println("start()");
}
@Override
public void stop() {
System.out.println("stop()");
}
@Override
public void destroy() {
System.out.println("destroy()");
}
}