public static void appendField(XContentBuilder builder, String field, byte[] data) throws IOException { XContentType contentType = XContentFactory.xContentType(data); if (contentType == null) { addSimpleField(builder, field, data); } else { addComplexField(builder, field, contentType, data); } }
public static XContentBuilder restContentBuilder(RestRequest request, @Nullable BytesReference autoDetectSource) throws IOException { XContentType contentType = XContentType.fromRestContentType(request.param("format", request.header("Content-Type"))); if (contentType == null) { // try and guess it from the auto detect source if (autoDetectSource != null) { contentType = XContentFactory.xContentType(autoDetectSource); } } if (contentType == null) { // default to JSON contentType = XContentType.JSON; } XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), new BytesStreamOutput()); if (request.paramAsBoolean("pretty", false)) { builder.prettyPrint().lfAtEnd(); } builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable())); String casing = request.param("case"); if (casing != null && "camelCase".equals(casing)) { builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE); } else { // we expect all REST interfaces to write results in underscore casing, so // no need for double casing builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE); } return builder; }
/** * Guesses the content type based on the provided bytes. * * @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type. * The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed. * This method is deprecated to prevent usages of it from spreading further without specific reasons. */ @Deprecated public static XContentType xContentType(BytesReference bytes) { BytesRef br = bytes.toBytesRef(); return XContentFactory.xContentType(br.bytes, br.offset, br.length); } }
@Deprecated public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException { return convertToJson(bytes, reformatJson, prettyPrint, XContentFactory.xContentType(bytes.toBytesRef().bytes)); }
/** * Creates a parser based on the bytes provided * @deprecated use {@link #createParser(NamedXContentRegistry, DeprecationHandler, BytesReference, XContentType)} * to avoid content type auto-detection */ @Deprecated public static XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, BytesReference bytes) throws IOException { Compressor compressor = CompressorFactory.compressor(bytes); if (compressor != null) { InputStream compressedInput = compressor.streamInput(bytes.streamInput()); if (compressedInput.markSupported() == false) { compressedInput = new BufferedInputStream(compressedInput); } final XContentType contentType = XContentFactory.xContentType(compressedInput); return XContentFactory.xContent(contentType).createParser(xContentRegistry, deprecationHandler, compressedInput); } else { return XContentFactory.xContent(xContentType(bytes)).createParser(xContentRegistry, deprecationHandler, bytes.streamInput()); } }
/** * Converts the given bytes into a map that is optionally ordered. The provided {@link XContentType} must be non-null. */ public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReference bytes, boolean ordered, XContentType xContentType) throws ElasticsearchParseException { try { final XContentType contentType; InputStream input; Compressor compressor = CompressorFactory.compressor(bytes); if (compressor != null) { InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput()); if (compressedStreamInput.markSupported() == false) { compressedStreamInput = new BufferedInputStream(compressedStreamInput); } input = compressedStreamInput; } else { input = bytes.streamInput(); } contentType = xContentType != null ? xContentType : XContentFactory.xContentType(input); try (InputStream stream = input) { return new Tuple<>(Objects.requireNonNull(contentType), convertToMap(XContentFactory.xContent(contentType), stream, ordered)); } } catch (IOException e) { throw new ElasticsearchParseException("Failed to parse content to map", e); } }
if (in.getVersion().before(Version.V_6_0_0_alpha1)) { // TODO change to 5.3.0 after backport source = XContentHelper.convertToJson(new BytesArray(source), false, false, XContentFactory.xContentType(source));
@Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); indices = in.readStringArray(); indicesOptions = IndicesOptions.readIndicesOptions(in); type = in.readOptionalString(); source = in.readString(); if (in.getVersion().before(Version.V_5_3_0)) { // we do not know the format from earlier versions so convert if necessary source = XContentHelper.convertToJson(new BytesArray(source), false, false, XContentFactory.xContentType(source)); } updateAllTypes = in.readBoolean(); concreteIndex = in.readOptionalWriteable(Index::new); }
/** * Guesses the content type based on the provided bytes. * * @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type. * The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed. * This method is deprecated to prevent usages of it from spreading further without specific reasons. */ @Deprecated public static XContentType xContentType(byte[] bytes) { return xContentType(bytes, 0, bytes.length); }
XContentHelper.convertToJson(new BytesArray(mappingSource), false, false, XContentFactory.xContentType(mappingSource));
/** * Sets the settings and mappings as a single source. * @deprecated use {@link #source(BytesReference, XContentType)} */ @Deprecated public CreateIndexRequest source(BytesReference source) { XContentType xContentType = XContentFactory.xContentType(source); source(source, xContentType); return this; }
/** * Guesses the content type based on the provided bytes. */ public static XContentType xContentType(byte[] data, int offset, int length) { return xContentType(new BytesArray(data, offset, length)); }
/** * Set the script source using bytes. * @deprecated this method is deprecated as it relies on content type detection. Use {@link #content(BytesReference, XContentType)} */ @Deprecated public PutStoredScriptRequest content(BytesReference content) { return content(content, XContentFactory.xContentType(content)); }
@Override public void writeRawField(String name, InputStream content) throws IOException { if (content.markSupported() == false) { // needed for the XContentFactory.xContentType call content = new BufferedInputStream(content); } XContentType contentType = XContentFactory.xContentType(content); if (contentType == null) { throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed"); } writeRawField(name, content, contentType); }
@Override public final void writeRawField(String name, BytesReference content) throws IOException { XContentType contentType = XContentFactory.xContentType(content); if (contentType == null) { throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed"); } writeRawField(name, content, contentType); }
public static XContent xContent(BytesReference bytes) { XContentType type = xContentType(bytes); if (type == null) { throw new ElasticsearchParseException("Failed to derive xcontent"); } return xContent(type); }
/** * Guesses the content (type) based on the provided char sequence. */ public static XContent xContent(CharSequence content) { XContentType type = xContentType(content); if (type == null) { throw new ElasticsearchParseException("Failed to derive xcontent"); } return xContent(type); }
private boolean autoDetectXContentType(RestRequest restRequest) { deprecationLogger.deprecated("Content type detection for rest requests is deprecated. Specify the content type using " + "the [Content-Type] header."); XContentType xContentType = XContentFactory.xContentType(restRequest.content()); if (xContentType == null) { return false; } else { restRequest.setXContentType(xContentType); } return true; }
@Deprecated public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String defaultRouting, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSourceContext, @Nullable String defaultPipeline, @Nullable Object payload, boolean allowExplicitIndex) throws IOException { XContentType xContentType = XContentFactory.xContentType(data); return add(data, defaultIndex, defaultType, defaultRouting, defaultFields, defaultFetchSourceContext, defaultPipeline, payload, allowExplicitIndex, xContentType); }
public static PipelineConfiguration readFrom(StreamInput in) throws IOException { if (in.getVersion().onOrAfter(Version.V_5_3_0)) { return new PipelineConfiguration(in.readString(), in.readBytesReference(), XContentType.readFrom(in)); } else { final String id = in.readString(); final BytesReference config = in.readBytesReference(); return new PipelineConfiguration(id, config, XContentFactory.xContentType(config)); } }