Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for headers for ExtensionRestRequest #643

Merged
merged 12 commits into from
Apr 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.extensions.rest.RestExecuteOnExtensionResponse;
import org.opensearch.http.HttpRequest;
import org.opensearch.http.HttpResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.rest.RestStatus;
import org.opensearch.sdk.ExtensionRestHandler;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.SDKNamedXContentRegistry;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.opensearch.sdk.rest.SDKHttpRequest;
import org.opensearch.sdk.rest.SDKRestRequest;

import org.opensearch.sdk.ExtensionRestPathRegistry;

Expand Down Expand Up @@ -77,73 +69,17 @@ public RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(Extens
);
}

// Temporary code to create a RestRequest from the ExtensionRestRequest before header code added
// Remove this and replace with SDKRestRequest being generated by this PR:
// https://github.com/opensearch-project/opensearch-sdk-java/pull/605
RestRequest restRequest = RestRequest.request(sdkNamedXContentRegistry.getRegistry(), new HttpRequest() {

@Override
public Method method() {
return request.method();
}

@Override
public String uri() {
// path strips query off uri but probably want to pass the whole uri
// this will make the request behave as expected (without query params)
return request.path();
}

@Override
public BytesReference content() {
return request.content();
}

@Override
public Map<String, List<String>> getHeaders() {
// This effectively recreates the only header we need right now
// PR replacing this will pass more headers
XContentType xContentType = request.getXContentType();
return xContentType == null ? Collections.emptyMap() : Map.of("Content-Type", List.of(xContentType.mediaType()));
}

@Override
public List<String> 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);
SDKRestRequest sdkRestRequest = new SDKRestRequest(
sdkNamedXContentRegistry.getRegistry(),
request.params(),
request.path(),
request.headers(),
new SDKHttpRequest(request),
null
);

// Get response from extension
ExtensionRestResponse response = restHandler.handleRequest(restRequest);
ExtensionRestResponse response = restHandler.handleRequest(sdkRestRequest);
logger.info("Sending extension response to OpenSearch: " + response.status());
return new RestExecuteOnExtensionResponse(
response.status(),
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/org/opensearch/sdk/rest/SDKHttpRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright OpenSearch Contributors
* 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.rest;

import org.opensearch.common.bytes.BytesReference;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.http.HttpRequest;
import org.opensearch.http.HttpResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestStatus;

import java.util.List;
import java.util.Map;

/**
* This class helps to get instance of HttpRequest
*/
public class SDKHttpRequest implements HttpRequest {
private final RestRequest.Method method;
private final String uri;
private final BytesReference content;
private final Map<String, List<String>> headers;
private final HttpVersion httpVersion;

/**
* Instantiates this class with a copy of {@link ExtensionRestRequest}
*
* @param request The request
*/
public SDKHttpRequest(ExtensionRestRequest request) {
this.method = request.method();
this.uri = request.uri();
this.content = request.content();
this.headers = request.headers();
this.httpVersion = request.httpVersion();
}

@Override
public RestRequest.Method method() {
return method;
}

@Override
public String uri() {
return uri;
}

@Override
public BytesReference content() {
return content;
}

@Override
public Map<String, List<String>> getHeaders() {
return headers;
}

dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
/**
* Not implemented. Does nothing.
* @return null
*/
@Override
public List<String> strictCookies() {
return null;
}

@Override
public HttpVersion protocolVersion() {
return httpVersion;
}

/**
* Not implemented. Does nothing.
* @return null
*/
@Override
public HttpRequest removeHeader(String s) {
return null;
}

/**
* Not implemented. Does nothing.
* @param restStatus response status
* @param bytesReference content
* @return null
*/
@Override
public HttpResponse createResponse(RestStatus restStatus, BytesReference bytesReference) {
return null;
}

/**
* Not implemented. Does nothing.
* @return null
*/
@Override
public Exception getInboundException() {
return null;
}

/**
* Not implemented. Does nothing.
*/
@Override
public void release() {

}

/**
* Not implemented. Does nothing.
* @return null
*/
@Override
public HttpRequest releaseAndCopy() {
return null;
}
}
44 changes: 44 additions & 0 deletions src/main/java/org/opensearch/sdk/rest/SDKRestRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright OpenSearch Contributors
* 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.rest;

import java.util.List;
import java.util.Map;

import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.http.HttpChannel;
import org.opensearch.http.HttpRequest;
import org.opensearch.rest.RestRequest;

/**
* This class helps to get instance of RestRequest
*/
public class SDKRestRequest extends RestRequest {
/**
* Instantiates this class with request's params
*
* @param xContentRegistry The request's content registry
* @param params The request's params
* @param path The request's path
* @param headers The request's headers
* @param httpRequest The request's httpRequest
* @param httpChannel The request's http channel
*/
public SDKRestRequest(
NamedXContentRegistry xContentRegistry,
Map<String, String> params,
String path,
Map<String, List<String>> headers,
HttpRequest httpRequest,
HttpChannel httpChannel
) {
super(xContentRegistry, params, path, headers, httpRequest, httpChannel);
}
}
Loading