Skip to content

Commit 4c59810

Browse files
authored
[Refactor] Remaining Strings utility methods to core library (#9103)
This commit refactors the remaining Strings# utility methods from the duplicate :server:Strings class to the :libs:opensearch-core:Strings class. The CollectionUtils class (used by the Strings utility) is also refactored to the :libs:opensearch-common library and the unnecessary Strings.toString(XContentBuilder... static method is finally refactored as an instance method in XContentBuilder. The Strings class is also cleaned up for clarity. Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
1 parent bb78930 commit 4c59810

350 files changed

Lines changed: 4002 additions & 4410 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.opensearch.action.support.ActiveShardCount;
3939
import org.opensearch.client.TimedRequest;
4040
import org.opensearch.client.Validatable;
41-
import org.opensearch.common.Strings;
4241
import org.opensearch.core.common.bytes.BytesArray;
4342
import org.opensearch.core.common.bytes.BytesReference;
4443
import org.opensearch.common.settings.Settings;
@@ -135,7 +134,7 @@ public CreateIndexRequest settings(String source, MediaType mediaType) {
135134
* Allows to set the settings using a json builder.
136135
*/
137136
public CreateIndexRequest settings(XContentBuilder builder) {
138-
settings(Strings.toString(builder), builder.contentType());
137+
settings(builder.toString(), builder.contentType());
139138
return this;
140139
}
141140

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
package org.opensearch.client.slm;
3434

3535
import org.opensearch.common.Nullable;
36-
import org.opensearch.common.Strings;
3736
import org.opensearch.common.xcontent.XContentType;
3837
import org.opensearch.core.ParseField;
38+
import org.opensearch.core.common.Strings;
3939
import org.opensearch.core.xcontent.ConstructingObjectParser;
4040
import org.opensearch.core.xcontent.ToXContentObject;
4141
import org.opensearch.core.xcontent.XContentBuilder;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
package org.opensearch.client.slm;
3434

3535
import org.opensearch.common.Nullable;
36-
import org.opensearch.common.Strings;
3736
import org.opensearch.common.xcontent.XContentType;
3837
import org.opensearch.core.ParseField;
38+
import org.opensearch.core.common.Strings;
3939
import org.opensearch.core.xcontent.ConstructingObjectParser;
4040
import org.opensearch.core.xcontent.ToXContentObject;
4141
import org.opensearch.core.xcontent.XContentBuilder;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
package org.opensearch.client.slm;
3434

35-
import org.opensearch.common.Strings;
3635
import org.opensearch.common.unit.TimeValue;
3736
import org.opensearch.common.xcontent.XContentType;
3837
import org.opensearch.core.ParseField;
38+
import org.opensearch.core.common.Strings;
3939
import org.opensearch.core.xcontent.ConstructingObjectParser;
4040
import org.opensearch.core.xcontent.ToXContentFragment;
4141
import org.opensearch.core.xcontent.ToXContentObject;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
package org.opensearch.client.slm;
3434

3535
import org.opensearch.common.Nullable;
36-
import org.opensearch.common.Strings;
3736
import org.opensearch.common.unit.TimeValue;
3837
import org.opensearch.common.xcontent.XContentType;
3938
import org.opensearch.core.ParseField;
39+
import org.opensearch.core.common.Strings;
4040
import org.opensearch.core.xcontent.ConstructingObjectParser;
4141
import org.opensearch.core.xcontent.ToXContentObject;
4242
import org.opensearch.core.xcontent.XContentBuilder;

client/rest-high-level/src/test/java/org/opensearch/client/BulkProcessorIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.opensearch.action.get.MultiGetResponse;
4343
import org.opensearch.action.index.IndexRequest;
4444
import org.opensearch.action.search.SearchRequest;
45-
import org.opensearch.common.Strings;
4645
import org.opensearch.core.common.bytes.BytesArray;
4746
import org.opensearch.core.common.unit.ByteSizeUnit;
4847
import org.opensearch.core.common.unit.ByteSizeValue;
@@ -423,7 +422,7 @@ private static BytesArray bytesBulkRequest(String localIndex, int id) throws IOE
423422

424423
XContentBuilder source = jsonBuilder().startObject().field("field", randomRealisticUnicodeOfLengthBetween(1, 30)).endObject();
425424

426-
String request = Strings.toString(action) + "\n" + Strings.toString(source) + "\n";
425+
String request = action + "\n" + source + "\n";
427426
return new BytesArray(request);
428427
}
429428

client/rest-high-level/src/test/java/org/opensearch/client/SearchIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.opensearch.action.search.SearchScrollRequest;
5353
import org.opensearch.client.core.CountRequest;
5454
import org.opensearch.client.core.CountResponse;
55-
import org.opensearch.common.Strings;
5655
import org.opensearch.core.common.bytes.BytesReference;
5756
import org.opensearch.common.unit.TimeValue;
5857
import org.opensearch.core.xcontent.XContentBuilder;
@@ -769,7 +768,7 @@ public void testSearchScroll() throws Exception {
769768
for (int i = 0; i < 100; i++) {
770769
XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject();
771770
Request doc = new Request(HttpPut.METHOD_NAME, "/test/_doc/" + Integer.toString(i));
772-
doc.setJsonEntity(Strings.toString(builder));
771+
doc.setJsonEntity(builder.toString());
773772
client().performRequest(doc);
774773
}
775774
client().performRequest(new Request(HttpPost.METHOD_NAME, "/test/_refresh"));
@@ -837,7 +836,7 @@ public void testSearchWithPit() throws Exception {
837836
for (int i = 0; i < 100; i++) {
838837
XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject();
839838
Request doc = new Request(HttpPut.METHOD_NAME, "/test/_doc/" + Integer.toString(i));
840-
doc.setJsonEntity(Strings.toString(builder));
839+
doc.setJsonEntity(builder.toString());
841840
client().performRequest(doc);
842841
}
843842
client().performRequest(new Request(HttpPost.METHOD_NAME, "/test/_refresh"));

client/rest-high-level/src/test/java/org/opensearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import org.opensearch.client.core.TermVectorsResponse;
7575
import org.opensearch.client.indices.CreateIndexRequest;
7676
import org.opensearch.client.indices.CreateIndexResponse;
77-
import org.opensearch.common.Strings;
77+
import org.opensearch.core.common.Strings;
7878
import org.opensearch.core.common.bytes.BytesArray;
7979
import org.opensearch.common.settings.Settings;
8080
import org.opensearch.core.common.unit.ByteSizeUnit;
@@ -298,15 +298,14 @@ public void testUpdate() throws Exception {
298298

299299
Request request = new Request("POST", "/_scripts/increment-field");
300300
request.setJsonEntity(
301-
Strings.toString(
302-
JsonXContent.contentBuilder()
303-
.startObject()
304-
.startObject("script")
305-
.field("lang", "painless")
306-
.field("source", "ctx._source.field += params.count")
307-
.endObject()
308-
.endObject()
309-
)
301+
JsonXContent.contentBuilder()
302+
.startObject()
303+
.startObject("script")
304+
.field("lang", "painless")
305+
.field("source", "ctx._source.field += params.count")
306+
.endObject()
307+
.endObject()
308+
.toString()
310309
);
311310
Response response = client().performRequest(request);
312311
assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode());

distribution/archives/integ-test-zip/src/test/java/org/opensearch/test/rest/NodeRestUsageIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import org.opensearch.client.Request;
3636
import org.opensearch.client.Response;
3737
import org.opensearch.client.ResponseException;
38-
import org.opensearch.common.Strings;
3938
import org.opensearch.common.xcontent.XContentType;
39+
import org.opensearch.core.common.Strings;
4040
import org.opensearch.search.aggregations.AggregationBuilders;
4141
import org.opensearch.search.builder.SearchSourceBuilder;
4242

libs/core/src/main/java/org/opensearch/core/common/Strings.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
package org.opensearch.core.common;
1010

1111
import org.apache.lucene.util.BytesRefBuilder;
12+
import org.opensearch.ExceptionsHelper;
13+
import org.opensearch.OpenSearchException;
1214
import org.opensearch.common.Nullable;
1315
import org.opensearch.core.common.bytes.BytesReference;
1416
import org.opensearch.core.common.util.CollectionUtils;
17+
import org.opensearch.core.xcontent.MediaType;
18+
import org.opensearch.core.xcontent.ToXContent;
19+
import org.opensearch.core.xcontent.XContentBuilder;
1520

1621
import java.io.BufferedReader;
22+
import java.io.IOException;
1723
import java.io.StringReader;
1824
import java.util.ArrayList;
1925
import java.util.Arrays;
@@ -680,6 +686,77 @@ public static boolean isAllOrWildcard(String data) {
680686
return "_all".equals(data) || "*".equals(data);
681687
}
682688

689+
/**
690+
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
691+
* Wraps the output into an anonymous object if needed. The content is not pretty-printed
692+
* nor human readable.
693+
*/
694+
public static String toString(MediaType mediaType, ToXContent toXContent) {
695+
return toString(mediaType, toXContent, false, false);
696+
}
697+
698+
/**
699+
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
700+
* Wraps the output into an anonymous object if needed.
701+
* Allows to configure the params.
702+
* The content is not pretty-printed nor human readable.
703+
*/
704+
public static String toString(MediaType mediaType, ToXContent toXContent, ToXContent.Params params) {
705+
return toString(mediaType, toXContent, params, false, false);
706+
}
707+
708+
/**
709+
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
710+
* Wraps the output into an anonymous object if needed. Allows to control whether the outputted
711+
* json needs to be pretty printed and human readable.
712+
*
713+
*/
714+
public static String toString(MediaType mediaType, ToXContent toXContent, boolean pretty, boolean human) {
715+
return toString(mediaType, toXContent, ToXContent.EMPTY_PARAMS, pretty, human);
716+
}
717+
718+
/**
719+
* Return a {@link String} that is the json representation of the provided {@link ToXContent}.
720+
* Wraps the output into an anonymous object if needed.
721+
* Allows to configure the params.
722+
* Allows to control whether the outputted json needs to be pretty printed and human readable.
723+
*/
724+
private static String toString(MediaType mediaType, ToXContent toXContent, ToXContent.Params params, boolean pretty, boolean human) {
725+
try {
726+
XContentBuilder builder = createBuilder(mediaType, pretty, human);
727+
if (toXContent.isFragment()) {
728+
builder.startObject();
729+
}
730+
toXContent.toXContent(builder, params);
731+
if (toXContent.isFragment()) {
732+
builder.endObject();
733+
}
734+
return builder.toString();
735+
} catch (IOException e) {
736+
try {
737+
XContentBuilder builder = createBuilder(mediaType, pretty, human);
738+
builder.startObject();
739+
builder.field("error", "error building toString out of XContent: " + e.getMessage());
740+
builder.field("stack_trace", ExceptionsHelper.stackTrace(e));
741+
builder.endObject();
742+
return builder.toString();
743+
} catch (IOException e2) {
744+
throw new OpenSearchException("cannot generate error message for deserialization", e);
745+
}
746+
}
747+
}
748+
749+
private static XContentBuilder createBuilder(MediaType mediaType, boolean pretty, boolean human) throws IOException {
750+
XContentBuilder builder = XContentBuilder.builder(mediaType.xContent());
751+
if (pretty) {
752+
builder.prettyPrint();
753+
}
754+
if (human) {
755+
builder.humanReadable(true);
756+
}
757+
return builder;
758+
}
759+
683760
/**
684761
* Truncates string to a length less than length. Backtracks to throw out
685762
* high surrogates.

0 commit comments

Comments
 (0)