Skip to content

Commit

Permalink
variable naming and superfluity cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Aug 9, 2023
1 parent c7ffe75 commit 73b1673
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.util.CollectionUtils;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.MediaTypeRegistry;
Expand Down Expand Up @@ -875,12 +874,12 @@ static String endpoint(String[] indices, String endpoint, String type) {
}

/**
* Returns a {@link ContentType} from a given {@link XContentType}.
* Returns a {@link ContentType} from a given {@link MediaType}.
*
* @param mediaType the {@link MediaType}
* @return the {@link ContentType}
*/
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
@SuppressForbidden(reason = "Only allowed place to convert a MediaType to a ContentType")
public static ContentType createContentType(final MediaType mediaType) {
return ContentType.create(mediaType.mediaTypeWithoutParameters(), (Charset) null);
}
Expand Down Expand Up @@ -1259,7 +1258,7 @@ Params withWaitForEvents(Priority waitForEvents) {
*/
static MediaType enforceSameContentType(IndexRequest indexRequest, @Nullable MediaType mediaType) {
MediaType requestContentType = indexRequest.getContentType();
if (requestContentType != MediaTypeRegistry.JSON && requestContentType != XContentType.SMILE) {
if (requestContentType != MediaTypeRegistry.JSON && requestContentType != MediaTypeRegistry.fromMediaType("smile")) {
throw new IllegalArgumentException(
"Unsupported content-type found for request with content-type ["
+ requestContentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

@defaultMessage Use Request#createContentType(XContentType) to be sure to pass the right MIME type
@defaultMessage Use Request#createContentType(MediaType) to be sure to pass the right MIME type
org.apache.hc.core5.http.ContentType#create(java.lang.String)
org.apache.hc.core5.http.ContentType#create(java.lang.String,java.lang.String)
org.apache.hc.core5.http.ContentType#create(java.lang.String,java.nio.charset.Charset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@
*/
public class MapXContentParser extends AbstractXContentParser {

private MediaType xContentType;
private MediaType mediaType;
private TokenIterator iterator;
private boolean closed;

public MapXContentParser(
NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler,
Map<String, Object> map,
MediaType xContentType
MediaType mediaType
) {
super(xContentRegistry, deprecationHandler);
this.xContentType = xContentType;
this.mediaType = mediaType;
this.iterator = new MapIterator(null, null, map);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ protected BigInteger doBigIntegerValue() throws IOException {

@Override
public MediaType contentType() {
return xContentType;
return mediaType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ private XContentHelper() {}
* by the {@link ToXContent#isFragment()} method returns.
*/
@Deprecated
public static BytesReference toXContent(ToXContent toXContent, MediaType xContentType, boolean humanReadable) throws IOException {
return toXContent(toXContent, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
public static BytesReference toXContent(ToXContent toXContent, MediaType mediaType, boolean humanReadable) throws IOException {
return toXContent(toXContent, mediaType, ToXContent.EMPTY_PARAMS, humanReadable);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public ClusterUpdateSettingsRequest persistentSettings(Settings.Builder settings
/**
* Sets the source containing the persistent settings to be updated. They will get applied cross restarts
*/
public ClusterUpdateSettingsRequest persistentSettings(String source, final MediaType xContentType) {
this.persistentSettings = Settings.builder().loadFromSource(source, xContentType).build();
public ClusterUpdateSettingsRequest persistentSettings(String source, final MediaType mediaType) {
this.persistentSettings = Settings.builder().loadFromSource(source, mediaType).build();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,13 @@ public BulkProcessor add(BytesReference data, @Nullable String defaultIndex, XCo
/**
* Adds the data from the bytes to be processed by the bulk processor
*/
public BulkProcessor add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultPipeline, MediaType xContentType)
public BulkProcessor add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultPipeline, MediaType mediaType)
throws Exception {
Tuple<BulkRequest, Long> bulkRequestToExecute = null;
lock.lock();
try {
ensureOpen();
bulkRequest.add(data, defaultIndex, null, null, defaultPipeline, null, true, xContentType);
bulkRequest.add(data, defaultIndex, null, null, defaultPipeline, null, true, mediaType);
bulkRequestToExecute = newBulkRequestIfNeeded();
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b
return convertToJson(bytes, reformatJson, prettyPrint, MediaTypeRegistry.xContent(bytes.toBytesRef().bytes));
}

public static String convertToJson(BytesReference bytes, boolean reformatJson, MediaType xContentType) throws IOException {
return convertToJson(bytes, reformatJson, false, xContentType);
public static String convertToJson(BytesReference bytes, boolean reformatJson, MediaType mediaType) throws IOException {
return convertToJson(bytes, reformatJson, false, mediaType);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions server/src/main/java/org/opensearch/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ private RestRequest(
HttpChannel httpChannel,
long requestId
) {
final MediaType xContentType;
final MediaType mediaType;
try {
xContentType = parseContentType(headers.get("Content-Type"));
mediaType = parseContentType(headers.get("Content-Type"));
} catch (final IllegalArgumentException e) {
throw new ContentTypeHeaderException(e);
}
if (xContentType != null) {
this.mediaType.set(xContentType);
if (mediaType != null) {
this.mediaType.set(mediaType);
}
this.xContentRegistry = xContentRegistry;
this.httpRequest = httpRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public class RestTable {

public static RestResponse buildResponse(Table table, RestChannel channel) throws Exception {
RestRequest request = channel.request();
MediaType xContentType = getXContentType(request);
if (xContentType != null) {
MediaType mediaType = getXContentType(request);
if (mediaType != null) {
return buildXContentBuilder(table, channel);
}
return buildTextPlainResponse(table, channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static GetResult mutateGetResult(GetResult getResult) {
return randomFrom(mutations).get();
}

public static Tuple<GetResult, GetResult> randomGetResult(MediaType xContentType) {
public static Tuple<GetResult, GetResult> randomGetResult(MediaType mediaType) {
final String index = randomAlphaOfLengthBetween(3, 10);
final String type = randomAlphaOfLengthBetween(3, 10);
final String id = randomAlphaOfLengthBetween(3, 10);
Expand All @@ -339,11 +339,11 @@ public static Tuple<GetResult, GetResult> randomGetResult(MediaType xContentType
source = RandomObjects.randomSource(random());
}
if (randomBoolean()) {
Tuple<Map<String, DocumentField>, Map<String, DocumentField>> tuple = randomDocumentFields(xContentType, false);
Tuple<Map<String, DocumentField>, Map<String, DocumentField>> tuple = randomDocumentFields(mediaType, false);
docFields = tuple.v1();
expectedDocFields = tuple.v2();

tuple = randomDocumentFields(xContentType, true);
tuple = randomDocumentFields(mediaType, true);
metaFields = tuple.v1();
expectedMetaFields = tuple.v2();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,21 +687,20 @@ public static void assertFileNotExists(Path file) {
* The comparison is done by parsing both into a map and comparing those two, so that keys ordering doesn't matter.
* Also binary values (byte[]) are properly compared through arrays comparisons.
*/
public static void assertToXContentEquivalent(BytesReference expected, BytesReference actual, MediaType xContentType)
throws IOException {
public static void assertToXContentEquivalent(BytesReference expected, BytesReference actual, MediaType mediaType) throws IOException {
// we tried comparing byte per byte, but that didn't fly for a couple of reasons:
// 1) whenever anything goes through a map while parsing, ordering is not preserved, which is perfectly ok
// 2) Jackson SMILE parser parses floats as double, which then get printed out as double (with double precision)
// Note that byte[] holding binary values need special treatment as they need to be properly compared item per item.
Map<String, Object> actualMap = null;
Map<String, Object> expectedMap = null;
try (
XContentParser actualParser = xContentType.xContent()
XContentParser actualParser = mediaType.xContent()
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, actual.streamInput())
) {
actualMap = actualParser.map();
try (
XContentParser expectedParser = xContentType.xContent()
XContentParser expectedParser = mediaType.xContent()
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, expected.streamInput())
) {
expectedMap = expectedParser.map();
Expand Down

0 comments on commit 73b1673

Please sign in to comment.