FileDocCategorySizeDatePackage
SSLWebServerFrame.javaAPI DocExample4349Thu Nov 08 00:23:28 GMT 2001com.ora.rmibook.chapter2.ssl

SSLWebServerFrame

public class SSLWebServerFrame extends ExitingFrame

Fields Summary
private static String
ANON_CIPHER_1
private static String
ANON_CIPHER_2
private static String
ANON_CIPHER_3
private static String
ANON_CIPHER_4
private static String
ANON_CIPHER_5
private static String[]
CIPHERS
private JTextArea
_displayArea
private static final String
_mainPage
Constructors Summary
Methods Summary
protected voidbuildGUI()

        JPanel newContentPane = new JPanel(new BorderLayout());

        newContentPane.add(buildMainPanel(), BorderLayout.CENTER);
        setContentPane(newContentPane);
        setSize(250, 300);
    
private javax.swing.JComponentbuildMainPanel()

        _displayArea = new JTextArea();
        _displayArea.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(_displayArea);

        scrollPane.setBorder(new EtchedBorder());
        return scrollPane;
    
private java.net.ServerSocketgetSSLServerSocket(int port)

        SSLServerSocketFactory socketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
        SSLServerSocket returnValue = (SSLServerSocket) socketFactory.createServerSocket(port);

        returnValue.setEnabledCipherSuites(CIPHERS);
        returnValue.setNeedClientAuth(false);
        returnValue.setEnableSessionCreation(true);
        return returnValue;
    
private voidprocessClientRequest(java.net.Socket client)

        _displayArea.append("Client connected from port " +
            client.getPort() + " on machine " + client.getInetAddress() + "\n");
        _displayArea.append("Request is: \n");
        readRequest(client);
        sendResponse(client);
        client.close();
    
private voidreadRequest(java.net.Socket client)

        BufferedReader request = null;

        request = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String nextLine;

        while (null != (nextLine = request.readLine())) {
            // Ideally, we'd do something. But this is a very
            // simple web server.
            if (nextLine.equals("")) {
                break;
            } else {
                _displayArea.append("\t" + nextLine + "\n");
            }
        }
        _displayArea.append("---------------------------------------\n");
        return;
    
private voidsendResponse(java.net.Socket client)

        BufferedWriter response;

        response = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        response.write(_mainPage);
        response.flush();
    
public voidstartListening()

        ServerSocket serverSocket;

        try {
            serverSocket = getSSLServerSocket(443);
        } catch (IOException e) {
            return;
        }
        while (true) {
            try {
                Socket client = serverSocket.accept(); // wait here

                processClientRequest(client);
                //bad design-- should  handle requests in separate threads
                // and immediately resume listening for connections            	client.close();
            } catch (IOException e) {
            }
        }