Skip to content

Commit

Permalink
[Refactor] Requests to use MediaType instead of XContentType (#6218)
Browse files Browse the repository at this point in the history
Refactors various REST requests to use MediaType instead of XContentType to
abstract media format from the REST interfaces.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Feb 8, 2023
1 parent 1eae021 commit 1f4cdd2
Show file tree
Hide file tree
Showing 25 changed files with 543 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
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 @@ -112,6 +113,11 @@
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 @@ -514,7 +520,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 ByteArrayEntity(source, createContentType(xContent.type())));
request.setEntity(new ByteArrayEntity(source, createContentType(xContent.mediaType())));
return request;
}

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

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

Expand Down Expand Up @@ -867,12 +873,26 @@ 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,6 +44,7 @@
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 @@ -64,6 +65,8 @@

/**
* 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 @@ -122,12 +125,23 @@ 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 @@ -159,11 +173,26 @@ 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 @@ -199,14 +228,32 @@ 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 @@ -233,15 +280,35 @@ 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 @@ -282,11 +349,23 @@ 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 @@ -300,13 +379,27 @@ 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,6 +43,7 @@
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 @@ -268,9 +269,10 @@ private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(source);
Objects.requireNonNull(builder.contentType());
MediaType mediaType = builder.contentType();
Objects.requireNonNull(mediaType);
try {
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, builder.contentType()));
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, mediaType));
return this;
} catch (IOException e) {
throw new UncheckedIOException("failed to convert source to json", e);
Expand Down Expand Up @@ -342,32 +344,72 @@ 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 1f4cdd2

Please sign in to comment.