2020 */
2121package eu .openanalytics .containerproxy .service ;
2222
23- import com .fasterxml .jackson .core .JsonProcessingException ;
2423import com .fasterxml .jackson .databind .ObjectMapper ;
2524import 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 ;
2727import eu .openanalytics .containerproxy .model .spec .ParameterDefinition ;
2828import eu .openanalytics .containerproxy .model .spec .Parameters ;
2929import eu .openanalytics .containerproxy .model .spec .ProxySpec ;
3030import eu .openanalytics .containerproxy .spec .IProxySpecProvider ;
3131import org .apache .commons .lang3 .StringUtils ;
32+ import org .apache .commons .lang3 .tuple .Pair ;
3233import org .springframework .security .core .Authentication ;
3334import 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 ) {
0 commit comments