|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.client.rulesengine; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.ConcurrentHashMap; |
| 13 | +import java.util.concurrent.ConcurrentMap; |
| 14 | +import software.amazon.smithy.java.core.schema.ApiOperation; |
| 15 | +import software.amazon.smithy.java.core.schema.Schema; |
| 16 | +import software.amazon.smithy.java.core.schema.SerializableStruct; |
| 17 | +import software.amazon.smithy.java.core.serde.document.Document; |
| 18 | +import software.amazon.smithy.java.jmespath.JMESPathDocumentQuery; |
| 19 | +import software.amazon.smithy.jmespath.JmespathExpression; |
| 20 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 21 | + |
| 22 | +/** |
| 23 | + * Provides context parameters from operations using {@code smithy.rules#contextParam}, |
| 24 | + * {@code smithy.rules#operationContextParams}, and {@code smithy.rules#staticContextParams} traits. |
| 25 | + * |
| 26 | + * <p>The results of finding operation context parameters from an operation are cached and reused over the life of |
| 27 | + * a client per/operation. |
| 28 | + */ |
| 29 | +sealed interface ContextProvider { |
| 30 | + |
| 31 | + void addContext(ApiOperation<?, ?> operation, SerializableStruct input, Map<String, Object> params); |
| 32 | + |
| 33 | + final class OrchestratingProvider implements ContextProvider { |
| 34 | + private final ConcurrentMap<ShapeId, ContextProvider> PROVIDERS = new ConcurrentHashMap<>(); |
| 35 | + |
| 36 | + @Override |
| 37 | + public void addContext(ApiOperation<?, ?> operation, SerializableStruct input, Map<String, Object> params) { |
| 38 | + var provider = PROVIDERS.get(operation.schema().id()); |
| 39 | + if (provider == null) { |
| 40 | + provider = createProvider(operation); |
| 41 | + var fresh = PROVIDERS.putIfAbsent(operation.schema().id(), provider); |
| 42 | + if (fresh != null) { |
| 43 | + provider = fresh; |
| 44 | + } |
| 45 | + } |
| 46 | + provider.addContext(operation, input, params); |
| 47 | + } |
| 48 | + |
| 49 | + private ContextProvider createProvider(ApiOperation<?, ?> operation) { |
| 50 | + List<ContextProvider> providers = new ArrayList<>(); |
| 51 | + var operationSchema = operation.schema(); |
| 52 | + var inputSchema = operation.inputSchema(); |
| 53 | + ContextParamProvider.compute(providers, inputSchema); |
| 54 | + ContextPathProvider.compute(providers, operationSchema); |
| 55 | + StaticParamsProvider.compute(providers, operationSchema); // overrides everything else |
| 56 | + return MultiContextParamProvider.from(providers); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Find the smithy.rules#staticContextParams on the operation. |
| 61 | + final class StaticParamsProvider implements ContextProvider { |
| 62 | + private final Map<String, Object> params; |
| 63 | + |
| 64 | + StaticParamsProvider(Map<String, Object> params) { |
| 65 | + this.params = params; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void addContext(ApiOperation<?, ?> operation, SerializableStruct input, Map<String, Object> params) { |
| 70 | + params.putAll(this.params); |
| 71 | + } |
| 72 | + |
| 73 | + static void compute(List<ContextProvider> providers, Schema operation) { |
| 74 | + var staticParamsTrait = operation.getTrait(EndpointRulesPlugin.STATIC_CONTEXT_PARAMS_TRAIT); |
| 75 | + if (staticParamsTrait == null) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + Map<String, Object> result = new HashMap<>(staticParamsTrait.getParameters().size()); |
| 80 | + for (var entry : staticParamsTrait.getParameters().entrySet()) { |
| 81 | + result.put(entry.getKey(), EndpointUtils.convertNode(entry.getValue().getValue())); |
| 82 | + } |
| 83 | + |
| 84 | + providers.add(new StaticParamsProvider(result)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Find smithy.rules#contextParam trait on operation input members. |
| 89 | + final class ContextParamProvider implements ContextProvider { |
| 90 | + private final Schema member; |
| 91 | + private final String name; |
| 92 | + |
| 93 | + ContextParamProvider(Schema member, String name) { |
| 94 | + this.member = member; |
| 95 | + this.name = name; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void addContext(ApiOperation<?, ?> operation, SerializableStruct input, Map<String, Object> params) { |
| 100 | + var value = input.getMemberValue(member); |
| 101 | + if (value != null) { |
| 102 | + params.put(name, value); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + static void compute(List<ContextProvider> providers, Schema inputSchema) { |
| 107 | + for (var member : inputSchema.members()) { |
| 108 | + var ctxTrait = member.getTrait(EndpointRulesPlugin.CONTEXT_PARAM_TRAIT); |
| 109 | + if (ctxTrait != null) { |
| 110 | + providers.add(new ContextParamProvider(member, ctxTrait.getName())); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Find the smithy.rules#operationContextParams trait on the operation and each JMESPath to extract. |
| 117 | + // TODO: I wish we didn't have to convert input to a document and could use the struct directly. |
| 118 | + // We'd need to add a new code path to the jmespath module, something like JMESPathStructQuery. |
| 119 | + final class ContextPathProvider implements ContextProvider { |
| 120 | + |
| 121 | + private final String name; |
| 122 | + private final JmespathExpression jp; |
| 123 | + |
| 124 | + ContextPathProvider(String name, JmespathExpression jp) { |
| 125 | + this.name = name; |
| 126 | + this.jp = jp; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void addContext(ApiOperation<?, ?> operation, SerializableStruct input, Map<String, Object> params) { |
| 131 | + var doc = Document.of(input); |
| 132 | + var result = JMESPathDocumentQuery.query(jp, doc); |
| 133 | + if (result != null) { |
| 134 | + params.put(name, result.asObject()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + static void compute(List<ContextProvider> providers, Schema operation) { |
| 139 | + var params = operation.getTrait(EndpointRulesPlugin.OPERATION_CONTEXT_PARAMS_TRAIT); |
| 140 | + if (params == null) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + for (var param : params.getParameters().entrySet()) { |
| 145 | + var name = param.getKey(); |
| 146 | + var path = param.getValue().getPath(); |
| 147 | + var jp = JmespathExpression.parse(path); |
| 148 | + providers.add(new ContextPathProvider(name, jp)); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Applies multiple context providers. |
| 154 | + final class MultiContextParamProvider implements ContextProvider { |
| 155 | + private final List<ContextProvider> providers; |
| 156 | + |
| 157 | + MultiContextParamProvider(List<ContextProvider> providers) { |
| 158 | + this.providers = providers; |
| 159 | + } |
| 160 | + |
| 161 | + static ContextProvider from(List<ContextProvider> providers) { |
| 162 | + return providers.size() == 1 ? providers.get(0) : new MultiContextParamProvider(providers); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void addContext(ApiOperation<?, ?> operation, SerializableStruct input, Map<String, Object> params) { |
| 167 | + for (ContextProvider provider : providers) { |
| 168 | + provider.addContext(operation, input, params); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments