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

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

Introduction

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

Prototype

public HeadMethod(String paramString) 

Source Link

Usage

From source file:org.apache.maven.plugin.docck.AbstractCheckDocumentationMojo.java

private void checkURL(String url, String description, DocumentationReporter reporter) {
    try {// w ww .j  a v  a  2  s.  c o  m
        String protocol = getURLProtocol(url);

        if (protocol.startsWith("http")) {
            if (offline) {
                reporter.warn("Cannot verify " + description + " in offline mode with URL: \'" + url + "\'.");
            } else if (!validUrls.contains(url)) {
                HeadMethod headMethod = new HeadMethod(url);
                headMethod.setFollowRedirects(true);
                headMethod.setDoAuthentication(false);

                try {
                    getLog().debug("Verifying http url: " + url);
                    if (httpClient.executeMethod(headMethod) != HTTP_STATUS_200) {
                        reporter.error("Cannot reach " + description + " with URL: \'" + url + "\'.");
                    } else {
                        validUrls.add(url);
                    }
                } catch (HttpException e) {
                    reporter.error("Cannot reach " + description + " with URL: \'" + url + "\'.\nError: "
                            + e.getMessage());
                } catch (IOException e) {
                    reporter.error("Cannot reach " + description + " with URL: \'" + url + "\'.\nError: "
                            + e.getMessage());
                } finally {
                    headMethod.releaseConnection();
                }
            }
        } else {
            reporter.warn("Non-HTTP " + description + " URL not verified.");
        }
    } catch (MalformedURLException e) {
        reporter.warn("The " + description + " appears to have an invalid URL \'" + url + "\'." + " Message: \'"
                + e.getMessage() + "\'. Trying to access it as a file instead.");

        checkFile(url, description, reporter);
    }
}

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

protected ProxyArtifact getMetaInformationInternal(String url) {
    try {// ww  w  .j a  v a2 s .  co  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.apache.maven.wagon.providers.webdav.AbstractHttpClientWagon.java

public boolean resourceExists(String resourceName) throws TransferFailedException, AuthorizationException {
    StringBuilder url = new StringBuilder(getRepository().getUrl());
    if (!url.toString().endsWith("/")) {
        url.append('/');
    }//from   ww  w.j ava 2 s  . co m
    url.append(resourceName);
    HeadMethod headMethod = new HeadMethod(url.toString());

    int statusCode;
    try {
        statusCode = execute(headMethod);
    } catch (IOException e) {
        throw new TransferFailedException(e.getMessage(), e);
    }
    try {
        switch (statusCode) {
        case HttpStatus.SC_OK:
            return true;

        case HttpStatus.SC_NOT_MODIFIED:
            return true;

        case SC_NULL:
            throw new TransferFailedException("Failed to transfer file: " + url);

        case HttpStatus.SC_FORBIDDEN:
            throw new AuthorizationException("Access denied to: " + url);

        case HttpStatus.SC_UNAUTHORIZED:
            throw new AuthorizationException("Not authorized.");

        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
            throw new AuthorizationException("Not authorized by proxy.");

        case HttpStatus.SC_NOT_FOUND:
            return false;

        //add more entries here
        default:
            throw new TransferFailedException(
                    "Failed to transfer file: " + url + ". Return code is: " + statusCode);
        }
    } finally {
        headMethod.releaseConnection();
    }
}

From source file:org.apache.maven.wagon.shared.http.AbstractHttpClientWagon.java

public boolean resourceExists(String resourceName) throws TransferFailedException, AuthorizationException {
    String url = getRepository().getUrl() + "/" + resourceName;
    HeadMethod headMethod = new HeadMethod(url);
    int statusCode;
    try {/*w  ww . ja  va  2  s . c o  m*/
        statusCode = execute(headMethod);
    } catch (IOException e) {
        throw new TransferFailedException(e.getMessage(), e);
    }
    try {
        switch (statusCode) {
        case HttpStatus.SC_OK:
            return true;

        case HttpStatus.SC_NOT_MODIFIED:
            return true;

        case SC_NULL:
            throw new TransferFailedException("Failed to transfer file: " + url);

        case HttpStatus.SC_FORBIDDEN:
            throw new AuthorizationException("Access denied to: " + url);

        case HttpStatus.SC_UNAUTHORIZED:
            throw new AuthorizationException("Not authorized.");

        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
            throw new AuthorizationException("Not authorized by proxy.");

        case HttpStatus.SC_NOT_FOUND:
            return false;

        //add more entries here
        default:
            throw new TransferFailedException(
                    "Failed to transfer file: " + url + ". Return code is: " + statusCode);
        }
    } finally {
        headMethod.releaseConnection();
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.HeadServletTest.java

@Test
public void htmlHead() throws IOException {
    final HeadMethod head = new HeadMethod(HTML_URL);
    final int status = H.getHttpClient().executeMethod(head);
    assertEquals(200, status);//from  w  w  w . j av a  2  s.c  o m
    assertNull("Expecting null body", head.getResponseBody());
    assertCommonHeaders(head, "text/html");
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.HeadServletTest.java

@Test
public void pngHead() throws IOException {
    final HeadMethod head = new HeadMethod(PNG_URL);
    final int status = H.getHttpClient().executeMethod(head);
    assertEquals(200, status);/*from  w  w w. ja v  a2s .  co  m*/
    assertNull("Expecting null body", head.getResponseBody());
    assertCommonHeaders(head, "image/png");
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.HeadServletTest.java

@Test
public void nonexistentHead() throws IOException {
    final HeadMethod head = new HeadMethod(NONEXISTENT_URL);
    assertEquals(404, H.getHttpClient().executeMethod(head));
}

From source file:org.apache.sling.maven.bundlesupport.AbstractBundleInstallMojo.java

private int performHead(String uri) throws HttpException, IOException {
    HeadMethod head = new HeadMethod(uri);
    try {//from ww  w.j  av a2  s.  c  om
        return getHttpClient().executeMethod(head);
    } finally {
        head.releaseConnection();
    }
}

From source file:org.apache.solr.servlet.CacheHeaderTestBase.java

protected HttpMethodBase getSelectMethod(String method) {
    CommonsHttpSolrServer httpserver = (CommonsHttpSolrServer) getSolrServer();
    HttpMethodBase m = null;//from   w  w  w .  j a v a  2s .c o  m
    if ("GET".equals(method)) {
        m = new GetMethod(httpserver.getBaseURL() + "/select");
    } else if ("HEAD".equals(method)) {
        m = new HeadMethod(httpserver.getBaseURL() + "/select");
    } else if ("POST".equals(method)) {
        m = new PostMethod(httpserver.getBaseURL() + "/select");
    }
    m.setQueryString(
            new NameValuePair[] { new NameValuePair("q", "solr"), new NameValuePair("qt", "standard") });
    return m;
}

From source file:org.apache.solr.servlet.CacheHeaderTestBase.java

protected HttpMethodBase getUpdateMethod(String method) {
    CommonsHttpSolrServer httpserver = (CommonsHttpSolrServer) getSolrServer();
    HttpMethodBase m = null;//w  w  w .  ja  va2  s .co  m

    if ("GET".equals(method)) {
        m = new GetMethod(httpserver.getBaseURL() + "/update/csv");
    } else if ("POST".equals(method)) {
        m = new PostMethod(httpserver.getBaseURL() + "/update/csv");
    } else if ("HEAD".equals(method)) {
        m = new HeadMethod(httpserver.getBaseURL() + "/update/csv");
    }

    return m;
}