-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass consumed params in RestResponse header
Signed-off-by: Daniel Widdis <[email protected]>
- Loading branch information
Showing
5 changed files
with
227 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/org/opensearch/sdk/ExtensionRestResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.sdk; | ||
|
||
import java.util.List; | ||
|
||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestStatus; | ||
|
||
/** | ||
* A subclass of {@link BytesRestResponse} which processes the consumed parameters into a custom header. | ||
*/ | ||
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"; | ||
|
||
/** | ||
* Creates a new response based on {@link XContentBuilder}. | ||
* | ||
* @param status The REST status. | ||
* @param builder The builder for the response. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, XContentBuilder builder, List<String> consumedParams) { | ||
super(status, builder); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a new plain text response. | ||
* | ||
* @param status The REST status. | ||
* @param content A plain text response string. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String content, List<String> consumedParams) { | ||
super(status, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a new plain text response. | ||
* | ||
* @param status The REST status. | ||
* @param contentType The content type of the response string. | ||
* @param content A response string. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String contentType, String content, List<String> consumedParams) { | ||
super(status, contentType, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a binary response. | ||
* | ||
* @param status The REST status. | ||
* @param contentType The content type of the response bytes. | ||
* @param content Response bytes. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String contentType, byte[] content, List<String> consumedParams) { | ||
super(status, contentType, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a binary response. | ||
* | ||
* @param status The REST status. | ||
* @param contentType The content type of the response bytes. | ||
* @param content Response bytes. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String contentType, BytesReference content, List<String> consumedParams) { | ||
super(status, contentType, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
private void addConsumedParamHeader(List<String> consumedParams) { | ||
consumedParams.stream().forEach(p -> addHeader(CONSUMED_PARAMS_KEY, p)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/test/java/org/opensearch/sdk/TestExtensionRestResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.opensearch.sdk; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import static org.opensearch.rest.BytesRestResponse.TEXT_CONTENT_TYPE; | ||
import static org.opensearch.rest.RestStatus.ACCEPTED; | ||
import static org.opensearch.rest.RestStatus.OK; | ||
import static org.opensearch.sdk.ExtensionRestResponse.CONSUMED_PARAMS_KEY; | ||
|
||
public class TestExtensionRestResponse extends OpenSearchTestCase { | ||
|
||
private static final String OCTET_CONTENT_TYPE = "application/octet-stream"; | ||
private static final String JSON_CONTENT_TYPE = "application/json; charset=UTF-8"; | ||
|
||
private String testText; | ||
private byte[] testBytes; | ||
private List<String> testConsumedParams; | ||
|
||
@Override | ||
@BeforeEach | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
testText = "plain text"; | ||
testBytes = new byte[] { 1, 2 }; | ||
testConsumedParams = List.of("foo", "bar"); | ||
} | ||
|
||
@Test | ||
public void testConstructorWithBuilder() throws IOException { | ||
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); | ||
builder.startObject(); | ||
builder.field("status", ACCEPTED); | ||
builder.endObject(); | ||
ExtensionRestResponse response = new ExtensionRestResponse(OK, builder, testConsumedParams); | ||
|
||
assertEquals(OK, response.status()); | ||
assertEquals(JSON_CONTENT_TYPE, response.contentType()); | ||
assertEquals("{\"status\":\"ACCEPTED\"}", response.content().utf8ToString()); | ||
List<String> consumedParams = response.getHeaders().get(CONSUMED_PARAMS_KEY); | ||
for (String param : consumedParams) { | ||
assertTrue(testConsumedParams.contains(param)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConstructorWithPlainText() { | ||
ExtensionRestResponse response = new ExtensionRestResponse(OK, testText, testConsumedParams); | ||
|
||
assertEquals(OK, response.status()); | ||
assertEquals(TEXT_CONTENT_TYPE, response.contentType()); | ||
assertEquals(testText, response.content().utf8ToString()); | ||
List<String> consumedParams = response.getHeaders().get(CONSUMED_PARAMS_KEY); | ||
for (String param : consumedParams) { | ||
assertTrue(testConsumedParams.contains(param)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConstructorWithText() { | ||
ExtensionRestResponse response = new ExtensionRestResponse(OK, TEXT_CONTENT_TYPE, testText, testConsumedParams); | ||
|
||
assertEquals(OK, response.status()); | ||
assertEquals(TEXT_CONTENT_TYPE, response.contentType()); | ||
assertEquals(testText, response.content().utf8ToString()); | ||
|
||
List<String> consumedParams = response.getHeaders().get(CONSUMED_PARAMS_KEY); | ||
for (String param : consumedParams) { | ||
assertTrue(testConsumedParams.contains(param)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConstructorWithByteArray() { | ||
ExtensionRestResponse response = new ExtensionRestResponse(OK, OCTET_CONTENT_TYPE, testBytes, testConsumedParams); | ||
|
||
assertEquals(OK, response.status()); | ||
assertEquals(OCTET_CONTENT_TYPE, response.contentType()); | ||
assertArrayEquals(testBytes, BytesReference.toBytes(response.content())); | ||
List<String> consumedParams = response.getHeaders().get(CONSUMED_PARAMS_KEY); | ||
for (String param : consumedParams) { | ||
assertTrue(testConsumedParams.contains(param)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConstructorWithBytesReference() { | ||
ExtensionRestResponse response = new ExtensionRestResponse( | ||
OK, | ||
OCTET_CONTENT_TYPE, | ||
BytesReference.fromByteBuffer(ByteBuffer.wrap(testBytes, 0, 2)), | ||
testConsumedParams | ||
); | ||
|
||
assertEquals(OK, response.status()); | ||
assertEquals(OCTET_CONTENT_TYPE, response.contentType()); | ||
assertArrayEquals(testBytes, BytesReference.toBytes(response.content())); | ||
List<String> consumedParams = response.getHeaders().get(CONSUMED_PARAMS_KEY); | ||
for (String param : consumedParams) { | ||
assertTrue(testConsumedParams.contains(param)); | ||
} | ||
} | ||
} |