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); } }
break; case "include": try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, value)) { parser.nextToken(); include = IncludeExclude.parseInclude(parser); try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, value)) { parser.nextToken(); exclude = IncludeExclude.parseExclude(parser);
@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; } }
/** * 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); } }
/** * Parses the value as a geopoint. The following types of values are supported: * <p> * Object: has to contain either lat and lon or geohash fields * <p> * String: expected to be in "latitude, longitude" format or a geohash * <p> * Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true */ public static GeoPoint parseGeoPoint(Object value, final boolean ignoreZValue) throws ElasticsearchParseException { try { XContentBuilder content = JsonXContent.contentBuilder(); content.startObject(); content.field("null_value", value); content.endObject(); try (InputStream stream = BytesReference.bytes(content).streamInput(); XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { parser.nextToken(); // start object parser.nextToken(); // field name parser.nextToken(); // field value return parseGeoPoint(parser, new GeoPoint(), ignoreZValue); } } catch (IOException ex) { throw new ElasticsearchParseException("error parsing geopoint", ex); } }
public T loadLatestState(Logger logger, NamedXContentRegistry namedXContentRegistry, String stringMetaData) throws IOException { try (XContentParser parser = JsonXContent.jsonXContent.createParser(namedXContentRegistry, stringMetaData)) { return fromXContent(parser); } }
@Override public XContentParser createParser(NamedXContentRegistry xContentRegistry, BytesReference bytes) throws IOException { return createParser(xContentRegistry, bytes.streamInput()); }
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 void addComplexField(final XContentBuilder builder, final String fieldName, final XContentType contentType, final byte[] data) throws IOException { builder.field(fieldName, JsonXContent.jsonXContent.createParser(data) .mapAndClose()); } }
private static ShapeBuilder GetShapeBuilder(Object object) { try { String geoJson = (String) object; XContentParser parser = JsonXContent.jsonXContent.createParser(geoJson); parser.nextToken(); return ShapeBuilder.parse(parser); } catch (Exception e) { return null; } } }
public static Map<String, Object> toMap(final BytesReference bytes) { try { return SmileXContent.smileXContent.createParser(bytes).map(); } catch (IOException e) { try { return JsonXContent.jsonXContent.createParser(bytes).map(); } catch (IOException e1) { throw new RuntimeException("Couldn't parse bytes as either SMILE or JSON", e); } } }
public static Map<String, Object> toMap(final BytesReference bytes) { try { return SmileXContent.smileXContent.createParser(bytes).map(); } catch (IOException e) { try { return JsonXContent.jsonXContent.createParser(bytes).map(); } catch (IOException e1) { throw new RuntimeException("Couldn't parse bytes as either SMILE or JSON", e); } } }
@Override public XContentParser createParser(BytesReference bytes) throws IOException { if (bytes.hasArray()) { return createParser(bytes.array(), bytes.arrayOffset(), bytes.length()); } return createParser(bytes.streamInput()); }
private Shape convertObjectToShapeIfPossible(Object o) throws IOException { if(o instanceof Shape) return (Shape) o; String geoShapeStringValue = null; if(o instanceof HashMap) { HashMap map = (HashMap) o; Preconditions.checkArgument(map.containsKey("coordinates") && map.containsKey("type")); geoShapeStringValue = (new JSONObject(map)).toString(); } else if (o instanceof String){ geoShapeStringValue = (String) o; } Preconditions.checkNotNull(geoShapeStringValue); XContentParser parser = JsonXContent.jsonXContent.createParser(geoShapeStringValue); parser.nextToken(); return ShapeBuilder.parse(parser).build(); }
/** * 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); } }
/** * 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); } }
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()); } }
@Override public R apply(final RestResponse restResponse) { try { //TODO check REST status and "ok" field and handle failure final Map<String, Object> map; final String contentTypes = Joiner.on(",").join(restResponse.contentTypeLowerCase()); if (contentTypes.contains("application/smile")) { map = SmileXContent.smileXContent.createParser(restResponse.response()).mapAndClose(); } else if (contentTypes.contains("application/json")) { final InputStream is = InputStreams.stripNullChars(restResponse.response()); map = JsonXContent.jsonXContent.createParser(is).mapAndClose(); } else { throw new RuntimeException(String.format("Could not parse response. Content-Type:[%s] Body:[%s]", restResponse.contentTypeLowerCase(), InputStreams.toString(InputStreams.stripNullChars(restResponse.response())))); } // If there was an error throw the proper exception if (map.containsKey("error")) { throw ElasticSearchExceptionHelper.getProperException(map); } return unmarshaller.fromXContent(map); } catch (IOException e) { // FIXME: which exception to use? It should match ES clients if possible. see https://github.com/bazaarvoice/es-client-java/issues/3 throw new RuntimeException(e); } } }
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); } }