|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.common.xcontent; |
| 10 | + |
| 11 | +import org.elasticsearch.common.bytes.BytesReference; |
| 12 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 13 | +import org.elasticsearch.common.util.CollectionUtils; |
| 14 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 15 | +import org.elasticsearch.core.CheckedFunction; |
| 16 | +import org.elasticsearch.core.Nullable; |
| 17 | +import org.elasticsearch.core.Tuple; |
| 18 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 19 | +import org.elasticsearch.xcontent.XContentFactory; |
| 20 | +import org.elasticsearch.xcontent.XContentParser; |
| 21 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
| 22 | +import org.elasticsearch.xcontent.XContentType; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.function.Function; |
| 30 | + |
| 31 | +/** |
| 32 | + * A filter that filter fields away from source |
| 33 | + */ |
| 34 | +public interface XContentFieldFilter { |
| 35 | + /** |
| 36 | + * filter source in {@link BytesReference} format and in {@link XContentType} content type |
| 37 | + * note that xContentType may be null in some case, we should guess xContentType from sourceBytes in such cases |
| 38 | + */ |
| 39 | + BytesReference apply(BytesReference sourceBytes, @Nullable XContentType xContentType) throws IOException; |
| 40 | + |
| 41 | + /** |
| 42 | + * Construct {@link XContentFieldFilter} using given includes and excludes |
| 43 | + * |
| 44 | + * @param includes fields to keep, wildcard supported |
| 45 | + * @param excludes fields to remove, wildcard supported |
| 46 | + * @return filter that filter {@link org.elasticsearch.xcontent.XContent} with given includes and excludes |
| 47 | + */ |
| 48 | + static XContentFieldFilter newFieldFilter(String[] includes, String[] excludes) { |
| 49 | + final CheckedFunction<XContentType, BytesReference, IOException> emptyValueSupplier = xContentType -> { |
| 50 | + BytesStreamOutput bStream = new BytesStreamOutput(); |
| 51 | + XContentBuilder builder = XContentFactory.contentBuilder(xContentType, bStream).map(Collections.emptyMap()); |
| 52 | + builder.close(); |
| 53 | + return bStream.bytes(); |
| 54 | + }; |
| 55 | + // Use the old map-based filtering mechanism if there are wildcards in the excludes. |
| 56 | + // TODO: Remove this if block once: https://github.com/elastic/elasticsearch/pull/80160 is merged |
| 57 | + if ((CollectionUtils.isEmpty(excludes) == false) && Arrays.stream(excludes).filter(field -> field.contains("*")).count() > 0) { |
| 58 | + return (originalSource, contentType) -> { |
| 59 | + if (originalSource == null || originalSource.length() <= 0) { |
| 60 | + if (contentType == null) { |
| 61 | + throw new IllegalStateException("originalSource and contentType can not be null at the same time"); |
| 62 | + } |
| 63 | + return emptyValueSupplier.apply(contentType); |
| 64 | + } |
| 65 | + Function<Map<String, ?>, Map<String, Object>> mapFilter = XContentMapValues.filter(includes, excludes); |
| 66 | + Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(originalSource, true, contentType); |
| 67 | + Map<String, Object> filteredSource = mapFilter.apply(mapTuple.v2()); |
| 68 | + BytesStreamOutput bStream = new BytesStreamOutput(); |
| 69 | + XContentType actualContentType = mapTuple.v1(); |
| 70 | + XContentBuilder builder = XContentFactory.contentBuilder(actualContentType, bStream).map(filteredSource); |
| 71 | + builder.close(); |
| 72 | + return bStream.bytes(); |
| 73 | + }; |
| 74 | + } else { |
| 75 | + final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering( |
| 76 | + Set.of(includes), |
| 77 | + Set.of(excludes), |
| 78 | + true |
| 79 | + ); |
| 80 | + return (originalSource, contentType) -> { |
| 81 | + if (originalSource == null || originalSource.length() <= 0) { |
| 82 | + if (contentType == null) { |
| 83 | + throw new IllegalStateException("originalSource and contentType can not be null at the same time"); |
| 84 | + } |
| 85 | + return emptyValueSupplier.apply(contentType); |
| 86 | + } |
| 87 | + if (contentType == null) { |
| 88 | + contentType = XContentHelper.xContentTypeMayCompressed(originalSource); |
| 89 | + } |
| 90 | + BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, originalSource.length())); |
| 91 | + XContentBuilder builder = new XContentBuilder(contentType.xContent(), streamOutput); |
| 92 | + XContentParser parser = contentType.xContent().createParser(parserConfig, originalSource.streamInput()); |
| 93 | + if ((parser.currentToken() == null) && (parser.nextToken() == null)) { |
| 94 | + return emptyValueSupplier.apply(contentType); |
| 95 | + } |
| 96 | + builder.copyCurrentStructure(parser); |
| 97 | + return BytesReference.bytes(builder); |
| 98 | + }; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments