Java Code Examples for org.elasticsearch.common.xcontent.XContentBuilder#builder()

The following examples show how to use org.elasticsearch.common.xcontent.XContentBuilder#builder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexFeatureStoreTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testBadValues() throws IOException {
    Map<String, Object> map = new HashMap<>();
    XContentBuilder builder = XContentBuilder.builder(Requests.INDEX_CONTENT_TYPE.xContent());
    BytesReference bytes = BytesReference.bytes(builder.map(map));
    assertThat(expectThrows(IllegalArgumentException.class,
            () -> IndexFeatureStore.parse(StoredFeature.class, StoredFeature.TYPE, bytes))
            .getMessage(), equalTo("No StorableElement found."));

    builder = XContentBuilder.builder(Requests.INDEX_CONTENT_TYPE.xContent());
    map.put("featureset", LtrTestUtils.randomFeatureSet());
    BytesReference bytes2 = BytesReference.bytes(builder.map(map));
    assertThat(expectThrows(IllegalArgumentException.class,
            () -> IndexFeatureStore.parse(StoredFeature.class, StoredFeature.TYPE, bytes2))
            .getMessage(), equalTo("Expected an element of type [" + StoredFeature.TYPE + "] but" +
            " got [" + StoredFeatureSet.TYPE + "]."));
}
 
Example 2
Source File: CustomRealmIT.java    From shield-custom-realm-example with Apache License 2.0 6 votes vote down vote up
public void testSettingsFiltering() throws Exception {
    Response response = getRestClient().performRequest("GET", "/_nodes/settings", Collections.emptyMap(), (HttpEntity) null,
        new BasicHeader(CustomRealm.USER_HEADER, randomFrom(KNOWN_USERS)),
        new BasicHeader(CustomRealm.PW_HEADER, PASSWORD));
    assertThat(response.getStatusLine().getStatusCode(), is(200));

    XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, response.getEntity().getContent());
    XContentParser.Token token;
    Settings settings = null;
    while ((token = parser.nextToken()) != null) {
        if (token == XContentParser.Token.FIELD_NAME && parser.currentName().equals("settings")) {
            parser.nextToken();
            XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent());
            settings = Settings.builder()
                    .loadFromSource(builder.copyCurrentStructure(parser).bytes().utf8ToString(), XContentType.JSON)
                    .build();
            break;
        }
    }
    assertThat(settings, notNullValue());

    logger.error("settings for shield.authc.realms.custom.users {}", settings.getGroups("shield.authc.realms.custom.users"));
    // custom is the name configured externally...
    assertTrue(settings.getGroups("shield.authc.realms.custom.users").isEmpty());
}
 
Example 3
Source File: AbstractXContentTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
public static <T> XContentTester<T> xContentTester(
        CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParser,
        Supplier<T> instanceSupplier,
        CheckedBiConsumer<T, XContentBuilder, IOException> toXContent,
        CheckedFunction<XContentParser, T, IOException> fromXContent) {
    return new XContentTester<T>(
            createParser,
            instanceSupplier,
            (testInstance, xContentType) -> {
                try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
                    toXContent.accept(testInstance, builder);
                    return BytesReference.bytes(builder);
                }
            },
            fromXContent);
}
 
Example 4
Source File: CustomMustacheFactory.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Function<String, String> createFunction(Object resolved) {
    return s -> {
        if (resolved == null) {
            return null;
        }
        try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
            if (resolved == null) {
                builder.nullValue();
            } else if (resolved instanceof Iterable) {
                builder.startArray();
                for (Object o : (Iterable) resolved) {
                    builder.value(o);
                }
                builder.endArray();
            } else if (resolved instanceof Map) {
                builder.map((Map<String, ?>) resolved);
            } else {
                // Do not handle as JSON
                return oh.stringify(resolved);
            }
            return Strings.toString(builder);
        } catch (IOException e) {
            throw new MustacheException("Failed to convert object to JSON", e);
        }
    };
}
 
Example 5
Source File: Setting.java    From crate with Apache License 2.0 5 votes vote down vote up
private static String arrayToParsableString(List<String> array) {
    try {
        XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
        builder.startArray();
        for (String element : array) {
            builder.value(element);
        }
        builder.endArray();
        return Strings.toString(builder);
    } catch (IOException ex) {
        throw new ElasticsearchException(ex);
    }
}
 
Example 6
Source File: Settings.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
        builder.startObject();
        toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true")));
        builder.endObject();
        return Strings.toString(builder);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 7
Source File: RepositoryDataTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testIndexThatReferenceANullSnapshot() throws IOException {
    final XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON).xContent());
    builder.startObject();
    {
        builder.startArray("snapshots");
        builder.value(new SnapshotId("_name", "_uuid"));
        builder.endArray();

        builder.startObject("indices");
        {
            builder.startObject("docs");
            {
                builder.field("id", "_id");
                builder.startArray("snapshots");
                {
                    builder.startObject();
                    if (randomBoolean()) {
                        builder.field("name", "_name");
                    }
                    builder.endObject();
                }
                builder.endArray();
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();

    try (XContentParser xParser = createParser(builder)) {
        ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () ->
            RepositoryData.snapshotsFromXContent(xParser, randomNonNegativeLong()));
        assertThat(e.getMessage(), equalTo("Detected a corrupted repository, " +
                                           "index [docs/_id] references an unknown snapshot uuid [null]"));
    }
}
 
Example 8
Source File: ObjectPath.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link XContentBuilder} from the xContent object underlying this {@link ObjectPath}.
 * This only works for {@link ObjectPath} instances created from an xContent object, not from nested
 * substructures. We throw an {@link UnsupportedOperationException} in those cases.
 */
@SuppressWarnings("unchecked")
public XContentBuilder toXContentBuilder(XContent xContent) throws IOException {
    XContentBuilder builder = XContentBuilder.builder(xContent);
    if (this.object instanceof Map) {
        builder.map((Map<String, Object>) this.object);
    } else {
        throw new UnsupportedOperationException("Only ObjectPath created from a map supported.");
    }
    return builder;
}
 
Example 9
Source File: AbstractAuditLog.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
@Override
public void logExternalConfig(Settings settings, Environment environment) {

    if(!checkComplianceFilter(Category.COMPLIANCE_EXTERNAL_CONFIG, null, getOrigin())) {
        return;
    }

    final Map<String, Object> configAsMap = Utils.convertJsonToxToStructuredMap(settings);

    final SecurityManager sm = System.getSecurityManager();

    if (sm != null) {
        sm.checkPermission(new SpecialPermission());
    }

    final Map<String, String> envAsMap = AccessController.doPrivileged(new PrivilegedAction<Map<String, String>>() {
        @Override
        public Map<String, String> run() {
            return System.getenv();
        }
    });

    final Map propsAsMap = AccessController.doPrivileged(new PrivilegedAction<Map>() {
        @Override
        public Map run() {
            return System.getProperties();
        }
    });

    final String sha256 = DigestUtils.sha256Hex(configAsMap.toString()+envAsMap.toString()+propsAsMap.toString());
    AuditMessage msg = new AuditMessage(Category.COMPLIANCE_EXTERNAL_CONFIG, clusterService, null, null);

    try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
        builder.startObject();
        builder.startObject("external_configuration");
        builder.field("elasticsearch_yml", configAsMap);
        builder.field("os_environment", envAsMap);
        builder.field("java_properties", propsAsMap);
        builder.field("sha256_checksum", sha256);
        builder.endObject();
        builder.endObject();
        builder.close();
        msg.addUnescapedJsonToRequestBody(Strings.toString(builder));
    } catch (Exception e) {
        log.error("Unable to build message",e);
    }

    Map<String, Path> paths = new HashMap<String, Path>();
    for(String key: settings.keySet()) {
        if(key.startsWith("opendistro_security") &&
                (key.contains("filepath") || key.contains("file_path"))) {
            String value = settings.get(key);
            if(value != null && !value.isEmpty()) {
                Path path = value.startsWith("/")?Paths.get(value):environment.configFile().resolve(value);
                paths.put(key, path);
            }
        }
    }
    msg.addFileInfos(paths);


    save(msg);
}
 
Example 10
Source File: FlsPerfTest.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
protected void populateData(TransportClient tc) {

        Map<String, Object> indexSettings = new HashMap<>(3);
        indexSettings.put("index.mapping.total_fields.limit",50000);
        indexSettings.put("number_of_shards", 10);
        indexSettings.put("number_of_replicas", 0);

        tc.admin().indices().create(new CreateIndexRequest("deals")
        .settings(indexSettings))
        .actionGet();

        try {

            IndexRequest ir =  new IndexRequest("deals").type("deals2").id("idx1");
            XContentBuilder b = XContentBuilder.builder(JsonXContent.jsonXContent);
            b.startObject();

            b.field("amount",1000);

            b.startObject("xyz");
            b.field("abc","val");
            b.endObject();

            b.endObject();
            ir.source(b);

            tc.index(ir).actionGet();

            for(int i=0; i<1500; i++) {

                ir =  new IndexRequest("deals").type("deals").id("id"+i);
                b = XContentBuilder.builder(JsonXContent.jsonXContent);
                b.startObject();
                for(int j=0; j<2000;j++) {
                    b.field("field"+j,"val"+j);
                }

                b.endObject();
                ir.source(b);

                tc.index(ir).actionGet();

            }

            tc.admin().indices().refresh(new RefreshRequest("deals")).actionGet();
        } catch (IOException e) {
            e.printStackTrace();
            Assert.fail(e.toString());
        }

    }
 
Example 11
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public static XContentBuilder builder() throws IOException {
    return XContentBuilder.builder(XContentType.JSON.xContent());
}
 
Example 12
Source File: YamlXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
public static XContentBuilder contentBuilder() throws IOException {
    return XContentBuilder.builder(YAML_XCONTENT);
}
 
Example 13
Source File: JsonXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
public static XContentBuilder contentBuilder() throws IOException {
    return XContentBuilder.builder(JSON_XCONTENT);
}
 
Example 14
Source File: SmileXContent.java    From crate with Apache License 2.0 4 votes vote down vote up
public static XContentBuilder contentBuilder() throws IOException {
    return XContentBuilder.builder(SMILE_XCONTENT);
}
 
Example 15
Source File: RepositoryDataTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testIndexThatReferencesAnUnknownSnapshot() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    final RepositoryData repositoryData = generateRandomRepoData();

    XContentBuilder builder = XContentBuilder.builder(xContent);
    repositoryData.snapshotsToXContent(builder, true);
    RepositoryData parsedRepositoryData;
    try (XContentParser xParser = createParser(builder)) {
        parsedRepositoryData = RepositoryData.snapshotsFromXContent(xParser, repositoryData.getGenId());
    }
    assertEquals(repositoryData, parsedRepositoryData);

    Map<String, SnapshotId> snapshotIds = new HashMap<>();
    Map<String, SnapshotState> snapshotStates = new HashMap<>();
    for (SnapshotId snapshotId : parsedRepositoryData.getSnapshotIds()) {
        snapshotIds.put(snapshotId.getUUID(), snapshotId);
        snapshotStates.put(snapshotId.getUUID(), parsedRepositoryData.getSnapshotState(snapshotId));
    }

    final IndexId corruptedIndexId = randomFrom(parsedRepositoryData.getIndices().values());

    Map<IndexId, Set<SnapshotId>> indexSnapshots = new HashMap<>();
    final ShardGenerations.Builder shardGenBuilder = ShardGenerations.builder();
    for (Map.Entry<String, IndexId> snapshottedIndex : parsedRepositoryData.getIndices().entrySet()) {
        IndexId indexId = snapshottedIndex.getValue();
        Set<SnapshotId> snapshotsIds = new LinkedHashSet<>(parsedRepositoryData.getSnapshots(indexId));
        if (corruptedIndexId.equals(indexId)) {
            snapshotsIds.add(new SnapshotId("_uuid", "_does_not_exist"));
        }
        indexSnapshots.put(indexId, snapshotsIds);
        final int shardCount = randomIntBetween(1, 10);
        for (int i = 0; i < shardCount; ++i) {
            shardGenBuilder.put(indexId, i, UUIDs.randomBase64UUID(random()));
        }
    }
    assertNotNull(corruptedIndexId);

    RepositoryData corruptedRepositoryData = new RepositoryData(parsedRepositoryData.getGenId(), snapshotIds, snapshotStates,
                                                                indexSnapshots, shardGenBuilder.build());

    final XContentBuilder corruptedBuilder = XContentBuilder.builder(xContent);
    corruptedRepositoryData.snapshotsToXContent(corruptedBuilder, true);

    try (XContentParser xParser = createParser(corruptedBuilder)) {
        ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () ->
            RepositoryData.snapshotsFromXContent(xParser, corruptedRepositoryData.getGenId()));
        assertThat(e.getMessage(), equalTo("Detected a corrupted repository, index " + corruptedIndexId + " references an unknown " +
                                           "snapshot uuid [_does_not_exist]"));
    }
}
 
Example 16
Source File: ESTestCaseTests.java    From crate with Apache License 2.0 4 votes vote down vote up
public void testShuffleXContentExcludeFields() throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
        builder.startObject();
        {
            builder.field("field1", "value1");
            builder.field("field2", "value2");
            {
                builder.startObject("object1");
                {
                    builder.field("inner1", "value1");
                    builder.field("inner2", "value2");
                    builder.field("inner3", "value3");
                }
                builder.endObject();
            }
            {
                builder.startObject("object2");
                {
                    builder.field("inner4", "value4");
                    builder.field("inner5", "value5");
                    builder.field("inner6", "value6");
                }
                builder.endObject();
            }
        }
        builder.endObject();
        BytesReference bytes = BytesReference.bytes(builder);
        final LinkedHashMap<String, Object> initialMap;
        try (XContentParser parser = createParser(xContentType.xContent(), bytes)) {
            initialMap = (LinkedHashMap<String, Object>)parser.mapOrdered();
        }

        List<String> expectedInnerKeys1 = Arrays.asList("inner1", "inner2", "inner3");
        Set<List<String>> distinctTopLevelKeys = new HashSet<>();
        Set<List<String>> distinctInnerKeys2 = new HashSet<>();
        for (int i = 0; i < 10; i++) {
            try (XContentParser parser = createParser(xContentType.xContent(), bytes)) {
                try (XContentBuilder shuffledBuilder = shuffleXContent(parser, randomBoolean(), "object1")) {
                    try (XContentParser shuffledParser = createParser(shuffledBuilder)) {
                        Map<String, Object> shuffledMap = shuffledParser.mapOrdered();
                        assertEquals("both maps should contain the same mappings", initialMap, shuffledMap);
                        List<String> shuffledKeys = new ArrayList<>(shuffledMap.keySet());
                        distinctTopLevelKeys.add(shuffledKeys);
                        @SuppressWarnings("unchecked")
                        Map<String, Object> innerMap1 = (Map<String, Object>)shuffledMap.get("object1");
                        List<String> actualInnerKeys1 = new ArrayList<>(innerMap1.keySet());
                        assertEquals("object1 should have been left untouched", expectedInnerKeys1, actualInnerKeys1);
                        @SuppressWarnings("unchecked")
                        Map<String, Object> innerMap2 = (Map<String, Object>)shuffledMap.get("object2");
                        List<String> actualInnerKeys2 = new ArrayList<>(innerMap2.keySet());
                        distinctInnerKeys2.add(actualInnerKeys2);
                    }
                }
            }
        }

        //out of 10 shuffling runs we expect to have at least more than 1 distinct output for both top level keys and inner object2
        assertThat(distinctTopLevelKeys.size(), greaterThan(1));
        assertThat(distinctInnerKeys2.size(), greaterThan(1));
    }
}