private static XContentParser createParser(String mediaType, String content) throws IOException { return XContentType.fromMediaTypeOrFormat(mediaType) // .xContent() // .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("whoami"); builder.field("dn", dn); builder.field("is_admin", isAdmin); builder.field("is_authenticated", isAuthenticated); builder.field("is_node_certificate_request", isNodeCertificateRequest); builder.endObject(); return builder; }
/** * Directly writes the source to the output builder */ public static void directSource(BytesReference source, XContentBuilder rawBuilder, ToXContent.Params params) throws IOException { XContentHelper.writeDirect(source, rawBuilder, params); }
private Response search(final String table, final String key) throws IOException { try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("query"); builder.startObject("term"); builder.field(KEY, key); builder.endObject(); builder.endObject(); builder.endObject(); return search(table, builder); } }
private void writeMappings(ImmutableOpenMap<String, MappingMetaData> mappings, XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(Fields.MAPPINGS); if (mappings != null) { for (ObjectObjectCursor<String, MappingMetaData> typeEntry : mappings) { builder.field(typeEntry.key); builder.map(typeEntry.value.sourceAsMap()); } } builder.endObject(); }
private static String convertToJsType(Object value) throws PermanentBackendException { try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); builder.field("value", convertToEsType(value)); String s = builder.string(); int prefixLength = "{\"value\":".length(); int suffixLength = "}".length(); String result = s.substring(prefixLength, s.length() - suffixLength); result = result.replace("$", "\\$"); return result; } catch (IOException e) { throw new PermanentBackendException("Could not write json"); } }
private static XContentBuilder source(Timestamp<?> time) throws IOException { XContentBuilder builder = jsonBuilder().startObject().startObject(TYPE); time.saveFields(builder); return builder.endObject().endObject(); }
private static BytesReference readXContent(final Reader reader, final XContentType xContentType) throws IOException { BytesReference retVal; XContentParser parser = null; try { parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, reader); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); retVal = BytesReference.bytes(builder); } finally { if (parser != null) { parser.close(); } } //validate Settings.builder().loadFromStream("dummy.json", new ByteArrayInputStream(BytesReference.toBytes(retVal)), true).build(); return retVal; }
private void writeAliases(List<AliasMetaData> aliases, XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(Fields.ALIASES); if (aliases != null) { for (AliasMetaData alias : aliases) { AliasMetaData.Builder.toXContent(alias, builder, params); } } builder.endObject(); }
@Override public XContentBuilder getContentBuilder(Event event) throws IOException { XContentBuilder builder = jsonBuilder().startObject(); appendBody(builder, event); appendHeaders(builder, event); return builder; }
public static void addSimpleField(XContentBuilder builder, String fieldName, byte[] data) throws IOException { builder.field(fieldName, new String(data, charset)); }
private ShapeBuilder getShapeBuilderFromJson(String json) throws IOException { XContentParser parser = null; parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, json); parser.nextToken(); return ShapeParser.parse(parser); }
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 void restDocumentSource(BytesReference source, XContentBuilder builder, ToXContent.Params params) throws IOException { XContentHelper.writeRawField("_source", source, builder, params); } }
public static void setRiverStatus(Client client, String riverName, Status status) { logger.info("setRiverStatus called with {} - {}", riverName, status); XContentBuilder xb; try { xb = jsonBuilder().startObject().startObject(MongoDBRiver.TYPE).field(MongoDBRiver.STATUS_FIELD, status).endObject() .endObject(); client.prepareIndex("_river", riverName, MongoDBRiver.STATUS_ID).setSource(xb).get(); } catch (IOException ioEx) { logger.error("setRiverStatus failed for river {}", ioEx, riverName); } }
public static BytesReference readXContent(final Reader reader, final XContentType xContentType) throws IOException { BytesReference retVal; XContentParser parser = null; try { parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, reader); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); retVal = BytesReference.bytes(builder); } finally { if (parser != null) { parser.close(); } } //validate Settings.builder().loadFromStream("dummy.json", new ByteArrayInputStream(BytesReference.toBytes(retVal)), true).build(); return retVal; }
private void writeSettings(Settings settings, XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(Fields.SETTINGS); settings.toXContent(builder, params); builder.endObject(); }
@Override public XContentBuilder getContentBuilder(Event event) throws IOException { XContentBuilder builder = jsonBuilder().startObject(); appendBody(builder, event); appendHeaders(builder, event); return builder; }
@Override public void saveFields(XContentBuilder builder) throws IOException { builder.field(MongoDBRiver.LAST_TIMESTAMP_FIELD, JSON.serialize(ts)); } }
public static BytesReference readYamlContentFromString(final String yaml) { XContentParser parser = null; try { parser = XContentFactory.xContent(XContentType.YAML).createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, new StringReader(yaml)); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } catch (Exception e) { throw new RuntimeException(e); } finally { if (parser != null) { try { parser.close(); } catch (IOException e) { //ignore } } } } }