Search in sources :

Example 1 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class MeanReciprocalRankTests method testXContentParsingIsNotLenient.

public void testXContentParsingIsNotLenient() throws IOException {
    MeanReciprocalRank testItem = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
    BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, null, random());
    try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
        parser.nextToken();
        parser.nextToken();
        XContentParseException exception = expectThrows(XContentParseException.class, () -> MeanReciprocalRank.fromXContent(parser));
        assertThat(exception.getMessage(), containsString("[reciprocal_rank] unknown field"));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 2 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class RemoteScrollableHitSource method execute.

private <T> void execute(Request request, BiFunction<XContentParser, XContentType, T> parser, RejectAwareActionListener<? super T> listener) {
    // Preserve the thread context so headers survive after the call
    java.util.function.Supplier<ThreadContext.StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(true);
    try {
        client.performRequestAsync(request, new ResponseListener() {

            @Override
            public void onSuccess(org.opensearch.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 {
                                logger.debug("Response didn't include Content-Type: " + bodyMessage(response.getEntity()));
                                throw new OpenSearchException("Response didn't include supported Content-Type, remote is likely not an OpenSearch instance");
                            } catch (IOException e) {
                                OpenSearchException ee = new OpenSearchException("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, LoggingDeprecationHandler.INSTANCE, content)) {
                            parsedResponse = parser.apply(xContentParser, xContentType);
                        } catch (XContentParseException 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 OpenSearchException("Error parsing the response, remote is likely not an OpenSearch instance", e);
                        }
                    } catch (IOException e) {
                        throw new OpenSearchException("Error deserializing response, remote is likely not an OpenSearch instance", e);
                    }
                    listener.onResponse(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;
                        int statusCode = re.getResponse().getStatusLine().getStatusCode();
                        e = wrapExceptionToPreserveStatus(statusCode, re.getResponse().getEntity(), re);
                        if (RestStatus.TOO_MANY_REQUESTS.getStatus() == statusCode) {
                            listener.onRejection(e);
                            return;
                        }
                    } else if (e instanceof ContentTooLongException) {
                        e = new IllegalArgumentException("Remote responded with a chunk that was too large. Use a smaller batch size.", e);
                    }
                    listener.onFailure(e);
                }
            }
        });
    } catch (Exception e) {
        listener.onFailure(e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) ContentTooLongException(org.apache.http.ContentTooLongException) ResponseListener(org.opensearch.client.ResponseListener) IOException(java.io.IOException) ContentTooLongException(org.apache.http.ContentTooLongException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) OpenSearchException(org.opensearch.OpenSearchException) IOException(java.io.IOException) ResponseException(org.opensearch.client.ResponseException) XContentParseException(org.opensearch.common.xcontent.XContentParseException) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchException(org.opensearch.OpenSearchException) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 3 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class PrecisionAtKTests method testXContentParsingIsNotLenient.

public void testXContentParsingIsNotLenient() throws IOException {
    PrecisionAtK testItem = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
    BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, null, random());
    try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
        parser.nextToken();
        parser.nextToken();
        XContentParseException exception = expectThrows(XContentParseException.class, () -> PrecisionAtK.fromXContent(parser));
        assertThat(exception.getMessage(), containsString("[precision] unknown field"));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 4 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class RatedDocumentTests method testXContentParsingIsNotLenient.

public void testXContentParsingIsNotLenient() throws IOException {
    RatedDocument testItem = createRatedDocument();
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
    BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, null, random());
    try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
        XContentParseException exception = expectThrows(XContentParseException.class, () -> RatedDocument.fromXContent(parser));
        assertThat(exception.getMessage(), containsString("[rated_document] unknown field"));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 5 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class CategoryQueryContext method fromXContent.

public static CategoryQueryContext fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    Builder builder = builder();
    if (token == XContentParser.Token.START_OBJECT) {
        try {
            CATEGORY_PARSER.parse(parser, builder, null);
        } catch (XContentParseException e) {
            throw new XContentParseException("category context must be a string, number or boolean");
        }
    } else if (token == XContentParser.Token.VALUE_STRING || token == XContentParser.Token.VALUE_BOOLEAN || token == XContentParser.Token.VALUE_NUMBER) {
        builder.setCategory(parser.text());
    } else {
        throw new XContentParseException("category context must be an object, string, number or boolean");
    }
    return builder.build();
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Aggregations

XContentParseException (org.opensearch.common.xcontent.XContentParseException)40 XContentParser (org.opensearch.common.xcontent.XContentParser)32 Matchers.containsString (org.hamcrest.Matchers.containsString)11 XContentType (org.opensearch.common.xcontent.XContentType)8 BytesReference (org.opensearch.common.bytes.BytesReference)7 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)4 ParsingException (org.opensearch.common.ParsingException)3 IOException (java.io.IOException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 HasAttributeNodeSelector (org.opensearch.client.HasAttributeNodeSelector)2 Node (org.opensearch.client.Node)2 NodeSelector (org.opensearch.client.NodeSelector)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ContentTooLongException (org.apache.http.ContentTooLongException)1 HttpEntity (org.apache.http.HttpEntity)1 LegacyESVersion (org.opensearch.LegacyESVersion)1 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchParseException (org.opensearch.OpenSearchParseException)1