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

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

Introduction

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

Prototype

String METHOD_NAME

To view the source code for org.apache.http.client.methods HttpHead METHOD_NAME.

Click Source Link

Usage

From source file:org.elasticsearch.client.RestClientSingleHostTests.java

/**
 * End to end test for error status codes: they should cause an exception to be thrown, apart from 404 with HEAD requests
 *///from   w ww .j a  v  a  2 s . c o m
public void testErrorStatusCodes() throws IOException {
    for (String method : getHttpMethods()) {
        Set<Integer> expectedIgnores = new HashSet<>();
        String ignoreParam = "";
        if (HttpHead.METHOD_NAME.equals(method)) {
            expectedIgnores.add(404);
        }
        if (randomBoolean()) {
            int numIgnores = randomIntBetween(1, 3);
            for (int i = 0; i < numIgnores; i++) {
                Integer code = randomFrom(getAllErrorStatusCodes());
                expectedIgnores.add(code);
                ignoreParam += code;
                if (i < numIgnores - 1) {
                    ignoreParam += ",";
                }
            }
        }
        //error status codes should cause an exception to be thrown
        for (int errorStatusCode : getAllErrorStatusCodes()) {
            try {
                Map<String, String> params;
                if (ignoreParam.isEmpty()) {
                    params = Collections.emptyMap();
                } else {
                    params = Collections.singletonMap("ignore", ignoreParam);
                }
                Response response = performRequest(method, "/" + errorStatusCode, params);
                if (expectedIgnores.contains(errorStatusCode)) {
                    //no exception gets thrown although we got an error status code, as it was configured to be ignored
                    assertEquals(errorStatusCode, response.getStatusLine().getStatusCode());
                } else {
                    fail("request should have failed");
                }
            } catch (ResponseException e) {
                if (expectedIgnores.contains(errorStatusCode)) {
                    throw e;
                }
                assertEquals(errorStatusCode, e.getResponse().getStatusLine().getStatusCode());
            }
            if (errorStatusCode <= 500 || expectedIgnores.contains(errorStatusCode)) {
                failureListener.assertNotCalled();
            } else {
                failureListener.assertCalled(httpHost);
            }
        }
    }
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testExists() {
    getAndExistsTest(RequestConverters::exists, HttpHead.METHOD_NAME);
}

From source file:org.hyperic.hq.plugin.netservices.HTTPCollector.java

private double checkPattern(HttpResponse response, double avail, StringBuilder msg) {
    if (!getMethod().equals(HttpHead.METHOD_NAME) && (avail == Metric.AVAIL_UP)) {
        if (pattern.get() != null) {
            if (!matchResponse(response)) {
                avail = Metric.AVAIL_WARN;
            } else if (matches.size() != 0) {
                msg.append(" match results=").append(matches);
            }// w w  w . j av a 2s .  c o  m
        } else {
            parseResults(response);
        }
    }
    return avail;
}

From source file:org.elasticsearch.client.RequestConvertersTests.java

public void testIndicesExist() {
    String[] indices = randomIndicesNames(1, 10);

    GetIndexRequest getIndexRequest = new GetIndexRequest().indices(indices);

    Map<String, String> expectedParams = new HashMap<>();
    setRandomIndicesOptions(getIndexRequest::indicesOptions, getIndexRequest::indicesOptions, expectedParams);
    setRandomLocal(getIndexRequest, expectedParams);
    setRandomHumanReadable(getIndexRequest, expectedParams);
    setRandomIncludeDefaults(getIndexRequest, expectedParams);

    final Request request = RequestConverters.indicesExist(getIndexRequest);

    assertEquals(HttpHead.METHOD_NAME, request.getMethod());
    assertEquals("/" + String.join(",", indices), request.getEndpoint());
    assertThat(expectedParams, equalTo(request.getParameters()));
    assertNull(request.getEntity());/*from  w  w  w  . j a v  a2s .  c o  m*/
}

From source file:org.elasticsearch.client.RestClient.java

/**
 * Sends a request to the Elasticsearch cluster that the client points to. The request is executed asynchronously
 * and the provided {@link ResponseListener} gets notified upon request completion or failure.
 * Selects a host out of the provided ones in a round-robin fashion. Failing hosts are marked dead and retried after a certain
 * amount of time (minimum 1 minute, maximum 30 minutes), depending on how many times they previously failed (the more failures,
 * the later they will be retried). In case of failures all of the alive nodes (or dead nodes that deserve a retry) are retried
 * until one responds or none of them does, in which case an {@link IOException} will be thrown.
 *
 * @param method the http method//w ww.jav a2s  .co  m
 * @param endpoint the path of the request (without host and port)
 * @param params the query_string parameters
 * @param entity the body of the request, null if not applicable
 * @param httpAsyncResponseConsumerFactory the {@link HttpAsyncResponseConsumerFactory} used to create one
 * {@link HttpAsyncResponseConsumer} callback per retry. Controls how the response body gets streamed from a non-blocking HTTP
 * connection on the client side.
 * @param responseListener the {@link ResponseListener} to notify when the request is completed or fails
 * @param headers the optional request headers
 */
public void performRequestAsync(String method, String endpoint, Map<String, String> params, HttpEntity entity,
        HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, ResponseListener responseListener,
        Header... headers) {
    try {
        Objects.requireNonNull(params, "params must not be null");
        Map<String, String> requestParams = new HashMap<>(params);
        //ignore is a special parameter supported by the clients, shouldn't be sent to es
        String ignoreString = requestParams.remove("ignore");
        Set<Integer> ignoreErrorCodes;
        if (ignoreString == null) {
            if (HttpHead.METHOD_NAME.equals(method)) {
                //404 never causes error if returned for a HEAD request
                ignoreErrorCodes = Collections.singleton(404);
            } else {
                ignoreErrorCodes = Collections.emptySet();
            }
        } else {
            String[] ignoresArray = ignoreString.split(",");
            ignoreErrorCodes = new HashSet<>();
            if (HttpHead.METHOD_NAME.equals(method)) {
                //404 never causes error if returned for a HEAD request
                ignoreErrorCodes.add(404);
            }
            for (String ignoreCode : ignoresArray) {
                try {
                    ignoreErrorCodes.add(Integer.valueOf(ignoreCode));
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException(
                            "ignore value should be a number, found [" + ignoreString + "] instead", e);
                }
            }
        }
        URI uri = buildUri(pathPrefix, endpoint, requestParams);
        HttpRequestBase request = createHttpRequest(method, uri, entity);
        setHeaders(request, headers);
        FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(
                responseListener);
        long startTime = System.nanoTime();
        performRequestAsync(startTime, nextHost(), request, ignoreErrorCodes, httpAsyncResponseConsumerFactory,
                failureTrackingResponseListener);
    } catch (Exception e) {
        responseListener.onFailure(e);
    }
}

From source file:org.trancecode.xproc.step.RequestParser.java

private HttpRequestBase constructMethod(final String method, final URI hrefUri) {
    final HttpEntity httpEntity = request.getEntity();
    final HeaderGroup headers = request.getHeaders();
    if (StringUtils.equalsIgnoreCase(HttpPost.METHOD_NAME, method)) {
        final HttpPost httpPost = new HttpPost(hrefUri);
        for (final Header h : headers.getAllHeaders()) {
            if (!StringUtils.equalsIgnoreCase("Content-Type", h.getName())) {
                httpPost.addHeader(h);/*from w w  w . j  ava2  s.  c o  m*/
            }
        }
        httpPost.setEntity(httpEntity);
        return httpPost;
    } else if (StringUtils.equalsIgnoreCase(HttpPut.METHOD_NAME, method)) {
        final HttpPut httpPut = new HttpPut(hrefUri);
        httpPut.setEntity(httpEntity);
        return httpPut;
    } else if (StringUtils.equalsIgnoreCase(HttpDelete.METHOD_NAME, method)) {
        final HttpDelete httpDelete = new HttpDelete(hrefUri);
        httpDelete.setHeaders(headers.getAllHeaders());
        return httpDelete;
    } else if (StringUtils.equalsIgnoreCase(HttpGet.METHOD_NAME, method)) {
        final HttpGet httpGet = new HttpGet(hrefUri);
        httpGet.setHeaders(headers.getAllHeaders());
        return httpGet;

    } else if (StringUtils.equalsIgnoreCase(HttpHead.METHOD_NAME, method)) {
        final HttpHead httpHead = new HttpHead(hrefUri);
        httpHead.setHeaders(headers.getAllHeaders());
        return httpHead;
    }
    return null;
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request exists(GetRequest getRequest) {
    return getStyleRequest(HttpHead.METHOD_NAME, getRequest);
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request ping() {
    return new Request(HttpHead.METHOD_NAME, "/");
}

From source file:org.elasticsearch.client.RestClient.java

private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
    switch (method.toUpperCase(Locale.ROOT)) {
    case HttpDeleteWithEntity.METHOD_NAME:
        return addRequestBody(new HttpDeleteWithEntity(uri), entity);
    case HttpGetWithEntity.METHOD_NAME:
        return addRequestBody(new HttpGetWithEntity(uri), entity);
    case HttpHead.METHOD_NAME:
        return addRequestBody(new HttpHead(uri), entity);
    case HttpOptions.METHOD_NAME:
        return addRequestBody(new HttpOptions(uri), entity);
    case HttpPatch.METHOD_NAME:
        return addRequestBody(new HttpPatch(uri), entity);
    case HttpPost.METHOD_NAME:
        HttpPost httpPost = new HttpPost(uri);
        addRequestBody(httpPost, entity);
        return httpPost;
    case HttpPut.METHOD_NAME:
        return addRequestBody(new HttpPut(uri), entity);
    case HttpTrace.METHOD_NAME:
        return addRequestBody(new HttpTrace(uri), entity);
    default:/*  www. j  a va 2 s. c om*/
        throw new UnsupportedOperationException("http method not supported: " + method);
    }
}

From source file:org.elasticsearch.client.RequestConverters.java

static Request existsAlias(GetAliasesRequest getAliasesRequest) {
    if ((getAliasesRequest.indices() == null || getAliasesRequest.indices().length == 0)
            && (getAliasesRequest.aliases() == null || getAliasesRequest.aliases().length == 0)) {
        throw new IllegalArgumentException("existsAlias requires at least an alias or an index");
    }//from   w w  w . ja v a2s. c  o  m
    String[] indices = getAliasesRequest.indices() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.indices();
    String[] aliases = getAliasesRequest.aliases() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.aliases();

    Request request = new Request(HttpHead.METHOD_NAME, endpoint(indices, "_alias", aliases));

    Params params = new Params(request);
    params.withIndicesOptions(getAliasesRequest.indicesOptions());
    params.withLocal(getAliasesRequest.local());
    return request;
}