Skip to content

Commit 54d54cf

Browse files
committed
Ref #25533: refactor in names and value runtimevalues
1 parent 476d81d commit 54d54cf

12 files changed

Lines changed: 208 additions & 82 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2021 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.model.runtime;
22+
23+
import com.fasterxml.jackson.core.JsonProcessingException;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
public class ParameterNames {
30+
private final List<ParameterName> values;
31+
32+
public ParameterNames(List<ParameterName> values) {
33+
this.values = values;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
ObjectMapper objectMapper = new ObjectMapper();
39+
try {
40+
return objectMapper.writeValueAsString(values);
41+
} catch (JsonProcessingException e) {
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
46+
public List<ParameterName> getParametersNames() {
47+
return values;
48+
}
49+
50+
public static class ParameterName {
51+
52+
private final String displayName;
53+
private final String description;
54+
private final String value;
55+
56+
public ParameterName(String displayName, String description, String value) {
57+
this.displayName = displayName;
58+
this.description = description;
59+
this.value = value;
60+
}
61+
62+
public String getDisplayName() {
63+
return displayName;
64+
}
65+
66+
public String getDescription() {
67+
return description;
68+
}
69+
70+
public String getValue() {
71+
return value;
72+
}
73+
74+
}
75+
76+
}

src/main/java/eu/openanalytics/containerproxy/model/runtime/ProvidedParameters.java renamed to src/main/java/eu/openanalytics/containerproxy/model/runtime/ParameterValues.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,21 @@
2020
*/
2121
package eu.openanalytics.containerproxy.model.runtime;
2222

23+
2324
import com.fasterxml.jackson.annotation.JsonCreator;
2425
import com.fasterxml.jackson.core.JsonProcessingException;
2526
import com.fasterxml.jackson.databind.ObjectMapper;
2627

27-
import java.util.HashMap;
2828
import java.util.Map;
2929

30-
public class ProvidedParameters {
30+
public class ParameterValues {
3131

3232
private final Map<String, String> backendValues;
33-
private final String stringRepresentation;
3433
private final String valueSetName;
3534

3635
@JsonCreator
37-
public ProvidedParameters(Map<String, String> backendValues, String stringRepresentation, String valueSetName) {
36+
public ParameterValues(Map<String, String> backendValues, String valueSetName) {
3837
this.backendValues = backendValues;
39-
this.stringRepresentation = stringRepresentation;
4038
this.valueSetName = valueSetName;
4139
}
4240

@@ -53,7 +51,12 @@ public String getValue(String parameterId) {
5351

5452
@Override
5553
public String toString() {
56-
return stringRepresentation;
54+
ObjectMapper objectMapper = new ObjectMapper();
55+
try {
56+
return objectMapper.writeValueAsString(backendValues);
57+
} catch (JsonProcessingException e) {
58+
throw new RuntimeException(e);
59+
}
5760
}
5861

5962
public String getValueSetName() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2021 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.model.runtime.runtimevalues;
22+
23+
import eu.openanalytics.containerproxy.model.runtime.ParameterNames;
24+
25+
public class ParameterNamesKey extends RuntimeValueKey<ParameterNames> {
26+
27+
public ParameterNamesKey() {
28+
super("openanalytics.eu/sp-parameters-names",
29+
"SHINYPROXY_PARAMETER_NAME",
30+
false,
31+
false,
32+
false,
33+
false, ParameterNames.class);
34+
}
35+
36+
public static ParameterNamesKey inst = new ParameterNamesKey();
37+
38+
39+
}

src/main/java/eu/openanalytics/containerproxy/model/runtime/runtimevalues/ParametersKey.java renamed to src/main/java/eu/openanalytics/containerproxy/model/runtime/runtimevalues/ParameterValuesKey.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
*/
2121
package eu.openanalytics.containerproxy.model.runtime.runtimevalues;
2222

23-
import eu.openanalytics.containerproxy.model.runtime.ProvidedParameters;
23+
import eu.openanalytics.containerproxy.model.runtime.ParameterValues;
2424

25-
public class ParametersKey extends RuntimeValueKey<ProvidedParameters> {
25+
public class ParameterValuesKey extends RuntimeValueKey<ParameterValues> {
2626

27-
public ParametersKey() {
27+
public ParameterValuesKey() {
2828
super("openanalytics.eu/sp-parameters",
2929
"SHINYPROXY_PARAMETERS",
3030
false,
31+
false, // TODO
3132
false,
32-
false,
33-
false, ProvidedParameters.class);
33+
false, ParameterValues.class);
3434
}
3535

36-
public static ParametersKey inst = new ParametersKey();
36+
public static ParameterValuesKey inst = new ParameterValuesKey();
3737

3838

3939
}

src/main/java/eu/openanalytics/containerproxy/model/runtime/runtimevalues/RuntimeValue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class RuntimeValue {
3333
private final Object value;
3434

3535
public RuntimeValue(RuntimeValueKey<?> key, Object value) {
36+
if (!key.isInstance(value)) {
37+
throw new IllegalArgumentException("Provided value is not of the correct type!");
38+
}
3639
this.key = Objects.requireNonNull(key, "key may not be null");
3740
this.value = Objects.requireNonNull(value, "value may not be null for key " + key.getKeyAsEnvVar());
3841
}
@@ -57,4 +60,3 @@ private <T> T getObject(Class<T> clazz) {
5760
}
5861

5962
}
60-

src/main/java/eu/openanalytics/containerproxy/model/runtime/runtimevalues/RuntimeValueKey.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ public Boolean isRequired() {
7979
public Class<T> getClazz() {
8080
return clazz;
8181
}
82+
83+
public boolean isInstance(Object object) {
84+
return clazz.isInstance(object);
85+
}
8286
}

src/main/java/eu/openanalytics/containerproxy/model/runtime/runtimevalues/RuntimeValueKeyRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static RuntimeValueKey<?> getRuntimeValue(String key) {
5959
addRuntimeValueKey(RealmIdKey.inst);
6060
addRuntimeValueKey(UserGroupsKey.inst);
6161
addRuntimeValueKey(UserIdKey.inst);
62-
addRuntimeValueKey(ParametersKey.inst);
62+
addRuntimeValueKey(ParameterNamesKey.inst);
63+
addRuntimeValueKey(ParameterValuesKey.inst);
6364
addRuntimeValueKey(HeartbeatTimeoutKey.inst);
6465
addRuntimeValueKey(MaxLifetimeKey.inst);
6566
}

src/main/java/eu/openanalytics/containerproxy/service/ParametersService.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
*/
2121
package eu.openanalytics.containerproxy.service;
2222

23-
import com.fasterxml.jackson.core.JsonProcessingException;
2423
import com.fasterxml.jackson.databind.ObjectMapper;
2524
import eu.openanalytics.containerproxy.model.runtime.AllowedParametersForUser;
26-
import eu.openanalytics.containerproxy.model.runtime.ProvidedParameters;
25+
import eu.openanalytics.containerproxy.model.runtime.ParameterValues;
26+
import eu.openanalytics.containerproxy.model.runtime.ParameterNames;
2727
import eu.openanalytics.containerproxy.model.spec.ParameterDefinition;
2828
import eu.openanalytics.containerproxy.model.spec.Parameters;
2929
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
3030
import eu.openanalytics.containerproxy.spec.IProxySpecProvider;
3131
import org.apache.commons.lang3.StringUtils;
32+
import org.apache.commons.lang3.tuple.Pair;
3233
import org.springframework.security.core.Authentication;
3334
import org.springframework.stereotype.Service;
3435

@@ -127,13 +128,14 @@ private void validateSpec(ProxySpec spec) {
127128
* - checks that a value is included for every parameter
128129
* - checks that the user is allowed to use these values
129130
* - converts the (human friendly) name to the backend value
130-
* @param auth the user
131-
* @param spec the Proxy spec to which this requests belong
131+
*
132+
* @param auth the user
133+
* @param spec the Proxy spec to which this requests belong
132134
* @param providedParameters the parameters as provided by the user (using human friendly names)
133135
* @return the parsed parameters (using backend values)
134136
* @throws InvalidParametersException
135137
*/
136-
public Optional<ProvidedParameters> parseAndValidateRequest(Authentication auth, ProxySpec spec, Map<String, String> providedParameters) throws InvalidParametersException {
138+
public Optional<Pair<ParameterNames, ParameterValues>> parseAndValidateRequest(Authentication auth, ProxySpec spec, Map<String, String> providedParameters) throws InvalidParametersException {
137139
Parameters parameters = spec.getParameters();
138140
if (parameters == null) {
139141
return Optional.empty();
@@ -160,7 +162,7 @@ public Optional<ProvidedParameters> parseAndValidateRequest(Authentication auth,
160162
if (!accessControlEvaluationService.checkAccess(auth, spec, valueSet.getAccessControl())) {
161163
continue;
162164
}
163-
Optional<ProvidedParameters> res = convertParametersIfAllowed(parameters.getDefinitions(), valueSet, providedParameters);
165+
Optional<Pair<ParameterNames, ParameterValues>> res = convertParametersIfAllowed(parameters.getDefinitions(), valueSet, providedParameters);
164166
if (res.isPresent()) {
165167
return res; // parameters are allowed, return the converted values
166168
}
@@ -173,12 +175,13 @@ public Optional<ProvidedParameters> parseAndValidateRequest(Authentication auth,
173175
* Checks whether the provided parameters are allowed by the given valueSet.
174176
* Returns the converted backend values if (and only if) the provided human-friendly values are allowed by this
175177
* valueSet.
176-
* @param parameters the parameter defintiions
177-
* @param valueSet the valueSet to check
178+
*
179+
* @param parameters the parameter defintiions
180+
* @param valueSet the valueSet to check
178181
* @param providedParameters the parameters as provided by the user (using human friendly names)
179182
* @return the converted values (i.e. using backend values) if allowed otherwise nothing
180183
*/
181-
private Optional<ProvidedParameters> convertParametersIfAllowed(List<ParameterDefinition> parameters, Parameters.ValueSet valueSet, Map<String, String> providedParameters) {
184+
private Optional<Pair<ParameterNames, ParameterValues>> convertParametersIfAllowed(List<ParameterDefinition> parameters, Parameters.ValueSet valueSet, Map<String, String> providedParameters) {
182185
Map<String, String> backendValues = new HashMap<>();
183186
Map<String, String> names = new HashMap<>();
184187
for (ParameterDefinition parameter: parameters) {
@@ -205,29 +208,25 @@ private Optional<ProvidedParameters> convertParametersIfAllowed(List<ParameterDe
205208
names.put(parameter.getDisplayNameOrId(), providedValue); // TODO description?
206209
}
207210
// providedParameters contains an allowed value for every parameter
208-
// return the backend values (instead of the names provided by the user)
209-
return Optional.of(new ProvidedParameters(
210-
backendValues,
211-
getStringRepresentation(parameters, providedParameters),
212-
valueSet.getName()
213-
));
211+
ParameterNames parameterNames = new ParameterNames(getParameterNames(parameters, providedParameters));
212+
ParameterValues parameterValues = new ParameterValues(backendValues, valueSet.getName());
213+
return Optional.of(Pair.of(parameterNames, parameterValues));
214214
}
215215

216-
public String getStringRepresentation(List<ParameterDefinition> parameters, Map<String, String> providedParameters) {
217-
List<Map<String, String>> res = new ArrayList<>();
216+
/**
217+
* Creates ParamaterNames representation for the chosen parameters.
218+
* This is the public value which can be seen by the user (e.g. in API responses).
219+
*
220+
* @param parameters parameter definitions
221+
* @param providedParameters values chosen by the user
222+
* @return
223+
*/
224+
private List<ParameterNames.ParameterName> getParameterNames(List<ParameterDefinition> parameters, Map<String, String> providedParameters) {
225+
List<ParameterNames.ParameterName> res = new ArrayList<>();
218226
for (ParameterDefinition parameter : parameters) {
219-
res.add(new HashMap<String, String>() {{
220-
put("displayName", parameter.getDisplayNameOrId());
221-
put("description", parameter.getDescription());
222-
put("value", providedParameters.get(parameter.getId())); // important: this is the human-friendly/frontend value
223-
}});
224-
}
225-
226-
try {
227-
return objectMapper.writeValueAsString(res);
228-
} catch (JsonProcessingException e) {
229-
throw new RuntimeException(e);
227+
res.add(new ParameterNames.ParameterName(parameter.getDisplayNameOrId(), parameter.getDescription(), providedParameters.get(parameter.getId())));
230228
}
229+
return res;
231230
}
232231

233232
public AllowedParametersForUser calculateAllowedParametersForUser(Authentication auth, ProxySpec proxySpec) {

src/main/java/eu/openanalytics/containerproxy/service/ProxyService.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@
4242
import eu.openanalytics.containerproxy.event.ProxyStartFailedEvent;
4343
import eu.openanalytics.containerproxy.event.ProxyStartEvent;
4444
import eu.openanalytics.containerproxy.event.ProxyStopEvent;
45-
import eu.openanalytics.containerproxy.model.runtime.ProvidedParameters;
46-
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.HeartbeatTimeoutKey;
47-
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.ParametersKey;
4845
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.RuntimeValue;
49-
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionContext;
50-
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver;
5146
import eu.openanalytics.containerproxy.util.SuccessOrFailure;
5247
import org.apache.logging.log4j.LogManager;
5348
import org.apache.logging.log4j.Logger;

src/main/java/eu/openanalytics/containerproxy/service/RuntimeValueService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@
2020
*/
2121
package eu.openanalytics.containerproxy.service;
2222

23-
import eu.openanalytics.containerproxy.model.runtime.ProvidedParameters;
23+
import eu.openanalytics.containerproxy.model.runtime.ParameterValues;
24+
import eu.openanalytics.containerproxy.model.runtime.ParameterNames;
2425
import eu.openanalytics.containerproxy.model.runtime.Proxy;
2526
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.HeartbeatTimeoutKey;
2627
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.MaxLifetimeKey;
27-
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.ParametersKey;
28+
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.ParameterValuesKey;
29+
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.ParameterNamesKey;
2830
import eu.openanalytics.containerproxy.model.runtime.runtimevalues.RuntimeValue;
2931
import eu.openanalytics.containerproxy.model.spec.ProxySpec;
3032
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionContext;
3133
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver;
34+
import org.apache.commons.lang3.tuple.Pair;
3235
import org.springframework.core.env.Environment;
3336
import org.springframework.stereotype.Service;
3437

@@ -72,10 +75,13 @@ public void init() {
7275
}
7376

7477
public void createRuntimeValues(ProxySpec spec, Map<String, String> parameters, Proxy proxy) throws InvalidParametersException {
75-
Optional<ProvidedParameters> providedParametersOptional = parametersService.parseAndValidateRequest(userService.getCurrentAuth(), spec, parameters);
78+
Optional<Pair<ParameterNames, ParameterValues>> providedParametersOptional = parametersService.parseAndValidateRequest(userService.getCurrentAuth(), spec, parameters);
79+
7680
if (providedParametersOptional.isPresent()) {
77-
proxy.addRuntimeValue(new RuntimeValue(ParametersKey.inst, providedParametersOptional.get()));
81+
proxy.addRuntimeValue(new RuntimeValue(ParameterNamesKey.inst, providedParametersOptional.get().getKey()));
82+
proxy.addRuntimeValue(new RuntimeValue(ParameterValuesKey.inst, providedParametersOptional.get().getValue()));
7883
}
84+
7985
SpecExpressionContext context = SpecExpressionContext.create(
8086
proxy,
8187
proxy.getSpec(),

0 commit comments

Comments
 (0)