Example usage for java.net URLConnection setReadTimeout

List of usage examples for java.net URLConnection setReadTimeout

Introduction

In this page you can find the example usage for java.net URLConnection setReadTimeout.

Prototype

public void setReadTimeout(int timeout) 

Source Link

Document

Sets the read timeout to a specified timeout, in milliseconds.

Usage

From source file:Opm_Package.New_CommitsInterval.java

public String callURL(String myURL) {
    //System.out.println("Requested URL:" + myURL);
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;//ww  w . ja  v  a2 s  . co  m
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null) {
            urlConn.setReadTimeout(60 * 1000);
        }
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }
        in.close();
    } catch (Exception e) {
        throw new RuntimeException("Exception while calling URL:" + myURL, e);
    }

    return sb.toString();
}

From source file:org.xbmc.httpapi.Connection.java

/**
 * Create a new URLConnection with the request headers set, including authentication.
 *
 * @param url The request url/*w  w  w . ja va 2  s.  c om*/
 * @return URLConnection
 * @throws IOException
 */
private URLConnection getUrlConnection(URL url) throws IOException {
    final URLConnection uc = url.openConnection();
    uc.setConnectTimeout(SOCKET_CONNECTION_TIMEOUT);
    uc.setReadTimeout(mSocketReadTimeout);
    uc.setRequestProperty("Connection", "close");

    if (authEncoded != null) {
        uc.setRequestProperty("Authorization", "Basic " + authEncoded);
    }

    return uc;
}

From source file:dk.dma.ais.downloader.QueryService.java

/**
 * Asynchronously loads the given file/*w w w . j ava2 s .c o  m*/
 * @param url the URL to load
 * @param path the path to save the file to
 */
private Future<Path> asyncLoadFile(final String url, final Path path) {
    Callable<Path> job = () -> {
        long t0 = System.currentTimeMillis();

        // For the resulting file, drop the ".download" suffix
        String name = path.getFileName().toString();
        name = name.substring(0, name.length() - DOWNLOAD_SUFFIX.length());

        try {

            // Set up a few timeouts and fetch the attachment
            URLConnection con = new URL(url).openConnection();
            con.setConnectTimeout(60 * 1000); // 1 minute
            con.setReadTimeout(60 * 60 * 1000); // 1 hour

            if (!StringUtils.isEmpty(authHeader)) {
                con.setRequestProperty("Authorization", authHeader);
            }

            try (ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
                    FileOutputStream fos = new FileOutputStream(path.toFile())) {
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }
            log.info(String.format("Copied %s -> %s in %d ms", url, path, System.currentTimeMillis() - t0));

        } catch (Exception e) {
            log.log(Level.SEVERE, "Failed downloading " + url + ": " + e.getMessage());

            // Delete the old file
            if (Files.exists(path)) {
                try {
                    Files.delete(path);
                } catch (IOException e1) {
                    log.finer("Failed deleting old file " + path);
                }
            }

            // Save an error file
            Path errorFile = path.getParent().resolve(name + ".err.txt");
            try (PrintStream err = new PrintStream(new FileOutputStream(errorFile.toFile()))) {
                e.printStackTrace(err);
            } catch (IOException ex) {
                log.finer("Failed generating error file " + errorFile);
            }
            return errorFile;
        }

        Path resultPath = path.getParent().resolve(name);
        try {
            Files.move(path, resultPath);
        } catch (IOException e) {
            log.log(Level.SEVERE, "Failed renaming path " + path + ": " + e.getMessage());
        }
        return resultPath;
    };

    log.info("Submitting new job: " + url);
    return processPool.submit(job);
}

From source file:org.geoserver.wps.executor.SimpleInputProvider.java

/**
 * Executes//from www.jav a  2 s  . c  o m
 * 
 * @param ref
 * @return
 */
Object executeRemoteRequest(InputReferenceType ref, ComplexPPIO ppio, String inputId) throws Exception {
    URL destination = new URL(ref.getHref());

    HttpMethod method = null;
    GetMethod refMethod = null;
    InputStream input = null;
    InputStream refInput = null;

    // execute the request
    try {
        if ("http".equalsIgnoreCase(destination.getProtocol())) {
            // setup the client
            HttpClient client = new HttpClient();
            // setting timeouts (30 seconds, TODO: make this configurable)
            HttpConnectionManagerParams params = new HttpConnectionManagerParams();
            params.setSoTimeout(executor.getConnectionTimeout());
            params.setConnectionTimeout(executor.getConnectionTimeout());
            // TODO: make the http client a well behaved http client, no more than x connections
            // per server (x admin configurable maybe), persistent connections and so on
            HttpConnectionManager manager = new SimpleHttpConnectionManager();
            manager.setParams(params);
            client.setHttpConnectionManager(manager);

            // prepare either a GET or a POST request
            if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) {
                GetMethod get = new GetMethod(ref.getHref());
                get.setFollowRedirects(true);
                method = get;
            } else {
                String encoding = ref.getEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }

                PostMethod post = new PostMethod(ref.getHref());
                Object body = ref.getBody();
                if (body == null) {
                    if (ref.getBodyReference() != null) {
                        URL refDestination = new URL(ref.getBodyReference().getHref());
                        if ("http".equalsIgnoreCase(refDestination.getProtocol())) {
                            // open with commons http client
                            refMethod = new GetMethod(ref.getBodyReference().getHref());
                            refMethod.setFollowRedirects(true);
                            client.executeMethod(refMethod);
                            refInput = refMethod.getResponseBodyAsStream();
                        } else {
                            // open with the built-in url management
                            URLConnection conn = refDestination.openConnection();
                            conn.setConnectTimeout(executor.getConnectionTimeout());
                            conn.setReadTimeout(executor.getConnectionTimeout());
                            refInput = conn.getInputStream();
                        }
                        post.setRequestEntity(new InputStreamRequestEntity(refInput, ppio.getMimeType()));
                    } else {
                        throw new WPSException("A POST request should contain a non empty body");
                    }
                } else if (body instanceof String) {
                    post.setRequestEntity(new StringRequestEntity((String) body, ppio.getMimeType(), encoding));
                } else {
                    throw new WPSException("The request body should be contained in a CDATA section, "
                            + "otherwise it will get parsed as XML instead of being preserved as is");

                }
                method = post;
            }
            // add eventual extra headers
            if (ref.getHeader() != null) {
                for (Iterator it = ref.getHeader().iterator(); it.hasNext();) {
                    HeaderType header = (HeaderType) it.next();
                    method.setRequestHeader(header.getKey(), header.getValue());
                }
            }
            int code = client.executeMethod(method);

            if (code == 200) {
                input = method.getResponseBodyAsStream();
            } else {
                throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error "
                        + code + ": " + method.getStatusText());
            }
        } else {
            // use the normal url connection methods then...
            URLConnection conn = destination.openConnection();
            conn.setConnectTimeout(executor.getConnectionTimeout());
            conn.setReadTimeout(executor.getConnectionTimeout());
            input = conn.getInputStream();
        }

        // actually parse teh data
        if (input != null) {
            return ppio.decode(input);
        } else {
            throw new WPSException("Could not find a mean to read input " + inputId);
        }
    } finally {
        // make sure to close the connection and streams no matter what
        if (input != null) {
            input.close();
        }
        if (method != null) {
            method.releaseConnection();
        }
        if (refMethod != null) {
            refMethod.releaseConnection();
        }
    }
}

From source file:org.botlibre.util.Utils.java

public static InputStream openStream(URL url, int timeout) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    connection.setConnectTimeout(timeout);
    connection.setReadTimeout(timeout);
    return connection.getInputStream();
}

From source file:com.intellij.util.net.HttpConfigurable.java

/**
 * todo [all] It is NOT nessesary to call anything if you obey common IDEA proxy settings;
 * todo if you want to define your own behaviour, refer to {@link com.intellij.util.proxy.CommonProxy}
 *
 * also, this method is useful in a way that it test connection to the host [through proxy]
 *
 * @param url URL for HTTP connection// www  . jav  a 2 s.  c o m
 * @throws IOException
 */
public void prepareURL(String url) throws IOException {
    //setAuthenticator();
    CommonProxy.isInstalledAssertion();

    final URLConnection connection = openConnection(url);
    try {
        connection.setConnectTimeout(3 * 1000);
        connection.setReadTimeout(3 * 1000);
        connection.connect();
        connection.getInputStream();
    } catch (Throwable e) {
        if (e instanceof IOException) {
            throw (IOException) e;
        }
    } finally {
        if (connection instanceof HttpURLConnection) {
            ((HttpURLConnection) connection).disconnect();
        }
    }
}

From source file:edu.ucuenca.authorsdisambiguation.nwd.NWD.java

public synchronized String Http(String s, String prefix) throws SQLException, IOException {

    String get = Cache.getInstance().get(prefix + s);
    String resp = "";
    if (get != null) {
        //System.out.print(".");
        resp = get;//  w  w w.  java 2s  .c o m
    } else {
        final URL url = new URL(s);
        final URLConnection connection = url.openConnection();
        connection.setConnectTimeout(60000);
        connection.setReadTimeout(60000);
        connection.addRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");
        connection.addRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8");
        while (reader.hasNextLine()) {
            final String line = reader.nextLine();
            resp += line + "\n";
        }
        reader.close();

        Cache.getInstance().put(prefix + s, resp);
    }

    return resp;
}

From source file:org.wattdepot.client.http.api.collector.NOAAWeatherCollector.java

@Override
public void run() {
    Measurement meas = null;/*from w w  w .  j  a v  a2  s . com*/
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = domFactory.newDocumentBuilder();

        // Have to make HTTP connection manually so we can set proper timeouts
        URL url = new URL(noaaWeatherUri);
        URLConnection httpConnection;
        httpConnection = url.openConnection();
        // Set both connect and read timeouts to 15 seconds. No point in long
        // timeouts since the
        // sensor will retry before too long anyway.
        httpConnection.setConnectTimeout(15 * 1000);
        httpConnection.setReadTimeout(15 * 1000);
        httpConnection.connect();

        // Record current time as close approximation to time for reading we are
        // about to make
        Date timestamp = new Date();

        Document doc = builder.parse(httpConnection.getInputStream());

        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        // path to the data
        String valString = "/current_observation/" + this.registerName + "/text()";
        XPathExpression exprValue = xPath.compile(valString);
        Object result = new Double(0);
        if (this.registerName.equals("weather")) {
            Object cloudCoverage = exprValue.evaluate(doc, XPathConstants.STRING);
            String cloudStr = cloudCoverage.toString();
            if (cloudStr.equalsIgnoreCase("sunny") || cloudStr.equalsIgnoreCase("clear")) {
                // 0 to 1/8 cloud coverage
                result = new Double(6.25);
            } else if (cloudStr.equalsIgnoreCase("mostly sunny") || cloudStr.equalsIgnoreCase("mostly clear")) {
                // 1/8 to 2/8 cloud coverage
                result = new Double(18.75);
            } else if (cloudStr.equalsIgnoreCase("partly sunny")
                    || cloudStr.equalsIgnoreCase("partly cloudy")) {
                // 3/8 to 5/8 cloud coverage
                result = new Double(50.0);
            } else if (cloudStr.equalsIgnoreCase("mostly cloudy")) {
                // 6/8 to 7/8 cloud coverage
                result = new Double(81.25);
            } else if (cloudStr.equalsIgnoreCase("cloudy")) {
                // 7/8 to 100% cloud coverage
                result = new Double(93.75);
            }
        } else {
            result = exprValue.evaluate(doc, XPathConstants.NUMBER);
        }

        Double value = (Double) result;
        meas = new Measurement(definition.getSensorId(), timestamp, value, measUnit);
    } catch (MalformedURLException e) {
        System.err.format("URI %s was invalid leading to malformed URL%n", noaaWeatherUri);
    } catch (XPathExpressionException e) {
        System.err.println("Bad XPath expression, this should never happen.");
    } catch (ParserConfigurationException e) {
        System.err.println("Unable to configure XML parser, this is weird.");
    } catch (SAXException e) {
        System.err.format("%s: Got bad XML from eGauge sensor %s (%s), hopefully this is temporary.%n",
                Tstamp.makeTimestamp(), sensor.getName(), e);
    } catch (IOException e) {
        System.err.format(
                "%s: Unable to retrieve data from eGauge sensor %s (%s), hopefully this is temporary.%n",
                Tstamp.makeTimestamp(), sensor.getName(), e);
    }

    if (meas != null) {
        try {
            this.client.putMeasurement(depository, meas);
        } catch (MeasurementTypeException e) {
            System.err.format("%s does not store %s measurements%n", depository.getName(),
                    meas.getMeasurementType());
        }
        if (debug) {
            System.out.println(meas);
        }
    }
}

From source file:net.milkbowl.vault.Vault.java

public double updateCheck(double currentVersion) {
    try {/*w  w  w. j a va  2s . co  m*/
        URL url = new URL("https://api.curseforge.com/servermods/files?projectids=33184");
        URLConnection conn = url.openConnection();
        conn.setReadTimeout(5000);
        conn.addRequestProperty("User-Agent", "Vault Update Checker");
        conn.setDoOutput(true);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        final String response = reader.readLine();
        final JSONArray array = (JSONArray) JSONValue.parse(response);

        if (array.size() == 0) {
            this.getLogger().warning("No files found, or Feed URL is bad.");
            return currentVersion;
        }
        // Pull the last version from the JSON
        newVersionTitle = ((String) ((JSONObject) array.get(array.size() - 1)).get("name")).replace("Vault", "")
                .trim();
        return Double.valueOf(newVersionTitle.replaceFirst("\\.", "").trim());
    } catch (Exception e) {
        log.info("There was an issue attempting to check for the latest version.");
    }
    return currentVersion;
}

From source file:at.gv.egiz.pdfas.lib.pki.impl.DefaultCertificateVerificationDataProvider.java

@Override
public CertificateVerificationData getCertificateVerificationData(
        java.security.cert.X509Certificate eeCertificate, ISettings settings)
        throws CertificateException, IOException {

    X509Certificate iaikEeCertificate = toIAIKX509Certificate(Objects.requireNonNull(eeCertificate));

    // @formatter:off
    final Set<java.security.cert.X509Certificate> certs = new LinkedHashSet<>(); // not thread-safe
    final List<byte[]> ocsps = new ArrayList<>(); // not thread-safe
    final Set<java.security.cert.X509CRL> crls = new LinkedHashSet<>(); // not thread-safe
    // @formatter:on

    StopWatch sw = new StopWatch();
    sw.start();/*w  w w  .  j  a v  a 2s.co  m*/

    if (log.isDebugEnabled()) {
        log.debug("Retrieving certificate validation info info for {}", iaikEeCertificate.getSubjectDN());
    } else if (log.isInfoEnabled()) {
        log.info("Retrieving certificate validation data for certificate (SHA-1 fingerprint): {}",
                Hex.encodeHexString(iaikEeCertificate.getFingerprintSHA()));
    }

    // retrieve certificate chain for eeCertificate
    X509Certificate[] caChainCertificates = retrieveChain(iaikEeCertificate, Objects.requireNonNull(settings));
    // build up full (sorted) chain including eeCertificate
    X509Certificate[] fullChainCertificates = Util.createCertificateChain(iaikEeCertificate,
            caChainCertificates);
    // add chain to certs list
    certs.addAll(Arrays.asList(fullChainCertificates));

    // determine revocation info, preferring OCSP
    // assume last certificate in chain is trust anchor
    OCSPClient ocspClient = OCSPClient.builder().setConnectTimeOutMillis(DEFAULT_CONNECTION_TIMEOUT_MS)
            .setSocketTimeOutMillis(DEFAULT_READ_TIMEOUT_MS).build();
    for (int i = 0; i < fullChainCertificates.length - 1; i++) {
        final X509Certificate subjectCertificate = fullChainCertificates[i];
        final X509Certificate issuerCertificate = fullChainCertificates[i + 1];
        OCSPResponse ocspResponse = null;
        if (OCSPClient.Util.hasOcspResponder(subjectCertificate)) {
            try {
                ocspResponse = ocspClient.getOcspResponse(issuerCertificate, subjectCertificate);
            } catch (Exception e) {
                log.info("Unable to retrieve OCSP response: {}", String.valueOf(e));
            }
        }

        if (ocspResponse != null) {

            ocsps.add(ocspResponse.getEncoded());

            // add ocsp signer certificate to certs
            // The currently used OCSP client support BasicOCSPResponse only, otherwise an exception would have been
            // thrown earlier. Therefore we can safely cast to BasicOCSPResponse here.
            X509Certificate ocspSignerCertificate = ((BasicOCSPResponse) ocspResponse.getResponse())
                    .getSignerCertificate();
            certs.add(ocspSignerCertificate);

        } else {

            // fall back to CRL

            CRLDistributionPoints cRLDistributionPoints;
            try {
                cRLDistributionPoints = (CRLDistributionPoints) subjectCertificate
                        .getExtension(CRLDistributionPoints.oid);
            } catch (X509ExtensionInitException e) {
                throw new IllegalStateException("Unable to initialize extension CRLDistributionPoints.", e);
            }
            X509CRL x509Crl = null;
            if (cRLDistributionPoints != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Retrieving CRL revocation info for: {}", subjectCertificate.getSubjectDN());
                } else if (log.isInfoEnabled()) {
                    log.info("Retrieving CRL revocation info for certificate (SHA-1 fingerprint): {}",
                            Hex.encodeHexString(subjectCertificate.getFingerprintSHA()));
                }

                Exception lastException = null;
                @SuppressWarnings("unchecked")
                Enumeration<DistributionPoint> e = cRLDistributionPoints.getDistributionPoints();
                while (e.hasMoreElements() && x509Crl == null) {
                    DistributionPoint distributionPoint = e.nextElement();

                    // inspect distribution point
                    if (distributionPoint.containsUriDpName()) {

                        String[] distributionPointNameURIs = distributionPoint.getDistributionPointNameURIs();
                        for (String distributionPointNameURI : distributionPointNameURIs) {
                            URL url;
                            try {
                                log.debug("Trying to download crl from distribution point: {}",
                                        distributionPointNameURI);
                                if (distributionPointNameURI.toLowerCase().startsWith("ldap://")) {
                                    url = new URL(null, distributionPointNameURI,
                                            new iaik.x509.net.ldap.Handler());
                                } else {
                                    url = new URL(distributionPointNameURI);
                                }
                                URLConnection urlConnection = url.openConnection();
                                urlConnection.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT_MS);
                                urlConnection.setReadTimeout(DEFAULT_READ_TIMEOUT_MS);
                                try (InputStream in = urlConnection.getInputStream()) {
                                    x509Crl = new X509CRL(in);
                                    // we got crl, exit loop
                                    break;
                                } catch (CRLException e1) {
                                    lastException = e1;
                                    log.debug("Unable to parse CRL read from distribution point: {} ({})",
                                            distributionPointNameURI, e1.getMessage());
                                }
                            } catch (MalformedURLException e1) {
                                log.debug("Unsupported CRL distribution point uri: {} ({})",
                                        distributionPointNameURI, e1.getMessage());
                                lastException = e1;
                            } catch (IOException e1) {
                                log.debug("Error reading from CRL distribution point uri: {} ({})",
                                        distributionPointNameURI, e1.getMessage());
                                lastException = e1;
                            } catch (Exception e1) {
                                log.debug("Unknown error reading from CRL distribution point uri: {} ({})",
                                        distributionPointNameURI, e1.getMessage());
                                lastException = e1;
                            }
                        }

                    }

                }
                if (x509Crl != null) {
                    crls.add(x509Crl);
                } else if (lastException != null) {
                    log.info("Unable to load CRL: {}", String.valueOf(lastException));
                }
            }

        }

    }
    sw.stop();
    log.debug("Querying certificate validation info took: {}ms", sw.getTime());

    return new CertificateVerificationData() {

        @Override
        public List<byte[]> getEncodedOCSPResponses() {
            return ocsps;
        }

        @Override
        public Set<java.security.cert.X509Certificate> getChainCerts() {
            return certs;
        }

        @Override
        public Set<java.security.cert.X509CRL> getCRLs() {
            return crls;
        }
    };
}