Relevant links










 

Send Soap by SSL "in 10 seconds"

Use of Soap protocol

Works from a commandline, a JMS MDB (message driven bean) bean, standard old EJB (enterprise bean), or whatever you might think of...


Soap dispatcher runtime code
SoapSendSSL.java

import java.io.*;
import java.net.URL;
import java.security.*;
import java.util.UUID;
import javax.net.ssl.*;

public class SoapSendSSL {


    private static TrustManager[] _trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null;    }

        public void checkClientTrusted(    java.security.cert.X509Certificate[] certs, String authType) { }

        public void checkServerTrusted(    java.security.cert.X509Certificate[] certs, String authType) { }
    } };

    private static SSLSocketFactory getSSLFactory(File pKeyFile,
            String pKeyPassword) throws Exception {
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance("SunX509");
        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        InputStream keyInput = new FileInputStream(pKeyFile);
        keyStore.load(keyInput, pKeyPassword.toCharArray());
        keyInput.close();

        keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());

        SSLContext context = SSLContext.getInstance("SSL");
        context.init(keyManagerFactory.getKeyManagers(), _trustAllCerts, // null
                new SecureRandom());

        return context.getSocketFactory();
    }

    public static String postHttpsXML(String soapHostname, int soapPort,
            String soapPath, File pKeyFile, String pKeyPassword,
            String soapXmldata) throws Exception {

        URL url = new URL("https://" + soapHostname
                + (soapPort < 0 ? "" : (":" + soapPort)) + soapPath);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setSSLSocketFactory(getSSLFactory(pKeyFile, pKeyPassword));

        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Content-Length", String.valueOf(soapXmldata
                .length()));
        con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        // con.setRequestProperty("SOAPAction","ElektroniskAkt");
        OutputStream oStream = con.getOutputStream();
        oStream.write(soapXmldata.getBytes());
        oStream.flush();
        // oStream.close();

        // Response
        BufferedReader rd = new BufferedReader(new InputStreamReader(con
                .getInputStream()));
        String line;
        String response = new String();
        while ((line = rd.readLine()) != null) {
            response = line + "\n";
        }
        oStream.close();
        con.disconnect();
        return response;

    }

    public static byte[] getByteStreamFromFile(String filename) {
        File f = new File(filename);
        byte[] fileBuffer = null;
        try {
            FileInputStream fis = new FileInputStream(f);
            long fileLength = f.length();
            fileBuffer = new byte[(int) fileLength];
            int fileRead = fis.read(fileBuffer);
        } catch (IOException x) {
            fileBuffer = null;
        }
        return fileBuffer;
    }

    public static void send(String soapHostname, String soapPath,
            String ocesCerticficateFile, String ocesCerticficatePassword) {

        String message = new String(),
               soapXmldata = new String(getByteStreamFromFile(soapHostname + ".xml"));

        String startPatt = "uuid:", stopPatt = "";
        int i = soapXmldata.indexOf(startPatt) + startPatt.length(), j = soapXmldata
                .indexOf(stopPatt);
        UUID u = UUID.randomUUID();
        String uuid = u.toString();
        soapXmldata = soapXmldata.substring(0, i) + uuid
                + soapXmldata.substring(j);

        File pKeyFile = new File(ocesCerticficateFile);
        String pKeyPassword = ocesCerticficatePassword;

        int soapPort = -1; // 443;
        long startTime = System.currentTimeMillis(), expiredTime = -1;
        String response;
        try {

            response = postHttpsXML(soapHostname, soapPort, soapPath,
                    pKeyFile, pKeyPassword, soapXmldata);
            expiredTime = System.currentTimeMillis() - startTime;
        } catch (Exception x) {
            x.printStackTrace();
            expiredTime = System.currentTimeMillis() - startTime;
            response = "failure: "+x.toString();
        }

        System.out.println("Status : \r\n\r\n" + response + 
                             "\r\n\r\nin millis="+expiredTime+"\r\n\r\n");
    }

    public static void main(String[] args) {

        //expects existing files:
        //'tinglysningsServer.xml' containing Soap XML to send
        //'tinglysningsServer.log' containing expected synchronous XML response to receive
        //
        //during the process, saves the received response as file : 
        //"xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx.log' where x... is the uuid used for sending file 
        
        send("tinglysningsServer", "/services/TinglysningService", 
                       "ocesSslTransportCertificate.p12", "keystorePassword");

    }

}



Short commentary on the java code..

The code does a few things, which might are only relevant when handling tinglysning.
  • inserts a new uuid each at each time of dispatch
  • retrieves the request to send from a file
  • measures time for transmission
When removing these few extra features, the example becomes even smaller.