@Override public void accept(RestChannel channel) throws Exception { XContentBuilder builder = channel.newBuilder(); RestStatus restStatus = RestStatus.OK; BytesRestResponse response = null; try { String status = "UP"; String message = null; builder.startObject(); if ("strict".equalsIgnoreCase(mode) && registry.isInitialized() == false) { status = "DOWN"; message = "Not initialized"; restStatus = RestStatus.SERVICE_UNAVAILABLE; } builder.field("message", message); builder.field("mode", mode); builder.field("status", status); builder.endObject(); response = new BytesRestResponse(restStatus, builder); } finally { builder.close(); } channel.sendResponse(response); }
} finally { if(builder != null) { builder.close();
} finally { if(builder != null) { builder.close();
} finally { if(builder != null) { builder.close();
/** * Convert an {@link XContentBuilder} into a BytesReference. This method closes the builder, * so no further fields may be added. */ public static BytesReference bytes(XContentBuilder xContentBuilder) { xContentBuilder.close(); OutputStream stream = xContentBuilder.getOutputStream(); if (stream instanceof ByteArrayOutputStream) { return new BytesArray(((ByteArrayOutputStream) stream).toByteArray()); } else { return ((BytesStream) stream).bytes(); } }
public AliasActions filter(QueryBuilder filter) { if (filter == null) { this.filter = null; return this; } try { XContentBuilder builder = XContentFactory.jsonBuilder(); filter.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); this.filter = Strings.toString(builder); return this; } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }
/** * Associates a filter to the alias */ public Alias filter(QueryBuilder filterBuilder) { if (filterBuilder == null) { this.filter = null; return this; } try { XContentBuilder builder = XContentFactory.jsonBuilder(); filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); this.filter = Strings.toString(builder); return this; } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }
/** * Writes the incompatible snapshot ids list to the `incompatible-snapshots` blob in the repository. * * Package private for testing. */ void writeIncompatibleSnapshots(RepositoryData repositoryData) throws IOException { assert isReadOnly() == false; // can not write to a read only repository final BytesReference bytes; try (BytesStreamOutput bStream = new BytesStreamOutput()) { try (StreamOutput stream = new OutputStreamStreamOutput(bStream)) { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream); repositoryData.incompatibleSnapshotsToXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); } bytes = bStream.bytes(); } // write the incompatible snapshots blob writeAtomic(INCOMPATIBLE_SNAPSHOTS_BLOB, bytes, false); }
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream); repositoryData.snapshotsToXContent(builder, ToXContent.EMPTY_PARAMS); builder.close();
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { BytesReference originalSource = context.sourceToParse().source(); BytesReference source = originalSource; if (enabled && fieldType().stored() && source != null) { // Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data if (filter != null) { // we don't update the context source if we filter, we want to keep it as is... Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(source, true, context.sourceToParse().getXContentType()); Map<String, Object> filteredSource = filter.apply(mapTuple.v2()); BytesStreamOutput bStream = new BytesStreamOutput(); XContentType contentType = mapTuple.v1(); XContentBuilder builder = XContentFactory.contentBuilder(contentType, bStream).map(filteredSource); builder.close(); source = bStream.bytes(); } BytesRef ref = source.toBytesRef(); fields.add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length)); } else { source = null; } if (originalSource != null && source != originalSource && context.indexSettings().isSoftDeleteEnabled()) { // if we omitted source or modified it we add the _recovery_source to ensure we have it for ops based recovery BytesRef ref = originalSource.toBytesRef(); fields.add(new StoredField(RECOVERY_SOURCE_NAME, ref.bytes, ref.offset, ref.length)); fields.add(new NumericDocValuesField(RECOVERY_SOURCE_NAME, 1)); } }
@Override public BytesReference bytes() { close(); return ((BytesStream) bos).bytes(); }
public BytesReference bytes() { close(); return ((BytesStream) bos).bytes(); }
/** * Convert an {@link XContentBuilder} into a BytesReference. This method closes the builder, * so no further fields may be added. */ public static BytesReference bytes(XContentBuilder xContentBuilder) { xContentBuilder.close(); OutputStream stream = xContentBuilder.getOutputStream(); if (stream instanceof ByteArrayOutputStream) { return new BytesArray(((ByteArrayOutputStream) stream).toByteArray()); } else { return ((BytesStream) stream).bytes(); } }
/** * Convert an {@link XContentBuilder} into a BytesReference. This method closes the builder, * so no further fields may be added. */ public static BytesReference bytes(XContentBuilder xContentBuilder) { xContentBuilder.close(); OutputStream stream = xContentBuilder.getOutputStream(); if (stream instanceof ByteArrayOutputStream) { return new BytesArray(((ByteArrayOutputStream) stream).toByteArray()); } else { return ((BytesStream) stream).bytes(); } }
/** * Returns a string representation of the builder (only applicable for text based xcontent). */ public String string() throws IOException { close(); BytesArray bytesArray = bytes().toBytesArray(); return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8); }
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 AliasAction filter(QueryBuilder queryBuilder) { if (queryBuilder == null) { this.filter = null; return this; } try { XContentBuilder builder = XContentFactory.jsonBuilder(); queryBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); this.filter = builder.string(); return this; } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }
public AliasActions filter(QueryBuilder filter) { if (filter == null) { this.filter = null; return this; } try { XContentBuilder builder = XContentFactory.jsonBuilder(); filter.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); this.filter = builder.string(); return this; } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }
public AliasActions filter(QueryBuilder filter) { if (filter == null) { this.filter = null; return this; } try { XContentBuilder builder = XContentFactory.jsonBuilder(); filter.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); this.filter = Strings.toString(builder); return this; } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }
public AliasActions filter(QueryBuilder filter) { if (filter == null) { this.filter = null; return this; } try { XContentBuilder builder = XContentFactory.jsonBuilder(); filter.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.close(); this.filter = Strings.toString(builder); return this; } catch (IOException e) { throw new ElasticsearchGenerationException("Failed to build json for alias request", e); } }