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 XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(Fields.MERGES); builder.field(Fields.CURRENT, current); builder.field(Fields.CURRENT_DOCS, currentNumDocs); builder.humanReadableField(Fields.CURRENT_SIZE_IN_BYTES, Fields.CURRENT_SIZE, getCurrentSize()); builder.field(Fields.TOTAL, total); builder.humanReadableField(Fields.TOTAL_TIME_IN_MILLIS, Fields.TOTAL_TIME, getTotalTime()); builder.field(Fields.TOTAL_DOCS, totalNumDocs); builder.humanReadableField(Fields.TOTAL_SIZE_IN_BYTES, Fields.TOTAL_SIZE, getTotalSize()); builder.humanReadableField(Fields.TOTAL_STOPPED_TIME_IN_MILLIS, Fields.TOTAL_STOPPED_TIME, getTotalStoppedTime()); builder.humanReadableField(Fields.TOTAL_THROTTLED_TIME_IN_MILLIS, Fields.TOTAL_THROTTLED_TIME, getTotalThrottledTime()); if (builder.humanReadable() && totalBytesPerSecAutoThrottle != -1) { builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC).value(new ByteSizeValue(totalBytesPerSecAutoThrottle).toString()); } builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC_IN_BYTES, totalBytesPerSecAutoThrottle); builder.endObject(); return builder; }
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; }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(Fields.MERGES); builder.field(Fields.CURRENT, current); builder.field(Fields.CURRENT_DOCS, currentNumDocs); builder.humanReadableField(Fields.CURRENT_SIZE_IN_BYTES, Fields.CURRENT_SIZE, getCurrentSize()); builder.field(Fields.TOTAL, total); builder.humanReadableField(Fields.TOTAL_TIME_IN_MILLIS, Fields.TOTAL_TIME, getTotalTime()); builder.field(Fields.TOTAL_DOCS, totalNumDocs); builder.humanReadableField(Fields.TOTAL_SIZE_IN_BYTES, Fields.TOTAL_SIZE, getTotalSize()); builder.humanReadableField(Fields.TOTAL_STOPPED_TIME_IN_MILLIS, Fields.TOTAL_STOPPED_TIME, getTotalStoppedTime()); builder.humanReadableField(Fields.TOTAL_THROTTLED_TIME_IN_MILLIS, Fields.TOTAL_THROTTLED_TIME, getTotalThrottledTime()); if (builder.humanReadable() && totalBytesPerSecAutoThrottle != -1) { builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC).value(new ByteSizeValue(totalBytesPerSecAutoThrottle).toString()); } builder.field(Fields.TOTAL_THROTTLE_BYTES_PER_SEC_IN_BYTES, totalBytesPerSecAutoThrottle); builder.endObject(); return builder; }
builder.humanReadable(human); return builder;
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("adaptive_selection"); Set<String> allNodeIds = Sets.union(clientOutgoingConnections.keySet(), nodeComputedStats.keySet()); for (String nodeId : allNodeIds) { builder.startObject(nodeId); ResponseCollectorService.ComputedNodeStats stats = nodeComputedStats.get(nodeId); if (stats != null) { long outgoingSearches = clientOutgoingConnections.getOrDefault(nodeId, 0L); builder.field("outgoing_searches", outgoingSearches); builder.field("avg_queue_size", stats.queueSize); if (builder.humanReadable()) { builder.field("avg_service_time", new TimeValue((long) stats.serviceTime, TimeUnit.NANOSECONDS).toString()); } builder.field("avg_service_time_ns", (long) stats.serviceTime); if (builder.humanReadable()) { builder.field("avg_response_time", new TimeValue((long) stats.responseTime, TimeUnit.NANOSECONDS).toString()); } builder.field("avg_response_time_ns", (long) stats.responseTime); builder.field("rank", String.format(Locale.ROOT, "%.1f", stats.rank(outgoingSearches))); } builder.endObject(); } builder.endObject(); return builder; }
/** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.humanReadable(humanReadable); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, params); if (toXContent.isFragment()) { builder.endObject(); } return BytesReference.bytes(builder); } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field("node", taskId.getNodeId()); builder.field("id", taskId.getId()); builder.field("type", type); builder.field("action", action); if (status != null) { builder.field("status", status, params); } if (description != null) { builder.field("description", description); } builder.timeField("start_time_in_millis", "start_time", startTime); if (builder.humanReadable()) { builder.field("running_time", new TimeValue(runningTimeNanos, TimeUnit.NANOSECONDS).toString()); } builder.field("running_time_in_nanos", runningTimeNanos); builder.field("cancellable", cancellable); if (parentTaskId.isSet()) { builder.field("parent_task_id", parentTaskId.toString()); } builder.startObject("headers"); for(Map.Entry<String, String> attribute : headers.entrySet()) { builder.field(attribute.getKey(), attribute.getValue()); } builder.endObject(); return builder; }
@Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder = builder.startObject(); builder.field(NAME.getPreferredName(), getName()); builder.field(REASON.getPreferredName(), getReason()); if (builder.humanReadable()) { builder.field(TIME.getPreferredName(), new TimeValue(getTime(), TimeUnit.NANOSECONDS).toString()); } builder.field(TIME_NANOS.getPreferredName(), getTime()); if (!children.isEmpty()) { builder = builder.startArray(CHILDREN.getPreferredName()); for (CollectorResult child : children) { builder = child.toXContent(builder, params); } builder = builder.endArray(); } builder = builder.endObject(); return builder; }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(TYPE.getPreferredName(), type); builder.field(DESCRIPTION.getPreferredName(), description); if (builder.humanReadable()) { builder.field(NODE_TIME.getPreferredName(), new TimeValue(getTime(), TimeUnit.NANOSECONDS).toString()); } builder.field(NODE_TIME_RAW.getPreferredName(), getTime()); builder.field(BREAKDOWN.getPreferredName(), timings); if (!children.isEmpty()) { builder = builder.startArray(CHILDREN.getPreferredName()); for (ProfileResult child : children) { builder = child.toXContent(builder, params); } builder = builder.endArray(); } builder = builder.endObject(); return builder; }
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 XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); if (pretty) { builder.prettyPrint(); } if (human) { builder.humanReadable(true); } return builder; }
public BytesReference source(IndexService indexService, DocumentMapper docMapper, Map sourceAsMap, Uid uid) throws JsonParseException, JsonMappingException, IOException { if (docMapper.sourceMapper().enabled()) { // retreive from _source columns stored as blob in cassandra if available. ByteBuffer bb = (ByteBuffer) sourceAsMap.get(SourceFieldMapper.NAME); if (bb != null) return new BytesArray(bb.array(), bb.position(), bb.limit() - bb.position()); } // rebuild _source from all cassandra columns. XContentBuilder builder = buildDocument(docMapper, sourceAsMap, true, isStaticDocument(indexService, uid)); builder.humanReadable(true); return builder.bytes(); }
private Map<String, String> getIndicesStatus(IndicesStatsResponse indicesStatsResponse) { Map<String, String> indexStatusMap = new HashMap<>(); for (Map.Entry<String, IndexStats> indexStat : indicesStatsResponse.getIndices().entrySet()) { try (XContentBuilder indexStatsBuilder = XContentFactory.jsonBuilder()) { indexStatsBuilder.humanReadable(true).prettyPrint().startObject(); indicesStatsResponse.getIndex(indexStat.getKey()) .getTotal() .toXContent(indexStatsBuilder, ToXContent.EMPTY_PARAMS); indexStatsBuilder.endObject(); indexStatusMap.put(indexStat.getKey(), indexStatsBuilder.string()); } catch (IOException e) { Exceptions.handle(e); } } return indexStatusMap; }
private String getClusterState(ClusterStateResponse clusterStateResponse) { String state = ""; try (XContentBuilder clusterStateBuilder = XContentFactory.jsonBuilder()) { clusterStateBuilder.humanReadable(true).prettyPrint().startObject(); clusterStateResponse.getState().toXContent(clusterStateBuilder, ToXContent.EMPTY_PARAMS); clusterStateBuilder.endObject(); state = clusterStateBuilder.string(); } catch (IOException e) { Exceptions.handle(e); } return state; }
private Map<String, String> getNodesStats(NodesStatsResponse nodesStatsResponse) { Map<String, String> nodeStatsMap = new HashMap<>(); for (NodeStats stat : nodesStatsResponse.getNodes()) { try (XContentBuilder nodesStatsBuilder = XContentFactory.jsonBuilder()) { nodesStatsBuilder.humanReadable(true).prettyPrint().startObject(); stat.toXContent(nodesStatsBuilder, ToXContent.EMPTY_PARAMS); nodesStatsBuilder.endObject(); nodeStatsMap.put(stat.getNode().getName(), nodesStatsBuilder.string()); } catch (IOException e) { Exceptions.handle(e); } } return nodeStatsMap; }
/** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.humanReadable(humanReadable); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, params); if (toXContent.isFragment()) { builder.endObject(); } return builder.bytes(); } } }
/** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.humanReadable(humanReadable); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, params); if (toXContent.isFragment()) { builder.endObject(); } return BytesReference.bytes(builder); } }
/** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.humanReadable(humanReadable); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, params); if (toXContent.isFragment()) { builder.endObject(); } return BytesReference.bytes(builder); } }
private Map<String, String> getIndicesMetadata(ClusterStateResponse clusterStateResponse) { Map<String, String> metaDataMap = new HashMap<>(); for (ObjectObjectCursor<String, IndexMetaData> metadata : clusterStateResponse.getState() .getMetaData() .getIndices()) { try (XContentBuilder metaDataBuilder = XContentFactory.jsonBuilder()) { metaDataBuilder.humanReadable(true).prettyPrint().startObject(); clusterStateResponse.getState() .getMetaData() .index(metadata.key) .toXContent(metaDataBuilder, ToXContent.EMPTY_PARAMS); metaDataBuilder.endObject(); metaDataMap.put(metadata.key, metaDataBuilder.string()); } catch (IOException e) { Exceptions.handle(e); } } return metaDataMap; }