Skip to content

Commit 749039a

Browse files
authored
Consolidate the last easy parser construction (#22095)
Moves the last of the "easy" parser construction into `RestRequest`, this time with a new method `RestRequest#contentParser`. The rest of the production code that builds `XContentParser` isn't "easy" because it is exposed in the Transport Client API (a Builder) object.
1 parent bf65a69 commit 749039a

25 files changed

+203
-444
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@
2222
import org.elasticsearch.ElasticsearchGenerationException;
2323
import org.elasticsearch.action.ActionRequestValidationException;
2424
import org.elasticsearch.action.support.master.AcknowledgedRequest;
25-
import org.elasticsearch.common.bytes.BytesReference;
2625
import org.elasticsearch.common.io.stream.StreamInput;
2726
import org.elasticsearch.common.io.stream.StreamOutput;
2827
import org.elasticsearch.common.settings.Settings;
2928
import org.elasticsearch.common.xcontent.XContentBuilder;
3029
import org.elasticsearch.common.xcontent.XContentFactory;
31-
import org.elasticsearch.common.xcontent.XContentParser;
3230
import org.elasticsearch.common.xcontent.XContentType;
3331

3432
import java.io.IOException;
3533
import java.util.Map;
3634

3735
import static org.elasticsearch.action.ValidateActions.addValidationError;
38-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
3936
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
4037
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
38+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
4139

4240
/**
4341
* Register repository request.
@@ -198,83 +196,23 @@ public boolean verify() {
198196
*
199197
* @param repositoryDefinition repository definition
200198
*/
201-
public PutRepositoryRequest source(XContentBuilder repositoryDefinition) {
202-
return source(repositoryDefinition.bytes());
203-
}
204-
205-
/**
206-
* Parses repository definition.
207-
*
208-
* @param repositoryDefinition repository definition
209-
*/
210-
public PutRepositoryRequest source(Map repositoryDefinition) {
211-
Map<String, Object> source = repositoryDefinition;
212-
for (Map.Entry<String, Object> entry : source.entrySet()) {
199+
public PutRepositoryRequest source(Map<String, Object> repositoryDefinition) {
200+
for (Map.Entry<String, Object> entry : repositoryDefinition.entrySet()) {
213201
String name = entry.getKey();
214202
if (name.equals("type")) {
215203
type(entry.getValue().toString());
216204
} else if (name.equals("settings")) {
217205
if (!(entry.getValue() instanceof Map)) {
218206
throw new IllegalArgumentException("Malformed settings section, should include an inner object");
219207
}
220-
settings((Map<String, Object>) entry.getValue());
208+
@SuppressWarnings("unchecked")
209+
Map<String, Object> sub = (Map<String, Object>) entry.getValue();
210+
settings(sub);
221211
}
222212
}
223213
return this;
224214
}
225215

226-
/**
227-
* Parses repository definition.
228-
* JSON, Smile and YAML formats are supported
229-
*
230-
* @param repositoryDefinition repository definition
231-
*/
232-
public PutRepositoryRequest source(String repositoryDefinition) {
233-
try (XContentParser parser = XContentFactory.xContent(repositoryDefinition).createParser(repositoryDefinition)) {
234-
return source(parser.mapOrdered());
235-
} catch (IOException e) {
236-
throw new IllegalArgumentException("failed to parse repository source [" + repositoryDefinition + "]", e);
237-
}
238-
}
239-
240-
/**
241-
* Parses repository definition.
242-
* JSON, Smile and YAML formats are supported
243-
*
244-
* @param repositoryDefinition repository definition
245-
*/
246-
public PutRepositoryRequest source(byte[] repositoryDefinition) {
247-
return source(repositoryDefinition, 0, repositoryDefinition.length);
248-
}
249-
250-
/**
251-
* Parses repository definition.
252-
* JSON, Smile and YAML formats are supported
253-
*
254-
* @param repositoryDefinition repository definition
255-
*/
256-
public PutRepositoryRequest source(byte[] repositoryDefinition, int offset, int length) {
257-
try (XContentParser parser = XContentFactory.xContent(repositoryDefinition, offset, length).createParser(repositoryDefinition, offset, length)) {
258-
return source(parser.mapOrdered());
259-
} catch (IOException e) {
260-
throw new IllegalArgumentException("failed to parse repository source", e);
261-
}
262-
}
263-
264-
/**
265-
* Parses repository definition.
266-
* JSON, Smile and YAML formats are supported
267-
*
268-
* @param repositoryDefinition repository definition
269-
*/
270-
public PutRepositoryRequest source(BytesReference repositoryDefinition) {
271-
try (XContentParser parser = XContentFactory.xContent(repositoryDefinition).createParser(repositoryDefinition)) {
272-
return source(parser.mapOrdered());
273-
} catch (IOException e) {
274-
throw new IllegalArgumentException("failed to parse template source", e);
275-
}
276-
}
277-
278216
@Override
279217
public void readFrom(StreamInput in) throws IOException {
280218
super.readFrom(in);

core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
import org.elasticsearch.action.support.IndicesOptions;
2626
import org.elasticsearch.action.support.master.MasterNodeRequest;
2727
import org.elasticsearch.common.Strings;
28-
import org.elasticsearch.common.bytes.BytesReference;
2928
import org.elasticsearch.common.io.stream.StreamInput;
3029
import org.elasticsearch.common.io.stream.StreamOutput;
3130
import org.elasticsearch.common.settings.Settings;
3231
import org.elasticsearch.common.xcontent.XContentBuilder;
3332
import org.elasticsearch.common.xcontent.XContentFactory;
34-
import org.elasticsearch.common.xcontent.XContentParser;
3533
import org.elasticsearch.common.xcontent.XContentType;
3634

3735
import java.io.IOException;
@@ -41,10 +39,9 @@
4139

4240
import static org.elasticsearch.action.ValidateActions.addValidationError;
4341
import static org.elasticsearch.common.Strings.EMPTY_ARRAY;
44-
import static org.elasticsearch.common.Strings.hasLength;
45-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
4642
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
4743
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
44+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
4845
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
4946

5047
/**
@@ -357,17 +354,7 @@ public boolean includeGlobalState() {
357354
* @param source snapshot definition
358355
* @return this request
359356
*/
360-
public CreateSnapshotRequest source(XContentBuilder source) {
361-
return source(source.bytes());
362-
}
363-
364-
/**
365-
* Parses snapshot definition.
366-
*
367-
* @param source snapshot definition
368-
* @return this request
369-
*/
370-
public CreateSnapshotRequest source(Map source) {
357+
public CreateSnapshotRequest source(Map<String, Object> source) {
371358
for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
372359
String name = entry.getKey();
373360
if (name.equals("indices")) {
@@ -393,66 +380,6 @@ public CreateSnapshotRequest source(Map source) {
393380
return this;
394381
}
395382

396-
/**
397-
* Parses snapshot definition. JSON, YAML and properties formats are supported
398-
*
399-
* @param source snapshot definition
400-
* @return this request
401-
*/
402-
public CreateSnapshotRequest source(String source) {
403-
if (hasLength(source)) {
404-
try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
405-
return source(parser.mapOrdered());
406-
} catch (Exception e) {
407-
throw new IllegalArgumentException("failed to parse repository source [" + source + "]", e);
408-
}
409-
}
410-
return this;
411-
}
412-
413-
/**
414-
* Parses snapshot definition. JSON, YAML and properties formats are supported
415-
*
416-
* @param source snapshot definition
417-
* @return this request
418-
*/
419-
public CreateSnapshotRequest source(byte[] source) {
420-
return source(source, 0, source.length);
421-
}
422-
423-
/**
424-
* Parses snapshot definition. JSON, YAML and properties formats are supported
425-
*
426-
* @param source snapshot definition
427-
* @param offset offset
428-
* @param length length
429-
* @return this request
430-
*/
431-
public CreateSnapshotRequest source(byte[] source, int offset, int length) {
432-
if (length > 0) {
433-
try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) {
434-
return source(parser.mapOrdered());
435-
} catch (IOException e) {
436-
throw new IllegalArgumentException("failed to parse repository source", e);
437-
}
438-
}
439-
return this;
440-
}
441-
442-
/**
443-
* Parses snapshot definition. JSON, YAML and properties formats are supported
444-
*
445-
* @param source snapshot definition
446-
* @return this request
447-
*/
448-
public CreateSnapshotRequest source(BytesReference source) {
449-
try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
450-
return source(parser.mapOrdered());
451-
} catch (IOException e) {
452-
throw new IllegalArgumentException("failed to parse snapshot source", e);
453-
}
454-
}
455-
456383
@Override
457384
public void readFrom(StreamInput in) throws IOException {
458385
super.readFrom(in);

core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
import org.elasticsearch.action.support.IndicesOptions;
2525
import org.elasticsearch.action.support.master.MasterNodeRequest;
2626
import org.elasticsearch.common.Strings;
27-
import org.elasticsearch.common.bytes.BytesReference;
2827
import org.elasticsearch.common.io.stream.StreamInput;
2928
import org.elasticsearch.common.io.stream.StreamOutput;
3029
import org.elasticsearch.common.settings.Settings;
3130
import org.elasticsearch.common.xcontent.XContentBuilder;
3231
import org.elasticsearch.common.xcontent.XContentFactory;
33-
import org.elasticsearch.common.xcontent.XContentParser;
3432
import org.elasticsearch.common.xcontent.XContentType;
3533

3634
import java.io.IOException;
@@ -39,10 +37,9 @@
3937
import java.util.Map;
4038

4139
import static org.elasticsearch.action.ValidateActions.addValidationError;
42-
import static org.elasticsearch.common.Strings.hasLength;
43-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
4440
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
4541
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
42+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
4643
import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenientNodeBooleanValue;
4744

4845
/**
@@ -472,22 +469,8 @@ public Settings indexSettings() {
472469
* @param source restore definition
473470
* @return this request
474471
*/
475-
public RestoreSnapshotRequest source(XContentBuilder source) {
476-
try {
477-
return source(source.bytes());
478-
} catch (Exception e) {
479-
throw new IllegalArgumentException("Failed to build json for repository request", e);
480-
}
481-
}
482-
483-
/**
484-
* Parses restore definition
485-
*
486-
* @param source restore definition
487-
* @return this request
488-
*/
489-
public RestoreSnapshotRequest source(Map source) {
490-
for (Map.Entry<String, Object> entry : ((Map<String, Object>) source).entrySet()) {
472+
public RestoreSnapshotRequest source(Map<String, Object> source) {
473+
for (Map.Entry<String, Object> entry : source.entrySet()) {
491474
String name = entry.getKey();
492475
if (name.equals("indices")) {
493476
if (entry.getValue() instanceof String) {
@@ -543,74 +526,6 @@ public RestoreSnapshotRequest source(Map source) {
543526
return this;
544527
}
545528

546-
/**
547-
* Parses restore definition
548-
* <p>
549-
* JSON, YAML and properties formats are supported
550-
*
551-
* @param source restore definition
552-
* @return this request
553-
*/
554-
public RestoreSnapshotRequest source(String source) {
555-
if (hasLength(source)) {
556-
try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
557-
return source(parser.mapOrdered());
558-
} catch (Exception e) {
559-
throw new IllegalArgumentException("failed to parse repository source [" + source + "]", e);
560-
}
561-
}
562-
return this;
563-
}
564-
565-
/**
566-
* Parses restore definition
567-
* <p>
568-
* JSON, YAML and properties formats are supported
569-
*
570-
* @param source restore definition
571-
* @return this request
572-
*/
573-
public RestoreSnapshotRequest source(byte[] source) {
574-
return source(source, 0, source.length);
575-
}
576-
577-
/**
578-
* Parses restore definition
579-
* <p>
580-
* JSON, YAML and properties formats are supported
581-
*
582-
* @param source restore definition
583-
* @param offset offset
584-
* @param length length
585-
* @return this request
586-
*/
587-
public RestoreSnapshotRequest source(byte[] source, int offset, int length) {
588-
if (length > 0) {
589-
try (XContentParser parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length)) {
590-
return source(parser.mapOrdered());
591-
} catch (IOException e) {
592-
throw new IllegalArgumentException("failed to parse repository source", e);
593-
}
594-
}
595-
return this;
596-
}
597-
598-
/**
599-
* Parses restore definition
600-
* <p>
601-
* JSON, YAML and properties formats are supported
602-
*
603-
* @param source restore definition
604-
* @return this request
605-
*/
606-
public RestoreSnapshotRequest source(BytesReference source) {
607-
try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
608-
return source(parser.mapOrdered());
609-
} catch (IOException e) {
610-
throw new IllegalArgumentException("failed to parse template source", e);
611-
}
612-
}
613-
614529
@Override
615530
public void readFrom(StreamInput in) throws IOException {
616531
super.readFrom(in);

0 commit comments

Comments
 (0)