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

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

Introduction

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

Prototype

@Override
public int getStatusCode() 

Source Link

Document

Returns the response status code.

Usage

From source file:example.HelloIvy.java

public static void main(String[] args) throws Exception {
    String message = "Hello Ivy!";
    System.out.println("standard message : " + message);
    System.out.println(//  w w  w  .  j  a va 2 s  .c  om
            "capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message));

    HttpClient client = new HttpClient();
    HeadMethod head = new HeadMethod("http://www.ibiblio.org/");
    client.executeMethod(head);

    int status = head.getStatusCode();
    System.out.println("head status code with httpclient: " + status);
    head.releaseConnection();

    System.out.println("now check if httpclient dependency on commons-logging has been realized");
    Class<?> clss = Class.forName("org.apache.commons.logging.Log");
    System.out.println("found logging class in classpath: " + clss);
}

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 {/*from w ww  . j  a va 2  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");
        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: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 {//from   www  . ja  v a2 s .  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");
        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 {/*from  www  . j av  a  2s. 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");
        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:fr.jayasoft.ivy.url.HttpClientHandler.java

public URLInfo getURLInfo(URL url, int timeout) {
    HeadMethod head = null;
    try {//  w ww .  ja  v  a2s . c  o m
        head = doHead(url, timeout);
        int status = head.getStatusCode();
        head.releaseConnection();
        if (status == HttpStatus.SC_OK) {
            return new URLInfo(true, getResponseContentLength(head), getLastModified(head));
        }
        if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
            Message.error("Your proxy requires authentication.");
        } else if (String.valueOf(status).startsWith("4")) {
            Message.verbose("CLIENT ERROR: " + head.getStatusText() + " url=" + url);
        } else if (String.valueOf(status).startsWith("5")) {
            Message.warn("SERVER ERROR: " + head.getStatusText() + " url=" + url);
        }
        Message.debug("HTTP response status: " + status + "=" + head.getStatusText() + " url=" + url);
    } catch (HttpException e) {
        Message.error("HttpClientHandler: " + e.getMessage() + ":" + e.getReasonCode() + "=" + e.getReason()
                + " url=" + url);
    } catch (UnknownHostException e) {
        Message.warn("Host " + e.getMessage() + " not found. url=" + url);
        Message.info(
                "You probably access the destination server through a proxy server that is not well configured.");
    } catch (IOException e) {
        Message.error("HttpClientHandler: " + e.getMessage() + " url=" + url);
    } finally {
        if (head != null) {
            head.releaseConnection();
        }
    }
    return UNAVAILABLE;
}

From source file:org.apache.hadoop.fs.swift.http.SwiftRestClient.java

/**
 * Issue a head request/*from w ww. jav a 2  s.  c  om*/
 * @param path path to query
 * @param requestHeaders request header
 * @return the response headers. This may be an empty list
 * @throws IOException IO problems
 * @throws FileNotFoundException if there is nothing at the end
 */
public Header[] headRequest(SwiftObjectPath path, final Header... requestHeaders) throws IOException {
    preRemoteCommand("headRequest");
    return perform(pathToURI(path), new HeadMethodProcessor<Header[]>() {
        @Override
        public Header[] extractResult(HeadMethod method) throws IOException {
            if (method.getStatusCode() == SC_NOT_FOUND) {
                throw new FileNotFoundException("Not Found " + method.getURI());
            }

            return method.getResponseHeaders();
        }

        @Override
        protected void setup(HeadMethod method) throws SwiftInternalStateException {
            setHeaders(method, requestHeaders);
        }
    });
}

From source file:org.apache.maven.proxy.config.HttpRepoConfiguration.java

protected ProxyArtifact getMetaInformationInternal(String url) {
    try {/* ww w.j  a  va 2  s.c o  m*/
        String fullUrl = getUrl() + url;
        LOGGER.info(this + ": Checking last modified time for " + fullUrl);
        HttpClient client = createHttpClient();
        HeadMethod method = new HeadMethod(fullUrl);
        client.executeMethod(method);

        if (method.getStatusCode() == HttpServletResponse.SC_NOT_FOUND) {
            return null;
        }

        if (method.getStatusCode() != HttpServletResponse.SC_OK) {
            LOGGER.info(this + ": Unable to find " + fullUrl + " because of [" + method.getStatusCode() + "] = "
                    + method.getStatusText());
            return null;
        }

        long lastModified = getLastModified(method);
        long size = getContentLength(method);
        ProxyArtifact snapshot = new ProxyArtifact(this, url);
        snapshot.setLastModified(lastModified);
        snapshot.setSize(size);
        return snapshot;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.deegree.framework.util.HttpUtils.java

/**
 * validates passed URL. If it is not a valid URL or a client can not connect to it an exception will be thrown
 * //  w  ww  .j  ava2s.  c om
 * @param url
 * @param user
 * @param password
 * @throws IOException
 */
public static int validateURL(String url, String user, String password) throws IOException {
    if (url.startsWith("http:")) {
        URL tmp = new URL(url);
        HeadMethod hm = new HeadMethod(url);
        setHTTPCredentials(hm, user, password);
        InetAddress.getByName(tmp.getHost());
        HttpClient client = new HttpClient();
        client.executeMethod(hm);
        if (hm.getStatusCode() != HttpURLConnection.HTTP_OK) {
            if (hm.getStatusCode() != HttpURLConnection.HTTP_UNAUTHORIZED && hm.getStatusCode() != 401) {
                // this method just evaluates if a URL/host is valid; it does not takes care
                // if authorization is available/valid
                throw new IOException("Host " + tmp.getHost() + " of URL + " + url + " does not exists");
            }
        }
        return hm.getStatusCode();
    } else if (url.startsWith("file:")) {
        URL tmp = new URL(url);
        InputStream is = tmp.openStream();
        is.close();
        return 200;
    }
    return HttpURLConnection.HTTP_UNAVAILABLE;
}

From source file:org.elasticsearch.hadoop.rest.RestClient.java

public boolean exists(String indexOrType) {
    HeadMethod headMethod = new HeadMethod(indexOrType);
    execute(headMethod, false);//  w  w  w .  j av  a2s. c  om
    return (headMethod.getStatusCode() == HttpStatus.SC_OK);
}

From source file:org.opensaml.util.resource.HttpResource.java

/** {@inheritDoc} */
public boolean exists() throws ResourceException {
    HeadMethod headMethod = new HeadMethod(resourceUrl);

    try {/*from   w w w . ja v  a 2 s.c om*/
        httpClient.executeMethod(headMethod);
        if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
            return false;
        }

        return true;
    } catch (IOException e) {
        throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
    }
}