Skip to content

Commit

Permalink
React to Strings changes from #9103 in core (#3110)
Browse files Browse the repository at this point in the history
### Description

React to changes from
opensearch-project/OpenSearch#9103 in core

### Check List
- [ ] New functionality includes testing
- [ ] New functionality has been documented
- [ ] Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and
signing off your commits, please check
[here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).

---------

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks authored Aug 7, 2023
1 parent 07f791a commit 6cc90e6
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.update.UpdateRequest;
import org.opensearch.client.Client;
import org.opensearch.common.Strings;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.ToXContentObject;
Expand Down Expand Up @@ -680,7 +679,7 @@ private static String configToJson(CType configType, Map<String, ? extends ToXCo

builder.endObject();

return Strings.toString(builder);
return builder.toString();
}

private void writeSingleEntryConfigToIndex(Client client, CType configType, ToXContentObject config) {
Expand All @@ -701,7 +700,7 @@ private void writeSingleEntryConfigToIndex(Client client, CType configType, Stri

builder.endObject();

String json = Strings.toString(builder);
String json = builder.toString();

log.info("Writing security plugin configuration into index " + configType + ":\n" + json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
import org.opensearch.client.AdminClient;
import org.opensearch.client.Client;
import org.opensearch.cluster.health.ClusterHealthStatus;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.http.BindHttpException;
import org.opensearch.node.PluginAwareNode;
import org.opensearch.plugins.Plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.security.DefaultObjectMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import java.io.IOException;

import org.opensearch.action.ActionResponse;
import org.opensearch.common.Strings;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;

Expand Down Expand Up @@ -105,6 +105,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this, true, true);
return Strings.toString(MediaTypeRegistry.JSON, this, true, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.opensearch.action.update.UpdateRequest;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.Strings;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -496,7 +495,7 @@ public void logDocumentRead(String index, String id, ShardId shardId, Map<String
builder.field("field_names", fieldNameValues.keySet());
builder.endObject();
builder.close();
msg.addUnescapedJsonToRequestBody(Strings.toString(builder));
msg.addUnescapedJsonToRequestBody(builder.toString());
} catch (IOException e) {
log.error(e.toString());
}
Expand Down Expand Up @@ -739,7 +738,7 @@ public Map run() {
builder.endObject();
builder.endObject();
builder.close();
msg.addUnescapedJsonToRequestBody(Strings.toString(builder));
msg.addUnescapedJsonToRequestBody(builder.toString());
} catch (Exception e) {
log.error("Unable to build message", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,15 @@ public String getDocId() {
@Override
public String toString() {
try {
return org.opensearch.common.Strings.toString(JsonXContent.contentBuilder().map(getAsMap()));
return JsonXContent.contentBuilder().map(getAsMap()).toString();
} catch (final IOException e) {
throw ExceptionsHelper.convertToOpenSearchException(e);
}
}

public String toPrettyString() {
try {
return org.opensearch.common.Strings.toString(JsonXContent.contentBuilder().prettyPrint().map(getAsMap()));
return JsonXContent.contentBuilder().prettyPrint().map(getAsMap()).toString();
} catch (final IOException e) {
throw ExceptionsHelper.convertToOpenSearchException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.opensearch.action.update.UpdateRequest;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.Strings;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -314,7 +313,7 @@ private static AuditMessage resolveInner(
builder.field("transient_settings", Utils.convertJsonToxToStructuredMap(persistentSettings));
}
builder.endObject();
msg.addUnescapedJsonToRequestBody(builder == null ? null : Strings.toString(builder));
msg.addUnescapedJsonToRequestBody(builder == null ? null : builder.toString());
} catch (IOException e) {
log.error(e.toString());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.util.concurrent.ThreadContext.StoredContext;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.env.Environment;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.security.auditlog.AuditLog;
Expand Down Expand Up @@ -464,7 +464,7 @@ public Map<CType, SecurityDynamicConfiguration<?>> getConfigurationsFromIndex(
if (logComplianceEvent && auditLog.getComplianceConfig().isEnabled()) {
CType configurationType = configTypes.iterator().next();
Map<String, String> fields = new HashMap<String, String>();
fields.put(configurationType.toLCString(), Strings.toString(XContentType.JSON, retVal.get(configurationType)));
fields.put(configurationType.toLCString(), Strings.toString(MediaTypeRegistry.JSON, retVal.get(configurationType)));
auditLog.logDocumentRead(this.securityIndex, configurationType.toLCString(), null, fields);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.index.query.ParsedQuery;
import org.opensearch.core.rest.RestStatus;
Expand Down Expand Up @@ -230,10 +230,10 @@ public boolean invoke(
StringBuilder sb = new StringBuilder();

if (searchRequest.source() != null) {
sb.append(Strings.toString(XContentType.JSON, searchRequest.source()) + System.lineSeparator());
sb.append(Strings.toString(MediaTypeRegistry.JSON, searchRequest.source()) + System.lineSeparator());
}

sb.append(Strings.toString(XContentType.JSON, af) + System.lineSeparator());
sb.append(Strings.toString(MediaTypeRegistry.JSON, af) + System.lineSeparator());

LogManager.getLogger("debuglogger").error(sb.toString());

Expand All @@ -245,7 +245,9 @@ public boolean invoke(
LogManager.getLogger("debuglogger")
.error(
"Shard requestcache enabled for "
+ (searchRequest.source() == null ? "<NULL>" : Strings.toString(XContentType.JSON, searchRequest.source()))
+ (searchRequest.source() == null
? "<NULL>"
: Strings.toString(MediaTypeRegistry.JSON, searchRequest.source()))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
import org.opensearch.client.indices.GetIndexResponse;
import org.opensearch.client.transport.NoNodeAvailableException;
import org.opensearch.cluster.health.ClusterHealthStatus;
import org.opensearch.common.Strings;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
Expand All @@ -118,6 +118,7 @@
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -1240,7 +1241,7 @@ private static String convertToYaml(String type, BytesReference bytes, boolean p
builder.prettyPrint();
}
builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML);
return Strings.toString(builder);
return builder.toString();
}
}

Expand All @@ -1267,7 +1268,7 @@ protected static void generateDiagnoseTrace(final RestHighLevelClient restHighLe
try {
sb.append("ClusterHealthRequest:" + System.lineSeparator());
ClusterHealthResponse nir = restHighLevelClient.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
sb.append(Strings.toString(XContentType.JSON, nir, true, true));
sb.append(Strings.toString(MediaTypeRegistry.JSON, nir, true, true));
} catch (Exception e1) {
sb.append(ExceptionsHelper.stackTrace(e1));
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/opensearch/security/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.junit.Assert;
import org.junit.Test;

import org.opensearch.common.Strings;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.security.securityconf.Migration;
import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.junit.Before;
import org.junit.Test;

import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -88,7 +87,7 @@ public void testDefaultSerialize() throws IOException {
.endObject()
.endObject();

assertTrue(compareJson(Strings.toString(jsonBuilder), json));
assertTrue(compareJson(jsonBuilder.toString(), json));
}

@Test
Expand Down Expand Up @@ -148,7 +147,7 @@ public void testDeserialize() throws IOException {
.field("write_ignore_users", Collections.singletonList("test-user-3"))
.endObject()
.endObject();
final String json = Strings.toString(jsonBuilder);
final String json = jsonBuilder.toString();

// act
final AuditConfig auditConfig = objectMapper.readValue(json, AuditConfig.class);
Expand Down Expand Up @@ -246,7 +245,7 @@ public void testSerialize() throws IOException {
// act
final String json = objectMapper.writeValueAsString(auditConfig);
// assert
assertTrue(compareJson(Strings.toString(jsonBuilder), json));
assertTrue(compareJson(jsonBuilder.toString(), json));
}

@Test
Expand Down Expand Up @@ -288,7 +287,7 @@ public void testNullSerialize() throws IOException {
// act
final String json = objectMapper.writeValueAsString(auditConfig);
// assert
assertTrue(compareJson(Strings.toString(jsonBuilder), json));
assertTrue(compareJson(jsonBuilder.toString(), json));
}

@Test
Expand Down Expand Up @@ -348,7 +347,7 @@ public void testCustomSettings() throws IOException {
.field("write_log_diffs", false)
.endObject()
.endObject();
final String json = Strings.toString(jsonBuilder);
final String json = jsonBuilder.toString();

// act
final AuditConfig auditConfig = customObjectMapper.readValue(json, AuditConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
import org.opensearch.client.Client;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;

public class DlsTest extends AbstractDlsFlsTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
import org.opensearch.client.Client;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.security.test.DynamicSecurityConfig;
Expand Down Expand Up @@ -65,7 +64,7 @@ protected void populateData(final Client tc) {
} catch (final Exception e) {
throw new RuntimeException(e);
}
final String mappings = Strings.toString(builder);
final String mappings = builder.toString();

final Consumer<String> createIndexWithMapping = (indexName) -> {
final CreateIndexRequest createIndex = new CreateIndexRequest(indexName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opensearch.common.Strings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.common.bytes.BytesArray;
Expand Down Expand Up @@ -301,7 +300,7 @@ public Map<String, RequestContentValidator.DataType> allowedKeys() {
private JsonNode xContentToJsonNode(final ToXContent toXContent) throws IOException {
try (final var xContentBuilder = XContentFactory.jsonBuilder()) {
toXContent.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
return DefaultObjectMapper.readTree(Strings.toString(xContentBuilder));
return DefaultObjectMapper.readTree(xContentBuilder.toString());
}
}

Expand Down

0 comments on commit 6cc90e6

Please sign in to comment.