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:com.basho.riak.client.response.TestDefaultHttpResponse.java

@Test
public void status_2xx_300_and_304_success_for_head() {
    HeadMethod head = new HeadMethod();

    for (int i = 200; i < 300; i++) {
        impl = new DefaultHttpResponse(null, null, i, null, null, null, head);
        assertTrue(impl.isSuccess());//ww  w .ja va2  s  .c  o m
    }

    impl = new DefaultHttpResponse(null, null, 300, null, null, null, head);
    assertTrue(impl.isSuccess());

    impl = new DefaultHttpResponse(null, null, 304, null, null, null, head);
    assertTrue(impl.isSuccess());
}

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

HeadMethod getHeadMethod() throws IOException {
    if (method != null) {
        return method;
    }//from   www.  jav  a2s  .  c o m
    method = new HeadMethod();
    setupMethod(method);
    final HttpClient client = getAbstractFileSystem().getClient();
    client.executeMethod(method);
    method.releaseConnection();
    return method;
}

From source file:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java

/**
 * @see it.greenvulcano.gvesb.virtual.CallOperation#perform(it.greenvulcano.gvesb.buffer.GVBuffer)
 *///ww w. j  a  v  a  2  s  .  c o m
@Override
public GVBuffer perform(GVBuffer gvBuffer) throws ConnectionException, CallException, InvalidDataException {
    logger.debug("BEGIN perform(GVBuffer gvBuffer)");
    HttpMethod method = null;
    try {
        String currMethodURI = null;
        Map<String, Object> params = GVBufferPropertiesHelper.getPropertiesMapSO(gvBuffer, true);

        String currHost = PropertiesHandler.expand(host, params, gvBuffer);
        String currPort = PropertiesHandler.expand(port, params, gvBuffer);
        logger.debug("Server Host: " + currHost + " - Port: " + currPort);
        httpClient.getHostConfiguration().setHost(currHost, Integer.parseInt(currPort), protocol);

        auth.setAuthentication(httpClient, host, Integer.parseInt(currPort), gvBuffer, params);
        proxy.setProxy(httpClient, gvBuffer, params);

        currMethodURI = PropertiesHandler.expand(contextPath + methodURI, params, gvBuffer);
        logger.debug("MethodURI[escaped:" + uriEscaped + "]=[" + currMethodURI + "]");
        switch (methodName) {
        case OPTIONS:
            method = new OptionsMethod();
            break;
        case GET:
            method = new GetMethod();
            break;
        case HEAD:
            method = new HeadMethod();
            break;
        case POST:
            method = new PostMethod();
            break;
        case PUT:
            method = new PutMethod();
            break;
        case DELETE:
            method = new DeleteMethod();
            break;
        default:
            throw new CallException("GV_CALL_SERVICE_ERROR",
                    new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                            { "id", gvBuffer.getId().toString() },
                            { "message", "Unknown method = " + methodName } });
        }
        method.setURI(new URI(currMethodURI, uriEscaped));

        if ((refDP != null) && (refDP.length() > 0)) {
            logger.debug("Calling configured Data Provider: " + refDP);
            DataProviderManager dataProviderManager = DataProviderManager.instance();
            IDataProvider dataProvider = dataProviderManager.getDataProvider(refDP);
            try {
                dataProvider.setContext(method);
                dataProvider.setObject(gvBuffer);
                method = (HttpMethod) dataProvider.getResult();
            } finally {
                dataProviderManager.releaseDataProvider(refDP, dataProvider);
            }
        }

        int status = httpClient.executeMethod(method);
        gvBuffer.setProperty(RESPONSE_STATUS, String.valueOf(status));
        String statusTxt = method.getStatusText();
        gvBuffer.setProperty(RESPONSE_MESSAGE, (statusTxt != null ? statusTxt : "NULL"));
        Header[] responseHeaders = method.getResponseHeaders();
        for (Header header : responseHeaders) {
            String headerName = RESPONSE_HEADER_PREFIX + header.getName();
            String value = header.getValue();
            if (value == null) {
                value = "";
            }
            gvBuffer.setProperty(headerName, value);
        }
        String cType = "text/html";
        Header cTypeHeader = method.getResponseHeader("Content-Type");
        if (cTypeHeader != null) {
            String cTypeValue = cTypeHeader.getValue();
            if (cTypeValue != null) {
                cType = cTypeValue;
            }
        }
        logger.debug("Response content-type: " + cType);
        ContentType contentType = new ContentType(cType);
        byte[] responseBody = method.getResponseBody();
        Object object = responseBody;
        if (contentType.getPrimaryType().equals("multipart")) {
            object = handleMultipart(responseBody, cType);
        }
        gvBuffer.setObject(object);
    } catch (CallException exc) {
        throw exc;
    } catch (Exception exc) {
        logger.error("ERROR perform(GVBuffer gvBuffer)", exc);
        throw new CallException("GV_CALL_SERVICE_ERROR",
                new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                        { "id", gvBuffer.getId().toString() }, { "message", exc.getMessage() } },
                exc);
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception exc) {
            logger.warn("Error while releasing connection", exc);
        }
        logger.debug("END perform(GVBuffer gvBuffer)");
    }
    return gvBuffer;
}

From source file:com.sa.npopa.samples.hbase.rest.client.Client.java

/**
 * Send a HEAD request /*from  w w  w. ja va  2  s  .co m*/
 * @param cluster the cluster definition
 * @param path the path or URI
 * @param headers the HTTP headers to include in the request
 * @return a Response object with response detail
 * @throws IOException
 */
public Response head(Cluster cluster, String path, Header[] headers) throws IOException {
    HeadMethod method = new HeadMethod();
    try {
        int code = execute(cluster, method, null, path);
        headers = method.getResponseHeaders();
        return new Response(code, headers, null);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagonTest.java

public void testSetPreemptiveAuthParamViaConfig() {
    HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
    methodConfig.addParam(HttpClientParams.PREEMPTIVE_AUTHENTICATION, "%b,true");

    HttpConfiguration config = new HttpConfiguration();
    config.setAll(methodConfig);/*from www  .  jav a  2  s . c  o m*/

    TestWagon wagon = new TestWagon();
    wagon.setHttpConfiguration(config);

    HeadMethod method = new HeadMethod();
    wagon.setParameters(method);

    HttpMethodParams params = method.getParams();
    assertNotNull(params);
    assertTrue(params.isParameterTrue(HttpClientParams.PREEMPTIVE_AUTHENTICATION));
}

From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagonTest.java

public void testSetMaxRedirectsParamViaConfig() {
    HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
    int maxRedirects = 2;
    methodConfig.addParam(HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects);

    HttpConfiguration config = new HttpConfiguration();
    config.setAll(methodConfig);/*  w w w  . j  av  a  2  s  .c o  m*/

    TestWagon wagon = new TestWagon();
    wagon.setHttpConfiguration(config);

    HeadMethod method = new HeadMethod();
    wagon.setParameters(method);

    HttpMethodParams params = method.getParams();
    assertNotNull(params);
    assertEquals(maxRedirects, params.getIntParameter(HttpClientParams.MAX_REDIRECTS, -1));
}

From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagonTest.java

public void testDefaultHeadersUsedByDefault() {
    HttpConfiguration config = new HttpConfiguration();
    config.setAll(new HttpMethodConfiguration());

    TestWagon wagon = new TestWagon();
    wagon.setHttpConfiguration(config);//w w w.  jav  a  2  s .  c o  m

    HeadMethod method = new HeadMethod();
    wagon.setHeaders(method);

    // these are the default headers.
    // method.addRequestHeader( "Cache-control", "no-cache" );
    // method.addRequestHeader( "Cache-store", "no-store" );
    // method.addRequestHeader( "Pragma", "no-cache" );
    // method.addRequestHeader( "Expires", "0" );
    // method.addRequestHeader( "Accept-Encoding", "gzip" );

    Header header = method.getRequestHeader("Cache-control");
    assertNotNull(header);
    assertEquals("no-cache", header.getValue());

    header = method.getRequestHeader("Cache-store");
    assertNotNull(header);
    assertEquals("no-store", header.getValue());

    header = method.getRequestHeader("Pragma");
    assertNotNull(header);
    assertEquals("no-cache", header.getValue());

    header = method.getRequestHeader("Expires");
    assertNotNull(header);
    assertEquals("0", header.getValue());

    header = method.getRequestHeader("Accept-Encoding");
    assertNotNull(header);
    assertEquals("gzip", header.getValue());
}

From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagonTest.java

public void testTurnOffDefaultHeaders() {
    HttpConfiguration config = new HttpConfiguration();
    config.setAll(new HttpMethodConfiguration().setUseDefaultHeaders(false));

    TestWagon wagon = new TestWagon();
    wagon.setHttpConfiguration(config);/*from   ww  w  .  ja  va  2 s. com*/

    HeadMethod method = new HeadMethod();
    wagon.setHeaders(method);

    // these are the default headers.
    // method.addRequestHeader( "Cache-control", "no-cache" );
    // method.addRequestHeader( "Cache-store", "no-store" );
    // method.addRequestHeader( "Pragma", "no-cache" );
    // method.addRequestHeader( "Expires", "0" );
    // method.addRequestHeader( "Accept-Encoding", "gzip" );

    Header header = method.getRequestHeader("Cache-control");
    assertNull(header);

    header = method.getRequestHeader("Cache-store");
    assertNull(header);

    header = method.getRequestHeader("Pragma");
    assertNull(header);

    header = method.getRequestHeader("Expires");
    assertNull(header);

    header = method.getRequestHeader("Accept-Encoding");
    assertNull(header);
}

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that a HEAD request can be sent to resource containing only a GET
 * method./*from   w ww.  j a v  a 2 s.  c  om*/
 */
public void testHEADRequest() {
    try {
        HeadMethod httpMethod = new HeadMethod();
        httpMethod.setURI(new URI(BASE_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals(null, responseBody);
            Header[] headers = httpMethod.getResponseHeaders();
            assertNotNull(headers);
            assertTrue("Response for HEAD request contained no headers", headers.length > 0);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that a HEAD request can be sent to resource annotated with a custom
 * HEAD annotation./*from  w w  w .j  a v a2 s.  co  m*/
 */
public void testCustomHEADRequest() {
    try {
        HeadMethod httpMethod = new HeadMethod();
        httpMethod.setURI(new URI(ALT_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals(null, responseBody);
            Header header = httpMethod.getResponseHeader("HEAD");
            assertNotNull(header);
            assertEquals("TRUE", header.getValue());
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}