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; }
@Override public String toString() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XContentBuilder builder = new XContentBuilder(JsonXContent.jsonXContent, baos); toXContent(builder, ToXContent.EMPTY_PARAMS, false); return Strings.toString(builder); } catch (IOException e) { throw new IllegalStateException(e); //should not be possible here } }
new XContentBuilder(XContentFactory.xContent(responseContentType), unclosableOutputStream, includes, excludes); if (pretty) { builder.prettyPrint().lfAtEnd();
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { boolean includeDefaults = params.paramAsBoolean("include_defaults", false); if (!includeDefaults) { // simulate the generation to make sure we don't add unnecessary content if all is default // if all are defaults, no need to write it at all - generating is twice is ok though BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0); XContentBuilder b = new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput); b.startObject().flush(); long pos = bytesStreamOutput.position(); innerToXContent(b, false); b.flush(); if (pos == bytesStreamOutput.position()) { return builder; } } builder.startObject(CONTENT_TYPE); innerToXContent(builder, includeDefaults); builder.endObject(); return builder; }
/** * Constructs a new json builder that will output the result into the provided output stream. */ public static XContentBuilder jsonBuilder(OutputStream os) throws IOException { return new XContentBuilder(JsonXContent.jsonXContent, os); }
/** * Constructs a new json builder that will output the result into the provided output stream. */ public static XContentBuilder smileBuilder(OutputStream os) throws IOException { return new XContentBuilder(SmileXContent.smileXContent, os); }
/** * Constructs a new json builder that will output the result into the provided output stream. */ public static XContentBuilder smileBuilder(OutputStream os) throws IOException { return new XContentBuilder(SmileXContent.smileXContent, os); }
/** * Constructs a new yaml builder that will output the result into the provided output stream. */ public static XContentBuilder yamlBuilder(OutputStream os) throws IOException { return new XContentBuilder(YamlXContent.yamlXContent, os); }
/** * Constructs a new json builder that will output the result into the provided output stream. */ public static XContentBuilder jsonBuilder(OutputStream os) throws IOException { return new XContentBuilder(JsonXContent.jsonXContent, os); }
/** * Constructs a new json builder that will output the result into the provided output stream. */ public static XContentBuilder smileBuilder(OutputStream os) throws IOException { return new XContentBuilder(SmileXContent.smileXContent, os); }
/** * Constructs a new yaml builder that will output the result into the provided output stream. */ public static XContentBuilder yamlBuilder(OutputStream os) throws IOException { return new XContentBuilder(YamlXContent.yamlXContent, os); }
/** * Constructs a new cbor builder that will output the result into the provided output stream. */ public static XContentBuilder cborBuilder(OutputStream os) throws IOException { return new XContentBuilder(CborXContent.cborXContent, os); }
/** * Constructs a new cbor builder that will output the result into the provided output stream. */ public static XContentBuilder cborBuilder(OutputStream os) throws IOException { return new XContentBuilder(CborXContent.cborXContent, os); }
/** * Constructs a new cbor builder that will output the result into the provided output stream. */ public static XContentBuilder cborBuilder(OutputStream os) throws IOException { return new XContentBuilder(CborXContent.cborXContent, os); }
/** * Constructs a new yaml builder that will output the result into the provided output stream. */ public static XContentBuilder yamlBuilder(OutputStream os) throws IOException { return new XContentBuilder(YamlXContent.yamlXContent, os); }
final int initialCapacity = Math.min(1024, sourceAsBytes.length()); BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity); try (XContentBuilder builder = new XContentBuilder(sourceContentType.xContent(), streamOutput)) { builder.value(value); sourceFilteredAsBytes = BytesReference.bytes(builder);
final int initialCapacity = nestedHit ? 1024 : Math.min(1024, source.internalSourceRef().length()); BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity); XContentBuilder builder = new XContentBuilder(source.sourceContentType().xContent(), streamOutput); if (value != null) { builder.value(value);
@Override public String toString() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XContentBuilder builder = new XContentBuilder(JsonXContent.jsonXContent, baos); toXContent(builder, ToXContent.EMPTY_PARAMS, false); return Strings.toString(builder); } catch (IOException e) { throw new IllegalStateException(e); //should not be possible here } }
@Override public String toString() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); XContentBuilder builder = new XContentBuilder(JsonXContent.jsonXContent, baos); toXContent(builder, ToXContent.EMPTY_PARAMS, false); return Strings.toString(builder); } catch (IOException e) { throw new IllegalStateException(e); //should not be possible here } }
/** * Create a new {@link XContentBuilder} using the given {@link XContent} content. * <p> * The builder uses an internal {@link BytesStreamOutput} output stream to build the content. * </p> * * @param xContent the {@link XContent} * @return a new {@link XContentBuilder} * @throws IOException if an {@link IOException} occurs while building the content */ public static XContentBuilder builder(XContent xContent) throws IOException { return new XContentBuilder(xContent, new BytesStreamOutput()); }