|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core.indexlifecycle; |
| 8 | + |
| 9 | +import org.elasticsearch.ElasticsearchParseException; |
| 10 | +import org.elasticsearch.common.bytes.BytesArray; |
| 11 | +import org.elasticsearch.common.bytes.BytesReference; |
| 12 | +import org.elasticsearch.common.compress.NotXContentException; |
| 13 | +import org.elasticsearch.common.io.Streams; |
| 14 | +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; |
| 15 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 16 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 17 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 18 | +import org.elasticsearch.common.xcontent.XContentType; |
| 19 | + |
| 20 | +import java.io.ByteArrayOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | + |
| 24 | +/** |
| 25 | + * A utility class used for loading index lifecycle policies from the resource classpath |
| 26 | + */ |
| 27 | +public class LifecyclePolicyUtils { |
| 28 | + |
| 29 | + private LifecyclePolicyUtils() {}; |
| 30 | + |
| 31 | + /** |
| 32 | + * Loads a built-in index lifecycle policy and returns its source. |
| 33 | + */ |
| 34 | + public static LifecyclePolicy loadPolicy(String name, String resource, NamedXContentRegistry xContentRegistry) { |
| 35 | + try { |
| 36 | + BytesReference source = load(resource); |
| 37 | + validate(source); |
| 38 | + |
| 39 | + try (XContentParser parser = XContentType.JSON.xContent() |
| 40 | + .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.utf8ToString())) { |
| 41 | + return LifecyclePolicy.parse(parser, name); |
| 42 | + } |
| 43 | + } catch (Exception e) { |
| 44 | + throw new IllegalArgumentException("unable to load policy [" + name + "] from [" + resource + "]", e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Loads a resource from the classpath and returns it as a {@link BytesReference} |
| 50 | + */ |
| 51 | + private static BytesReference load(String name) throws IOException { |
| 52 | + try (InputStream is = LifecyclePolicyUtils.class.getResourceAsStream(name)) { |
| 53 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { |
| 54 | + Streams.copy(is, out); |
| 55 | + return new BytesArray(out.toByteArray()); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Parses and validates that the source is not empty. |
| 62 | + */ |
| 63 | + private static void validate(BytesReference source) { |
| 64 | + if (source == null) { |
| 65 | + throw new ElasticsearchParseException("policy must not be null"); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + XContentHelper.convertToMap(source, false, XContentType.JSON).v2(); |
| 70 | + } catch (NotXContentException e) { |
| 71 | + throw new ElasticsearchParseException("policy must not be empty"); |
| 72 | + } catch (Exception e) { |
| 73 | + throw new ElasticsearchParseException("invalid policy", e); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments