Skip to content

Commit

Permalink
Add contentParser method to ExtensionRestRequest (#4760)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <widdis@gmail.com>

Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis authored Oct 13, 2022
1 parent dfeefa6 commit 401c210
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Pass REST params and content to extensions ([#4633](https://github.com/opensearch-project/OpenSearch/pull/4633))
- Return consumed params and content from extensions ([#4705](https://github.com/opensearch-project/OpenSearch/pull/4705))
- Modified EnvironmentSettingsRequest to pass entire Settings object ([#4731](https://github.com/opensearch-project/OpenSearch/pull/4731))
- Added contentParser method to ExtensionRestRequest ([#4760](https://github.com/opensearch-project/OpenSearch/pull/4760))

## [2.x]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

package org.opensearch.extensions.rest;

import org.opensearch.OpenSearchParseException;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.identity.PrincipalIdentifierToken;
import org.opensearch.rest.RestRequest;
Expand Down Expand Up @@ -229,6 +233,21 @@ public boolean isContentConsumed() {
return contentConsumed;
}

/**
* Gets a parser for the contents of this request if there is content and an xContentType.
*
* @param xContentRegistry The extension's xContentRegistry
* @return A parser for the given content and content type.
* @throws OpenSearchParseException on missing body or xContentType.
* @throws IOException on a failure creating the parser.
*/
public final XContentParser contentParser(NamedXContentRegistry xContentRegistry) throws IOException {
if (!hasContent() || getXContentType() == null) {
throw new OpenSearchParseException("There is no request body or the ContentType is invalid.");
}
return getXContentType().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content.streamInput());
}

/**
* @return This REST request issuer's identity token
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import org.opensearch.identity.ExtensionTokenProcessor;
import org.opensearch.identity.PrincipalIdentifierToken;
import org.opensearch.rest.RestStatus;
import org.opensearch.OpenSearchParseException;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.io.stream.BytesStreamInput;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.opensearch.rest.BytesRestResponse;
Expand Down Expand Up @@ -48,7 +51,7 @@ public void setUp() throws Exception {
expectedPath = "/test/uri";
expectedParams = Map.ofEntries(entry("foo", "bar"), entry("baz", "42"));
expectedContentType = XContentType.JSON;
expectedContent = new BytesArray("content".getBytes(StandardCharsets.UTF_8));
expectedContent = new BytesArray("{\"key\": \"value\"}".getBytes(StandardCharsets.UTF_8));
extensionUniqueId1 = "ext_1";
userPrincipal = () -> "user1";
extensionTokenProcessor = new ExtensionTokenProcessor(extensionUniqueId1);
Expand Down Expand Up @@ -94,6 +97,10 @@ public void testExtensionRestRequest() throws Exception {
assertEquals(expectedContent, request.content());
assertTrue(request.isContentConsumed());

XContentParser parser = request.contentParser(NamedXContentRegistry.EMPTY);
Map<String, String> contentMap = parser.mapStrings();
assertEquals("value", contentMap.get("key"));

assertEquals(expectedRequestIssuerIdentity, request.getRequestIssuerIdentity());

try (BytesStreamOutput out = new BytesStreamOutput()) {
Expand Down Expand Up @@ -129,6 +136,9 @@ public void testExtensionRestRequestWithNoContent() throws Exception {
assertEquals(0, request.content().length());
assertEquals(expectedRequestIssuerIdentity, request.getRequestIssuerIdentity());

final ExtensionRestRequest requestWithNoContent = request;
assertThrows(OpenSearchParseException.class, () -> requestWithNoContent.contentParser(NamedXContentRegistry.EMPTY));

try (BytesStreamOutput out = new BytesStreamOutput()) {
request.writeTo(out);
out.flush();
Expand Down Expand Up @@ -165,6 +175,9 @@ public void testExtensionRestRequestWithPlainTextContent() throws Exception {
assertEquals(expectedText, request.content());
assertEquals(expectedRequestIssuerIdentity, request.getRequestIssuerIdentity());

final ExtensionRestRequest requestWithNoContentType = request;
assertThrows(OpenSearchParseException.class, () -> requestWithNoContentType.contentParser(NamedXContentRegistry.EMPTY));

try (BytesStreamOutput out = new BytesStreamOutput()) {
request.writeTo(out);
out.flush();
Expand Down

0 comments on commit 401c210

Please sign in to comment.