Example usage for java.net URLConnection getHeaderField

List of usage examples for java.net URLConnection getHeaderField

Introduction

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

Prototype

public String getHeaderField(int n) 

Source Link

Document

Returns the value for the n th header field.

Usage

From source file:net.seedboxer.sources.processor.QueueProcessor.java

private String downloadFile(URL url, String path) throws IOException {
    final URLConnection conn = url.openConnection();

    String disposition = conn.getHeaderField("Content-Disposition");
    String fileNameProperty = "filename=\"";
    String fileName = disposition.substring(disposition.indexOf(fileNameProperty),
            disposition.lastIndexOf("\""));
    fileName = fileName.substring(fileNameProperty.length(), fileName.length());
    path += File.separator + fileName;

    FileUtils.copyFile(conn.getInputStream(), path, true, true);

    return fileName;
}

From source file:org.mycore.common.content.MCRURLContent.java

@Override
public String getETag() throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.connect();//from   w  w w.  j a  v  a  2  s  .c o m
    String eTag = openConnection.getHeaderField("ETag");
    if (eTag != null) {
        return eTag;
    }
    long lastModified = openConnection.getLastModified();
    long length = openConnection.getContentLengthLong();
    eTag = getSimpleWeakETag(url.toString(), length, lastModified);
    return eTag == null ? null : eTag.substring(2);
}

From source file:io.druid.segment.realtime.firehose.HttpFirehoseFactory.java

@JsonCreator
public HttpFirehoseFactory(@JsonProperty("uris") List<URI> uris,
        @JsonProperty("maxCacheCapacityBytes") Long maxCacheCapacityBytes,
        @JsonProperty("maxFetchCapacityBytes") Long maxFetchCapacityBytes,
        @JsonProperty("prefetchTriggerBytes") Long prefetchTriggerBytes,
        @JsonProperty("fetchTimeout") Long fetchTimeout, @JsonProperty("maxFetchRetry") Integer maxFetchRetry)
        throws IOException {
    super(maxCacheCapacityBytes, maxFetchCapacityBytes, prefetchTriggerBytes, fetchTimeout, maxFetchRetry);
    this.uris = uris;

    Preconditions.checkArgument(uris.size() > 0, "Empty URIs");
    final URLConnection connection = uris.get(0).toURL().openConnection();
    final String acceptRanges = connection.getHeaderField(HttpHeaders.ACCEPT_RANGES);
    this.supportContentRange = acceptRanges != null && acceptRanges.equalsIgnoreCase("bytes");
}

From source file:com.seedboxer.seedboxer.sources.processors.QueueProcessor.java

private String downloadFile(URL url, String path) throws IOException {
    URLConnection conn = url.openConnection();
    InputStream in = conn.getInputStream();
    String disposition = conn.getHeaderField("Content-Disposition");
    String fileNameProperty = "filename=\"";
    String fileName = disposition.substring(disposition.indexOf(fileNameProperty),
            disposition.lastIndexOf("\""));
    fileName = fileName.substring(fileNameProperty.length(), fileName.length());
    path += File.separator + fileName;
    File file = new File(path);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    byte[] bytes = new byte[1024];
    int read;//  ww  w .j a va  2 s.  c  om
    while ((read = in.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }
    in.close();
    out.close();
    file.setReadable(true, false);
    file.setWritable(true, false);
    return fileName;
}

From source file:edu.kit.dama.ui.simon.impl.WebServerProbe.java

@Override
public boolean checkProbe() {
    try {//from  www. j  a va2 s . co m
        URLConnection con = serverUrl.openConnection();
        con.setConnectTimeout(timeout);
        con.connect();
        String header0 = con.getHeaderField(0);
        return header0 != null && header0.endsWith("200 OK");
    } catch (IOException ex) {
        LOGGER.error("Failed to check Web server probe", ex);
    }
    return false;
}

From source file:de.gesundkrank.wikipedia.hadoop.io.WikiDumpLoader.java

/**
 * Return last change of the latest online dump of the given language
 *
 * @param locale Language of wikidump//from www.  j a  v a2  s  .c  o m
 * @return
 */
private long checkNewDump(Locale locale) throws IOException {
    try {
        String localeDumpUrl = String.format(DUMP_URL, locale.getLanguage(), locale.getLanguage());
        URLConnection connection = new URL(localeDumpUrl).openConnection();
        String lastModified = connection.getHeaderField("Last-Modified");
        return new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).parse(lastModified).getTime();
    } catch (ParseException e) {
        throw new IOException(e);
    }
}

From source file:io.github.microcks.task.ImportServiceDefinitionTask.java

/**
 * Extract URL Etag in order to avoid a reimport on unchanged resource.
 * @param repositoryUrl The URL to test as
 * @return The etag or null if none.// w  w w .j  a  v  a 2s  .c  o m
 */
private String getRepositoryUrlEtag(String repositoryUrl) {
    Authenticator.setDefault(new UsernamePasswordAuthenticator(username, password));
    try {
        URLConnection connection = new URL(repositoryUrl).openConnection();
        // Try simple syntax.
        String etag = connection.getHeaderField("Etag");
        if (etag != null) {
            log.debug("Found an Etag for " + repositoryUrl + ": " + etag);
            return etag;
        }
        // Try other syntax.
        etag = connection.getHeaderField("ETag");
        if (etag != null) {
            log.debug("Found an ETag for " + repositoryUrl + ": " + etag);
            return etag;
        }
    } catch (Exception e) {
        log.error("Caught an exception while retrieving Etag for " + repositoryUrl, e);
    }
    log.debug("No Etag found for " + repositoryUrl + " !");
    return null;
}

From source file:com.xeiam.xchange.rest.HttpTemplate.java

/**
 * Determine the response encoding if specified
 * //from   ww  w .j  a  va2  s . c  om
 * @param connection The HTTP connection
 * @return The response encoding as a string (taken from "Content-Type")
 */
private String getResponseEncoding(URLConnection connection) {

    String charset = null;

    String contentType = connection.getHeaderField("Content-Type");
    if (contentType != null) {
        for (String param : contentType.replace(" ", "").split(";")) {
            if (param.startsWith("charset=")) {
                charset = param.split("=", 2)[1];
                break;
            }
        }
    }
    return charset;
}

From source file:org.niord.core.mail.CachedUrlDataSource.java

/**
 * Checks if the data is cached. Otherwise the URL data is loaded and cached
 * @return the URL data/*from   w ww  .j  a v a2 s.  c o  m*/
 */
protected synchronized CachedUrlData loadData() {
    // Check if the attachment has been loaded already
    if (data != null) {
        return data;
    }

    // Check if the attachment is stored in the global attachment cache
    if (cache != null && cache.containsKey(url)) {
        data = cache.get(url);
        return data;
    }

    data = new CachedUrlData();
    data.setUrl(url);
    try {
        // Resolve the name from the URL path
        String name = url.getPath();
        if (name.contains("/")) {
            name = name.substring(name.lastIndexOf("/") + 1);
        }
        data.setName(name);

        URLConnection urlc = url.openConnection();

        // give it 15 seconds to respond
        urlc.setReadTimeout(15 * 1000);

        // Fetch the content type from the header
        data.setContentType(urlc.getHeaderField("Content-Type"));

        // Load the content
        try (InputStream in = urlc.getInputStream()) {
            data.setContent(IOUtils.toByteArray(in));
        }
    } catch (Exception ex) {
        // We don't really want to fail the mail if an attachment fails...
        log.warn("Silently failed loading mail attachment from " + url);
    }

    // Cache the result
    if (cache != null) {
        cache.put(url, data);
    }

    return data;
}

From source file:com.amastigote.xdu.query.module.WaterAndElectricity.java

private void preLogin() throws IOException {
    URL url = new URL(HOST + PRE_LOGIN_SUFFIX);
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();/*from   w  w w  .  java  2 s  .  co m*/
    String tmp = urlConnection.getHeaderField("Set-Cookie");

    ASP_dot_NET_SessionId = tmp.substring(tmp.indexOf("=") + 1, tmp.indexOf(";"));
}