Skip to content

Commit

Permalink
Revert "[Refactor] Requests to use MediaType instead of XContentType (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishikesh1159 committed Mar 19, 2023
1 parent 5765ac2 commit b74c1f1
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 543 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.CollectionUtils;
import org.opensearch.common.xcontent.DeprecationHandler;
import org.opensearch.common.xcontent.MediaType;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.XContent;
Expand Down Expand Up @@ -113,11 +112,6 @@
import java.util.Map;
import java.util.StringJoiner;

/**
* Converts OpenSearch writeable requests to an HTTP Request
*
* @opensearch.api
*/
final class RequestConverters {
static final XContentType REQUEST_BODY_CONTENT_TYPE = XContentType.JSON;

Expand Down Expand Up @@ -520,7 +514,7 @@ static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOExcep
XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
byte[] source = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, xContent);
request.addParameters(params.asMap());
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.mediaType())));
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
return request;
}

Expand Down Expand Up @@ -555,7 +549,7 @@ static Request multiSearchTemplate(MultiSearchTemplateRequest multiSearchTemplat

XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
byte[] source = MultiSearchTemplateRequest.writeMultiLineFormat(multiSearchTemplateRequest, xContent);
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.mediaType())));
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
return request;
}

Expand Down Expand Up @@ -873,26 +867,12 @@ static String endpoint(String[] indices, String endpoint, String type) {
*
* @param xContentType the {@link XContentType}
* @return the {@link ContentType}
*
* @deprecated use {@link #createContentType(MediaType)} instead
*/
@Deprecated
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
public static ContentType createContentType(final XContentType xContentType) {
return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null);
}

/**
* Returns a {@link ContentType} from a given {@link XContentType}.
*
* @param mediaType the {@link MediaType}
* @return the {@link ContentType}
*/
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
public static ContentType createContentType(final MediaType mediaType) {
return ContentType.create(mediaType.mediaTypeWithoutParameters(), (Charset) null);
}

/**
* Utility class to help with common parameter names and patterns. Wraps
* a {@link Request} and adds the parameters to it directly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.DeprecationHandler;
import org.opensearch.common.xcontent.MediaType;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;
Expand All @@ -65,8 +64,6 @@

/**
* A request to create an index.
*
* @opensearch.api
*/
public class CreateIndexRequest extends TimedRequest implements Validatable, ToXContentObject {
static final ParseField MAPPINGS = new ParseField("mappings");
Expand Down Expand Up @@ -125,23 +122,12 @@ public CreateIndexRequest settings(Settings settings) {

/**
* The settings to create the index with (either json or yaml format)
*
* @deprecated use {@link #settings(String source, MediaType mediaType)} instead
*/
@Deprecated
public CreateIndexRequest settings(String source, XContentType xContentType) {
this.settings = Settings.builder().loadFromSource(source, xContentType).build();
return this;
}

/**
* The settings to create the index with (either json or yaml format)
*/
public CreateIndexRequest settings(String source, MediaType mediaType) {
this.settings = Settings.builder().loadFromSource(source, mediaType).build();
return this;
}

/**
* Allows to set the settings using a json builder.
*/
Expand Down Expand Up @@ -173,26 +159,11 @@ public XContentType mappingsXContentType() {
*
* @param source The mapping source
* @param xContentType The content type of the source
*
* @deprecated use {@link #mapping(String source, MediaType mediaType)} instead
*/
@Deprecated
public CreateIndexRequest mapping(String source, XContentType xContentType) {
return mapping(new BytesArray(source), xContentType);
}

/**
* Adds mapping that will be added when the index gets created.
*
* Note that the definition should *not* be nested under a type name.
*
* @param source The mapping source
* @param mediaType The media type of the source
*/
public CreateIndexRequest mapping(String source, MediaType mediaType) {
return mapping(new BytesArray(source), mediaType);
}

/**
* Adds mapping that will be added when the index gets created.
*
Expand Down Expand Up @@ -228,32 +199,14 @@ public CreateIndexRequest mapping(Map<String, ?> source) {
*
* @param source The mapping source
* @param xContentType the content type of the mapping source
*
* @deprecated use {@link #mapping(BytesReference source, MediaType mediaType)} instead
*/
@Deprecated
public CreateIndexRequest mapping(BytesReference source, XContentType xContentType) {
Objects.requireNonNull(xContentType);
mappings = source;
mappingsXContentType = xContentType;
return this;
}

/**
* Adds mapping that will be added when the index gets created.
*
* Note that the definition should *not* be nested under a type name.
*
* @param source The mapping source
* @param mediaType the content type of the mapping source
*/
public CreateIndexRequest mapping(BytesReference source, MediaType mediaType) {
Objects.requireNonNull(mediaType);
mappings = source;
mappingsXContentType = (XContentType) mediaType;
return this;
}

public Set<Alias> aliases() {
return this.aliases;
}
Expand All @@ -280,35 +233,15 @@ public CreateIndexRequest aliases(XContentBuilder source) {

/**
* Sets the aliases that will be associated with the index when it gets created
*
* @deprecated use {@link #aliases(String, MediaType)} instead
*/
@Deprecated
public CreateIndexRequest aliases(String source, XContentType contentType) {
return aliases(new BytesArray(source), contentType);
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(String source, MediaType mediaType) {
return aliases(new BytesArray(source), mediaType);
}

/**
* Sets the aliases that will be associated with the index when it gets created
*
* @deprecated use {@link #aliases(BytesReference source, MediaType contentType)} instead
*/
@Deprecated
public CreateIndexRequest aliases(BytesReference source, XContentType contentType) {
return aliases(source, (MediaType) contentType);
}

/**
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(BytesReference source, MediaType contentType) {
// EMPTY is safe here because we never call namedObject
try (
XContentParser parser = XContentHelper.createParser(
Expand Down Expand Up @@ -349,23 +282,11 @@ public CreateIndexRequest aliases(Collection<Alias> aliases) {
* Sets the settings and mappings as a single source.
*
* Note that the mapping definition should *not* be nested under a type name.
*
* @deprecated use {@link #source(String, MediaType)} instead
*/
@Deprecated
public CreateIndexRequest source(String source, XContentType xContentType) {
return source(new BytesArray(source), xContentType);
}

/**
* Sets the settings and mappings as a single source.
*
* Note that the mapping definition should *not* be nested under a type name.
*/
public CreateIndexRequest source(String source, MediaType mediaType) {
return source(new BytesArray(source), mediaType);
}

/**
* Sets the settings and mappings as a single source.
*
Expand All @@ -379,27 +300,13 @@ public CreateIndexRequest source(XContentBuilder source) {
* Sets the settings and mappings as a single source.
*
* Note that the mapping definition should *not* be nested under a type name.
*
* @deprecated use {@link #source(BytesReference, MediaType)} instead
*/
@Deprecated
public CreateIndexRequest source(BytesReference source, XContentType xContentType) {
Objects.requireNonNull(xContentType);
source(XContentHelper.convertToMap(source, false, xContentType).v2());
return this;
}

/**
* Sets the settings and mappings as a single source.
*
* Note that the mapping definition should *not* be nested under a type name.
*/
public CreateIndexRequest source(BytesReference source, MediaType mediaType) {
Objects.requireNonNull(mediaType);
source(XContentHelper.convertToMap(source, false, mediaType).v2());
return this;
}

/**
* Sets the settings and mappings as a single source.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.DeprecationHandler;
import org.opensearch.common.xcontent.MediaType;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.ToXContentFragment;
import org.opensearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -269,10 +268,9 @@ private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(source);
MediaType mediaType = builder.contentType();
Objects.requireNonNull(mediaType);
Objects.requireNonNull(builder.contentType());
try {
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, mediaType));
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, builder.contentType()));
return this;
} catch (IOException e) {
throw new UncheckedIOException("failed to convert source to json", e);
Expand Down Expand Up @@ -344,72 +342,32 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {

/**
* The template source definition.
*
* @deprecated use {@link #source(String, MediaType)} instead
*/
@Deprecated
public PutIndexTemplateRequest source(String templateSource, XContentType xContentType) {
return source(XContentHelper.convertToMap(xContentType.xContent(), templateSource, true));
}

/**
* The template source definition.
*/
public PutIndexTemplateRequest source(String templateSource, MediaType mediaType) {
return source(XContentHelper.convertToMap(mediaType.xContent(), templateSource, true));
}

/**
* The template source definition.
*
* @deprecated use {@link #source(byte[], MediaType)} instead
*/
@Deprecated
public PutIndexTemplateRequest source(byte[] source, XContentType xContentType) {
return source(source, 0, source.length, xContentType);
}

/**
* The template source definition.
*/
public PutIndexTemplateRequest source(byte[] source, MediaType mediaType) {
return source(source, 0, source.length, mediaType);
}

/**
* The template source definition.
*
* @deprecated use {@link #source(byte[], int, int, MediaType)} instead
*/
@Deprecated
public PutIndexTemplateRequest source(byte[] source, int offset, int length, XContentType xContentType) {
return source(new BytesArray(source, offset, length), xContentType);
}

/**
* The template source definition.
*/
public PutIndexTemplateRequest source(byte[] source, int offset, int length, MediaType mediaType) {
return source(new BytesArray(source, offset, length), mediaType);
}

/**
* The template source definition.
*
* @deprecated use {@link #source(BytesReference, MediaType)} instead
*/
@Deprecated
public PutIndexTemplateRequest source(BytesReference source, XContentType xContentType) {
return source(XContentHelper.convertToMap(source, true, xContentType).v2());
}

/**
* The template source definition.
*/
public PutIndexTemplateRequest source(BytesReference source, MediaType mediaType) {
return source(XContentHelper.convertToMap(source, true, mediaType).v2());
}

public Set<Alias> aliases() {
return this.aliases;
}
Expand Down
Loading

0 comments on commit b74c1f1

Please sign in to comment.