Skip to content

Commit

Permalink
Added the addition of the server version and distribution as a respon… (
Browse files Browse the repository at this point in the history
opensearch-project#8084)

* Added server distribution and version into response header

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Removed extra lines

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Update server/src/main/java/org/opensearch/http/DefaultRestChannel.java

Co-authored-by: Owais Kazi <owaiskazi19@gmail.com>
Signed-off-by: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com>

* Moved defining of server header string

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Removed wildcard import

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Applied spotless to files

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Added to testHeadersSet test within the default rest channel test file to test that server version is returned

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Ran spotless

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Added comment

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Created static string for reused server header title and name of application strings, also refactored server header to server version

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Refactored hash map from server header to server version and updated test

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Ran spotless

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Moved server version field from global variable to local, defined sever string as static value

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Added prefix to header title

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Removed redundant serverVersion field and instead inlined creation of map passed into addCustomHeader method

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Ran spotless

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Removed redundant OPEN_SEARCH_NAME_VALUE

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Update server/src/test/java/org/opensearch/http/DefaultRestChannelTests.java

Co-authored-by: Andriy Redko <drreta@gmail.com>
Signed-off-by: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com>

* Moved change log addition to 2.x unreleased and wrote message in imperative

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Corrected name of fetched header in header test

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

* Added server version header value as constant

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>

---------

Signed-off-by: Sam Hobbs <shobbs@scottlogic.com>
Signed-off-by: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com>
Co-authored-by: Owais Kazi <owaiskazi19@gmail.com>
Co-authored-by: Andriy Redko <drreta@gmail.com>
Signed-off-by: sahil buddharaju <sahilbud@amazon.com>
  • Loading branch information
3 people authored and sahil buddharaju committed Jul 18, 2023
1 parent ac25fd6 commit 172d16c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Create concept of persistent ThreadContext headers that are unstashable ([#8291]()https://github.com/opensearch-project/OpenSearch/pull/8291)
- [Search pipelines] Add Global Ignore_failure options for Processors ([#8373](https://github.com/opensearch-project/OpenSearch/pull/8373))
- Enable Partial Flat Object ([#7997](https://github.com/opensearch-project/OpenSearch/pull/7997))
- Add server version as REST response header [#6583](https://github.com/opensearch-project/OpenSearch/issues/6583)
- Add jdk.incubator.vector module support for JDK 20+ ([#8601](https://github.com/opensearch-project/OpenSearch/pull/8601))
- Introduce full support for Search Pipeline ([#8613](https://github.com/opensearch-project/OpenSearch/pull/8613))

Expand Down
11 changes: 10 additions & 1 deletion server/src/main/java/org/opensearch/http/DefaultRestChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.http;

import org.opensearch.Build;
import org.opensearch.action.ActionListener;
import org.opensearch.common.Nullable;
import org.opensearch.core.common.bytes.BytesArray;
Expand All @@ -48,7 +49,6 @@
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.core.rest.RestStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -69,13 +69,20 @@ public class DefaultRestChannel extends AbstractRestChannel implements RestChann
static final String CONTENT_TYPE = "content-type";
static final String CONTENT_LENGTH = "content-length";
static final String SET_COOKIE = "set-cookie";
static final String SERVER_VERSION = "X-OpenSearch-Version";
static final String SERVER_VERSION_VALUE = "OpenSearch/"
+ Build.CURRENT.getQualifiedVersion()
+ " ("
+ Build.CURRENT.getDistribution()
+ ")";

private final HttpRequest httpRequest;
private final BigArrays bigArrays;
private final HttpHandlingSettings settings;
private final ThreadContext threadContext;
private final HttpChannel httpChannel;
private final CorsHandler corsHandler;
private final Map<String, List<String>> SERVER_VERSION_HEADER = Map.of(SERVER_VERSION, List.of(SERVER_VERSION_VALUE));

@Nullable
private final HttpTracer tracerLog;
Expand Down Expand Up @@ -147,6 +154,8 @@ public void sendResponse(RestResponse restResponse) {
addCustomHeaders(httpResponse, restResponse.getHeaders());
addCustomHeaders(httpResponse, threadContext.getResponseHeaders());

addCustomHeaders(httpResponse, SERVER_VERSION_HEADER);

// If our response doesn't specify a content-type header, set one
setHeaderField(httpResponse, CONTENT_TYPE, restResponse.contentType(), false);
// If our response has no content-length, calculate and set one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.http;

import org.opensearch.Build;
import org.opensearch.action.ActionListener;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
Expand Down Expand Up @@ -189,6 +190,10 @@ public void testHeadersSet() {
assertEquals("abc", headers.get(Task.X_OPAQUE_ID).get(0));
assertEquals(Integer.toString(resp.content().length()), headers.get(DefaultRestChannel.CONTENT_LENGTH).get(0));
assertEquals(resp.contentType(), headers.get(DefaultRestChannel.CONTENT_TYPE).get(0));
assertEquals(
"OpenSearch/" + Build.CURRENT.getQualifiedVersion() + " (" + Build.CURRENT.getDistribution() + ")",
headers.get("X-OpenSearch-Version").get(0)
);
}

public void testCookiesSet() {
Expand Down

0 comments on commit 172d16c

Please sign in to comment.