Example usage for java.net Socket connect

List of usage examples for java.net Socket connect

Introduction

In this page you can find the example usage for java.net Socket connect.

Prototype

public void connect(SocketAddress endpoint, int timeout) throws IOException 

Source Link

Document

Connects this socket to the server with a specified timeout value.

Usage

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

/**
 * Default public constructor./*from   w ww  .j  av  a2 s .co  m*/
 * @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();

    if (!"http".equals(url.getProtocol()) || !!"https".equals(url.getProtocol()))
        throw new HttpException("Not an HTTP 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) {
        port = 80;
        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());

        this.conf = http.getConf();

        this.htmlParseFilters = (HtmlParseFilter[]) PluginRepository.get(conf).getOrderedPlugins(
                HtmlParseFilter.class, HtmlParseFilter.X_POINT_ID, HtmlParseFilters.HTMLPARSEFILTER_ORDER);

        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()) {
            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 (datum.getModifiedTime() > 0) {
            reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(datum.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 (this.code == 200 && !url.toString().endsWith("robots.txt")) {
            readPlainContent(url);
        } else {
            readPlainContent(in);
        }

        try {
            byte[] decodeContent = null;
            String contentEncoding = getHeader(Response.CONTENT_ENCODING);
            if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
                decodeContent = http.processGzipEncoded(content, url);
            } else if ("deflate".equals(contentEncoding)) {
                decodeContent = http.processDeflateEncoded(content, url);
            } else {
                if (Http.LOG.isTraceEnabled()) {
                    Http.LOG.trace("fetched " + content.length + " bytes from " + url);
                }
            }
            if (decodeContent != null) {
                content = decodeContent;
            }
        } catch (Exception e) {
            headers.remove(Response.CONTENT_ENCODING);
        }
    } finally {
        if (socket != null)
            socket.close();
    }

}

From source file:com.example.will.sendpic.Camera2BasicFragment.java

private String sendFileGetResult(File file, String url, int port, int secTimeout) {
    Socket s = new Socket();
    String res = "Fail";

    try {/*from ww w  . ja v  a 2 s . com*/
        s.connect(new InetSocketAddress(url, port), secTimeout * 1000);
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        FileInputStream fis = new FileInputStream(file);
        byte[] sendBytes = new byte[1024 * 4];
        int len = 0;

        while ((len = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
            dos.write(sendBytes, 0, len);
            dos.flush();
        }
        s.shutdownOutput();

        //get the result from server
        //you can change the protocol
        InputStream in = s.getInputStream();
        byte[] result = new byte[1024];
        int num = in.read(result);

        res = new String(result, 0, num);
        System.out.println(res);

        //closing resources
        s.close();
        dos.close();
        fis.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}

From source file:com.syncedsynapse.kore2.jsonrpc.HostConnection.java

/**
 * Auxiliary method to open the TCP {@link Socket}.
 * This method calls connect() so that any errors are cathced
 * @param hostInfo Host info//  w ww. ja  v  a2s  .  c om
 * @return Connection set up
 * @throws ApiException
 */
private Socket openTcpConnection(HostInfo hostInfo) throws ApiException {
    try {
        LogUtils.LOGD(TAG, "Opening TCP connection on host: " + hostInfo.getAddress());

        Socket socket = new Socket();
        final InetSocketAddress address = new InetSocketAddress(hostInfo.getAddress(), hostInfo.getTcpPort());
        // We're setting a read timeout on the socket, so no need to explicitly close it
        socket.setSoTimeout(TCP_READ_TIMEOUT);
        socket.connect(address, connectTimeout);

        return socket;
    } catch (IOException e) {
        LogUtils.LOGW(TAG, "Failed to open TCP connection to host: " + hostInfo.getAddress());
        throw new ApiException(ApiException.IO_EXCEPTION_WHILE_CONNECTING, e);
    }
}

From source file:io.hops.hopsworks.api.util.CustomSSLProtocolSocketFactory.java

@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams httpConnectionParams)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (httpConnectionParams == null) {
        LOG.log(Level.SEVERE, "Creating SSL socket but HTTP connection parameters is null");
        throw new IllegalArgumentException("HTTP connection parameters cannot be null");
    }/*ww w  .j  a  v a 2 s.  co m*/

    Socket socket = getSslContext().getSocketFactory().createSocket();
    SocketAddress localSocketAddress = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteSocketAddress = new InetSocketAddress(host, port);

    socket.setSoTimeout(httpConnectionParams.getSoTimeout());
    if (httpConnectionParams.getLinger() > 0) {
        socket.setSoLinger(true, httpConnectionParams.getLinger());
    } else {
        socket.setSoLinger(false, 0);
    }
    socket.setTcpNoDelay(httpConnectionParams.getTcpNoDelay());
    if (httpConnectionParams.getSendBufferSize() >= 0) {
        socket.setSendBufferSize(httpConnectionParams.getSendBufferSize());
    }
    if (httpConnectionParams.getReceiveBufferSize() >= 0) {
        socket.setReceiveBufferSize(httpConnectionParams.getReceiveBufferSize());
    }

    socket.bind(localSocketAddress);
    socket.connect(remoteSocketAddress, httpConnectionParams.getConnectionTimeout());
    return socket;
}

From source file:org.apache.hadoop.hdfs.server.namenode.FileSystemProvider.java

/**
 * ?? ? ?? ? ?  ??  ?./*from   w w  w  . ja v  a 2 s. c  om*/
 *
 * @param blk LocatedBlock
 * @return ?? 
 */
public static DatanodeInfo bestNode(LocatedBlock blk) {
    TreeSet<DatanodeInfo> deadNodes = new TreeSet<>();
    DatanodeInfo chosenNode = null;
    int failures = 0;
    Socket socket = null;
    DatanodeInfo[] nodes = blk.getLocations();

    if (nodes == null || nodes.length == 0) {
        throw new ServiceException("No nodes contain this block");
    }

    while (socket == null) {
        if (chosenNode == null) {
            do {
                chosenNode = nodes[rand.nextInt(nodes.length)];
            } while (deadNodes.contains(chosenNode));
        }
        int index = rand.nextInt(nodes.length);
        chosenNode = nodes[index];

        //just ping to check whether the node is alive
        InetSocketAddress address = NetUtils
                .createSocketAddr(chosenNode.getIpAddr() + ":" + chosenNode.getInfoPort());

        try {
            socket = new Socket();
            socket.connect(address, HdfsServerConstants.READ_TIMEOUT);
            socket.setSoTimeout(HdfsServerConstants.READ_TIMEOUT);
        } catch (IOException e) {
            deadNodes.add(chosenNode);
            try {
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            socket = null;
            failures++;
        }

        if (failures == nodes.length) {
            throw new ServiceException("Could not reach the block containing the data. Please try again");
        }
    }
    try {
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return chosenNode;
}

From source file:com.googlecode.xremoting.core.commonshttpclient.ssl.AuthSSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>//w w w  .ja v  a2s.c o  m
 * To circumvent the limitations of older JREs that do not support connect timeout a
 * controller thread is executed. The controller thread attempts to create a new socket
 * within the given limit of time. If socket constructor does not return until the
 * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *
 * @param host the host name/IP
 * @param port the port on the host
 * @param localAddress the local host name/IP to bind the socket to
 * @param localPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 *
 * @return Socket a new socket
 *
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        Socket socket = socketfactory.createSocket(host, port, localAddress, localPort);
        doPreConnectSocketStuff(socket);
        return socket;
    } else {
        Socket socket = socketfactory.createSocket();
        doPreConnectSocketStuff(socket);
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);

        if (timeout > 0 && socket.getSoTimeout() == 0) {
            // force SO timeout if not set so we don't freeze forever
            // during a handshake
            socket.setSoTimeout(timeout);
        }

        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}

From source file:com.clavain.munin.MuninNode.java

public void run() {
    b_isRunning = true;/*from w ww . ja v a2 s .  co m*/

    if (this.str_via.equals("unset")) {
        logger.info(getHostname() + " Monitoring job started");
    } else {
        logger.info(getHostname() + " (VIA: " + this.str_via + ") Monitoring job started");
    }

    int iCurTime = getUnixtime();
    int iPluginRefreshTime = last_plugin_load + Integer.parseInt(p.getProperty("plugin.refreshtime"));
    try {
        // update plugins, maybe we have some new :)
        // double try to load plugins if fail

        if (getPluginList().size() > 0) {
            if (!is_init) {
                logger.info("[Job: " + getHostname() + "] Updating Database");
                // update graphs in database too
                for (MuninPlugin it_pl : getPluginList()) {
                    if (it_pl.getGraphs().size() > 0) {
                        //logger.info(it_pl.getPluginName());
                        dbUpdatePluginForNode(getNode_id(), it_pl);
                    }
                }
                // delete now missing plugins
                dbDeleteMissingPlugins(getNode_id(), getPluginList());
                logger.info("[Job: " + getHostname() + "] Databaseupdate Done");
                is_init = true;
            } else {
                if (iCurTime > iPluginRefreshTime) {
                    logger.info("Refreshing Plugins on " + this.getHostname());
                    this.loadPlugins();
                    dbUpdateAllPluginsForNode(this);
                }
            }
        } else {
            this.loadPlugins();
        }

        Socket clientSocket = new Socket();
        clientSocket.setSoTimeout(com.clavain.muninmxcd.socketTimeout);
        clientSocket.setKeepAlive(false);
        clientSocket.setReuseAddress(true);
        if (this.str_via.equals("unset")) {
            clientSocket.connect(new InetSocketAddress(this.getHostname(), this.getPort()),
                    com.clavain.muninmxcd.socketTimeout);
        } else {
            clientSocket.connect(new InetSocketAddress(this.getStr_via(), this.getPort()),
                    com.clavain.muninmxcd.socketTimeout);
        }
        lastSocket = clientSocket;
        SocketCheck sc = new SocketCheck(clientSocket, getUnixtime());
        if (p.getProperty("kill.sockets").equals("true")) {
            sc.setHostname(this.getHostname());
            com.clavain.muninmxcd.v_sockets.add(sc);
        }
        this.i_lastRun = getUnixtime();

        // track packages?
        if (this.track_pkg) {
            updateTrackPackages(clientSocket);
        }

        // gather essentials?
        if (this.essentials) {
            updateEssentials(clientSocket);
        }

        // update graphs for all plugins
        Iterator it = this.getLoadedPlugins().iterator();
        while (it.hasNext()) {
            MuninPlugin l_mp = (MuninPlugin) it.next();
            if (logMore) {
                logger.info(getHostname() + " fetching graphs for " + l_mp.getPluginName().toUpperCase());
            }
            // snmp support
            if (!str_via.equals("unset")) {
                l_mp.updateAllGraps(this.getStr_via(), this.getPort(), clientSocket, getQueryInterval());
            } else {
                l_mp.updateAllGraps(this.getHostname(), this.getPort(), clientSocket, getQueryInterval());
            }
            // add all graphs to insertion queue for mongodb
            queuePluginFetch(l_mp.returnAllGraphs(), l_mp.getPluginName());
        }
        clientSocket.close();

        if (p.getProperty("kill.sockets").equals("true")) {
            com.clavain.muninmxcd.v_sockets.remove(sc);
        }
        sc = null;
    } catch (Exception ex) {
        logger.fatal("Error in thread for host: " + getHostname() + " : " + ex.getLocalizedMessage());
        ex.printStackTrace();
    }
    int iRunTime = getUnixtime() - iCurTime;
    dbUpdateLastContact(this.getNode_id());
    logger.info(getHostname() + " Monitoring job stopped - runtime: " + iRunTime);

}

From source file:org.kepler.ddp.director.DDPEngine.java

/** Check if the DDP engine server is running. If not, try to start it.
 *  @param socketAddress Host and port of the server to check.
 *  @param startScriptStr The script to start the server if not running.
 *  @return True if a server was started, false if could connect to already running server. 
 *//*from w  w  w.j a  v  a  2s.  c  o m*/
protected boolean _checkServer(InetSocketAddress socketAddress, String startScriptStr)
        throws IllegalActionException {

    boolean startedServer = false;

    synchronized (_serverStartStopLock) {
        Socket socket = null;
        try {
            socket = new Socket();
            boolean connected = false;
            try {
                socket.connect(socketAddress, _CONNECT_TIMEOUT);
                connected = true;
            } catch (IOException e) {

                System.out.println(_engineName + " server " + socketAddress
                        + " does not appear to be running. Starting...");

                // start the server

                if (!_checkFilesBeforeStartingServer()) {
                    throw new IllegalActionException(_director,
                            "One or more files required to start the server were not found.");
                }

                // see if the script is executable. kepler modules are zipped,
                // which does not preserve the permissions.
                File startScriptFile = new File(startScriptStr);
                if (!startScriptFile.canExecute()) {
                    throw new IllegalActionException(_director,
                            "The script " + startScriptFile + " is not executable.\n"
                                    + "You must change the permissions so that " + startScriptFile.getName()
                                    + " and all the other scripts in \n" + startScriptFile.getParent()
                                    + " are executable.");
                }

                ProcessBuilder builder = new ProcessBuilder(startScriptStr);

                // make sure JAVA_HOME is set
                java.util.Map<String, String> env = builder.environment();
                if (env.get("JAVA_HOME") == null) {
                    env.put("JAVA_HOME", System.getProperty("java.home"));
                }

                builder.redirectErrorStream(true);

                try {
                    Process process = builder.start();
                    InetSocketAddress newAddress = _parseOutputFromStartingServer(process.getInputStream());
                    if (newAddress != null) {
                        socketAddress = newAddress;
                    }
                    process.waitFor();
                    startedServer = true;
                } catch (Exception e1) {
                    throw new IllegalActionException(_director, e1,
                            "Unable to start " + _engineName + " server.");
                }

                int tries = 0;
                while (tries < 5) {
                    // wait for the server to start
                    try {
                        Thread.sleep(5000);
                        tries++;
                        System.out.print("Connecting to " + _engineName + " server port try #" + tries + ": ");
                        try {
                            socket.close();
                            socket = new Socket();
                            socket.connect(socketAddress, _CONNECT_TIMEOUT);
                            connected = true;
                            System.out.println("connected.");
                            break;
                        } catch (IOException e1) {
                            // do nothing
                            System.out.println(e1);
                        }
                    } catch (InterruptedException e2) {
                        throw new IllegalActionException(_director, e2, "Error while sleeping.");
                    }
                }

                // if we get here, we were able to connect to the master/job manager port.
                // however, the server may not be completely initialized, so wait a few more seconds
                System.out.println("Waiting 15 seconds for " + _engineName + " server to initialize.");
                try {
                    Thread.sleep(15000);
                } catch (InterruptedException e2) {
                    throw new IllegalActionException(_director, e2,
                            "Error while waiting " + " for " + _engineName + " server to initialize.");
                }

            }

            if (connected) {
                try {
                    socket.close();
                    socket = null;
                } catch (IOException e) {
                    throw new IllegalActionException(_director, e, "Error closing socket.");
                }
            } else {
                throw new IllegalActionException(_director,
                        "Could not connect to " + _engineName + " server: " + socketAddress);
            }
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new IllegalActionException(_director, e, "Error closing socket.");
                }
            }
        }
    }

    return startedServer;
}

From source file:org.archive.modules.fetcher.HeritrixSSLProtocolSocketFactory.java

public synchronized Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException, UnknownHostException {
    // Below code is from the DefaultSSLProtocolSocketFactory#createSocket
    // method only it has workarounds to deal with pre-1.4 JVMs.  I've
    // cut these out.
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }//from   w  ww .j a  va  2 s .  c om
    Socket socket = null;
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        socket = createSocket(host, port, localAddress, localPort);
    } else {
        SSLSocketFactory factory = (SSLSocketFactory) params.getParameter(FetchHTTP.SSL_FACTORY_KEY);
        SSLSocketFactory f = (factory != null) ? factory : this.sslDefaultFactory;
        socket = f.createSocket();

        Thread current = Thread.currentThread();
        InetAddress hostAddress;
        if (current instanceof HostResolver) {
            HostResolver resolver = (HostResolver) current;
            hostAddress = resolver.resolve(host);
        } else {
            hostAddress = null;
        }
        InetSocketAddress address = (hostAddress != null) ? new InetSocketAddress(hostAddress, port)
                : new InetSocketAddress(host, port);
        socket.bind(new InetSocketAddress(localAddress, localPort));
        try {
            socket.connect(address, timeout);
        } catch (SocketTimeoutException e) {
            // Add timeout info. to the exception.
            throw new SocketTimeoutException(
                    e.getMessage() + ": timeout set at " + Integer.toString(timeout) + "ms.");
        }
        assert socket.isConnected() : "Socket not connected " + host;
    }
    return socket;
}

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

/**
 * Default public constructor./*from  w w  w . j a  v  a  2  s  .c  o  m*/
 *
 * @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();
    }

}