The Source for Java Technology Collaboration
User: Password:



Evan Summers's Blog

May 2007 Archives


Java SSL Sockets

Posted by evanx on May 28, 2007 at 10:45 PM | Permalink | Comments (0)

Let's build a trivial client/server demo using SSLServerSocket and SSLSocket as provided by the Java Secure Socket Extension (JSSE). We create a self-signed public key certificate for the server using keytool, and install this on the client.


Code Snippet

We implement the client as follows.

public class EnigmaClient extends Thread {
    EnigmaSocket enigmaSocket;
    KeyStore keyStore;
    KeyManager[] keyManagers;
    TrustManager[] trustManagers;
    SSLContext sslContext;
    
    public void init() throws Exception {
        initKeyManagers();
        initTrustManagers();
        initSSLContext();
    }
    
    public void connect(String host, int port) throws Exception {
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        Socket clientSocket = sslSocketFactory.createSocket(host, port);
        this.enigmaSocket = new EnigmaSocket(clientSocket);
    }
    ...
}

where the keystore containing the server's digital certificate eg. created using keytool, is loaded from a resource or file, and a KeyManagerFactory and TrustManagerFactory are initialised with the keyStore instance. Finally, an SSLContext is initialised with key managers and trust managers, and this is used to create SSL socket connections.




Java Highlighter

Posted by evanx on May 08, 2007 at 02:10 AM | Permalink | Comments (0)

In the Hyper Text Processor prequel, we processed half-baked HTML for this Foundation Beans series of articles.

Now let's meet the QHyperJavaProcessor, which highlights Java code in HTML pre blocks.


Code Snippet

We split up the Java text into an array of string tokens, using the following delimiter tokens, which include Java keywords to be highlighted.

public class QHyperJavaProcessor {
    static final String[] keywords = new String[] {
        "package", "import",
        "static", "final", "abstract", "synchronized", "transient",
        "default",
        "class", "interface", "@interface", "enum",
        "extends", "implements",
        "public", "private", "protected",
        "true", "false", "null",
        "instanceof",
        "void", "boolean", "int", "char", "long",
        "throws", "throw", "try", "catch", "finally",
        "new", "this", "super",
        "if", "else", "for", "while",
        "continue", "break",
        "return"
    };
    ...
}        






Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds