Skip to content

Commit 18565d9

Browse files
nknizeandrross
authored andcommitted
[Refactor] Requests to use MediaType instead of XContentType (opensearch-project#6218)
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> (cherry picked from commit 1f4cdd2) Signed-off-by: Andrew Ross <andrross@amazon.com>
1 parent 9aa6cab commit 18565d9

File tree

25 files changed

+543
-48
lines changed

25 files changed

+543
-48
lines changed

client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.opensearch.common.unit.TimeValue;
8080
import org.opensearch.common.util.CollectionUtils;
8181
import org.opensearch.common.xcontent.DeprecationHandler;
82+
import org.opensearch.common.xcontent.MediaType;
8283
import org.opensearch.common.xcontent.NamedXContentRegistry;
8384
import org.opensearch.common.xcontent.ToXContent;
8485
import org.opensearch.common.xcontent.XContent;
@@ -112,6 +113,11 @@
112113
import java.util.Map;
113114
import java.util.StringJoiner;
114115

116+
/**
117+
* Converts OpenSearch writeable requests to an HTTP Request
118+
*
119+
* @opensearch.api
120+
*/
115121
final class RequestConverters {
116122
static final XContentType REQUEST_BODY_CONTENT_TYPE = XContentType.JSON;
117123

@@ -514,7 +520,7 @@ static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOExcep
514520
XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
515521
byte[] source = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, xContent);
516522
request.addParameters(params.asMap());
517-
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
523+
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.mediaType())));
518524
return request;
519525
}
520526

@@ -549,7 +555,7 @@ static Request multiSearchTemplate(MultiSearchTemplateRequest multiSearchTemplat
549555

550556
XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
551557
byte[] source = MultiSearchTemplateRequest.writeMultiLineFormat(multiSearchTemplateRequest, xContent);
552-
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
558+
request.setEntity(new NByteArrayEntity(source, createContentType(xContent.mediaType())));
553559
return request;
554560
}
555561

@@ -867,12 +873,26 @@ static String endpoint(String[] indices, String endpoint, String type) {
867873
*
868874
* @param xContentType the {@link XContentType}
869875
* @return the {@link ContentType}
876+
*
877+
* @deprecated use {@link #createContentType(MediaType)} instead
870878
*/
879+
@Deprecated
871880
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
872881
public static ContentType createContentType(final XContentType xContentType) {
873882
return ContentType.create(xContentType.mediaTypeWithoutParameters(), (Charset) null);
874883
}
875884

885+
/**
886+
* Returns a {@link ContentType} from a given {@link XContentType}.
887+
*
888+
* @param mediaType the {@link MediaType}
889+
* @return the {@link ContentType}
890+
*/
891+
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
892+
public static ContentType createContentType(final MediaType mediaType) {
893+
return ContentType.create(mediaType.mediaTypeWithoutParameters(), (Charset) null);
894+
}
895+
876896
/**
877897
* Utility class to help with common parameter names and patterns. Wraps
878898
* a {@link Request} and adds the parameters to it directly.

client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.opensearch.common.bytes.BytesReference;
4545
import org.opensearch.common.settings.Settings;
4646
import org.opensearch.common.xcontent.DeprecationHandler;
47+
import org.opensearch.common.xcontent.MediaType;
4748
import org.opensearch.common.xcontent.NamedXContentRegistry;
4849
import org.opensearch.common.xcontent.ToXContentObject;
4950
import org.opensearch.common.xcontent.XContentBuilder;
@@ -64,6 +65,8 @@
6465

6566
/**
6667
* A request to create an index.
68+
*
69+
* @opensearch.api
6770
*/
6871
public class CreateIndexRequest extends TimedRequest implements Validatable, ToXContentObject {
6972
static final ParseField MAPPINGS = new ParseField("mappings");
@@ -122,12 +125,23 @@ public CreateIndexRequest settings(Settings settings) {
122125

123126
/**
124127
* The settings to create the index with (either json or yaml format)
128+
*
129+
* @deprecated use {@link #settings(String source, MediaType mediaType)} instead
125130
*/
131+
@Deprecated
126132
public CreateIndexRequest settings(String source, XContentType xContentType) {
127133
this.settings = Settings.builder().loadFromSource(source, xContentType).build();
128134
return this;
129135
}
130136

137+
/**
138+
* The settings to create the index with (either json or yaml format)
139+
*/
140+
public CreateIndexRequest settings(String source, MediaType mediaType) {
141+
this.settings = Settings.builder().loadFromSource(source, mediaType).build();
142+
return this;
143+
}
144+
131145
/**
132146
* Allows to set the settings using a json builder.
133147
*/
@@ -159,11 +173,26 @@ public XContentType mappingsXContentType() {
159173
*
160174
* @param source The mapping source
161175
* @param xContentType The content type of the source
176+
*
177+
* @deprecated use {@link #mapping(String source, MediaType mediaType)} instead
162178
*/
179+
@Deprecated
163180
public CreateIndexRequest mapping(String source, XContentType xContentType) {
164181
return mapping(new BytesArray(source), xContentType);
165182
}
166183

184+
/**
185+
* Adds mapping that will be added when the index gets created.
186+
*
187+
* Note that the definition should *not* be nested under a type name.
188+
*
189+
* @param source The mapping source
190+
* @param mediaType The media type of the source
191+
*/
192+
public CreateIndexRequest mapping(String source, MediaType mediaType) {
193+
return mapping(new BytesArray(source), mediaType);
194+
}
195+
167196
/**
168197
* Adds mapping that will be added when the index gets created.
169198
*
@@ -199,14 +228,32 @@ public CreateIndexRequest mapping(Map<String, ?> source) {
199228
*
200229
* @param source The mapping source
201230
* @param xContentType the content type of the mapping source
231+
*
232+
* @deprecated use {@link #mapping(BytesReference source, MediaType mediaType)} instead
202233
*/
234+
@Deprecated
203235
public CreateIndexRequest mapping(BytesReference source, XContentType xContentType) {
204236
Objects.requireNonNull(xContentType);
205237
mappings = source;
206238
mappingsXContentType = xContentType;
207239
return this;
208240
}
209241

242+
/**
243+
* Adds mapping that will be added when the index gets created.
244+
*
245+
* Note that the definition should *not* be nested under a type name.
246+
*
247+
* @param source The mapping source
248+
* @param mediaType the content type of the mapping source
249+
*/
250+
public CreateIndexRequest mapping(BytesReference source, MediaType mediaType) {
251+
Objects.requireNonNull(mediaType);
252+
mappings = source;
253+
mappingsXContentType = (XContentType) mediaType;
254+
return this;
255+
}
256+
210257
public Set<Alias> aliases() {
211258
return this.aliases;
212259
}
@@ -233,15 +280,35 @@ public CreateIndexRequest aliases(XContentBuilder source) {
233280

234281
/**
235282
* Sets the aliases that will be associated with the index when it gets created
283+
*
284+
* @deprecated use {@link #aliases(String, MediaType)} instead
236285
*/
286+
@Deprecated
237287
public CreateIndexRequest aliases(String source, XContentType contentType) {
238288
return aliases(new BytesArray(source), contentType);
239289
}
240290

241291
/**
242292
* Sets the aliases that will be associated with the index when it gets created
243293
*/
294+
public CreateIndexRequest aliases(String source, MediaType mediaType) {
295+
return aliases(new BytesArray(source), mediaType);
296+
}
297+
298+
/**
299+
* Sets the aliases that will be associated with the index when it gets created
300+
*
301+
* @deprecated use {@link #aliases(BytesReference source, MediaType contentType)} instead
302+
*/
303+
@Deprecated
244304
public CreateIndexRequest aliases(BytesReference source, XContentType contentType) {
305+
return aliases(source, (MediaType) contentType);
306+
}
307+
308+
/**
309+
* Sets the aliases that will be associated with the index when it gets created
310+
*/
311+
public CreateIndexRequest aliases(BytesReference source, MediaType contentType) {
245312
// EMPTY is safe here because we never call namedObject
246313
try (
247314
XContentParser parser = XContentHelper.createParser(
@@ -282,11 +349,23 @@ public CreateIndexRequest aliases(Collection<Alias> aliases) {
282349
* Sets the settings and mappings as a single source.
283350
*
284351
* Note that the mapping definition should *not* be nested under a type name.
352+
*
353+
* @deprecated use {@link #source(String, MediaType)} instead
285354
*/
355+
@Deprecated
286356
public CreateIndexRequest source(String source, XContentType xContentType) {
287357
return source(new BytesArray(source), xContentType);
288358
}
289359

360+
/**
361+
* Sets the settings and mappings as a single source.
362+
*
363+
* Note that the mapping definition should *not* be nested under a type name.
364+
*/
365+
public CreateIndexRequest source(String source, MediaType mediaType) {
366+
return source(new BytesArray(source), mediaType);
367+
}
368+
290369
/**
291370
* Sets the settings and mappings as a single source.
292371
*
@@ -300,13 +379,27 @@ public CreateIndexRequest source(XContentBuilder source) {
300379
* Sets the settings and mappings as a single source.
301380
*
302381
* Note that the mapping definition should *not* be nested under a type name.
382+
*
383+
* @deprecated use {@link #source(BytesReference, MediaType)} instead
303384
*/
385+
@Deprecated
304386
public CreateIndexRequest source(BytesReference source, XContentType xContentType) {
305387
Objects.requireNonNull(xContentType);
306388
source(XContentHelper.convertToMap(source, false, xContentType).v2());
307389
return this;
308390
}
309391

392+
/**
393+
* Sets the settings and mappings as a single source.
394+
*
395+
* Note that the mapping definition should *not* be nested under a type name.
396+
*/
397+
public CreateIndexRequest source(BytesReference source, MediaType mediaType) {
398+
Objects.requireNonNull(mediaType);
399+
source(XContentHelper.convertToMap(source, false, mediaType).v2());
400+
return this;
401+
}
402+
310403
/**
311404
* Sets the settings and mappings as a single source.
312405
*

client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.opensearch.common.bytes.BytesReference;
4444
import org.opensearch.common.settings.Settings;
4545
import org.opensearch.common.xcontent.DeprecationHandler;
46+
import org.opensearch.common.xcontent.MediaType;
4647
import org.opensearch.common.xcontent.NamedXContentRegistry;
4748
import org.opensearch.common.xcontent.ToXContentFragment;
4849
import org.opensearch.common.xcontent.XContentBuilder;
@@ -268,9 +269,10 @@ private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
268269
try {
269270
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
270271
builder.map(source);
271-
Objects.requireNonNull(builder.contentType());
272+
MediaType mediaType = builder.contentType();
273+
Objects.requireNonNull(mediaType);
272274
try {
273-
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, builder.contentType()));
275+
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, mediaType));
274276
return this;
275277
} catch (IOException e) {
276278
throw new UncheckedIOException("failed to convert source to json", e);
@@ -342,32 +344,72 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
342344

343345
/**
344346
* The template source definition.
347+
*
348+
* @deprecated use {@link #source(String, MediaType)} instead
345349
*/
350+
@Deprecated
346351
public PutIndexTemplateRequest source(String templateSource, XContentType xContentType) {
347352
return source(XContentHelper.convertToMap(xContentType.xContent(), templateSource, true));
348353
}
349354

350355
/**
351356
* The template source definition.
352357
*/
358+
public PutIndexTemplateRequest source(String templateSource, MediaType mediaType) {
359+
return source(XContentHelper.convertToMap(mediaType.xContent(), templateSource, true));
360+
}
361+
362+
/**
363+
* The template source definition.
364+
*
365+
* @deprecated use {@link #source(byte[], MediaType)} instead
366+
*/
367+
@Deprecated
353368
public PutIndexTemplateRequest source(byte[] source, XContentType xContentType) {
354369
return source(source, 0, source.length, xContentType);
355370
}
356371

357372
/**
358373
* The template source definition.
359374
*/
375+
public PutIndexTemplateRequest source(byte[] source, MediaType mediaType) {
376+
return source(source, 0, source.length, mediaType);
377+
}
378+
379+
/**
380+
* The template source definition.
381+
*
382+
* @deprecated use {@link #source(byte[], int, int, MediaType)} instead
383+
*/
384+
@Deprecated
360385
public PutIndexTemplateRequest source(byte[] source, int offset, int length, XContentType xContentType) {
361386
return source(new BytesArray(source, offset, length), xContentType);
362387
}
363388

364389
/**
365390
* The template source definition.
366391
*/
392+
public PutIndexTemplateRequest source(byte[] source, int offset, int length, MediaType mediaType) {
393+
return source(new BytesArray(source, offset, length), mediaType);
394+
}
395+
396+
/**
397+
* The template source definition.
398+
*
399+
* @deprecated use {@link #source(BytesReference, MediaType)} instead
400+
*/
401+
@Deprecated
367402
public PutIndexTemplateRequest source(BytesReference source, XContentType xContentType) {
368403
return source(XContentHelper.convertToMap(source, true, xContentType).v2());
369404
}
370405

406+
/**
407+
* The template source definition.
408+
*/
409+
public PutIndexTemplateRequest source(BytesReference source, MediaType mediaType) {
410+
return source(XContentHelper.convertToMap(source, true, mediaType).v2());
411+
}
412+
371413
public Set<Alias> aliases() {
372414
return this.aliases;
373415
}

0 commit comments

Comments
 (0)