Example usage for org.apache.commons.httpclient.methods HeadMethod getResponseHeader

List of usage examples for org.apache.commons.httpclient.methods HeadMethod getResponseHeader

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods HeadMethod getResponseHeader.

Prototype

@Override
public Header getResponseHeader(String headerName) 

Source Link

Document

Gets the response header associated with the given name.

Usage

From source file:com.gnizr.core.util.GnizrDaoUtil.java

public static Integer detectMIMEType(String url) {
    try {/*from  w  w  w  .  j a v a  2  s.  c  o m*/
        HttpClient httpClient = new HttpClient();
        HeadMethod method = new HeadMethod(url);
        method.getParams().setIntParameter("http.socket.timeout", 5000);
        int code = httpClient.executeMethod(method);
        if (code == 200) {
            Header h = method.getResponseHeader("Content-Type");
            if (h != null) {
                HeaderElement[] headElm = h.getElements();
                if (headElm != null & headElm.length > 0) {
                    String mimeType = headElm[0].getValue();
                    if (mimeType == null) {
                        mimeType = headElm[0].getName();
                    }
                    if (mimeType != null) {
                        return getMimeTypeIdCode(mimeType);
                    }
                }
            }
        }
    } catch (Exception e) {
        // no code;
    }
    return MIMEType.UNKNOWN;
}

From source file:com.datos.vfs.provider.http.HttpFileContentInfoFactory.java

@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    final HttpFileObject httpFile = (HttpFileObject) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile());

    String contentType = null;//from  w w w  .j  av a2  s .co m
    String contentEncoding = null;

    HeadMethod headMethod;
    try {
        headMethod = httpFile.getHeadMethod();
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
    final Header header = headMethod.getResponseHeader("content-type");
    if (header != null) {
        final HeaderElement[] element = header.getElements();
        if (element != null && element.length > 0) {
            contentType = element[0].getName();
        }
    }

    contentEncoding = headMethod.getResponseCharSet();

    return new DefaultFileContentInfo(contentType, contentEncoding);
}

From source file:net.sf.ufsc.http.HttpFile.java

/**
 * @see net.sf.ufsc.File#length()/*from w  ww .  ja va  2 s.c o m*/
 */
public long length() throws java.io.IOException {
    HeadMethod method = new HeadMethod(this.uri.toString());

    try {
        this.execute(method);

        return Long.parseLong(method.getResponseHeader(CONTENT_LENGTH).getValue());
    } finally {
        method.releaseConnection();
    }
}

From source file:fr.jayasoft.ivy.url.HttpClientHandler.java

private long getLastModified(HeadMethod head) {
    Header header = head.getResponseHeader("last-modified");
    if (header != null) {
        String lastModified = header.getValue();
        try {/*from   w  w  w .jav  a  2  s  . c o m*/
            return LAST_MODIFIED_FORMAT.parse(lastModified).getTime();
        } catch (ParseException e) {
        }
        return System.currentTimeMillis();
    } else {
        return System.currentTimeMillis();
    }
}

From source file:net.sf.ufsc.http.HttpFile.java

/**
 * @see net.sf.ufsc.File#lastModified()/*ww  w .j av  a  2 s . com*/
 */
public Date lastModified() throws java.io.IOException {
    HeadMethod method = new HeadMethod(this.uri.toString());

    try {
        this.execute(method);

        return DateUtil.parseDate(method.getResponseHeader(LAST_MODIFIED).getValue());
    } catch (DateParseException e) {
        throw new IOException(e.toString());
    } finally {
        method.releaseConnection();
    }
}

From source file:de.mpg.escidoc.services.common.util.Util.java

public static Node getSize(String url) {
    DocumentBuilder documentBuilder;

    HttpClient httpClient = new HttpClient();
    HeadMethod headMethod = new HeadMethod(url);

    try {/*w w  w.  jav  a2s  .  c  om*/
        logger.info("Getting size of " + url);
        ProxyHelper.executeMethod(httpClient, headMethod);

        if (headMethod.getStatusCode() != 200) {
            logger.warn("Wrong status code " + headMethod.getStatusCode() + " at " + url);
        }

        documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element element = document.createElement("size");
        document.appendChild(element);
        Header header = headMethod.getResponseHeader("Content-Length");
        if (header != null) {
            element.setTextContent(header.getValue());
            return document;
        } else {
            element.setTextContent("0");
            return document;
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.zimbra.cs.store.triton.TritonIncomingBlob.java

@Override
protected long getRemoteSize() throws IOException {
    outStream.flush();//from   w ww .  jav a 2  s.  c  o m
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    HeadMethod head = new HeadMethod(baseUrl + uploadUrl);
    ZimbraLog.store.info("heading %s", head.getURI());
    try {
        head.addRequestHeader(TritonHeaders.SERVER_TOKEN, serverToken.getToken());
        int statusCode = HttpClientUtil.executeMethod(client, head);
        if (statusCode == HttpStatus.SC_OK) {
            String contentLength = head.getResponseHeader(TritonHeaders.CONTENT_LENGTH).getValue();
            long remoteSize = -1;
            try {
                remoteSize = Long.valueOf(contentLength);
            } catch (NumberFormatException nfe) {
                throw new IOException("Content length can't be parsed to Long", nfe);
            }
            return remoteSize;
        } else {
            ZimbraLog.store.error("failed with code %d response: %s", statusCode,
                    head.getResponseBodyAsString());
            throw new IOException("unable to head blob " + statusCode + ":" + head.getStatusText(), null);
        }
    } finally {
        head.releaseConnection();
    }
}

From source file:de.mpg.mpdl.inge.transformation.Util.java

public static Node getSize(String url) {
    DocumentBuilder documentBuilder;

    HttpClient httpClient = new HttpClient();
    HeadMethod headMethod = new HeadMethod(url);

    try {/*  w w w. j  av  a2 s.  c o  m*/
        logger.info("Getting size of " + url);
        ProxyHelper.executeMethod(httpClient, headMethod);

        if (headMethod.getStatusCode() != 200) {
            logger.warn("Wrong status code " + headMethod.getStatusCode() + " at " + url);
        }

        documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element element = document.createElement("size");
        document.appendChild(element);
        Header header = headMethod.getResponseHeader("Content-Length");
        logger.info("HEAD Request to " + url + " returned Content-Length: "
                + (header != null ? header.getValue() : null));
        if (header != null) {
            element.setTextContent(header.getValue());
            return document;
        } else {
            // did not get length via HEAD request, try to do a GET request
            // workaround for biomed central, where HEAD requests sometimes return Content-Length,
            // sometimes not

            logger.info("GET request to " + url + " did not return any Content-Length. Trying GET request.");
            httpClient = new HttpClient();
            GetMethod getMethod = new GetMethod(url);
            ProxyHelper.executeMethod(httpClient, getMethod);

            if (getMethod.getStatusCode() != 200) {
                logger.warn("Wrong status code " + getMethod.getStatusCode() + " at " + url);
            }

            InputStream is = getMethod.getResponseBodyAsStream();
            long size = 0;

            while (is.read() != -1) {
                size++;
            }
            is.close();

            logger.info("GET request to " + url + " returned a file with length: " + size);
            element.setTextContent(String.valueOf(size));
            return document;
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.mpg.escidoc.services.transformation.Util.java

public static Node getSize(String url) {
    DocumentBuilder documentBuilder;

    HttpClient httpClient = new HttpClient();
    HeadMethod headMethod = new HeadMethod(url);

    try {//ww w.  j  a v a 2s .co m
        logger.info("Getting size of " + url);
        ProxyHelper.executeMethod(httpClient, headMethod);

        if (headMethod.getStatusCode() != 200) {
            logger.warn("Wrong status code " + headMethod.getStatusCode() + " at " + url);
        }

        documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element element = document.createElement("size");
        document.appendChild(element);
        Header header = headMethod.getResponseHeader("Content-Length");
        logger.info("HEAD Request to " + url + " returned Content-Length: "
                + (header != null ? header.getValue() : null));
        if (header != null) {
            element.setTextContent(header.getValue());
            return document;
        } else {
            //did not get length via HEAD request, try to do a GET request 
            //workaround for biomed central, where HEAD requests sometimes return Content-Length, sometimes not

            logger.info("GET request to " + url + " did not return any Content-Length. Trying GET request.");
            httpClient = new HttpClient();
            GetMethod getMethod = new GetMethod(url);
            ProxyHelper.executeMethod(httpClient, getMethod);

            if (getMethod.getStatusCode() != 200) {
                logger.warn("Wrong status code " + getMethod.getStatusCode() + " at " + url);
            }

            InputStream is = getMethod.getResponseBodyAsStream();
            long size = 0;

            while (is.read() != -1) {
                size++;
            }
            is.close();

            logger.info("GET request to " + url + " returned a file with length: " + size);
            element.setTextContent(String.valueOf(size));
            return document;
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:fedora.client.FedoraClient.java

public Date getLastModifiedDate(String locator) throws IOException {
    if (locator.startsWith(FEDORA_URI_PREFIX)) {
        String query = "select $date " + "from <#ri> " + "where <" + locator + "> <"
                + VIEW.LAST_MODIFIED_DATE.uri + "> $date";
        Map<String, String> map = new HashMap<String, String>();
        map.put("lang", "itql");
        map.put("query", query);
        TupleIterator tuples = getTuples(map);
        try {/*from w  w w .  j av a2 s  .  c  o  m*/
            if (tuples.hasNext()) {
                Map<String, Node> row = tuples.next();
                Literal dateLiteral = (Literal) row.get("date");
                if (dateLiteral == null) {
                    throw new IOException("A row was returned, but it did not contain a 'date' binding");
                }
                return DateUtility.parseDateAsUTC(dateLiteral.getLexicalForm());
            } else {
                throw new IOException("No rows were returned");
            }
        } catch (TrippiException e) {
            throw new IOException(e.getMessage());
        } finally {
            try {
                tuples.close();
            } catch (Exception e) {
            }
        }
    } else {
        HttpClient client = getHttpClient();

        HeadMethod head = new HeadMethod(locator);
        head.setDoAuthentication(true);
        head.setFollowRedirects(FOLLOW_REDIRECTS);

        try {
            int statusCode = client.executeMethod(head);
            if (statusCode != HttpStatus.SC_OK) {
                throw new IOException("Method failed: " + head.getStatusLine());
            }
            //Header[] headers = head.getResponseHeaders();

            // Retrieve just the last modified header value.
            Header header = head.getResponseHeader("last-modified");
            if (header != null) {
                String lastModified = header.getValue();
                return DateUtility.convertStringToDate(lastModified);
            } else {
                // return current date time
                return new Date();
            }
        } finally {
            head.releaseConnection();
        }
    }
}