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"));
}
}
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);
}
}
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"));
}
}
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"));
}
}
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();
}
Aggregations