Refine search
/** * Parse the script configured in the given settings. */ public static Script parse(Settings settings) { try (XContentBuilder builder = JsonXContent.contentBuilder()){ builder.startObject(); settings.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); try (InputStream stream = BytesReference.bytes(builder).streamInput(); XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { return parse(parser); } } catch (IOException e) { // it should not happen since we are not actually reading from a stream but an in-memory byte[] throw new IllegalStateException(e); } }
@Test public void testComplianceLicenseMap() throws Exception { SearchGuardLicense license = new SearchGuardLicense(XContentHelper .convertToMap(new BytesArray(FileHelper.loadFile("license1.json")), false, JsonXContent.jsonXContent.type()).v2(), cs); Assert.assertFalse(license.hasFeature(Feature.COMPLIANCE)); Assert.assertArrayEquals(license.getFeatures(), new Feature[0]); license = new SearchGuardLicense(XContentHelper .convertToMap(new BytesArray(FileHelper.loadFile("license3.json")), false, JsonXContent.jsonXContent.type()).v2(), cs); Assert.assertFalse(license.hasFeature(Feature.COMPLIANCE)); Assert.assertArrayEquals(license.getFeatures(), new Feature[0]); license = new SearchGuardLicense(XContentHelper .convertToMap(new BytesArray(FileHelper.loadFile("license2.json")), false, JsonXContent.jsonXContent.type()).v2(), cs); Assert.assertTrue(license.hasFeature(Feature.COMPLIANCE)); Assert.assertArrayEquals(license.getFeatures(), Feature.values()); }
private void sendResponse(final RestRequest request, final RestChannel channel, final String file) { try { final XContentBuilder builder = JsonXContent.contentBuilder(); final String pretty = request.param("pretty"); if (pretty != null && !"false".equalsIgnoreCase(pretty)) { builder.prettyPrint().lfAtEnd(); } builder.startObject(); builder.field("acknowledged", true); builder.field("file", file); builder.endObject(); channel.sendResponse(new BytesRestResponse(OK, builder)); } catch (final IOException e) { throw new ElasticsearchException("Failed to create a resposne.", e); } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field("index_patterns", indexPatterns); builder.field("order", order); if (version != null) { builder.field("version", version); } builder.startObject("settings"); settings.toXContent(builder, params); builder.endObject(); builder.startObject("mappings"); for (Map.Entry<String, String> entry : mappings.entrySet()) { builder.field(entry.getKey()); XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue()); builder.copyCurrentStructure(parser); } builder.endObject(); builder.startObject("aliases"); for (Alias alias : aliases) { alias.toXContent(builder, params); } builder.endObject(); return builder; } }
public JsonOutput availableIndices(String... indices) throws Exception { ClusterStateResponse response = this.client.admin().cluster().state(new ClusterStateRequest() .filterBlocks(true).filterNodes(true).filteredIndices(indices) .filterRoutingTable(true)).actionGet(); XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); for (IndexMetaData indexMetaData : response.getState().metaData()) { builder.startObject(indexMetaData.index()); if (indexMetaData.aliases() != null && indexMetaData.aliases().size() > 0) { builder.startArray("aliases"); for (String alias : indexMetaData.aliases().keySet()) { builder.value(alias); } builder.endArray(); } if (indexMetaData.mappings() != null && indexMetaData.mappings().size() > 0) { builder.startArray("types"); for (String alias : indexMetaData.mappings().keySet()) { builder.value(alias); } builder.endArray(); } builder.endObject(); } builder.endObject(); return stringToJson.stringToJson(builder.string()); }
static Request clearScroll(String scroll, Version remoteVersion) { Request request = new Request("DELETE", "/_search/scroll"); if (remoteVersion.before(Version.fromId(2000099))) { // Versions before 2.0.0 extract the plain scroll_id from the body request.setEntity(new NStringEntity(scroll, ContentType.TEXT_PLAIN)); return request; } try (XContentBuilder entity = JsonXContent.contentBuilder()) { entity.startObject() .array("scroll_id", scroll) .endObject(); request.setJsonEntity(Strings.toString(entity)); } catch (IOException e) { throw new ElasticsearchException("failed to build clear scroll entity", e); } return request; } }
public void addComplexField(final XContentBuilder builder, final String fieldName, final XContentType contentType, final byte[] data) throws IOException { builder.field(fieldName, JsonXContent.jsonXContent.createParser(data) .mapAndClose()); } }
@Override public String toString() { try { return toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).string(); } catch (IOException e) { return super.toString(); } }
@Override public String toString() { try { XContentBuilder xcontent = JsonXContent.contentBuilder(); return toXContent(xcontent, EMPTY_PARAMS).prettyPrint().string(); } catch (IOException e) { return super.toString(); } }
private static GeoPoint parseGeoPointString(String value) { try { XContentBuilder content = JsonXContent.contentBuilder(); content.value(value); content.flush(); content.close(); try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, ((ByteArrayOutputStream) content.getOutputStream()).toByteArray())) { parser.nextToken(); return GeoUtils.parseGeoPoint(parser); } } catch (IOException e) { throw new IllegalArgumentException("Invalid value for geopoint: " + e.getMessage()); } }
public void declareRawObject(BiConsumer<Value, BytesReference> consumer, ParseField field) { CheckedFunction<XContentParser, BytesReference, IOException> bytesParser = p -> { try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.prettyPrint(); builder.copyCurrentStructure(p); return builder.bytes(); } }; declareField(consumer, bytesParser, field, ValueType.OBJECT); }
private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); if (pretty) { builder.prettyPrint(); } if (human) { builder.humanReadable(true); } return builder; }
private static BytesReference queryForRemote(Map<String, Object> source) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); Object query = source.remove("query"); if (query == null) { return BytesReference.bytes(matchAllQuery().toXContent(builder, ToXContent.EMPTY_PARAMS)); } if (!(query instanceof Map)) { throw new IllegalArgumentException("Expected [query] to be an object but was [" + query + "]"); } @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) query; return BytesReference.bytes(builder.map(map)); } }
/** * Helper to declare an object that will be parsed into a {@link BytesReference} */ public void declareRawObject(final AbstractObjectParser<Value, Context> parser, final BiConsumer<Value, BytesReference> consumer, final ParseField field) { final CheckedFunction<XContentParser, BytesReference, IOException> bytesParser = p -> { try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.copyCurrentStructure(p); return BytesReference.bytes(builder); } }; parser.declareField(consumer, bytesParser, field, ValueType.OBJECT); }
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); }
private static String convertToYaml(String type, BytesReference bytes, boolean prettyPrint) throws IOException { try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, bytes.streamInput())) { parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { return null; } parser.nextToken(); XContentBuilder builder = XContentFactory.yamlBuilder(); if (prettyPrint) { builder.prettyPrint(); } builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML); return Strings.toString(builder); } }
@Override public String convert(QueryBuilder anItem) { try { BytesReference bytes = anItem.toXContent(JsonXContent.contentBuilder(), new ToXContent.MapParams(null)) .bytes(); return bytes.utf8ToString(); } catch (IOException e) { log.error("Unable to transform querybuilder to a json string due to an exception", e); throw new RuntimeException("Unable to transform querybuilder to a json string due to an exception", e); } } }
@Override public String toString() { try { return Strings.toString(toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS)); } catch (IOException e) { return super.toString(); } }
private static BytesReference parseBytes(XContentParser parser) throws IOException { XContentBuilder contentBuilder = JsonXContent.contentBuilder(); contentBuilder.generator().copyCurrentStructure(parser); return BytesReference.bytes(contentBuilder); }
private static byte[] readRaw(final Object source) { ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder(); try { XContentGenerator generator = JsonXContent.jsonXContent.createGenerator(byteArrayBuilder); if (source == null) { generator.writeNull();