Skip to content

Commit

Permalink
Add content and expand param getters
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 30, 2022
1 parent 2caf42f commit 159e6f2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/main/java/org/opensearch/sdk/ExtensionRestResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class ExtensionRestResponse extends BytesRestResponse {
* Key passed in {@link BytesRestResponse} headers to identify parameters consumed by the handler. For internal use.
*/
static final String CONSUMED_PARAMS_KEY = "extension.consumed.parameters";
/**
* Key passed in {@link BytesRestResponse} headers to identify content consumed by the handler. For internal use.
*/
static final String CONSUMED_CONTENT_KEY = "extension.consumed.content";

/**
* Creates a new response based on {@link XContentBuilder}.
Expand All @@ -34,7 +38,7 @@ public class ExtensionRestResponse extends BytesRestResponse {
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, XContentBuilder builder) {
super(status, builder);
addConsumedParamHeader(request.consumedParams());
addConsumedHeaders(request.consumedParams(), request.isContentConsumed());
}

/**
Expand All @@ -46,7 +50,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, XC
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String content) {
super(status, content);
addConsumedParamHeader(request.consumedParams());
addConsumedHeaders(request.consumedParams(), request.isContentConsumed());
}

/**
Expand All @@ -59,7 +63,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, String content) {
super(status, contentType, content);
addConsumedParamHeader(request.consumedParams());
addConsumedHeaders(request.consumedParams(), request.isContentConsumed());
}

/**
Expand All @@ -72,7 +76,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, byte[] content) {
super(status, contentType, content);
addConsumedParamHeader(request.consumedParams());
addConsumedHeaders(request.consumedParams(), request.isContentConsumed());
}

/**
Expand All @@ -85,10 +89,11 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, BytesReference content) {
super(status, contentType, content);
addConsumedParamHeader(request.consumedParams());
addConsumedHeaders(request.consumedParams(), request.isContentConsumed());
}

private void addConsumedParamHeader(List<String> consumedParams) {
private void addConsumedHeaders(List<String> consumedParams, boolean contentConusmed) {
consumedParams.stream().forEach(p -> addHeader(CONSUMED_PARAMS_KEY, p));
addHeader(CONSUMED_CONTENT_KEY, Boolean.toString(contentConusmed));
}
}
10 changes: 9 additions & 1 deletion src/test/java/org/opensearch/sdk/TestExtensionRestResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -34,10 +35,12 @@ public void setUp() throws Exception {
super.setUp();
testText = "plain text";
testBytes = new byte[] { 1, 2 };
request = new ExtensionRestRequest(Method.GET, "/foo", Collections.emptyMap(), null);
request = new ExtensionRestRequest(Method.GET, "/foo", Collections.emptyMap(), null, new BytesArray("Text Content"), null);
// consume params "foo" and "bar"
request.param("foo");
request.param("bar");
// consume content
request.content();
}

@Test
Expand All @@ -55,6 +58,7 @@ public void testConstructorWithBuilder() throws IOException {
for (String param : consumedParams) {
assertTrue(request.consumedParams().contains(param));
}
assertTrue(request.isContentConsumed());
}

@Test
Expand All @@ -68,6 +72,7 @@ public void testConstructorWithPlainText() {
for (String param : consumedParams) {
assertTrue(request.consumedParams().contains(param));
}
assertTrue(request.isContentConsumed());
}

@Test
Expand All @@ -82,6 +87,7 @@ public void testConstructorWithText() {
for (String param : consumedParams) {
assertTrue(request.consumedParams().contains(param));
}
assertTrue(request.isContentConsumed());
}

@Test
Expand All @@ -95,6 +101,7 @@ public void testConstructorWithByteArray() {
for (String param : consumedParams) {
assertTrue(request.consumedParams().contains(param));
}
assertTrue(request.isContentConsumed());
}

@Test
Expand All @@ -113,5 +120,6 @@ public void testConstructorWithBytesReference() {
for (String param : consumedParams) {
assertTrue(request.consumedParams().contains(param));
}
assertTrue(request.isContentConsumed());
}
}
3 changes: 3 additions & 0 deletions src/test/java/org/opensearch/sdk/TestExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.jupiter.api.Test;
import org.opensearch.Version;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.io.stream.NamedWriteableRegistryResponse;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.Setting.Property;
Expand Down Expand Up @@ -167,6 +168,8 @@ public void testHandleExtensionRestRequest() throws Exception {
Method.GET,
"/foo",
Collections.emptyMap(),
null,
new BytesArray("bar"),
ext.generateToken(userPrincipal)
);
RestExecuteOnExtensionResponse response = extensionsRunner.handleRestExecuteOnExtensionRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.opensearch.identity.PrincipalIdentifierToken;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.rest.BytesRestResponse;
Expand Down Expand Up @@ -56,15 +57,31 @@ public void testHandleRequest() {
PrincipalIdentifierToken token = extensionTokenProcessor.generateToken(userPrincipal);
Map<String, String> params = Collections.emptyMap();

ExtensionRestRequest getRequest = new ExtensionRestRequest(Method.GET, "/hello", params, token);
ExtensionRestRequest getRequest = new ExtensionRestRequest(Method.GET, "/hello", params, null, new BytesArray(""), token);
ExtensionRestRequest putRequest = new ExtensionRestRequest(
Method.PUT,
"/hello/Passing+Test",
Map.of("name", "Passing+Test"),
null,
new BytesArray(""),
token
);
ExtensionRestRequest badRequest = new ExtensionRestRequest(
Method.PUT,
"/hello/Bad%Request",
Map.of("name", "Bad%Request"),
null,
new BytesArray(""),
token
);
ExtensionRestRequest unsuccessfulRequest = new ExtensionRestRequest(
Method.POST,
"/goodbye",
params,
null,
new BytesArray(""),
token
);
ExtensionRestRequest badRequest = new ExtensionRestRequest(Method.PUT, "/hello/Bad%Request", Map.of("name", "Bad%Request"), token);
ExtensionRestRequest unsuccessfulRequest = new ExtensionRestRequest(Method.POST, "/goodbye", params, token);

RestResponse response = restHelloAction.handleRequest(getRequest);
assertEquals(RestStatus.OK, response.status());
Expand Down

0 comments on commit 159e6f2

Please sign in to comment.