Example usage for javax.net.ssl SSLSocketFactory createSocket

List of usage examples for javax.net.ssl SSLSocketFactory createSocket

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocketFactory createSocket.

Prototype

public abstract Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException;

Source Link

Document

Returns a socket layered over an existing socket connected to the named host, at the given port.

Usage

From source file:com.iiordanov.tigervnc.rfb.CSecurityTLS.java

private void initGlobal() {
    try {//from ww w. j a va  2  s .c  om
        SSLSocketFactory sslfactory;
        SSLContext ctx = SSLContext.getInstance("TLS");

        if (anon) {
            ctx.init(null, null, null);
        } else {
            TrustManager[] myTM = new TrustManager[] { new MyX509TrustManager() };
            ctx.init(null, myTM, null);
        }

        sslfactory = ctx.getSocketFactory();
        try {
            ssl = (SSLSocket) sslfactory.createSocket(CConnection.sock,
                    CConnection.sock.getInetAddress().getHostName(), CConnection.sock.getPort(), true);
            ssl.setTcpNoDelay(true);
        } catch (java.io.IOException e) {
            throw new Exception(e.toString());
        }

        if (anon) {
            String[] supported;
            ArrayList<String> enabled = new ArrayList<String>();

            supported = ssl.getSupportedCipherSuites();

            for (int i = 0; i < supported.length; i++) {
                //Log.e("SUPPORTED CIPHERS", supported[i]);
                if (supported[i].matches("TLS_DH_anon.*"))
                    enabled.add(supported[i]);
            }

            if (enabled.size() == 0)
                throw new Exception("Your device lacks support for ciphers necessary for this encryption mode "
                        + "(Anonymous Diffie-Hellman ciphers). "
                        + "This is a known issue with devices running Android 2.2.x and older. You can "
                        + "work around this by using VeNCrypt with x509 certificates instead.");

            ssl.setEnabledCipherSuites(enabled.toArray(new String[0]));
        } else {
            ssl.setEnabledCipherSuites(ssl.getSupportedCipherSuites());
        }

        ssl.setEnabledProtocols(ssl.getSupportedProtocols());
        ssl.addHandshakeCompletedListener(new MyHandshakeListener());
    } catch (java.security.GeneralSecurityException e) {
        vlog.error("TLS handshake failed " + e.toString());
        return;
    }
}

From source file:com.digitalpebble.storm.crawler.protocol.http.HttpResponse.java

/**
 * Default public constructor./*from   ww  w  .j  a  va2 s. com*/
 * 
 * @param http
 * @param url
 * @param knownMetadata
 * @throws IOException
 * @throws HttpException
 */
public HttpResponse(HttpProtocol http, URL url, Metadata knownMetadata) throws IOException, HttpException {

    this.http = http;
    this.url = url;

    Scheme scheme = null;

    if ("http".equals(url.getProtocol())) {
        scheme = Scheme.HTTP;
    } else if ("https".equals(url.getProtocol())) {
        scheme = Scheme.HTTPS;
    } else {
        throw new IOException("Unknown scheme (not http/https) for url:" + url);
    }

    String path = "".equals(url.getFile()) ? "/" : url.getFile();

    // some servers will redirect a request with a host line like
    // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they
    // don't want the :80...

    String host = url.getHost();
    int port;
    String portString;
    if (url.getPort() == -1) {
        if (scheme == Scheme.HTTP) {
            port = 80;
        } else {
            port = 443;
        }
        portString = "";
    } else {
        port = url.getPort();
        portString = ":" + port;
    }
    Socket socket = null;

    try {
        socket = new Socket(); // create the socket
        socket.setSoTimeout(http.getTimeout());

        // connect
        String sockHost = http.useProxy() ? http.getProxyHost() : host;
        int sockPort = http.useProxy() ? http.getProxyPort() : port;
        InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
        socket.connect(sockAddr, http.getTimeout());

        if (scheme == Scheme.HTTPS) {
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, sockHost, sockPort, true);
            sslsocket.setUseClientMode(true);

            // Get the protocols and ciphers supported by this JVM
            Set<String> protocols = new HashSet<String>(Arrays.asList(sslsocket.getSupportedProtocols()));
            Set<String> ciphers = new HashSet<String>(Arrays.asList(sslsocket.getSupportedCipherSuites()));

            // Intersect with preferred protocols and ciphers
            protocols.retainAll(http.getTlsPreferredProtocols());
            ciphers.retainAll(http.getTlsPreferredCipherSuites());

            sslsocket.setEnabledProtocols(protocols.toArray(new String[protocols.size()]));
            sslsocket.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));

            sslsocket.startHandshake();
            socket = sslsocket;
        }

        this.conf = http.getConf();
        if (ConfUtils.getBoolean(conf, "store.ip.address", false) == true) {
            headers.setValue("_ip_", sockAddr.getAddress().getHostAddress());
        }

        // make request
        OutputStream req = socket.getOutputStream();

        StringBuffer reqStr = new StringBuffer("GET ");
        if (http.useProxy()) {
            reqStr.append(url.getProtocol() + "://" + host + portString + path);
        } else {
            reqStr.append(path);
        }

        reqStr.append(" HTTP/1.0\r\n");

        reqStr.append("Host: ");
        reqStr.append(host);
        reqStr.append(portString);
        reqStr.append("\r\n");

        reqStr.append("Accept-Encoding: x-gzip, gzip, deflate\r\n");

        String userAgent = http.getUserAgent();
        if ((userAgent == null) || (userAgent.length() == 0)) {
            if (HttpProtocol.LOGGER.isErrorEnabled()) {
                HttpProtocol.LOGGER.error("User-agent is not set!");
            }
        } else {
            reqStr.append("User-Agent: ");
            reqStr.append(userAgent);
            reqStr.append("\r\n");
        }

        reqStr.append("Accept-Language: ");
        reqStr.append(this.http.getAcceptLanguage());
        reqStr.append("\r\n");

        reqStr.append("Accept: ");
        reqStr.append(this.http.getAccept());
        reqStr.append("\r\n");

        if (knownMetadata != null) {
            String ifModifiedSince = knownMetadata.getFirstValue("cachedLastModified");
            if (StringUtils.isNotBlank(ifModifiedSince)) {
                reqStr.append("If-Modified-Since: ");
                reqStr.append(ifModifiedSince);
                reqStr.append("\r\n");
            }

            String ifNoneMatch = knownMetadata.getFirstValue("cachedEtag");
            if (StringUtils.isNotBlank(ifNoneMatch)) {
                reqStr.append("If-None-Match: ");
                reqStr.append(ifNoneMatch);
                reqStr.append("\r\n");
            }
        }

        reqStr.append("\r\n");

        // @see http://www.w3.org/Protocols/rfc2068/rfc2068.txt for default
        // charset
        // TODO use UTF-8 and set a charset value explicitely
        byte[] reqBytes = reqStr.toString().getBytes(StandardCharsets.ISO_8859_1);

        req.write(reqBytes);
        req.flush();

        PushbackInputStream in = // process response
                new PushbackInputStream(
                        new BufferedInputStream(socket.getInputStream(), HttpProtocol.BUFFER_SIZE),
                        HttpProtocol.BUFFER_SIZE);

        StringBuffer line = new StringBuffer();

        boolean haveSeenNonContinueStatus = false;
        while (!haveSeenNonContinueStatus) {
            // parse status code line
            this.code = parseStatusLine(in, line);
            // parse headers
            parseHeaders(in, line);
            haveSeenNonContinueStatus = code != 100; // 100 is
                                                     // "Continue"
        }
        String transferEncoding = getHeader(HttpHeaders.TRANSFER_ENCODING);
        if (transferEncoding != null && "chunked".equalsIgnoreCase(transferEncoding.trim())) {
            readChunkedContent(in, line);
        } else {
            readPlainContent(in);
        }

        String contentEncoding = getHeader(HttpHeaders.CONTENT_ENCODING);
        if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
            content = http.processGzipEncoded(content, url);
        } else if ("deflate".equals(contentEncoding)) {
            content = http.processDeflateEncoded(content, url);
        } else {
            HttpProtocol.LOGGER.trace("fetched {}  bytes from {}", content.length, url);
        }

    } finally {
        if (socket != null)
            socket.close();
    }

}

From source file:org.apache.jmeter.protocol.amf.proxy.AmfProxy.java

/**
 * Negotiate a SSL connection.//from  w w  w .  ja  v a 2s  . co m
 *
 * @param sock socket in
 * @param host
 * @return a new client socket over ssl
 * @throws Exception if negotiation failed
 */
private Socket startSSL(Socket sock, String host) throws IOException {
    SSLSocketFactory sslFactory = getSSLSocketFactory(host);
    SSLSocket secureSocket;
    if (sslFactory != null) {
        try {
            secureSocket = (SSLSocket) sslFactory.createSocket(sock, sock.getInetAddress().getHostName(),
                    sock.getPort(), true);
            secureSocket.setUseClientMode(false);
            if (log.isDebugEnabled()) {
                log.debug("SSL transaction ok with cipher: " + secureSocket.getSession().getCipherSuite());
            }
            return secureSocket;
        } catch (IOException e) {
            log.error("Error in SSL socket negotiation: ", e);
            throw e;
        }
    } else {
        log.warn("Unable to negotiate SSL transaction, no keystore?");
        throw new IOException("Unable to negotiate SSL transaction, no keystore?");
    }
}

From source file:org.bombusim.networking.NetworkSocketDataStream.java

public void setTLS() throws IOException {
    LimeLog.i("Socket", "Switching to secure socket layer", null);

    //TODO: check on different devices:
    // !!! ENSURE TLS enabled in account settings before test
    // 1. emulator/2.2 - SSLPeerUnverifiedException (jabber.ru, google.com) - bug in emulator v2.2
    // 2. cyanogen/2.3 - works (all hosts)
    // 3. emulator/ics - works
    // 4. Gratia/2.2 - works
    SSLSocketFactory sf =
            //SSLCertificateSocketFactory.getDefault(20000, null);
            SSLCertificateSocketFactory.getInsecure(20000, null);

    //TODO: check on different devices:
    // 1. emulator/2.2 - works
    // 2. cyanogen/2.3 - works
    //KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
    //trustStore.load(null, null); 
    //SSLSocketFactory sf = new AndroidSSLSocketFactory(trustStore); 
    //sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

    final SSLSocket ssls = (SSLSocket) sf.createSocket(socket, host, port, true);

    ssls.addHandshakeCompletedListener(new HandshakeCompletedListener() {
        @Override/*from  w ww  .  jav  a2 s.  c om*/
        public void handshakeCompleted(HandshakeCompletedEvent event) {
            X509Certificate[] certs;
            try {
                certs = ssls.getSession().getPeerCertificateChain();
            } catch (SSLPeerUnverifiedException e) {
                return;
            }

            StringBuilder so = new StringBuilder();

            for (X509Certificate cert : certs) {
                so.append("X509 Certificate:\n").append(" Subject:");
                appendPrincipal(so, cert.getSubjectDN());
                so.append("\n Issued by:");
                appendPrincipal(so, cert.getIssuerDN());
                so.append("\n Valid from:    ").append(DateFormat.getInstance().format(cert.getNotBefore()));
                so.append("\n Expired after: ").append(DateFormat.getInstance().format(cert.getNotAfter()));
                so.append("\n\n");
            }

            certificateInfo = so.toString();
            LimeLog.i("Socket", "Certificate chain verified", certificateInfo);
        }

        private void appendPrincipal(StringBuilder so, Principal p) {
            String name = p.getName();
            if (name == null) {
                so.append("<null>\n");
                return;
            }

            String elements[] = name.split(",");
            for (String e : elements) {
                so.append("\n   ").append(e);
            }

            so.append("\n");
        }
    });

    ssls.startHandshake();
    socket = ssls;

    istream = socket.getInputStream();
    ostream = socket.getOutputStream();

}

From source file:org.apache.nutch.protocol.http.HttpResponse.java

/**
 * Default public constructor./* ww w. j  a v  a  2 s  .c  om*/
 *
 * @param http
 * @param url
 * @param datum
 * @throws ProtocolException
 * @throws IOException
 */
public HttpResponse(HttpBase http, URL url, CrawlDatum datum) throws ProtocolException, IOException {

    this.http = http;
    this.url = url;
    this.orig = url.toString();
    this.base = url.toString();

    Scheme scheme = null;

    if ("http".equals(url.getProtocol())) {
        scheme = Scheme.HTTP;
    } else if ("https".equals(url.getProtocol())) {
        scheme = Scheme.HTTPS;
    } else {
        throw new HttpException("Unknown scheme (not http/https) for url:" + url);
    }

    if (Http.LOG.isTraceEnabled()) {
        Http.LOG.trace("fetching " + url);
    }

    String path = "".equals(url.getFile()) ? "/" : url.getFile();

    // some servers will redirect a request with a host line like
    // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they
    // don't want the :80...

    LOG.info("Fetching " + url.toString());

    String host = url.getHost();
    int port;
    String portString;
    if (url.getPort() == -1) {
        if (scheme == Scheme.HTTP) {
            port = 80;
        } else {
            port = 443;
        }
        portString = "";
    } else {
        port = url.getPort();
        portString = ":" + port;
    }
    Socket socket = null;

    try {
        socket = new Socket(); // create the socket
        socket.setSoTimeout(http.getTimeout());

        // connect
        String sockHost = http.useProxy(url) ? http.getProxyHost() : host;
        int sockPort = http.useProxy(url) ? http.getProxyPort() : port;
        InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
        socket.connect(sockAddr, http.getTimeout());

        if (scheme == Scheme.HTTPS) {
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, sockHost, sockPort, true);
            sslsocket.setUseClientMode(true);

            // Get the protocols and ciphers supported by this JVM
            Set<String> protocols = new HashSet<String>(Arrays.asList(sslsocket.getSupportedProtocols()));
            Set<String> ciphers = new HashSet<String>(Arrays.asList(sslsocket.getSupportedCipherSuites()));

            // Intersect with preferred protocols and ciphers
            protocols.retainAll(http.getTlsPreferredProtocols());
            ciphers.retainAll(http.getTlsPreferredCipherSuites());

            sslsocket.setEnabledProtocols(protocols.toArray(new String[protocols.size()]));
            sslsocket.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));

            sslsocket.startHandshake();
            socket = sslsocket;
        }

        this.conf = http.getConf();
        if (sockAddr != null && conf.getBoolean("store.ip.address", false) == true) {
            headers.add("_ip_", sockAddr.getAddress().getHostAddress());
        }

        // make request
        OutputStream req = socket.getOutputStream();

        StringBuffer reqStr = new StringBuffer("GET ");
        if (http.useProxy(url)) {
            reqStr.append(url.getProtocol() + "://" + host + portString + path);
        } else {
            reqStr.append(path);
        }

        reqStr.append(" HTTP/1.0\r\n");

        reqStr.append("Host: ");
        reqStr.append(host);
        reqStr.append(portString);
        reqStr.append("\r\n");

        reqStr.append("Accept-Encoding: x-gzip, gzip, deflate\r\n");

        String userAgent = http.getUserAgent();
        if ((userAgent == null) || (userAgent.length() == 0)) {
            if (Http.LOG.isErrorEnabled()) {
                Http.LOG.error("User-agent is not set!");
            }
        } else {
            reqStr.append("User-Agent: ");
            reqStr.append(userAgent);
            reqStr.append("\r\n");
        }

        reqStr.append("Accept-Language: ");
        reqStr.append(this.http.getAcceptLanguage());
        reqStr.append("\r\n");

        reqStr.append("Accept: ");
        reqStr.append(this.http.getAccept());
        reqStr.append("\r\n");

        if (http.isIfModifiedSinceEnabled() && datum.getModifiedTime() > 0) {
            reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(datum.getModifiedTime()));
            reqStr.append("\r\n");
        }
        reqStr.append("\r\n");

        // store the request in the metadata?
        if (conf.getBoolean("store.http.request", false) == true) {
            headers.add("_request_", reqStr.toString());
        }

        byte[] reqBytes = reqStr.toString().getBytes();

        req.write(reqBytes);
        req.flush();

        LOG.info("Processing response..");

        PushbackInputStream in = // process response
                new PushbackInputStream(new BufferedInputStream(socket.getInputStream(), Http.BUFFER_SIZE),
                        Http.BUFFER_SIZE);

        StringBuffer line = new StringBuffer();

        // store the http headers verbatim
        if (conf.getBoolean("store.http.headers", false) == true) {
            httpHeaders = new StringBuffer();
        }

        headers.add("nutch.fetch.time", Long.toString(System.currentTimeMillis()));

        boolean haveSeenNonContinueStatus = false;
        while (!haveSeenNonContinueStatus) {
            // parse status code line
            this.code = parseStatusLine(in, line);
            if (httpHeaders != null)
                httpHeaders.append(line).append("\n");
            // parse headers
            parseHeaders(in, line, httpHeaders);
            haveSeenNonContinueStatus = code != 100; // 100 is "Continue"
        }

        if (httpHeaders != null) {
            headers.add("_response.headers_", httpHeaders.toString());
        }

        String transferEncoding = getHeader(Response.TRANSFER_ENCODING);
        LOG.info("Transfer Encoding for " + url + ":" + transferEncoding);
        if (transferEncoding != null && "chunked".equalsIgnoreCase(transferEncoding.trim())) {
            readChunkedContent(in, line);
        } else {
            readPlainContent(in);
        }

        String contentEncoding = getHeader(Response.CONTENT_ENCODING);
        if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
            content = http.processGzipEncoded(content, url);
        } else if ("deflate".equals(contentEncoding)) {
            content = http.processDeflateEncoded(content, url);
        } else {
            if (Http.LOG.isTraceEnabled()) {
                Http.LOG.trace("fetched " + content.length + " bytes from " + url);
            }
        }

        LOG.info("Checking URL:" + url.toString());
        //check if url contains google drive string
        if (url.toString().toLowerCase().contains("https://drive.google.com/")) {
            //split into two string separated by '=' to get the article id
            LOG.info("Google Drive URL Detected!");
            String[] parts = url.toString().split("=");
            url = new URL("http://drive.google.com/uc?export=download&id=" + parts[1]);

            LOG.info("New URL:" + url.toString());
            this.http = http;
            this.url = url;
            this.orig = url.toString();
            this.base = url.toString();

            HttpClient client = new HttpClient();
            GetMethod method = new GetMethod(url.toString());
            int statusCode = client.executeMethod(method);
            content = method.getResponseBody();
            LOG.info("File Size on Drive: " + content.length);
            //   return;

        }

        LOG.info("Fetch Bytes: " + content.length + " bytes from " + url);

    } finally {
        if (socket != null)
            socket.close();
    }

}

From source file:org.apache.nutch.protocol.s2jh.HttpResponse.java

public HttpResponse(HttpBase http, URL url, WebPage page) throws ProtocolException, IOException {
    conf = http.getConf();//ww  w  .java  2 s. c o  m
    this.http = http;
    this.url = url;
    Scheme scheme = null;

    if ("http".equals(url.getProtocol())) {
        scheme = Scheme.HTTP;
    } else if ("https".equals(url.getProtocol())) {
        scheme = Scheme.HTTPS;
    } else {
        throw new HttpException("Unknown scheme (not http/https) for url:" + url);
    }

    if (Http.LOG.isTraceEnabled()) {
        Http.LOG.trace("fetching " + url);
    }

    String path = "".equals(url.getFile()) ? "/" : url.getFile();

    // some servers will redirect a request with a host line like
    // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they
    // don't want the :80...

    String host = url.getHost();
    int port;
    String portString;
    if (url.getPort() == -1) {
        if (scheme == Scheme.HTTP) {
            port = 80;
        } else {
            port = 443;
        }
        portString = "";
    } else {
        port = url.getPort();
        portString = ":" + port;
    }
    Socket socket = null;

    try {
        socket = new Socket(); // create the socket
        socket.setSoTimeout(http.getTimeout());

        // connect
        String sockHost = http.useProxy() ? http.getProxyHost() : host;
        int sockPort = http.useProxy() ? http.getProxyPort() : port;
        InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort);
        socket.connect(sockAddr, http.getTimeout());

        if (scheme == Scheme.HTTPS) {
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, sockHost, sockPort, true);
            sslsocket.setUseClientMode(true);

            // Get the protocols and ciphers supported by this JVM
            Set<String> protocols = new HashSet<String>(Arrays.asList(sslsocket.getSupportedProtocols()));
            Set<String> ciphers = new HashSet<String>(Arrays.asList(sslsocket.getSupportedCipherSuites()));

            // Intersect with preferred protocols and ciphers
            protocols.retainAll(http.getTlsPreferredProtocols());
            ciphers.retainAll(http.getTlsPreferredCipherSuites());

            sslsocket.setEnabledProtocols(protocols.toArray(new String[protocols.size()]));
            sslsocket.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));

            sslsocket.startHandshake();
            socket = sslsocket;
        }

        if (sockAddr != null && conf.getBoolean("store.ip.address", false) == true) {
            String ipString = sockAddr.getAddress().getHostAddress(); // get the ip
                                                                      // address
            page.getMetadata().put(new Utf8("_ip_"), ByteBuffer.wrap(ipString.getBytes()));
        }

        Http.LOG.debug("HTTP fetching: " + url);
        // make request
        OutputStream req = socket.getOutputStream();

        StringBuffer reqStr = new StringBuffer("GET ");
        if (http.useProxy()) {
            reqStr.append(url.getProtocol() + "://" + host + portString + path);
        } else {
            reqStr.append(path);
        }

        reqStr.append(" HTTP/1.0\r\n");

        reqStr.append("Host: ");
        reqStr.append(host);
        reqStr.append(portString);
        reqStr.append("\r\n");

        reqStr.append("Accept-Encoding: x-gzip, gzip\r\n");

        reqStr.append("Accept: ");
        reqStr.append(this.http.getAccept());
        reqStr.append("\r\n");

        String userAgent = http.getUserAgent();
        if ((userAgent == null) || (userAgent.length() == 0)) {
            if (Http.LOG.isErrorEnabled()) {
                Http.LOG.error("User-agent is not set!");
            }
        } else {
            reqStr.append("User-Agent: ");
            reqStr.append(userAgent);
            reqStr.append("\r\n");
        }

        // if (page.isReadable(WebPage.Field.MODIFIED_TIME.getIndex())) {
        reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(page.getModifiedTime()));
        reqStr.append("\r\n");
        // }
        reqStr.append("\r\n");

        byte[] reqBytes = reqStr.toString().getBytes();

        req.write(reqBytes);
        req.flush();

        PushbackInputStream in = // process response
                new PushbackInputStream(new BufferedInputStream(socket.getInputStream(), Http.BUFFER_SIZE),
                        Http.BUFFER_SIZE);

        StringBuffer line = new StringBuffer();

        boolean haveSeenNonContinueStatus = false;
        while (!haveSeenNonContinueStatus) {
            // parse status code line
            this.code = parseStatusLine(in, line);
            // parse headers
            parseHeaders(in, line);
            haveSeenNonContinueStatus = code != 100; // 100 is "Continue"
        }

        if (!url.toString().endsWith("robots.txt")) {
            if (readPlainContent(url.toString(), in)) {
            } else if (readPlainContentByHtmlunit(url)) {
            } else {
                readPlainContentByWebDriver(url);
            }
        }

        if (content != null && content.length > 0) {
            String html = charset == null ? new String(content) : new String(content, charset);
            //System.out.println("URL: " + url + ", CharsetName: " + charset + " , Page HTML=\n" + html);
            Http.LOG_HTML.trace("URL: " + url + ", CharsetName: " + charset + " , Page HTML=\n" + html);
        }

        // add headers in metadata to row
        if (page.getHeaders() != null) {
            page.getHeaders().clear();
        }
        for (String key : headers.names()) {
            page.getHeaders().put(new Utf8(key), new Utf8(headers.get(key)));
        }

    } catch (Exception e) {
        Http.LOG.error(e.getMessage(), e);
    } finally {
        if (socket != null)
            socket.close();
    }

}

From source file:com.fluffypeople.managesieve.ManageSieveClient.java

/**
 * Upgrade connection to TLS. Should be called before authenticating,
 * especially if you are using the PLAIN scheme.
 *
 * @param sslSocketFactory//from  w w w.  j  av a 2 s. c  o  m
 * @return ManageSieveResponse OK on successful upgrade, NO on error or if
 * the server doesn't support SSL
 * @throws IOException
 * @throws ParseException
 */
public synchronized ManageSieveResponse starttls(final SSLSocketFactory sslSocketFactory,
        final boolean rfcCheck) throws IOException, ParseException {
    sendCommand("STARTTLS");
    ManageSieveResponse resp = parseResponse();
    if (resp.isOk()) {
        secureSocket = (SSLSocket) sslSocketFactory.createSocket(socket,
                socket.getInetAddress().getHostAddress(), socket.getPort(), true);
        if (rfcCheck) {
            // The manage sieve rfc says we should check that the name in the certificate
            // matches the hostname that we want.

            Principal p = secureSocket.getSession().getPeerPrincipal();
            if (p instanceof X500Principal) {
                String serverName = getHostnameFromCert((X500Principal) p);
                if (!hostname.equals(serverName)) {
                    throw new IOException("Secure connect failed: Server name " + serverName
                            + " doesn't match wanted " + hostname);
                }
            } else {
                log.warn("Unexpected principle: {}", p.getName());
            }
        }
        setupAfterConnect(secureSocket);
        return parseCapabilities();

    } else {
        return resp;
    }
}

From source file:com.isecpartners.gizmo.HttpRequest.java

private SSLSocket negotiateSSL(Socket sock, String hostname) throws KeyManagementException, KeyStoreException,
        NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, IOException,
        InvalidKeyException, SignatureException, NoSuchProviderException, NoCertException {
    synchronized (_factories) {
        SSLSocketFactory factory = _factories.get(hostname);

        if (factory == null) {
            factory = initSSL(hostname);
        }//from  w  w w  . ja  v  a2 s .com
        String inbound_hostname = sock.getInetAddress().getHostName();
        int inbound_port = sock.getPort();
        SSLSocket sslsock = (SSLSocket) factory.createSocket(sock, inbound_hostname, inbound_port, true);
        sslsock.setUseClientMode(false);
        if (!sslsock.isConnected()) {
            Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, "Couldn't negotiate SSL");
            System.exit(-1);
        }
        return sslsock;
    }
}

From source file:com.techcavern.pircbotz.InputParser.java

/**
 * Process any lines relevant to connect. Only called before bot is logged into the server
 * @param rawLine Raw, unprocessed line from the server
 * @param code //from   w  w  w  . j av a2 s . co m
 * @param target
 * @param parsedLine Processed line
 * @throws IrcException If the server rejects the bot (nick already in use or a 4** or 5** code
 * @throws IOException If an error occurs during upgrading to SSL
 */
public void processConnect(String rawLine, String code, String target, List<String> parsedLine)
        throws IrcException, IOException {
    if (CONNECT_CODES.contains(code)) {
        // We're connected to the server.
        bot.loggedIn(configuration.getName() + (nickSuffix == 0 ? "" : nickSuffix));
        log.debug("Logged onto server.");

        configuration.getListenerManager().dispatchEvent(new ConnectEvent<PircBotZ>(bot));

        //Handle automatic on connect stuff
        if (configuration.getNickservPassword() != null)
            bot.sendIRC().identify(configuration.getNickservPassword());
        ImmutableMap<String, String> autoConnectChannels = bot.reconnectChannels();
        if (autoConnectChannels == null)
            autoConnectChannels = configuration.getAutoJoinChannels();
        for (Map.Entry<String, String> channelEntry : autoConnectChannels.entrySet())
            bot.sendIRC().joinChannel(channelEntry.getKey(), channelEntry.getValue());
    } else if (code.equals("433")) {
        //EXAMPLE: * AnAlreadyUsedName :Nickname already in use
        //Nickname in use, rename
        String usedNick = parsedLine.get(1);
        boolean autoNickChange = configuration.isAutoNickChange();
        String autoNewNick = null;
        if (autoNickChange) {
            nickSuffix++;
            bot.sendIRC().changeNick(autoNewNick = configuration.getName() + nickSuffix);
        }
        configuration.getListenerManager()
                .dispatchEvent(new NickAlreadyInUseEvent<PircBotZ>(bot, usedNick, autoNewNick, autoNickChange));
    } else if (code.equals("439")) {
        //EXAMPLE: PircBotX: Target change too fast. Please wait 104 seconds
        // No action required.
        //TODO: Should we delay joining channels here or something?
        log.warn("Ignoring too fast error");
    } else if (configuration.isCapEnabled() && code.equals("421") && parsedLine.get(1).equals("CAP")) {
        //EXAMPLE: 421 you CAP :Unknown command
        log.warn("Ignoring unknown command error, server does not support CAP negotiation");
    } else if (configuration.isCapEnabled() && code.equals("451") && target.equals("CAP")) {
        //EXAMPLE: 451 CAP :You have not registered
        //Ignore, this is from servers that don't support CAP
        log.warn("Ignoring not registered error, server does not support CAP negotiation");
    } else if (code.startsWith("5") || code.startsWith("4"))
        throw new IrcException(IrcException.Reason.CannotLogin, "Received error: " + rawLine);
    else if (code.equals("670")) {
        //Server is saying that we can upgrade to TLS
        SSLSocketFactory sslSocketFactory = ((SSLSocketFactory) SSLSocketFactory.getDefault());
        for (CapHandler curCapHandler : configuration.getCapHandlers())
            if (curCapHandler instanceof TLSCapHandler)
                sslSocketFactory = ((TLSCapHandler) curCapHandler).getSslSocketFactory();
        SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(bot.getSocket(),
                bot.getLocalAddress().getHostAddress(), bot.getSocket().getPort(), true);
        sslSocket.startHandshake();
        bot.changeSocket(sslSocket);
        //Notify CAP Handlers
        for (CapHandler curCapHandler : configuration.getCapHandlers())
            curCapHandler.handleUnknown(bot, rawLine);
    } else if (code.equals("CAP")) {
        //Handle CAP Code; remove extra from params
        String capCommand = parsedLine.get(1);
        ImmutableList<String> capParams = ImmutableList.copyOf(StringUtils.split(parsedLine.get(2)));
        if (capCommand.equals("LS"))
            for (CapHandler curCapHandler : configuration.getCapHandlers()) {
                log.debug("Executing cap handler " + curCapHandler);
                if (curCapHandler.handleLS(bot, capParams)) {
                    log.debug("Cap handler " + curCapHandler + " finished");
                    capHandlersFinished.add(curCapHandler);
                }
            }
        else if (capCommand.equals("ACK")) {
            //Server is enabling a capability, store that
            bot.getEnabledCapabilities().addAll(capParams);

            for (CapHandler curCapHandler : configuration.getCapHandlers())
                if (curCapHandler.handleACK(bot, capParams)) {
                    log.trace("Removing cap handler " + curCapHandler);
                    capHandlersFinished.add(curCapHandler);
                }
        } else if (capCommand.equals("NAK")) {
            for (CapHandler curCapHandler : configuration.getCapHandlers())
                if (curCapHandler.handleNAK(bot, capParams))
                    capHandlersFinished.add(curCapHandler);
        } else
            //Maybe the CapHandlers know how to use it
            for (CapHandler curCapHandler : configuration.getCapHandlers())
                if (curCapHandler.handleUnknown(bot, rawLine))
                    capHandlersFinished.add(curCapHandler);
    } else
        //Pass to CapHandlers, could be important
        for (CapHandler curCapHandler : configuration.getCapHandlers())
            if (curCapHandler.handleUnknown(bot, rawLine))
                capHandlersFinished.add(curCapHandler);

    //Send CAP END if all CapHandlers are finished
    if (configuration.isCapEnabled() && !capEndSent
            && capHandlersFinished.containsAll(configuration.getCapHandlers())) {
        capEndSent = true;
        bot.sendCAP().end();
        bot.enabledCapabilities = Collections.unmodifiableList(bot.enabledCapabilities);
    }
}

From source file:org.pircbotx.InputParser.java

/**
 * Process any lines relevant to connect. Only called before bot is logged
 * into the server/* w  ww . j  a v  a  2  s. c  o m*/
 *
 * @param rawLine Raw, unprocessed line from the server
 * @param code
 * @param target
 * @param parsedLine Processed line
 * @throws IrcException If the server rejects the bot (nick already in use
 * or a 4** or 5** code
 * @throws IOException If an error occurs during upgrading to SSL
 */
public void processConnect(String rawLine, String code, String target, List<String> parsedLine)
        throws IrcException, IOException {
    if (CONNECT_CODES.contains(code)) {
        // We're connected to the server.
        bot.onLoggedIn(parsedLine.get(0));
        log.debug("Logged onto server.");

        configuration.getListenerManager().dispatchEvent(new ConnectEvent(bot));

        //Handle automatic on connect stuff
        if (configuration.getNickservPassword() != null)
            bot.sendIRC().identify(configuration.getNickservPassword());
        ImmutableMap<String, String> autoConnectChannels = bot.reconnectChannels();
        if (autoConnectChannels == null)
            if (configuration.isNickservDelayJoin())
                autoConnectChannels = ImmutableMap.of();
            else
                autoConnectChannels = configuration.getAutoJoinChannels();
        for (Map.Entry<String, String> channelEntry : autoConnectChannels.entrySet())
            bot.sendIRC().joinChannel(channelEntry.getKey(), channelEntry.getValue());
    } else if (code.equals("439"))
        //EXAMPLE: PircBotX: Target change too fast. Please wait 104 seconds
        // No action required.
        //TODO: Should we delay joining channels here or something?
        log.warn("Ignoring too fast error");
    else if (configuration.isCapEnabled() && code.equals("421") && parsedLine.get(1).equals("CAP"))
        //EXAMPLE: 421 you CAP :Unknown command
        log.warn("Ignoring unknown command error, server does not support CAP negotiation");
    else if (configuration.isCapEnabled() && code.equals("451") && target.equals("CAP")) {
        //EXAMPLE: 451 CAP :You have not registered
        //Ignore, this is from servers that don't support CAP
        log.warn("Ignoring not registered error, server does not support CAP negotiation");
    } else if (configuration.isCapEnabled() && code.equals("410") && parsedLine.get(1).contains("CAP")) {
        //EXAMPLE: 410 :Invalid CAP command
        //Ignore, Twitch.tv uses this code for some reason
        log.warn("Ignoring invalid command error, server does not support CAP negotiation");
    } else if ((code.startsWith("5") || code.startsWith("4")) && !code.equals("433"))
        //Ignore 433 NickAlreadyInUse, handled later
        throw new IrcException(IrcException.Reason.CannotLogin, "Received error: " + rawLine);
    else if (code.equals("670")) {
        //Server is saying that we can upgrade to TLS
        log.debug("Upgrading to TLS connection");
        SSLSocketFactory sslSocketFactory = ((SSLSocketFactory) SSLSocketFactory.getDefault());
        for (CapHandler curCapHandler : configuration.getCapHandlers())
            if (curCapHandler instanceof TLSCapHandler)
                sslSocketFactory = ((TLSCapHandler) curCapHandler).getSslSocketFactory();
        SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(bot.getSocket(),
                bot.getLocalAddress().getHostAddress(), bot.getSocket().getPort(), true);
        sslSocket.startHandshake();
        bot.changeSocket(sslSocket);

        //Notify CAP Handlers
        for (CapHandler curCapHandler : configuration.getCapHandlers())
            if (curCapHandler.handleUnknown(bot, rawLine))
                addCapHandlerFinished(curCapHandler);
    } else if (code.equals("CAP") && configuration.isCapEnabled()) {
        //Handle CAP Code; remove extra from params
        String capCommand = parsedLine.get(1);
        ImmutableList<String> capParams = ImmutableList.copyOf(StringUtils.split(parsedLine.get(2)));
        if (capCommand.equals("LS")) {
            log.debug("Starting Cap Handlers {}", getCapHandlersRemaining());
            for (CapHandler curCapHandler : getCapHandlersRemaining()) {
                if (curCapHandler.handleLS(bot, capParams))
                    addCapHandlerFinished(curCapHandler);
            }
        } else if (capCommand.equals("ACK")) {
            //Server is enabling a capability, store that
            bot.getEnabledCapabilities().addAll(capParams);

            for (CapHandler curCapHandler : getCapHandlersRemaining())
                if (curCapHandler.handleACK(bot, capParams))
                    addCapHandlerFinished(curCapHandler);
        } else if (capCommand.equals("NAK")) {
            for (CapHandler curCapHandler : getCapHandlersRemaining())
                if (curCapHandler.handleNAK(bot, capParams))
                    addCapHandlerFinished(curCapHandler);
        } else {
            //Maybe the CapHandlers know how to use it
            for (CapHandler curCapHandler : getCapHandlersRemaining())
                if (curCapHandler.handleUnknown(bot, rawLine))
                    addCapHandlerFinished(curCapHandler);
        }
    } else
        //Pass to CapHandlers, could be important
        for (CapHandler curCapHandler : getCapHandlersRemaining())
            if (curCapHandler.handleUnknown(bot, rawLine))
                addCapHandlerFinished(curCapHandler);
}