-
Notifications
You must be signed in to change notification settings - Fork 272
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
Use BytesRestResponse constructor with contentType in asRestResponse #3717
Changes from 6 commits
025aa8f
8452abe
40770c9
ce089e8
49f9bee
bf52697
5fe7396
147be07
e95cbe2
87b1203
b8477eb
5d2a32a
90255b7
538d2eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,15 @@ | |
package org.opensearch.security.filter; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.http.HttpHeaders; | ||
|
||
import org.opensearch.common.xcontent.XContentFactory; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestResponse; | ||
|
@@ -26,26 +30,63 @@ public class SecurityResponse { | |
public static final Map<String, String> CONTENT_TYPE_APP_JSON = Map.of(HttpHeaders.CONTENT_TYPE, "application/json"); | ||
|
||
private final int status; | ||
private final Map<String, String> headers; | ||
private Map<String, List<String>> headers; | ||
private final String body; | ||
private final String contentType; | ||
|
||
public SecurityResponse(final int status, final Exception e) { | ||
this.status = status; | ||
this.headers = CONTENT_TYPE_APP_JSON; | ||
populateHeaders(CONTENT_TYPE_APP_JSON); | ||
this.body = generateFailureMessage(e); | ||
this.contentType = XContentType.JSON.mediaType(); | ||
} | ||
|
||
public SecurityResponse(final int status, String body) { | ||
this.status = status; | ||
this.body = body; | ||
this.contentType = null; | ||
} | ||
|
||
public SecurityResponse(final int status, final Map<String, String> headers, final String body) { | ||
this.status = status; | ||
this.headers = headers; | ||
populateHeaders(headers); | ||
this.body = body; | ||
this.contentType = null; | ||
} | ||
|
||
public SecurityResponse(final int status, final Map<String, String> headers, final String body, String contentType) { | ||
this.status = status; | ||
this.body = body; | ||
this.contentType = contentType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. call other constructor here |
||
populateHeaders(headers); | ||
} | ||
|
||
private void populateHeaders(Map<String, String> headers) { | ||
if (headers != null) { | ||
headers.entrySet().forEach(entry -> addHeader(entry.getKey(), entry.getValue())); | ||
} | ||
} | ||
|
||
/** | ||
* Add a custom header. | ||
*/ | ||
public void addHeader(String name, String value) { | ||
if (headers == null) { | ||
headers = new HashMap<>(2); | ||
} | ||
List<String> header = headers.get(name); | ||
if (header == null) { | ||
header = new ArrayList<>(); | ||
headers.put(name, header); | ||
} | ||
header.add(value); | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public Map<String, String> getHeaders() { | ||
public Map<String, List<String>> getHeaders() { | ||
return headers; | ||
} | ||
|
||
|
@@ -54,9 +95,14 @@ public String getBody() { | |
} | ||
|
||
public RestResponse asRestResponse() { | ||
final RestResponse restResponse = new BytesRestResponse(RestStatus.fromCode(getStatus()), getBody()); | ||
final RestResponse restResponse; | ||
if (this.contentType != null) { | ||
restResponse = new BytesRestResponse(RestStatus.fromCode(getStatus()), this.contentType, getBody()); | ||
} else { | ||
restResponse = new BytesRestResponse(RestStatus.fromCode(getStatus()), getBody()); | ||
} | ||
if (getHeaders() != null) { | ||
getHeaders().forEach(restResponse::addHeader); | ||
getHeaders().entrySet().forEach(entry -> { entry.getValue().forEach(value -> restResponse.addHeader(entry.getKey(), value)); }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should understand why the header approach was not working and which part of the code is populating the content type header now? |
||
} | ||
return restResponse; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.filter; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.rest.RestResponse; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class SecurityResponseTests { | ||
|
||
@Test | ||
public void testSecurityResponseHasSingleContentType() { | ||
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_OK, null, "foo bar", XContentType.JSON.mediaType()); | ||
|
||
final RestResponse restResponse = response.asRestResponse(); | ||
assertThat(restResponse.status(), equalTo(RestStatus.OK)); | ||
assertThat(restResponse.contentType(), equalTo(XContentType.JSON.mediaType())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should understand who populates the header if we explicitly don't populate here?