/
HeaderBasedPlaceholderSubstitutionAlgorithm.java
113 lines (96 loc) · 4.69 KB
/
HeaderBasedPlaceholderSubstitutionAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.concierge.enforcement.placeholders;
import static java.util.Objects.requireNonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.base.common.Placeholders;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.WithDittoHeaders;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayPlaceholderNotResolvableException;
/**
* An algorithm for placeholder substitution based on {@link DittoHeaders}.
*/
@Immutable
public final class HeaderBasedPlaceholderSubstitutionAlgorithm {
private final Map<String, Function<DittoHeaders, String>> replacementDefinitions;
private final List<CharSequence> knownPlaceHolders;
private HeaderBasedPlaceholderSubstitutionAlgorithm(final Map<String, Function<DittoHeaders, String>> replacementDefinitions) {
this.replacementDefinitions = Collections.unmodifiableMap(new LinkedHashMap<>(replacementDefinitions));
this.knownPlaceHolders = Collections.unmodifiableList(new ArrayList<>(replacementDefinitions.keySet()));
}
/**
* Creates a new instance based on the given replacement definitions.
* @param replacementDefinitions the replacement definitions.
*
* @return the created instance.
*/
public static HeaderBasedPlaceholderSubstitutionAlgorithm newInstance(
final Map<String, Function<DittoHeaders, String>> replacementDefinitions) {
return new HeaderBasedPlaceholderSubstitutionAlgorithm(requireNonNull(replacementDefinitions));
}
/**
* Substitutes all placeholders contained in the {@code input} based on the {@link DittoHeaders} contained in
* {@code withDittoHeaders}.
*
* @param input the input.
* @param withDittoHeaders the instance of {@link WithDittoHeaders}, from which the headers are extracted
* (normally a command).
* @return the replaced input, if the input contains placeholders; the (same) input object, if no placeholders
* were contained in the input.
*/
public String substitute(final String input, final WithDittoHeaders withDittoHeaders) {
requireNonNull(input);
requireNonNull(withDittoHeaders);
return substitute(input, withDittoHeaders.getDittoHeaders());
}
/**
* Substitutes all placeholders contained in the {@code input} based on the {@code dittoHeaders}.
*
* @param input the input.
* @param dittoHeaders the extracted {@link DittoHeaders}.
* @return the replaced input, if the input contains placeholders; the (same) input object, if no placeholders
* were contained in the input.
*/
public String substitute(final String input, final DittoHeaders dittoHeaders) {
requireNonNull(input);
requireNonNull(dittoHeaders);
final Function<String, Optional<String>> placeholderReplacerFunction = createReplacerFunction(dittoHeaders);
final Function<String, DittoRuntimeException> unresolvedInputHandler = createUnresolvedInputHandler(dittoHeaders);
return Placeholders.substitute(input, placeholderReplacerFunction, unresolvedInputHandler);
}
private Function<String, Optional<String>> createReplacerFunction(final DittoHeaders dittoHeaders) {
return placeholder -> {
final Function<DittoHeaders, String> placeholderResolver = replacementDefinitions.get(placeholder);
if (placeholderResolver == null) {
throw GatewayPlaceholderNotResolvableException.newUnknownPlaceholderBuilder(placeholder, knownPlaceHolders)
.dittoHeaders(dittoHeaders)
.build();
}
return Optional.ofNullable(placeholderResolver.apply(dittoHeaders));
};
}
private Function<String, DittoRuntimeException> createUnresolvedInputHandler(
final DittoHeaders dittoHeaders) {
return input -> GatewayPlaceholderNotResolvableException.newNotResolvableInputBuilder(input)
.dittoHeaders(dittoHeaders)
.build();
}
}