From b993a6af878801bdba5144936916c58b53ba1582 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Thu, 30 Mar 2023 10:01:28 -0700 Subject: [PATCH] Update ExtensionRestResponse to handle RestRequest object (#6874) Signed-off-by: Daniel Widdis Signed-off-by: Valentin Mitrofanov --- .../rest/ExtensionRestResponse.java | 11 +-- .../java/org/opensearch/rest/RestRequest.java | 2 +- .../rest/ExtensionRestResponseTests.java | 83 +++++++++++++++---- 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/server/src/main/java/org/opensearch/extensions/rest/ExtensionRestResponse.java b/server/src/main/java/org/opensearch/extensions/rest/ExtensionRestResponse.java index 9455df1bd0d6b..5cbc877f39eda 100644 --- a/server/src/main/java/org/opensearch/extensions/rest/ExtensionRestResponse.java +++ b/server/src/main/java/org/opensearch/extensions/rest/ExtensionRestResponse.java @@ -11,6 +11,7 @@ import org.opensearch.common.bytes.BytesReference; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.rest.BytesRestResponse; +import org.opensearch.rest.RestRequest; import org.opensearch.rest.RestStatus; import java.util.List; @@ -32,7 +33,7 @@ public class ExtensionRestResponse extends BytesRestResponse { * @param status The REST status. * @param builder The builder for the response. */ - public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, XContentBuilder builder) { + public ExtensionRestResponse(RestRequest request, RestStatus status, XContentBuilder builder) { super(status, builder); this.consumedParams = request.consumedParams(); this.contentConsumed = request.isContentConsumed(); @@ -45,7 +46,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, XC * @param status The REST status. * @param content A plain text response string. */ - public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String content) { + public ExtensionRestResponse(RestRequest request, RestStatus status, String content) { super(status, content); this.consumedParams = request.consumedParams(); this.contentConsumed = request.isContentConsumed(); @@ -59,7 +60,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St * @param contentType The content type of the response string. * @param content A response string. */ - public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, String content) { + public ExtensionRestResponse(RestRequest request, RestStatus status, String contentType, String content) { super(status, contentType, content); this.consumedParams = request.consumedParams(); this.contentConsumed = request.isContentConsumed(); @@ -73,7 +74,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St * @param contentType The content type of the response bytes. * @param content Response bytes. */ - public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, byte[] content) { + public ExtensionRestResponse(RestRequest request, RestStatus status, String contentType, byte[] content) { super(status, contentType, content); this.consumedParams = request.consumedParams(); this.contentConsumed = request.isContentConsumed(); @@ -87,7 +88,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St * @param contentType The content type of the response bytes. * @param content Response bytes. */ - public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, BytesReference content) { + public ExtensionRestResponse(RestRequest request, RestStatus status, String contentType, BytesReference content) { super(status, contentType, content); this.consumedParams = request.consumedParams(); this.contentConsumed = request.isContentConsumed(); diff --git a/server/src/main/java/org/opensearch/rest/RestRequest.java b/server/src/main/java/org/opensearch/rest/RestRequest.java index eccaacc73c068..b4f70e359d618 100644 --- a/server/src/main/java/org/opensearch/rest/RestRequest.java +++ b/server/src/main/java/org/opensearch/rest/RestRequest.java @@ -382,7 +382,7 @@ public Map params() { * * @return the list of currently consumed parameters. */ - List consumedParams() { + public List consumedParams() { return new ArrayList<>(consumedParams); } diff --git a/server/src/test/java/org/opensearch/extensions/rest/ExtensionRestResponseTests.java b/server/src/test/java/org/opensearch/extensions/rest/ExtensionRestResponseTests.java index 5d5d3769a9427..cdc79e9778e2d 100644 --- a/server/src/test/java/org/opensearch/extensions/rest/ExtensionRestResponseTests.java +++ b/server/src/test/java/org/opensearch/extensions/rest/ExtensionRestResponseTests.java @@ -11,11 +11,18 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Collections; +import java.util.List; +import java.util.Map; import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesReference; import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.http.HttpRequest; +import org.opensearch.http.HttpResponse; +import org.opensearch.http.HttpRequest.HttpVersion; import org.opensearch.common.xcontent.XContentType; +import org.opensearch.rest.RestRequest; +import org.opensearch.rest.RestStatus; import org.opensearch.rest.RestRequest.Method; import org.opensearch.test.OpenSearchTestCase; @@ -38,15 +45,63 @@ public void setUp() throws Exception { testBytes = new byte[] { 1, 2 }; } - private ExtensionRestRequest generateTestRequest() { - ExtensionRestRequest request = new ExtensionRestRequest( - Method.GET, - "/foo", - Collections.emptyMap(), - null, - new BytesArray("Text Content"), - null - ); + private RestRequest generateTestRequest() { + RestRequest request = RestRequest.request(null, new HttpRequest() { + + @Override + public Method method() { + return Method.GET; + } + + @Override + public String uri() { + return "/foo"; + } + + @Override + public BytesReference content() { + return new BytesArray("Text Content"); + } + + @Override + public Map> getHeaders() { + return Collections.emptyMap(); + } + + @Override + public List strictCookies() { + return Collections.emptyList(); + } + + @Override + public HttpVersion protocolVersion() { + return null; + } + + @Override + public HttpRequest removeHeader(String header) { + // we don't use + return null; + } + + @Override + public HttpResponse createResponse(RestStatus status, BytesReference content) { + return null; + } + + @Override + public Exception getInboundException() { + return null; + } + + @Override + public void release() {} + + @Override + public HttpRequest releaseAndCopy() { + return null; + } + }, null); // consume params "foo" and "bar" request.param("foo"); request.param("bar"); @@ -60,7 +115,7 @@ public void testConstructorWithBuilder() throws IOException { builder.startObject(); builder.field("status", ACCEPTED); builder.endObject(); - ExtensionRestRequest request = generateTestRequest(); + RestRequest request = generateTestRequest(); ExtensionRestResponse response = new ExtensionRestResponse(request, OK, builder); assertEquals(OK, response.status()); @@ -73,7 +128,7 @@ public void testConstructorWithBuilder() throws IOException { } public void testConstructorWithPlainText() { - ExtensionRestRequest request = generateTestRequest(); + RestRequest request = generateTestRequest(); ExtensionRestResponse response = new ExtensionRestResponse(request, OK, testText); assertEquals(OK, response.status()); @@ -86,7 +141,7 @@ public void testConstructorWithPlainText() { } public void testConstructorWithText() { - ExtensionRestRequest request = generateTestRequest(); + RestRequest request = generateTestRequest(); ExtensionRestResponse response = new ExtensionRestResponse(request, OK, TEXT_CONTENT_TYPE, testText); assertEquals(OK, response.status()); @@ -100,7 +155,7 @@ public void testConstructorWithText() { } public void testConstructorWithByteArray() { - ExtensionRestRequest request = generateTestRequest(); + RestRequest request = generateTestRequest(); ExtensionRestResponse response = new ExtensionRestResponse(request, OK, OCTET_CONTENT_TYPE, testBytes); assertEquals(OK, response.status()); @@ -113,7 +168,7 @@ public void testConstructorWithByteArray() { } public void testConstructorWithBytesReference() { - ExtensionRestRequest request = generateTestRequest(); + RestRequest request = generateTestRequest(); ExtensionRestResponse response = new ExtensionRestResponse( request, OK,