dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Short tip: Using SSL in Java to access a Domino site

by Johan Känngård / [Java] / 2002-12-14 / #10


You have to download JSSE (if you have J2SE 1.4 it´s built in). JSSE requires J2SE 1.2 or higher, so it can not be used in R5. Put the JSSE-jars (jsse.jar, jcert.jar, jnet.jar) into the classpath and use something like this (it can be rewritten to work with arguments from the command line):
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLConnection;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.net.ssl.SSLSocketFactory;
import javax.net.SocketFactory;

/**
 * A tool for testing HTTP / HTTPS connections.
 *
 * @author Johan Känngård, http://dev.kanngard.net
 */
public class URLConnectionTester {
    
    private String address = "server.com";
    private int port = 443;
    private boolean sslEnabled = true;
    
    private String request = null;
    /**
     * Called when this class is invoked from the command line.
     *
     * @param args the arguments from the command line.
     */
    public static void main(String[] args) {
        // This outputs interesting debug things:
        System.setProperty("javax.net.debug", "all");
        java.security.Security.addProvider(
        new com.sun.net.ssl.internal.ssl.Provider());
        URLConnectionTester tester = new URLConnectionTester();
    }
    
    /**
     * Creates a new URLConnectionTester.
     */
    public URLConnectionTester() {
        Socket socket = null;
        Writer out = null;
        LineNumberReader lnr = null;
        
        try {
            if (sslEnabled) {
                SocketFactory socketFactory = SSLSocketFactory.getDefault();
                socket = socketFactory.createSocket(address, port);
            } else {
                socket = new Socket(address, port);
            }
            out = new BufferedWriter(
                    new OutputStreamWriter(
                    socket.getOutputStream()));
            
            request = getLoginRequest(address, "user", "password");
            //request = getRestrictedRequest(
                    address,
                    "/secret.nsf",
                    "A06394EE78E2EF7847DDFA08C15A6A6B");
            //request = getLogoutRequest(
                    address,
                    "A06394EE78E2EF7847DDFA08C15A6A6B");
            out.write(request + "\n");
            out.flush();
            lnr = new LineNumberReader(
                    new BufferedReader(
                    new InputStreamReader(
                    socket.getInputStream())));
            for (String s = lnr.readLine(); s != null; s = lnr.readLine()) {
                System.out.println((s);
            }
        } catch (IOException e) {
            e.printStackTrace(e);
        } finally {
            
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            
            if (lnr != null) {
                try {
                    lnr.close();
                } catch (IOException e) {
                }
            }
            
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                }
            }
        }
    }
    
    private String getFormData(Hashtable formTable) {
        StringBuffer formData = new StringBuffer();
        String key = null;
        Enumeration enum = formTable.keys();
        
        while (enum.hasMoreElements()) {
            key = (String) enum.nextElement();
            formData.append(URLEncoder.encode(key) + "=" +
            URLEncoder.encode((String) formTable.get(key)) + "&");
        }
        
        return formData.toString().substring(0, formData.length() - 1);
    }
    
    private String getLoginRequest(
            String host,
            String username,
            String password) {

        Hashtable formTable = new Hashtable();
        formTable.put("Username", username);
        formTable.put("Password", password);
        String formData = getFormData(formTable);
        
        return "POST /?Login HTTP/1.1\n" +
        "Host: " + host + "\n" +
        "Content-Type: application/x-www-form-urlencoded\n" +
        "Content-Length: " + formData.length() + "\n" +
        "\n" +
        formData + "\n";
    }
    
    private String getRestrictedRequest(
            String host,
            String url,
            String domCookie) {
        
        return "GET " + url + " HTTP/1.1\n" +
        "Host: " + host + "\n" +
        "Cookie: DomAuthSessId=" + domCookie + "\n";
    }
    
    private String getLogoutRequest(
            String host,
            String domCookie) {

        return getRestrictedRequest(host, "/?Logout", domCookie);
    }
}


Floppy icon URLConnectionTest.java5 Kb