org.elasticsearch.common.xcontent.XContent Java Examples
The following examples show how to use
org.elasticsearch.common.xcontent.XContent.
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: AbstractXContentTestCase.java From crate with Apache License 2.0 | 6 votes |
public static <T extends ToXContent> void testFromXContent( int numberOfTestRuns, Supplier<T> instanceSupplier, boolean supportsUnknownFields, String[] shuffleFieldsExceptions, Predicate<String> randomFieldsExcludeFilter, CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParserFunction, CheckedFunction<XContentParser, T, IOException> fromXContent, BiConsumer<T, T> assertEqualsConsumer, boolean assertToXContentEquivalence, ToXContent.Params toXContentParams) throws IOException { xContentTester(createParserFunction, instanceSupplier, toXContentParams, fromXContent) .numberOfTestRuns(numberOfTestRuns) .supportsUnknownFields(supportsUnknownFields) .shuffleFieldsExceptions(shuffleFieldsExceptions) .randomFieldsExcludeFilter(randomFieldsExcludeFilter) .assertEqualsConsumer(assertEqualsConsumer) .assertToXContentEquivalence(assertToXContentEquivalence) .test(); }
Example #2
Source File: AbstractXContentTestCase.java From crate with Apache License 2.0 | 6 votes |
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 #3
Source File: ESTestCase.java From crate with Apache License 2.0 | 5 votes |
/** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. Shuffles the keys to make sure that parsing never relies on keys ordering. */ protected static BytesReference toShuffledXContent(ToXContent toXContent, XContentType xContentType, ToXContent.Params params, boolean humanReadable, CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> parserFunction, String... exceptFieldNames) throws IOException{ BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, params, humanReadable); try (XContentParser parser = parserFunction.apply(xContentType.xContent(), bytes)) { try (XContentBuilder builder = shuffleXContent(parser, rarely(), exceptFieldNames)) { return BytesReference.bytes(builder); } } }
Example #4
Source File: XContentTestUtils.java From crate with Apache License 2.0 | 5 votes |
/** * Inserts key/value pairs into xContent passed in as {@link BytesReference} and returns a new {@link XContentBuilder} * The paths argument uses dot separated fieldnames and numbers for array indices, similar to what we do in * {@link ObjectPath}. * The key/value arguments can suppliers that either return fixed or random values. */ public static XContentBuilder insertIntoXContent(XContent xContent, BytesReference original, List<String> paths, Supplier<String> key, Supplier<Object> value) throws IOException { ObjectPath object = ObjectPath.createFromXContent(xContent, original); for (String path : paths) { Map<String, Object> insertMap = object.evaluate(path); insertMap.put(key.get(), value.get()); } return object.toXContentBuilder(xContent); }
Example #5
Source File: ObjectPath.java From crate with Apache License 2.0 | 5 votes |
/** * 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 #6
Source File: ObjectPath.java From crate with Apache License 2.0 | 5 votes |
public static ObjectPath createFromXContent(XContent xContent, BytesReference input) throws IOException { try (XContentParser parser = xContent .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input.streamInput())) { if (parser.nextToken() == XContentParser.Token.START_ARRAY) { return new ObjectPath(parser.listOrderedMap()); } return new ObjectPath(parser.mapOrdered()); } }
Example #7
Source File: AbstractXContentTestCase.java From crate with Apache License 2.0 | 5 votes |
static BytesReference insertRandomFieldsAndShuffle(BytesReference xContent, XContentType xContentType, boolean supportsUnknownFields, String[] shuffleFieldsExceptions, Predicate<String> randomFieldsExcludeFilter, CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParserFunction) throws IOException { BytesReference withRandomFields; if (supportsUnknownFields) { // add a few random fields to check that the parser is lenient on new fields withRandomFields = XContentTestUtils.insertRandomFields(xContentType, xContent, randomFieldsExcludeFilter, random()); } else { withRandomFields = xContent; } XContentParser parserWithRandonFields = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields); return BytesReference.bytes(ESTestCase.shuffleXContent(parserWithRandonFields, false, shuffleFieldsExceptions)); }
Example #8
Source File: AbstractXContentTestCase.java From crate with Apache License 2.0 | 5 votes |
private XContentTester( CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParser, Supplier<T> instanceSupplier, CheckedBiFunction<T, XContentType, BytesReference, IOException> toXContent, CheckedFunction<XContentParser, T, IOException> fromXContent) { this.createParser = createParser; this.instanceSupplier = instanceSupplier; this.toXContent = toXContent; this.fromXContent = fromXContent; }
Example #9
Source File: AbstractXContentTestCase.java From crate with Apache License 2.0 | 5 votes |
public static <T extends ToXContent> XContentTester<T> xContentTester( CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParser, Supplier<T> instanceSupplier, ToXContent.Params toXContentParams, CheckedFunction<XContentParser, T, IOException> fromXContent) { return new XContentTester<T>( createParser, instanceSupplier, (testInstance, xContentType) -> XContentHelper.toXContent(testInstance, xContentType, toXContentParams, false), fromXContent); }
Example #10
Source File: JsonXContentGenerator.java From crate with Apache License 2.0 | 5 votes |
protected void copyRawValue(InputStream stream, XContent xContent) throws IOException { // EMPTY is safe here because we never call namedObject try (XContentParser parser = xContent // It's okay to pass the throwing deprecation handler because we // should not be writing raw fields when generating JSON .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) { copyCurrentStructure(parser); } }
Example #11
Source File: BaseTransportCoordinateSearchAction.java From siren-join with GNU Affero General Public License v3.0 | 5 votes |
protected XContentBuilder buildSource(XContent content, Map<String, Object> map) { try { // Enforce the content type to be CBOR as it is more efficient for large byte arrays return XContentBuilder.builder(XContentType.CBOR.xContent()).map(map); } catch (IOException e) { logger.error("failed to build source", e); throw new IllegalStateException("Failed to build source", e); } }
Example #12
Source File: XmlXContentFactory.java From elasticsearch-xml with Apache License 2.0 | 5 votes |
/** * Guesses the content (type) based on the provided char sequence. */ public static XContent xContent(CharSequence content) { XmlXContentType type = xContentType(content); if (type == null) { throw new ElasticsearchParseException("Failed to derive xcontent from " + content); } return xContent(type); }
Example #13
Source File: XmlXContentFactory.java From elasticsearch-xml with Apache License 2.0 | 5 votes |
/** * Guesses the content type based on the provided bytes. */ public static XContent xContent(byte[] data, int offset, int length) { XmlXContentType type = xContentType(data, offset, length); if (type == null) { throw new ElasticsearchParseException("Failed to derive xcontent from (offset=" + offset + ", length=" + length + "): " + Arrays.toString(data)); } return xContent(type); }
Example #14
Source File: XmlXContentFactory.java From elasticsearch-xml with Apache License 2.0 | 5 votes |
public static XContent xContent(BytesReference bytes) { XmlXContentType type = xContentType(bytes); if (type == null) { throw new ElasticsearchParseException("Failed to derive xcontent from " + bytes); } return xContent(type); }
Example #15
Source File: ElasticsearchJavaAPIScriptTest.java From syncer with BSD 3-Clause "New" or "Revised" License | 4 votes |
private XContentParser createParser(XContent xContent, BytesReference data) throws IOException { return xContent.createParser(xContentRegistry(), data); }
Example #16
Source File: XmlXContentBuilder.java From elasticsearch-xml with Apache License 2.0 | 4 votes |
/** * Constructs a new builder using the provided xcontent and an OutputStream. Make sure * to call {@link #close()} when the builder is done with. */ public XmlXContentBuilder(XContent xContent, OutputStream bos) throws IOException { this.bos = bos; this.generator = xContent.createGenerator(bos); }
Example #17
Source File: MultiSearchRequest.java From Elasticsearch with Apache License 2.0 | 4 votes |
public MultiSearchRequest add(BytesReference data, boolean isTemplateRequest, @Nullable String[] indices, @Nullable String[] types, @Nullable String searchType, @Nullable String routing, IndicesOptions indicesOptions, boolean allowExplicitIndex) throws Exception { XContent xContent = XContentFactory.xContent(data); int from = 0; int length = data.length(); byte marker = xContent.streamSeparator(); while (true) { int nextMarker = findNextMarker(marker, from, data, length); if (nextMarker == -1) { break; } // support first line with \n if (nextMarker == 0) { from = nextMarker + 1; continue; } SearchRequest searchRequest = new SearchRequest(); if (indices != null) { searchRequest.indices(indices); } if (indicesOptions != null) { searchRequest.indicesOptions(indicesOptions); } if (types != null && types.length > 0) { searchRequest.types(types); } if (routing != null) { searchRequest.routing(routing); } searchRequest.searchType(searchType); IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); // now parse the action if (nextMarker - from > 0) { try (XContentParser parser = xContent.createParser(data.slice(from, nextMarker - from))) { Map<String, Object> source = parser.map(); for (Map.Entry<String, Object> entry : source.entrySet()) { Object value = entry.getValue(); if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) { if (!allowExplicitIndex) { throw new IllegalArgumentException("explicit index in multi percolate is not allowed"); } searchRequest.indices(nodeStringArrayValue(value)); } else if ("type".equals(entry.getKey()) || "types".equals(entry.getKey())) { searchRequest.types(nodeStringArrayValue(value)); } else if ("search_type".equals(entry.getKey()) || "searchType".equals(entry.getKey())) { searchRequest.searchType(nodeStringValue(value, null)); } else if ("request_cache".equals(entry.getKey()) || "requestCache".equals(entry.getKey())) { searchRequest.requestCache(nodeBooleanValue(value)); } else if ("preference".equals(entry.getKey())) { searchRequest.preference(nodeStringValue(value, null)); } else if ("routing".equals(entry.getKey())) { searchRequest.routing(nodeStringValue(value, null)); } } defaultOptions = IndicesOptions.fromMap(source, defaultOptions); } } searchRequest.indicesOptions(defaultOptions); // move pointers from = nextMarker + 1; // now for the body nextMarker = findNextMarker(marker, from, data, length); if (nextMarker == -1) { break; } if (isTemplateRequest) { searchRequest.templateSource(data.slice(from, nextMarker - from)); } else { searchRequest.source(data.slice(from, nextMarker - from)); } // move pointers from = nextMarker + 1; add(searchRequest); } return this; }
Example #18
Source File: XmlXContentFactory.java From elasticsearch-xml with Apache License 2.0 | 4 votes |
/** * Returns the {@link org.elasticsearch.common.xcontent.XContent} for the provided content type. */ public static XContent xContent(XmlXContentType type) { return type.xContent(); }
Example #19
Source File: XmlXContentFactory.java From elasticsearch-xml with Apache License 2.0 | 4 votes |
/** * Guesses the content type based on the provided bytes. */ public static XContent xContent(byte[] data) { return xContent(data, 0, data.length); }
Example #20
Source File: AbstractXContentTestCase.java From crate with Apache License 2.0 | 4 votes |
public static <T extends ToXContent> XContentTester<T> xContentTester( CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParser, Supplier<T> instanceSupplier, CheckedFunction<XContentParser, T, IOException> fromXContent) { return xContentTester(createParser, instanceSupplier, ToXContent.EMPTY_PARAMS, fromXContent); }
Example #21
Source File: ESTestCase.java From crate with Apache License 2.0 | 4 votes |
/** * Create a new {@link XContentParser}. */ protected final XContentParser createParser(XContent xContent, BytesReference data) throws IOException { return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data.streamInput()); }
Example #22
Source File: ESTestCase.java From crate with Apache License 2.0 | 4 votes |
/** * Create a new {@link XContentParser}. */ protected final XContentParser createParser(XContent xContent, byte[] data) throws IOException { return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data); }
Example #23
Source File: ESTestCase.java From crate with Apache License 2.0 | 4 votes |
/** * Create a new {@link XContentParser}. */ protected final XContentParser createParser(XContent xContent, InputStream data) throws IOException { return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data); }
Example #24
Source File: ESTestCase.java From crate with Apache License 2.0 | 4 votes |
/** * Create a new {@link XContentParser}. */ protected final XContentParser createParser(XContent xContent, String data) throws IOException { return xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data); }
Example #25
Source File: AnomalyDetectorRestTestCase.java From anomaly-detection with Apache License 2.0 | 4 votes |
protected final XContentParser createAdParser(XContent xContent, InputStream data) throws IOException { return xContent.createParser(TestHelpers.xContentRegistry(), LoggingDeprecationHandler.INSTANCE, data); }
Example #26
Source File: RepositoryDataTests.java From crate with Apache License 2.0 | 4 votes |
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 #27
Source File: XmlXContentBuilder.java From elasticsearch-xml with Apache License 2.0 | 4 votes |
public static XmlXContentBuilder builder(XContent xContent) throws IOException { return new XmlXContentBuilder(xContent, new BytesStreamOutput()); }