Example usage for java.net HttpURLConnection getContentLengthLong

List of usage examples for java.net HttpURLConnection getContentLengthLong

Introduction

In this page you can find the example usage for java.net HttpURLConnection getContentLengthLong.

Prototype

public long getContentLengthLong() 

Source Link

Document

Returns the value of the content-length header field as a long.

Usage

From source file:net.nikr.eve.jeveasset.io.online.CitadelGetter.java

private void updateCache(UpdateTask updateTask) {
    LOG.info("Citadels updating:");
    if (citadelSettings.getNextUpdate().after(new Date()) && !Settings.get().isForceUpdate()
            && !Program.isForceUpdate()) { //Check if we can update now
        if (updateTask != null) {
            updateTask.addError(DialoguesUpdate.get().citadel(), "Not allowed yet.\r\n(Fix: Just wait a bit)");
        }//ww  w  .  j a v a  2  s .c om
        LOG.info("   Citadels failed to update (NOT ALLOWED YET)");
        return;
    }
    //Update citadel
    InputStream in = null;
    try { //Update from API
        ObjectMapper mapper = new ObjectMapper(); //create once, reuse
        URL url = new URL(URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("Accept-Encoding", "gzip");

        long contentLength = con.getContentLengthLong();
        String contentEncoding = con.getContentEncoding();
        InputStream inputStream = new UpdateTaskInputStream(con.getInputStream(), contentLength, updateTask);
        if ("gzip".equals(contentEncoding)) {
            in = new GZIPInputStream(inputStream);
        } else {
            in = inputStream;
        }
        Map<Long, Citadel> results = mapper.readValue(in, new TypeReference<Map<Long, Citadel>>() {
        });
        if (results != null) { //Updated OK
            for (Map.Entry<Long, Citadel> entry : results.entrySet()) {
                citadelSettings.put(entry.getKey(), entry.getValue());
            }
        }
        citadelSettings.setNextUpdate();
        saveXml();
        LOG.info("   Updated citadels for jEveAssets");
    } catch (IOException ex) {
        if (updateTask != null) {
            updateTask.addError(DialoguesUpdate.get().citadel(), ex.getMessage());
        }
        LOG.error("   Citadels failed to update", ex);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                //No problem...
            }
        }
    }
}

From source file:eu.creatingfuture.propeller.webLoader.servlets.WebRequestServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.addHeader("Access-Control-Allow-Origin", "*");
    String getUrl = req.getParameter("get");
    if (getUrl == null) {
        resp.sendError(400);//ww  w  .  j  a  v  a 2  s .c o  m
        return;
    }
    logger.info(getUrl);
    URL url;
    HttpURLConnection connection = null;
    try {
        url = new URL(getUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);

        resp.setContentType(connection.getContentType());
        resp.setContentLengthLong(connection.getContentLengthLong());
        logger.log(Level.INFO, "Content length: {0} response code: {1} content type: {2}", new Object[] {
                connection.getContentLengthLong(), connection.getResponseCode(), connection.getContentType() });
        resp.setStatus(connection.getResponseCode());
        IOUtils.copy(connection.getInputStream(), resp.getOutputStream());
    } catch (IOException ioe) {
        resp.sendError(500, ioe.getMessage());
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:mSearch.filmlisten.FilmlisteLesen.java

private InputStream getInputStreamForLocation(String source) throws IOException, URISyntaxException {
    InputStream in;//from   w ww .j  ava2 s .  c o m
    long size = 0;
    final URI uri;
    if (source.startsWith("http")) {
        uri = new URI(source);
        //remote address for internet download
        HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
        conn.setRequestProperty("User-Agent", Config.getUserAgent());
        if (conn.getResponseCode() < 400) {
            size = conn.getContentLengthLong();
        }
        in = new ProgressMonitorInputStream(conn.getInputStream(), size, new InputStreamProgressMonitor() {
            private int oldProgress = 0;

            @Override
            public void progress(long bytesRead, long size) {
                final int iProgress = (int) (bytesRead * 100 / size);
                if (iProgress != oldProgress) {
                    oldProgress = iProgress;
                    notifyProgress(uri.toASCIIString(), "Download", iProgress);
                }
            }
        });
    } else {
        //local file
        notifyProgress(source, "Download", PROGRESS_MAX);
        in = new FileInputStream(source);
    }

    return in;
}

From source file:com.anritsu.mcrepositorymanager.utils.Packing.java

public long getPackageSize(McPackage p) {
    HttpURLConnection conn = null;
    long result = 0;
    try {/*from  ww w. ja v a  2  s  .c  o m*/
        for (String link : p.getDownloadLinks()) {
            System.out.println("Checking package size for: " + link);
            URL url = new URL(link);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("HEAD");
            conn.getInputStream();
            result += conn.getContentLengthLong();
        }
        return result;

    } catch (MalformedURLException ex) {
        Logger.getLogger(Packing.class.getName()).log(Level.SEVERE, null, ex);
        logDeadLink(p, ex);
    } catch (IOException ex) {
        Logger.getLogger(Packing.class.getName()).log(Level.SEVERE, null, ex);
        logDeadLink(p, ex);
    } finally {
        try {
            conn.disconnect();
        } catch (Exception exp) {
            System.out.println("Error while disconnecting!");
        }
    }
    return 0;
}

From source file:msearch.filmlisten.MSFilmlisteLesen.java

private InputStream getInputStreamForLocation(String source) throws Exception {
    InputStream in;//from  w  ww.  ja v a 2 s .  c o m
    long size = 0;
    final URI uri;
    if (source.startsWith("http")) {
        uri = new URI(source);
        //remote address for internet download
        HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
        conn.setRequestProperty("User-Agent", MSConfig.getUserAgent());
        if (conn.getResponseCode() < 400) {
            size = conn.getContentLengthLong();
        }
        in = new SizeInputStream(conn.getInputStream(), size, uri.toASCIIString());
    } else {
        //local file
        notifyProgress(source, "Download", PROGRESS_MAX);
        in = new FileInputStream(source);
    }

    return in;
}

From source file:com.jwrapper.maven.java.JavaDownloadMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {//from ww  w  .j a v  a2  s .com

        setupNonVerifingSSL();

        final String javaRemoteURL = javaRemoteURL();
        final String javaLocalURL = javaLocalURL();

        logger().info("javaRemoteURL: {}", javaRemoteURL);
        logger().info("javaLocalURL : {}", javaLocalURL);

        final File file = new File(javaLocalURL());

        if (!javaEveryTime() && file.exists()) {
            logger().info("Java artifact is present, skip download.");
            return;
        } else {
            logger().info("Java artifact is missing, make download.");
        }

        file.getParentFile().mkdirs();

        /** Oracle likes redirects. */
        HttpURLConnection connection = connection(javaRemoteURL());
        while (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
            connection = connection(connection.getHeaderField("Location"));
            logger().info("redirect: {}", connection);
        }

        final ProgressInputStream input = new ProgressInputStream(connection.getInputStream(),
                connection.getContentLengthLong());

        final PropertyChangeListener listener = new PropertyChangeListener() {
            long current = System.currentTimeMillis();

            @Override
            public void propertyChange(final PropertyChangeEvent event) {
                if (System.currentTimeMillis() - current > 1000) {
                    current = System.currentTimeMillis();
                    logger().info("progress: {}", event.getNewValue());
                }
            }
        };

        input.addPropertyChangeListener(listener);

        final OutputStream output = new FileOutputStream(file);

        IOUtils.copy(input, output);

        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);

        if (file.length() < 1000 * 1000) {
            throw new IllegalStateException("Download failure.");
        }

        logger().info("Java artifact downloaded: {} bytes.", file.length());

    } catch (final Throwable e) {
        logger().error("", e);
        throw new MojoExecutionException("", e);
    }
}

From source file:org.transitime.utils.HttpGetFile.java

/**
 * Actually gets and stores the file. The User-Agency property is always set
 * to USER_AGENT./*from   w  w  w  . j ava2s  . c om*/
 * 
 * @return The http response code such as HttpStatus.SC_OK
 * @throws IOException
 */
public int getFile() throws IOException {
    IntervalTimer timer = new IntervalTimer();

    logger.debug("Getting URL={}", urlStr);
    URL url = new URL(urlStr);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agency", USER_AGENT);

    // Set request properties
    for (int i = 0; i < headerKeys.size(); ++i) {
        connection.setRequestProperty(headerKeys.get(i), headerValues.get(i));
    }

    // Get and log response code
    int responseCode = connection.getResponseCode();
    long expectedContentLength = connection.getContentLengthLong();
    long remoteFileLastModified = connection.getLastModified();
    logger.debug(
            "Response code for getting file {} is {} and file size "
                    + "is {} bytes and remote file lastModified=\"{}\" or {} msec",
            urlStr, responseCode, expectedContentLength, Time.httpDate(remoteFileLastModified),
            remoteFileLastModified);

    // Open file for where results are to be written
    File file = new File(fullFileNameForResult);

    // If file could not be read in or is not newer that lastModified time
    // of the existing file on the server then don't need to continue
    // reading it in.
    if (responseCode != HttpStatus.SC_OK) {
        logger.debug("Response code was {} so not reading in file", responseCode);
        return responseCode;
    }

    // Sometimes a web server will return http status OK (200) even
    // when the remote file is older than the time set for If-Modified-Since
    // header. For this situation still don't want to read in the file
    // so simply return http status NO_MODIFIED (304).
    if (file.lastModified() > 0 && remoteFileLastModified < file.lastModified()) {
        logger.warn("Response code was {} but the local file was modified "
                + "after the remote file so it must be up to date. " + "Therefore remote file not read in.",
                responseCode);
        return HttpStatus.SC_NOT_MODIFIED;
    }

    logger.debug(
            "Actually reading data from URL {} . Local file "
                    + "lastModified={} or {} msec and remoteFileLastModified={} " + "or {} msec.",
            urlStr, Time.httpDate(file.lastModified()), file.lastModified(),
            Time.httpDate(remoteFileLastModified), remoteFileLastModified);

    // Make sure output directory exists
    file.getParentFile().mkdirs();

    // Open input stream for reading data
    InputStream in = connection.getInputStream();

    // Open the stream
    FileOutputStream fos = new FileOutputStream(file);

    IntervalTimer loopTimer = new IntervalTimer();
    long lengthSinceLoggingMsg = 0;

    // Copy contents to file
    byte[] buffer = new byte[4096];
    int length;
    int totalLength = 0;
    while ((length = in.read(buffer)) > 0) {
        fos.write(buffer, 0, length);
        totalLength += length;
        lengthSinceLoggingMsg += length;

        // Every once in a while log progress. Don't want to
        // check timer every loop since that would be expensive.
        // So only check timer for every MB downloaded.
        if (lengthSinceLoggingMsg > 1024 * 1024) {
            lengthSinceLoggingMsg = 0;
            if (loopTimer.elapsedMsec() > 10 * Time.MS_PER_SEC) {
                loopTimer.resetTimer();
                logger.debug("Read in {} bytes or {}% of file {}", totalLength,
                        StringUtils.oneDigitFormat(100.0 * totalLength / expectedContentLength), urlStr);
            }
        }
    }

    // Close things up
    fos.close();

    // Set the last modified time so that it is the same as on the 
    // web server. 
    file.setLastModified(connection.getLastModified());

    if (totalLength == expectedContentLength)
        logger.debug("Successfully copied {} to file {}. Length was {} " + "bytes. Took {} msec.", urlStr,
                fullFileNameForResult, totalLength, timer.elapsedMsec());
    else
        logger.error("When copying {} to file {} the expected length was " + "{} but only copied {} bytes",
                urlStr, fullFileNameForResult, expectedContentLength, totalLength);

    // Return the http response code such as 200 for OK or 304 for 
    // Not Modified
    return connection.getResponseCode();
}

From source file:com.eucalyptus.tokens.oidc.OidcDiscoveryCache.java

protected OidcResource resolve(final HttpURLConnection conn) throws IOException {
    SslSetup.configureHttpsUrlConnection(conn);
    try (final InputStream istr = conn.getInputStream()) {
        final int statusCode = conn.getResponseCode();
        if (statusCode == 304) {
            return new OidcResource(statusCode);
        } else {/*from   w w w. j  av a  2  s .com*/
            Certificate[] certs = new Certificate[0];
            if (conn instanceof HttpsURLConnection) {
                certs = ((HttpsURLConnection) conn).getServerCertificates();
            }
            final long contentLength = conn.getContentLengthLong();
            if (contentLength > MAX_LENGTH) {
                throw new IOException(conn.getURL() + " content exceeds maximum size, " + MAX_LENGTH);
            }
            final byte[] content = ByteStreams.toByteArray(new BoundedInputStream(istr, MAX_LENGTH + 1));
            if (content.length > MAX_LENGTH) {
                throw new IOException(conn.getURL() + " content exceeds maximum size, " + MAX_LENGTH);
            }
            return new OidcResource(statusCode, conn.getHeaderField(HttpHeaders.LAST_MODIFIED),
                    conn.getHeaderField(HttpHeaders.ETAG), certs, content);
        }
    }
}

From source file:sh.isaac.api.util.DownloadUnzipTask.java

/**
 * Download.//from  ww w  .  j  av  a  2  s  .c  o  m
 *
 * @param url the url
 * @return the file
 * @throws Exception the exception
 */
private File download(URL url) throws Exception {
    LOG.debug("Beginning download from " + url);
    updateMessage("Download from " + url);

    final HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    if (StringUtils.isNotBlank(this.username) || StringUtils.isNotBlank(this.psswrd)) {
        final String encoded = Base64.getEncoder()
                .encodeToString((this.username + ":" + this.psswrd).getBytes());

        httpCon.setRequestProperty("Authorization", "Basic " + encoded);
    }

    httpCon.setDoInput(true);
    httpCon.setRequestMethod("GET");
    httpCon.setConnectTimeout(30 * 1000);
    httpCon.setReadTimeout(60 * 60 * 1000);

    final long fileLength = httpCon.getContentLengthLong();
    String temp = url.toString();

    temp = temp.substring(temp.lastIndexOf('/') + 1, temp.length());

    final File file = new File(this.targetFolder, temp);

    try (InputStream in = httpCon.getInputStream(); FileOutputStream fos = new FileOutputStream(file);) {
        final byte[] buf = new byte[1048576];
        int read = 0;
        long totalRead = 0;

        while (!this.cancel && (read = in.read(buf, 0, buf.length)) > 0) {
            totalRead += read;

            // update every 1 MB
            updateProgress(totalRead, fileLength);

            final float percentDone = ((int) (((float) totalRead / (float) fileLength) * 1000)) / 10f;

            updateMessage(
                    "Downloading - " + url + " - " + percentDone + " % - out of " + fileLength + " bytes");
            fos.write(buf, 0, read);
        }
    }

    if (this.cancel) {
        LOG.debug("Download cancelled");
        throw new Exception("Cancelled!");
    } else {
        LOG.debug("Download complete");
    }

    return file;
}

From source file:org.nuxeo.http.blobprovider.HttpBlobProvider.java

/**
 * Sends a HEAD request to get the info without downloading the file.
 * <p>/*ww w .  j  a v  a2 s .  c  om*/
 * If an error occurs, returns null.
 *
 * @param urlStr
 * @return the BlobInfo
 * @since 8.1
 */
public BlobInfo guessInfosFromURL(String urlStr) {

    BlobInfo bi = null;
    String attrLowerCase;
    try {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        addHeaders(connection, urlStr);

        connection.setRequestMethod("HEAD");
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {

            bi = new BlobInfo();

            bi.mimeType = connection.getContentType();
            // Remove possible ...;charset="something"
            int idx = bi.mimeType.indexOf(";");
            if (idx >= 0) {
                bi.mimeType = bi.mimeType.substring(0, idx);
            }

            bi.encoding = connection.getContentEncoding();
            bi.length = connection.getContentLengthLong();
            if (bi.length < 0) {
                bi.length = 0L;
            }

            String disposition = connection.getHeaderField("Content-Disposition");
            String fileName = null;
            if (disposition != null) {
                String[] attributes = disposition.split(";");

                for (String attr : attributes) {
                    attrLowerCase = attr.toLowerCase();
                    if (attrLowerCase.contains("filename=")) {
                        attr = attr.trim();
                        // Remove filename=
                        fileName = attr.substring(9);
                        idx = fileName.indexOf("\"");
                        if (idx > -1) {
                            fileName = fileName.substring(idx + 1, fileName.lastIndexOf("\""));
                        }
                        bi.filename = fileName;
                        break;

                    } else if (attrLowerCase.contains("filename*=utf-8''")) {
                        attr = attr.trim();
                        // Remove filename=
                        fileName = attr.substring(17);
                        idx = fileName.indexOf("\"");
                        if (idx > -1) {
                            fileName = fileName.substring(idx + 1, fileName.lastIndexOf("\""));
                        }
                        fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
                        bi.filename = fileName;
                        break;
                    }
                }
            } else {
                // Try from the url
                idx = urlStr.lastIndexOf("/");
                if (idx > -1) {
                    fileName = urlStr.substring(idx + 1);
                    bi.filename = java.net.URLDecoder.decode(fileName, "UTF-8");
                }
            }
        }

    } catch (Exception e) { // Whatever the error, we fail. No need to be
        // granular here.
        bi = null;
    }

    return bi;
}