Skip to content

Commit

Permalink
Adapt MediaType library change in core (#3050)
Browse files Browse the repository at this point in the history
* Adapt the MediaType changes in core

Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 authored Jul 25, 2023
1 parent 93ee2ba commit 8063e1b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private boolean handleLowLevel(RestRequest restRequest, RestChannel restChannel)
XPathExpressionException, ParserConfigurationException, SAXException, SettingsException {
try {

if (restRequest.getXContentType() != XContentType.JSON) {
if (restRequest.getMediaType() != XContentType.JSON) {
throw new OpenSearchSecurityException(
"/_opendistro/_security/api/authtoken expects content with type application/json",
RestStatus.UNSUPPORTED_MEDIA_TYPE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.transport.TransportAddress;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -657,7 +658,7 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
// originalResult.internalSourceRef()));

// current source, normally not null or empty
msg.addTupleToRequestBody(new Tuple<XContentType, BytesReference>(XContentType.JSON, currentIndex.source()));
msg.addTupleToRequestBody(new Tuple<MediaType, BytesReference>(XContentType.JSON, currentIndex.source()));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.transport.TransportAddress;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.Strings;
Expand Down Expand Up @@ -205,7 +206,7 @@ void addSecurityConfigWriteDiffSource(final String diff, final String id) {
// }
// }

public void addTupleToRequestBody(Tuple<XContentType, BytesReference> xContentTuple) {
public void addTupleToRequestBody(Tuple<MediaType, BytesReference> xContentTuple) {
if (xContentTuple != null) {
try {
auditInfo.put(REQUEST_BODY, XContentHelper.convertToJson(xContentTuple.v2(), false, xContentTuple.v1()));
Expand Down Expand Up @@ -377,7 +378,7 @@ void addRestRequestInfo(final RestRequest request, final AuditConfig.Filter filt
addRestMethod(request.method());
if (filter.shouldLogRequestBody() && request.hasContentOrSourceParam()) {
try {
final Tuple<XContentType, BytesReference> xContentTuple = request.contentOrSourceParam();
final Tuple<MediaType, BytesReference> xContentTuple = request.contentOrSourceParam();
final String requestBody = XContentHelper.convertToJson(xContentTuple.v2(), false, xContentTuple.v1());
if (path != null
&& requestBody != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.transport.TransportAddress;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.index.Index;
Expand Down Expand Up @@ -381,7 +382,7 @@ private static void addIndicesSourceSafe(
final String[] indices,
final IndexNameExpressionResolver resolver,
final ClusterService cs,
final XContentType xContentType,
final MediaType mediaType,
final Object source,
final Settings settings,
boolean resolveIndices,
Expand Down Expand Up @@ -423,27 +424,27 @@ private static void addIndicesSourceSafe(
if (sourceIsSensitive && source != null) {
if (!allIndicesMatcher.test(securityIndex)) {
if (source instanceof BytesReference) {
msg.addTupleToRequestBody(convertSource(xContentType, (BytesReference) source));
msg.addTupleToRequestBody(convertSource(mediaType, (BytesReference) source));
} else {
msg.addMapToRequestBody((Map) source);
}
}
} else if (source != null) {
if (source instanceof BytesReference) {
msg.addTupleToRequestBody(convertSource(xContentType, (BytesReference) source));
msg.addTupleToRequestBody(convertSource(mediaType, (BytesReference) source));
} else {
msg.addMapToRequestBody((Map) source);
}
}
}
}

private static Tuple<XContentType, BytesReference> convertSource(XContentType type, BytesReference bytes) {
private static Tuple<MediaType, BytesReference> convertSource(MediaType type, BytesReference bytes) {
if (type == null) {
type = XContentType.JSON;
}

return new Tuple<XContentType, BytesReference>(type, bytes);
return new Tuple<MediaType, BytesReference>(type, bytes);
}

private static String[] arrayOrEmpty(String[] array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public PatchableResourceApiAction(
}

private void handlePatch(RestChannel channel, final RestRequest request, final Client client) throws IOException {
if (request.getXContentType() != XContentType.JSON) {
if (request.getMediaType() != XContentType.JSON) {
badRequestResponse(channel, "PATCH accepts only application/json");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;
Expand Down Expand Up @@ -992,7 +992,7 @@ public XContentBuilder newErrorBuilder() throws IOException {
}

@Override
public XContentBuilder newBuilder(XContentType xContentType, boolean useFiltering) throws IOException {
public XContentBuilder newBuilder(MediaType mediaType, boolean useFiltering) throws IOException {
return null;
}

Expand All @@ -1018,8 +1018,7 @@ public void sendResponse(RestResponse response) {
}

@Override
public XContentBuilder newBuilder(XContentType xContentType, XContentType responseContentType, boolean useFiltering)
throws IOException {
public XContentBuilder newBuilder(MediaType mediaType, MediaType responseContentType, boolean useFiltering) throws IOException {
return null;
}

Expand Down

0 comments on commit 8063e1b

Please sign in to comment.