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() 

Source Link

Usage

From source file:org.easyj.http.RESTHttpClient.java

/**
 * Executes a HEAD HTTP request and returns the body response as {@code String}
 *
 * @param uri URI of the resource to be requested
 * @return The body response of the request as {@code String}
 *//*from  www.j  a va  2 s.  c o m*/
public RESTHttpClient head(String uri) {
    method = new HeadMethod();
    return execute(uri);
}

From source file:org.elasticsearch.hadoop.rest.commonshttp.CommonsHttpTransport.java

@Override
public Response execute(Request request) throws IOException {
    HttpMethod http = null;//from w  ww  .j  av a  2 s.  c  o m

    switch (request.method()) {
    case DELETE:
        http = new DeleteMethodWithBody();
        break;
    case HEAD:
        http = new HeadMethod();
        break;
    case GET:
        http = (request.body() == null ? new GetMethod() : new GetMethodWithBody());
        break;
    case POST:
        http = new PostMethod();
        break;
    case PUT:
        http = new PutMethod();
        break;

    default:
        throw new EsHadoopTransportException("Unknown request method " + request.method());
    }

    CharSequence uri = request.uri();
    if (StringUtils.hasText(uri)) {
        http.setURI(new URI(escapeUri(uri.toString(), settings.getNetworkSSLEnabled()), false));
    }
    // NB: initialize the path _after_ the URI otherwise the path gets reset to /
    http.setPath(prefixPath(request.path().toString()));

    try {
        // validate new URI
        uri = http.getURI().toString();
    } catch (URIException uriex) {
        throw new EsHadoopTransportException("Invalid target URI " + request, uriex);
    }

    CharSequence params = request.params();
    if (StringUtils.hasText(params)) {
        http.setQueryString(params.toString());
    }

    ByteSequence ba = request.body();
    if (ba != null && ba.length() > 0) {
        if (!(http instanceof EntityEnclosingMethod)) {
            throw new IllegalStateException(String.format("Method %s cannot contain body - implementation bug",
                    request.method().name()));
        }
        EntityEnclosingMethod entityMethod = (EntityEnclosingMethod) http;
        entityMethod.setRequestEntity(new BytesArrayRequestEntity(ba));
        entityMethod.setContentChunked(false);
    }

    // when tracing, log everything
    if (log.isTraceEnabled()) {
        log.trace(String.format("Tx %s[%s]@[%s][%s] w/ payload [%s]", proxyInfo, request.method().name(),
                httpInfo, request.path(), request.body()));
    }

    long start = System.currentTimeMillis();
    try {
        client.executeMethod(http);
    } finally {
        stats.netTotalTime += (System.currentTimeMillis() - start);
    }

    if (log.isTraceEnabled()) {
        Socket sk = ReflectionUtils.invoke(GET_SOCKET, conn, (Object[]) null);
        String addr = sk.getLocalAddress().getHostAddress();
        log.trace(String.format("Rx %s@[%s] [%s-%s] [%s]", proxyInfo, addr, http.getStatusCode(),
                HttpStatus.getStatusText(http.getStatusCode()), http.getResponseBodyAsString()));
    }

    // the request URI is not set (since it is retried across hosts), so use the http info instead for source
    return new SimpleResponse(http.getStatusCode(), new ResponseInputStream(http), httpInfo);
}

From source file:org.executequery.http.spi.DefaultRemoteHttpClient.java

public boolean hostReachable(String host) {

    /*/*from  w ww. j  a va  2 s. c o m*/
    String  urlString = host;
    if (!host.startsWith("http://")) {
            
    urlString = "http://" + host;
    }
            
    URLConnection connection = null;
    try {
            
    URL url = new URL(urlString);
    connection = url.openConnection();
    connection.connect();
            
    return true;
            
    } catch (MalformedURLException e) {
            
    throw new RemoteHttpClientException(e);
            
    } catch (IOException e) {
            
    Log.warning("Host not reachable - " + host);
    return false;
            
    } finally {
            
    connection = null;
    }
    */

    HttpMethod method = null;
    HttpConnectionManager httpConnectionManager = createConnectionManager();

    try {

        HttpClient client = createHttpClientForManager(host, httpConnectionManager);

        method = new HeadMethod();

        RemoteHttpResponse remoteHttpResponse = executeMethod(method, client);

        return remoteHttpResponse.getResponseCode() == HttpStatus.SC_OK;

    } finally {

        releaseMethod(method);

        releaseConnectionManager(httpConnectionManager);
    }

}

From source file:org.osaf.caldav4j.CalDAVCollection.java

public int testConnection(HttpClient httpClient) throws CalDAV4JException {
    HeadMethod method = new HeadMethod();
    method.setPath(getCalendarCollectionRoot());
    try {/*from ww  w. j  a  va2  s  . c o m*/
        httpClient.executeMethod(hostConfiguration, method);
    } catch (Exception e) {
        throw new CalDAV4JException(e.getMessage(), new Throwable(e.getCause()));
    }

    switch (method.getStatusCode()) {
    case CaldavStatus.SC_OK:
        break;
    default:
        throw new BadStatusException(method.getStatusCode(), method.getName(), getCalendarCollectionRoot());
    }
    return method.getStatusCode();
}