org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource Maven / Gradle / Ivy
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.core.watcher.support.xcontent;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectPath;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.XContentUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Encapsulates the xcontent source
*/
public class XContentSource implements ToXContent {
private final BytesReference bytes;
private final XContentType contentType;
private Object data;
/**
* Constructs a new XContentSource out of the given bytes reference.
*/
public XContentSource(BytesReference bytes, XContentType xContentType) throws ElasticsearchParseException {
if (xContentType == null) {
throw new IllegalArgumentException("xContentType must not be null");
}
this.bytes = bytes;
this.contentType = xContentType;
}
/**
* Constructs a new xcontent source from the bytes of the given xcontent builder
*/
public XContentSource(XContentBuilder builder) {
this(BytesReference.bytes(builder), builder.contentType());
}
/**
* @return The content type of the source
*/
public XContentType getContentType() {
return contentType;
}
/**
* @return The bytes reference of the source
*/
public BytesReference getBytes() {
return bytes;
}
/**
* @return true if the top level value of the source is a map
*/
public boolean isMap() {
return data() instanceof Map;
}
/**
* @return The source as a map
*/
public Map getAsMap() {
return (Map) data();
}
/**
* @return true if the top level value of the source is a list
*/
public boolean isList() {
return data() instanceof List;
}
/**
* @return The source as a list
*/
public List