Skip to content

Commit

Permalink
[Refactor] Requests to use MediaType instead of XContentType
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 <[email protected]>
  • Loading branch information
nknize committed Feb 7, 2023
1 parent 8c09378 commit c5f0c89
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 27 deletions.
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 c5f0c89

Please sign in to comment.