Example usage for org.apache.http.client.methods HttpHead HttpHead

List of usage examples for org.apache.http.client.methods HttpHead HttpHead

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpHead HttpHead.

Prototype

public HttpHead(final String uri) 

Source Link

Usage

From source file:com.kolich.http.common.HttpClient4ClosureBase.java

public T head(final URI uri) {
    return head(new HttpHead(uri));
}

From source file:controller.HeadIT.java

@Test
public void testHeadToGetSwitchOnMissingPage() throws Exception {
    HttpHead head = new HttpHead(getHttpURl("/hello/missing"));

    HttpResponse<String> response;
    try {//w  w  w.  j  a va  2 s .  c  om
        org.apache.http.HttpResponse resp = ClientFactory.getHttpClient().execute(head);
        response = new HttpResponse<>(resp, String.class);
    } finally {
        head.releaseConnection();
    }

    assertThat(response.code()).isEqualTo(NOT_FOUND);
    assertThat(response.body()).isNull();
}

From source file:org.gradle.internal.resource.transport.http.AlwaysRedirectRedirectStrategy.java

public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
        throws ProtocolException {
    URI uri = this.getLocationURI(request, response, context);
    String method = request.getRequestLine().getMethod();
    if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
        return new HttpHead(uri);
    } else if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
        return this.copyEntity(new HttpPost(uri), request);
    } else if (method.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
        return this.copyEntity(new HttpPut(uri), request);
    } else if (method.equalsIgnoreCase(HttpDelete.METHOD_NAME)) {
        return new HttpDelete(uri);
    } else if (method.equalsIgnoreCase(HttpTrace.METHOD_NAME)) {
        return new HttpTrace(uri);
    } else if (method.equalsIgnoreCase(HttpOptions.METHOD_NAME)) {
        return new HttpOptions(uri);
    } else if (method.equalsIgnoreCase(HttpPatch.METHOD_NAME)) {
        return this.copyEntity(new HttpPatch(uri), request);
    } else {//from   w  w w  . j ava 2 s  .  co m
        return new HttpGet(uri);
    }
}

From source file:net.jimj.automaton.commands.HeadCommand.java

@Override
public void execute(User user, String args) {
    if (!args.startsWith("http://") && !args.startsWith("https://")) {
        args = "http://" + args;
    }//from  www  .  j  a  v a  2  s  . c  o  m

    HttpHead headMethod = new HttpHead(args);
    try {
        HttpResponse response = HTTP_CLIENT.execute(headMethod);
        StatusLine status = response.getStatusLine();

        addEvent(new ReplyEvent(user, status.getStatusCode() + " " + status.getReasonPhrase()));

        Header[] serverHeaders = response.getHeaders("Server");
        if (serverHeaders != null) {
            addEvent(new ReplyEvent(user, "Server: " + serverHeaders[0].getValue()));
        }
    } catch (UnknownHostException uhe) {
        addEvent(new BehaviorEvent(user, BehaviorEvent.Behavior.SASSY));
    } catch (Exception e) {
        addEvent(new BehaviorEvent(user, BehaviorEvent.Behavior.SORRY));
        LOGGER.error("Error in HEAD", e);
    } finally {
        headMethod.releaseConnection();
    }
}

From source file:org.eclipse.mylyn.commons.repositories.http.core.CommonHttpOperation.java

protected HttpHead createHeadRequest(String requestPath) {
    return new HttpHead(requestPath);
}

From source file:tech.beshu.ror.integration.IndicesReverseWildcardTests.java

private static void insertDoc(String docName, RestClient restClient) {
    if (adminClient == null) {
        adminClient = restClient;/*from   w  ww . j a va2  s  . c  o m*/
    }

    try {
        HttpPut request = new HttpPut(restClient.from("/logstash-" + docName + "/documents/doc-" + docName));
        request.setHeader("refresh", "true");
        request.setHeader("timeout", "50s");
        request.setHeader("Content-Type", "application/json");
        request.setEntity(new StringEntity("{\"title\": \"" + docName + "\"}"));
        System.out.println(body(restClient.execute(request)));

    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException("Test problem", e);
    }

    // Polling phase.. #TODO is there a better way?
    try {
        HttpResponse response;
        do {
            Thread.sleep(200);
            HttpHead request = new HttpHead(
                    restClient.from("/logstash-" + docName + "/documents/doc-" + docName));
            response = restClient.execute(request);
            System.out.println(
                    "polling for " + docName + ".. result: " + response.getStatusLine().getReasonPhrase());
        } while (response.getStatusLine().getStatusCode() != 200);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalStateException("Cannot configure test case", e);
    }
}

From source file:org.fcrepo.integration.http.api.ldp.LdprIT.java

@Test
public void testGeneralHttpVersion() throws IOException {
    final HttpHead testMethod = new HttpHead(serverAddress + "");
    final HttpResponse response = client.execute(testMethod);
    assertTrue(response.getProtocolVersion().greaterEquals(HTTP_1_1));
}

From source file:com.aliyun.android.oss.task.HeadObjectTask.java

/**
 * HttpHead//ww  w  .ja  v  a 2  s . c om
 */
protected HttpUriRequest generateHttpRequest() {
    String requestUri = this.getOSSEndPoint()
            + httpTool.generateCanonicalizedResource("/" + OSSHttpTool.encodeUri(objectKey));
    HttpHead httpHead = new HttpHead(requestUri);

    String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/" + objectKey);
    String dateStr = Helper.getGMTDate();
    String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), "", "",
            dateStr, "", resource);

    httpHead.setHeader(AUTHORIZATION, authorization);
    httpHead.setHeader(DATE, dateStr);

    OSSHttpTool.addHttpRequestHeader(httpHead, IF_MODIFIED_SINCE, Helper.getGMTDate(modifiedSince));
    OSSHttpTool.addHttpRequestHeader(httpHead, IF_UNMODIFIED_SINCE, Helper.getGMTDate(unModifiedSince));
    OSSHttpTool.addHttpRequestHeader(httpHead, IF_MATCH, expectedETag);
    OSSHttpTool.addHttpRequestHeader(httpHead, IF_NONE_MATCH, unexpectedETag);

    return httpHead;
}

From source file:org.wso2.carbon.esb.rest.test.api.APIHeadMethod.java

@Test(groups = "wso2.esb", description = "API HTTP HEAD Method")
public void apiHTTPHeadMethodTest() throws Exception {
    String restURL = "http://localhost:8480/headTest";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpHead httpHead = new HttpHead(restURL);
    HttpResponse response = httpclient.execute(httpHead);

    Assert.assertTrue(stringExistsInLog("API_HIT"));

    // http head method should return a 200 OK
    assertTrue(response.getStatusLine().getStatusCode() == 200);
    // it should not contain a message body
    assertTrue(response.getEntity() == null);
}

From source file:com.helger.httpclient.HttpClientHelper.java

@Nonnull
public static HttpRequestBase createRequest(@Nonnull final EHttpMethod eHTTPMethod,
        @Nonnull final ISimpleURL aSimpleURL) {
    final String sURL = aSimpleURL.getAsStringWithEncodedParameters();
    switch (eHTTPMethod) {
    case DELETE:/*from   www . j  a  v a2  s  .  c om*/
        return new HttpDelete(sURL);
    case GET:
        return new HttpGet(sURL);
    case HEAD:
        return new HttpHead(sURL);
    case OPTIONS:
        return new HttpOptions(sURL);
    case TRACE:
        return new HttpTrace(sURL);
    case POST:
        return new HttpPost(sURL);
    case PUT:
        return new HttpPut(sURL);
    default:
        throw new IllegalStateException("Unsupported HTTP method: " + eHTTPMethod);
    }
}