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

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

Introduction

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

Prototype

@Override
public Header[] getResponseHeaders(String headerName) 

Source Link

Usage

From source file:com.gisgraphy.domain.geoloc.importer.ImporterHelper.java

/**
 * @param URL the HTTP URL//from  w w  w  .jav a 2  s.co m
 * @return The size of the HTTP file using HTTP head method.
 */
public static long getHttpFileSize(String URL) {
    HeadMethod headMethod = new HeadMethod(URL);

    try {
        client.executeMethod(headMethod);
        Header[] contentLengthHeaders = headMethod.getResponseHeaders("Content-Length");
        if (contentLengthHeaders.length == 1) {
            logger.error("HTTP file " + URL + " = " + contentLengthHeaders[0].getValue());
            return new Long(contentLengthHeaders[0].getValue());
        } else if (contentLengthHeaders.length <= 0) {
            return -1L;
        }
    } catch (HttpException e) {
        logger.error("can not execute head method for " + URL + " : " + e.getMessage(), e);
    } catch (IOException e) {
        logger.error("can not execute head method for " + URL + " : " + e.getMessage(), e);
    } finally {
        headMethod.releaseConnection();
    }
    return -1;

}

From source file:com.gisgraphy.importer.ImporterHelper.java

/**
 * @param URL the HTTP URL/*  w  w  w  .j  a v a2 s .c  o  m*/
 * @return The size of the HTTP file using HTTP head method 
 * or -1 if error or the file doesn't exists
 */
public static long getHttpFileSize(String URL) {
    HeadMethod headMethod = new HeadMethod(URL);
    //we can not follow redirect because Geonames send a 302 found HTTP status code when a file doen't exists
    headMethod.setFollowRedirects(false);
    try {
        int code = client.executeMethod(headMethod);
        int firstDigitOfCode = code / 100;
        switch (firstDigitOfCode) {
        case 4:
            logger.error("Can not determine HTTP file size of " + URL + " because it does not exists (" + code
                    + ")");
            return -1;
        //needed to catch 3XX code because Geonames send a 302 found HTTP status code when a file doen't exists
        case 3:
            logger.error("Can not determine HTTP file size of " + URL + " because it does not exists (" + code
                    + ")");
            return -1;
        case 5:
            logger.error(
                    "Can not determine HTTP file size of " + URL + " because the server send an error " + code);
            return -1;

        default:
            break;
        }
        Header[] contentLengthHeaders = headMethod.getResponseHeaders("Content-Length");
        if (contentLengthHeaders.length == 1) {
            logger.info("HTTP file size of " + URL + " = " + contentLengthHeaders[0].getValue());
            return new Long(contentLengthHeaders[0].getValue());
        } else if (contentLengthHeaders.length <= 0) {
            return -1L;
        }
    } catch (HttpException e) {
        logger.error("can not execute head method for " + URL + " : " + e.getMessage(), e);
    } catch (IOException e) {
        logger.error("can not execute head method for " + URL + " : " + e.getMessage(), e);
    } finally {
        headMethod.releaseConnection();
    }
    return -1;
}