001    package org.apache.commons.ssl;
002    
003    import javax.net.SocketFactory;
004    import java.io.IOException;
005    import java.io.InputStream;
006    import java.io.OutputStream;
007    import java.net.Socket;
008    import java.security.GeneralSecurityException;
009    
010    public class CRLSocket extends SSLClient {
011        private final static CRLSocket secureInstance;
012        private final static CRLSocket plainInstance;    
013    
014        static {
015            CRLSocket sf1 = null, sf2 = null;
016            try {
017                sf1 = new CRLSocket();
018                sf2 = new CRLSocket();
019                sf2.setIsSecure(false);
020            }
021            catch (Exception e) {
022                System.out.println("could not create CRLSocket: " + e);
023                e.printStackTrace();
024            }
025            finally {
026                secureInstance = sf1;
027                plainInstance = sf2;            
028            }
029        }
030    
031        private CRLSocket() throws GeneralSecurityException, IOException {
032            super();
033    
034            // For now we setup the usual trust infrastructure, but consumers
035            // are encouraged to call getInstance().addTrustMaterial() or
036            // getInstance().setTrustMaterial() to customize the trust.
037            if (TrustMaterial.JSSE_CACERTS != null) {
038                setTrustMaterial(TrustMaterial.JSSE_CACERTS);
039            } else {
040                setTrustMaterial(TrustMaterial.CACERTS);
041            }
042            setConnectTimeout(5000);
043            setSoTimeout(5000);
044            setCheckCRL(false);
045        }
046    
047        public static SocketFactory getDefault() {
048            return getSecureInstance();
049        }
050    
051        public static CRLSocket getSecureInstance() {
052            return secureInstance;
053        }
054    
055        public static CRLSocket getPlainInstance() {
056            return plainInstance;
057        }
058    
059        public static void main(String[] args) throws Exception {
060            String host = args[0];
061            String port = args[1];
062            String hello
063                    = "HEAD / HTTP/1.1\r\n"
064                    + "Host:" + host + ":" + port + "\r\n\r\n";
065            byte[] helloBytes = hello.getBytes("UTF-8");
066    
067            System.out.println("About to getInstance() ");
068            CRLSocket sf = getPlainInstance();
069            long now = System.currentTimeMillis();
070            System.out.println("About to create socket: [" + host + ":" + port + "]");
071            Socket s = sf.createSocket(host, Integer.parseInt(port));
072            long delay = System.currentTimeMillis() - now;
073            System.out.println("Created socket! took " + delay + "ms ");
074            OutputStream out = s.getOutputStream();
075            out.write(helloBytes);
076            out.flush();
077    
078            System.out.println("\n" + new String(helloBytes, "UTF-8"));
079    
080            InputStream in = s.getInputStream();
081            int c = in.read();
082            StringBuffer buf = new StringBuffer();
083            System.out.println("Reading: ");
084            System.out.println("================================================================================");
085            while (c >= 0) {
086                byte b = (byte) c;
087                buf.append((char) b);
088                System.out.print((char) b);
089                if (-1 == buf.toString().indexOf("\r\n\r\n")) {
090                    c = in.read();
091                } else {
092                    break;
093                }
094            }
095            in.close();
096            out.close();
097            s.close();
098        }
099    
100    }