Search in sources :

Example 1 with ResponseException

use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.

the class RemoteScrollableHitSource method execute.

private <T> void execute(String method, String uri, Map<String, String> params, HttpEntity entity, BiFunction<XContentParser, XContentType, T> parser, Consumer<? super T> listener) {
    // Preserve the thread context so headers survive after the call
    java.util.function.Supplier<ThreadContext.StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(true);
    class RetryHelper extends AbstractRunnable {

        private final Iterator<TimeValue> retries = backoffPolicy.iterator();

        @Override
        protected void doRun() throws Exception {
            client.performRequestAsync(method, uri, params, entity, new ResponseListener() {

                @Override
                public void onSuccess(org.elasticsearch.client.Response response) {
                    // Restore the thread context to get the precious headers
                    try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                        // eliminates compiler warning
                        assert ctx != null;
                        T parsedResponse;
                        try {
                            HttpEntity responseEntity = response.getEntity();
                            InputStream content = responseEntity.getContent();
                            XContentType xContentType = null;
                            if (responseEntity.getContentType() != null) {
                                final String mimeType = ContentType.parse(responseEntity.getContentType().getValue()).getMimeType();
                                xContentType = XContentType.fromMediaType(mimeType);
                            }
                            if (xContentType == null) {
                                try {
                                    throw new ElasticsearchException("Response didn't include Content-Type: " + bodyMessage(response.getEntity()));
                                } catch (IOException e) {
                                    ElasticsearchException ee = new ElasticsearchException("Error extracting body from response");
                                    ee.addSuppressed(e);
                                    throw ee;
                                }
                            }
                            // EMPTY is safe here because we don't call namedObject
                            try (XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
                                parsedResponse = parser.apply(xContentParser, xContentType);
                            } catch (ParsingException e) {
                                /* Because we're streaming the response we can't get a copy of it here. The best we can do is hint that it
                                 * is totally wrong and we're probably not talking to Elasticsearch. */
                                throw new ElasticsearchException("Error parsing the response, remote is likely not an Elasticsearch instance", e);
                            }
                        } catch (IOException e) {
                            throw new ElasticsearchException("Error deserializing response, remote is likely not an Elasticsearch instance", e);
                        }
                        listener.accept(parsedResponse);
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                        // eliminates compiler warning
                        assert ctx != null;
                        if (e instanceof ResponseException) {
                            ResponseException re = (ResponseException) e;
                            if (RestStatus.TOO_MANY_REQUESTS.getStatus() == re.getResponse().getStatusLine().getStatusCode()) {
                                if (retries.hasNext()) {
                                    TimeValue delay = retries.next();
                                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("retrying rejected search after [{}]", delay), e);
                                    countSearchRetry.run();
                                    threadPool.schedule(delay, ThreadPool.Names.SAME, RetryHelper.this);
                                    return;
                                }
                            }
                            e = wrapExceptionToPreserveStatus(re.getResponse().getStatusLine().getStatusCode(), re.getResponse().getEntity(), re);
                        } else if (e instanceof ContentTooLongException) {
                            e = new IllegalArgumentException("Remote responded with a chunk that was too large. Use a smaller batch size.", e);
                        }
                        fail.accept(e);
                    }
                }
            });
        }

        @Override
        public void onFailure(Exception t) {
            fail.accept(t);
        }
    }
    new RetryHelper().run();
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) HttpEntity(org.apache.http.HttpEntity) ResponseException(org.elasticsearch.client.ResponseException) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentType(org.elasticsearch.common.xcontent.XContentType) ParsingException(org.elasticsearch.common.ParsingException) Iterator(java.util.Iterator) Supplier(org.apache.logging.log4j.util.Supplier) TimeValue(org.elasticsearch.common.unit.TimeValue) InputStream(java.io.InputStream) ContentTooLongException(org.apache.http.ContentTooLongException) ResponseListener(org.elasticsearch.client.ResponseListener) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResponseException(org.elasticsearch.client.ResponseException) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with ResponseException

use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.

the class CorsRegexIT method testThatRegularExpressionReturnsForbiddenOnNonMatch.

public void testThatRegularExpressionReturnsForbiddenOnNonMatch() throws IOException {
    try {
        getRestClient().performRequest("GET", "/", new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", "http://evil-host:9200"));
        fail("request should have failed");
    } catch (ResponseException e) {
        Response response = e.getResponse();
        // a rejected origin gets a FORBIDDEN - 403
        assertThat(response.getStatusLine().getStatusCode(), is(403));
        assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
    }
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader)

Example 3 with ResponseException

use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.

the class ElasticsearchHostsSnifferTests method testSniffNodes.

public void testSniffNodes() throws IOException {
    HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    try (RestClient restClient = RestClient.builder(httpHost).build()) {
        ElasticsearchHostsSniffer sniffer = new ElasticsearchHostsSniffer(restClient, sniffRequestTimeout, scheme);
        try {
            List<HttpHost> sniffedHosts = sniffer.sniffHosts();
            if (sniffResponse.isFailure) {
                fail("sniffNodes should have failed");
            }
            assertThat(sniffedHosts.size(), equalTo(sniffResponse.hosts.size()));
            Iterator<HttpHost> responseHostsIterator = sniffResponse.hosts.iterator();
            for (HttpHost sniffedHost : sniffedHosts) {
                assertEquals(sniffedHost, responseHostsIterator.next());
            }
        } catch (ResponseException e) {
            Response response = e.getResponse();
            if (sniffResponse.isFailure) {
                assertThat(e.getMessage(), containsString("GET " + httpHost + "/_nodes/http?timeout=" + sniffRequestTimeout + "ms"));
                assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
                assertThat(response.getHost(), equalTo(httpHost));
                assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
                assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
            } else {
                fail("sniffNodes should have succeeded: " + response.getStatusLine());
            }
        }
    }
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException) HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient)

Example 4 with ResponseException

use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.

the class Netty4BadRequestIT method testBadRequest.

public void testBadRequest() throws IOException {
    final Response response = client().performRequest("GET", "/_nodes/settings", Collections.emptyMap());
    final ObjectPath objectPath = ObjectPath.createFromResponse(response);
    final Map<String, Object> map = objectPath.evaluate("nodes");
    int maxMaxInitialLineLength = Integer.MIN_VALUE;
    final Setting<ByteSizeValue> httpMaxInitialLineLength = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    final String key = httpMaxInitialLineLength.getKey().substring("http.".length());
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        @SuppressWarnings("unchecked") final Map<String, Object> settings = (Map<String, Object>) ((Map<String, Object>) entry.getValue()).get("settings");
        final int maxIntialLineLength;
        if (settings.containsKey("http")) {
            @SuppressWarnings("unchecked") final Map<String, Object> httpSettings = (Map<String, Object>) settings.get("http");
            if (httpSettings.containsKey(key)) {
                maxIntialLineLength = ByteSizeValue.parseBytesSizeValue((String) httpSettings.get(key), key).bytesAsInt();
            } else {
                maxIntialLineLength = httpMaxInitialLineLength.getDefault(Settings.EMPTY).bytesAsInt();
            }
        } else {
            maxIntialLineLength = httpMaxInitialLineLength.getDefault(Settings.EMPTY).bytesAsInt();
        }
        maxMaxInitialLineLength = Math.max(maxMaxInitialLineLength, maxIntialLineLength);
    }
    final String path = "/" + new String(new byte[maxMaxInitialLineLength], Charset.forName("UTF-8")).replace('\0', 'a');
    final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(randomFrom("GET", "POST", "PUT"), path, Collections.emptyMap()));
    assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(BAD_REQUEST.getStatus()));
    assertThat(e, hasToString(containsString("too_long_frame_exception")));
    assertThat(e, hasToString(matches("An HTTP line is larger than \\d+ bytes")));
}
Also used : Response(org.elasticsearch.client.Response) ObjectPath(org.elasticsearch.test.rest.yaml.ObjectPath) ResponseException(org.elasticsearch.client.ResponseException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map)

Example 5 with ResponseException

use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.

the class ClientYamlTestClient method callApi.

/**
     * Calls an api with the provided parameters and body
     */
public ClientYamlTestResponse callApi(String apiName, Map<String, String> params, HttpEntity entity, Map<String, String> headers) throws IOException {
    if ("raw".equals(apiName)) {
        // Raw requests are bit simpler....
        Map<String, String> queryStringParams = new HashMap<>(params);
        String method = Objects.requireNonNull(queryStringParams.remove("method"), "Method must be set to use raw request");
        String path = "/" + Objects.requireNonNull(queryStringParams.remove("path"), "Path must be set to use raw request");
        // And everything else is a url parameter!
        try {
            Response response = restClient.performRequest(method, path, queryStringParams, entity);
            return new ClientYamlTestResponse(response);
        } catch (ResponseException e) {
            throw new ClientYamlTestResponseException(e);
        }
    }
    ClientYamlSuiteRestApi restApi = restApi(apiName);
    //divide params between ones that go within query string and ones that go within path
    Map<String, String> pathParts = new HashMap<>();
    Map<String, String> queryStringParams = new HashMap<>();
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (restApi.getPathParts().contains(entry.getKey())) {
            pathParts.put(entry.getKey(), entry.getValue());
        } else {
            if (restApi.getParams().contains(entry.getKey()) || restSpec.isGlobalParameter(entry.getKey()) || restSpec.isClientParameter(entry.getKey())) {
                queryStringParams.put(entry.getKey(), entry.getValue());
            } else {
                throw new IllegalArgumentException("param [" + entry.getKey() + "] not supported in [" + restApi.getName() + "] " + "api");
            }
        }
    }
    List<String> supportedMethods = restApi.getSupportedMethods(pathParts.keySet());
    String requestMethod;
    if (entity != null) {
        if (!restApi.isBodySupported()) {
            throw new IllegalArgumentException("body is not supported by [" + restApi.getName() + "] api");
        }
        String contentType = entity.getContentType().getValue();
        //randomly test the GET with source param instead of GET/POST with body
        if (sendBodyAsSourceParam(supportedMethods, contentType)) {
            logger.debug("sending the request body as source param with GET method");
            queryStringParams.put("source", EntityUtils.toString(entity));
            queryStringParams.put("source_content_type", contentType);
            requestMethod = HttpGet.METHOD_NAME;
            entity = null;
        } else {
            requestMethod = RandomizedTest.randomFrom(supportedMethods);
        }
    } else {
        if (restApi.isBodyRequired()) {
            throw new IllegalArgumentException("body is required by [" + restApi.getName() + "] api");
        }
        requestMethod = RandomizedTest.randomFrom(supportedMethods);
    }
    //the rest path to use is randomized out of the matching ones (if more than one)
    ClientYamlSuiteRestPath restPath = RandomizedTest.randomFrom(restApi.getFinalPaths(pathParts));
    //Encode rules for path and query string parameters are different. We use URI to encode the path.
    //We need to encode each path part separately, as each one might contain slashes that need to be escaped, which needs to
    //be done manually.
    String requestPath;
    if (restPath.getPathParts().length == 0) {
        requestPath = "/";
    } else {
        StringBuilder finalPath = new StringBuilder();
        for (String pathPart : restPath.getPathParts()) {
            try {
                finalPath.append('/');
                // We append "/" to the path part to handle parts that start with - or other invalid characters
                URI uri = new URI(null, null, null, -1, "/" + pathPart, null, null);
                //manually escape any slash that each part may contain
                finalPath.append(uri.getRawPath().substring(1).replaceAll("/", "%2F"));
            } catch (URISyntaxException e) {
                throw new RuntimeException("unable to build uri", e);
            }
        }
        requestPath = finalPath.toString();
    }
    Header[] requestHeaders = new Header[headers.size()];
    int index = 0;
    for (Map.Entry<String, String> header : headers.entrySet()) {
        logger.info("Adding header {}\n with value {}", header.getKey(), header.getValue());
        requestHeaders[index++] = new BasicHeader(header.getKey(), header.getValue());
    }
    logger.debug("calling api [{}]", apiName);
    try {
        Response response = restClient.performRequest(requestMethod, requestPath, queryStringParams, entity, requestHeaders);
        return new ClientYamlTestResponse(response);
    } catch (ResponseException e) {
        throw new ClientYamlTestResponseException(e);
    }
}
Also used : HashMap(java.util.HashMap) ResponseException(org.elasticsearch.client.ResponseException) ClientYamlSuiteRestPath(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestPath) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Response(org.elasticsearch.client.Response) ClientYamlSuiteRestApi(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HashMap(java.util.HashMap) Map(java.util.Map) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

ResponseException (org.elasticsearch.client.ResponseException)9 Response (org.elasticsearch.client.Response)7 BasicHeader (org.apache.http.message.BasicHeader)4 Map (java.util.Map)2 HttpHost (org.apache.http.HttpHost)2 RestClient (org.elasticsearch.client.RestClient)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 FileSystem (java.nio.file.FileSystem)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ContentTooLongException (org.apache.http.ContentTooLongException)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1