In our case, people accept a digital certificate to use our applet.
Signed ehh?
<sscce>
import java.applet.Applet;
import java.awt.Button;
import java.awt.TextArea;
import java.awt.Label;
import java.awt.Checkbox;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.MalformedURLException;
import java.io.IOException;
/** This Applet queries the success of
openning a web-page using showDocument().
The 'pop-up' opens a server socket that
the original page attempts to connect to.
This code requires security access to
establish sockets, so short of altering
the policy file of the browser, it will
only run as signed code.
This version opens a dialog asking the user
if they wish to proceed with the test, even
after they have accepted the applet through
the JVM.
@author Andrew Thompson
@version 2004-08-24 */
public class ShowDocumentTestApplet
extends Applet implements ActionListener {
/** The URL of the target (pop-up) web page. */
URL target = null;
/** The 'pop-up' is a ServerSocket.. the openning page
is a client that attempts to detect the server socket,
and uses that to determine success of the pop-up window. */
boolean server = false;
/** The server socket which the pop-up creates, and the source
page attempts to detect. */
ServerSocket ss;
/** Default port number */
int port = 21219;
/** Establish port number and role, and
set-up the GUI and resources for each. */
public void init() {
String sPort = getParameter("port");
try {
port = Integer.parseInt(sPort) ;
} catch (NumberFormatException nfe) {
// use default port
} catch (NullPointerException npe) {
// use default port
}
String tempRole = getParameter("role");
if ( tempRole!=null ) {
// the 'target' roles opens a ServerSocket, so that the
// original 'source' applet can connect to it (or not)..
server = tempRole.trim().toLowerCase().equals("target");
}
if (server) {
// This is the pop-up. It establishes a server socket so that
// other applets can test for that socket.
try {
// start the server socket, so that the
// openning page can test for it..
ss = new ServerSocket(port);
add( new Label("Server active on port: " + port ) );
showStatus( "Server socket created at port: " + port );
}
catch (UnknownHostException uhe) {
uhe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (SecurityException se) {
se.printStackTrace();
}
} else {
// This page launches, and tests for,
// the page that has (maybe) pops-up..
Frame f = new Frame("");
// ..but only if the client specifically agrees.
WarningDialog wd = new WarningDialog(f);
wd.setVisible(true);
wd.toFront();
setLayout(new FlowLayout());
if ( wd.isApproved() ) {
Button b1 = new Button("Applets");
b1.addActionListener(this);
add(b1);
Button b2 = new Button("Show Document");
b2.addActionListener(this);
add(b2);
try {
String s = getDocumentBase().getFile();
s = "file:" +
s.substring(0,s.lastIndexOf("/")) + "/target.html";
target = new URL(s);
} catch (MalformedURLException murle) {
murle.printStackTrace();
}
} else {
add(new Label("Pop-up test declined by guest."));
}
}
}
/** Ensure the server socket is disposed. */
public void dispose() {
if (ss!=null) ss = null;
}
/** Check for the existence of the server socket by attempting to
connect to it. If the connection is successful, return true. */
public boolean isServerSocketActive() {
try {
Socket s = new Socket("localhost", port);
}
catch (UnknownHostException uhe) {
uhe.printStackTrace();
return false;
}
catch (IOException ioe) {
ioe.printStackTrace();
return false;
}
catch (SecurityException se) {
se.printStackTrace();
return false;
}
return true;
}
/** Show the target document, or test for it, as needed. */
public void actionPerformed(ActionEvent ae) {
if ( ((Button)ae.getSource()).getLabel().equals("Applets") ) {
showStatus( "Server socket detected: "
+ isServerSocketActive() );
} else {
getAppletContext().showDocument(target, "_blank");
}
}
}
/** The user/client warning dialog used in this example that
ensures the client is informed of the reason for the test.
@author Andrew Thompson
@version 2004-08-24 */
class WarningDialog extends Dialog {
Checkbox approved;
/** Construct a simple warning message dialog. */
WarningDialog(Frame f) {
super(f, true);
setLayout(new BorderLayout());
String sep = System.getProperty( "line.separator" ),
message = "Warning!" + sep + sep +
"This applet attempts to open 'pop-up' windows." + sep +
"Check the box below to proceed." + sep + sep +
"If you close the dialog without agreeing," + sep +
"the test will not proceed." + sep + sep +
"Should you change your mind, " + sep +
"simply refresh the page.";
add(new TextArea(message), BorderLayout.CENTER);
approved = new Checkbox("Agree");
add(approved, BorderLayout.SOUTH);
pack();
setSize(getPreferredSize());
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
setVisible(false);
}
} );
}
/** Has the user approved the pop-up test? */
public boolean isApproved() {
return approved.getState();
}
}
</sscce>
[ index.html ]
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=iso-8859-1">
<title>Applet showDocument() experiment</title>
<meta name="author" content="Andrew Thompson">
</head>
<body>
<div>
<applet alt='Java Disabled! (applet)'
code='SignedShowDocumentTestApplet1.class'
archive='SignedShowDocumentTestApplet1.jar'
width=220 height=100>
</applet>
</div>
</body>
</html>
[ end: index.html ]
[ target.html ]
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=iso-8859-1">
<title>Applet showDocument() experiment</title>
<meta name="author" content="Andrew Thompson">
</head>
<body>
<div>
<applet alt='Java Disabled! (applet)'
code='SignedShowDocumentTestApplet1.class'
archive='SignedShowDocumentTestApplet1.jar'
width=200 height=100>
<param name='role' value='target'>
</applet>
</div>
</body>
</html>
[ end: target.html ]
How's that go for you Mickey?