Skip to content

Commit

Permalink
Merge pull request #7 from jenkinsci/fix/rareMarshalingError
Browse files Browse the repository at this point in the history
fix(filtering): rare marshaling error and prevent more filtering errors
  • Loading branch information
h1dden-da3m0n authored Oct 8, 2020
2 parents 19e660c + 336bfde commit 2da6661
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private RestValueService() {

/**
* Returns a {@link ResultContainer} capsuling a optional String error message and a collection of parsed string values.
*
* <p>
* This method uses its parameters to query a REST/Web endpoint to receive a {@link MimeType} response, which then
* gets parsed with a supported Path expression to extract a collection of string values.
*
Expand Down Expand Up @@ -61,9 +61,11 @@ public static ResultContainer<Collection<String>> get(final String restEndpoint,
valueCollection.setErrorMsg(rawValueError.get());
}

if (!valueCollection.getErrorMsg().isPresent() && StringUtils.isNotBlank(filter)) {
Collection<String> filteredValues = filterValues(valueCollection.getValue(), filter);
valueCollection.setValue(filteredValues);
if (!valueCollection.getErrorMsg().isPresent()
&& StringUtils.isNotBlank(filter)
&& !filter.equalsIgnoreCase(".*"))
{
valueCollection = filterValues(valueCollection.getValue(), filter);
}

return valueCollection;
Expand Down Expand Up @@ -118,10 +120,10 @@ else if (statusCode < 500) {

/**
* Builds the OKHttp Headers that should get applied to the request.
*
* <p>
* Sets a <em>ACCEPT</em> header and optionally a <em>AUTHORIZATION</em> header.
* The <em>AUTHORIZATION</em> header gets set to <em>BASIC</em> or <em>BEARER</em> depending on credential type supplied.
*
* <p>
* Currently supported credential types are {@link StandardUsernamePasswordCredentials} for BASIC and
* {@link StringCredentials} for BEARER <em>AUTHORIZATION</em>.
*
Expand Down Expand Up @@ -193,14 +195,32 @@ private static ResultContainer<Collection<String>> convertToValuesCollection(fin
*
* @param values The collection of string values
* @param filter The regex expression string
* @return A filtered string collection based on the result of the applied regex filter
* @return A {@link ResultContainer} capsuling a filtered string collection or a user friendly error message
*/
private static Collection<String> filterValues(final Collection<String> values,
final String filter)
private static ResultContainer<Collection<String>> filterValues(final Collection<String> values,
final String filter)
{
return values.stream()
.filter(value -> value.matches(filter))
.collect(Collectors.toList());
ResultContainer<Collection<String>> container = new ResultContainer<>(Collections.emptyList());

try {
Collection<String> filteredValues = values.stream()
.filter(value -> value.matches(filter))
.collect(Collectors.toList());
if (!filteredValues.isEmpty()) {
container.setValue(filteredValues);
}
else {
container.setErrorMsg(Messages.RLP_RestValueService_info_FilterReturnedNoValues(filter));
}
}
catch (Exception ex) {
log.warning(Messages.RLP_RestValueService_warn_FilterErr());
container.setErrorMsg(Messages.RLP_RestValueService_warn_FilterErr());
log.fine("Exception Class: " + ex.getClass().getName() + "\n"
+ "Exception Message: " + ex.getMessage());
}

return container;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static ResultContainer<Collection<String>> resolveJsonPath(final String j
final Collection<String> resolved = JsonPath.parse(jsonStr).read(expression);

if (!resolved.isEmpty()) {
container.setValue(resolved);
container.setValue(new ArrayList<>(resolved));
}
else {
log.warning(Messages.RLP_ValueResolver_warn_jPath_NoValues());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ RLP.RestValueService.warn.OkHttpErr=OKHttp request threw {0}
RLP.RestValueService.fine.UsingBasicAuth=Using Basic auth type to request REST-Values
RLP.RestValueService.fine.UsingBearerAuth=Using Bearer auth type to request REST-Values
RLP.RestValueService.warn.UnsupportedCredential=Attempted to use unsupported Credential type: {0}
RLP.RestValueService.info.FilterReturnedNoValues=The filter `{0}` left no values to be displayed
RLP.RestValueService.warn.FilterErr=Filtering value list threw a exception (see Jenkins log)
# ValueResolver xPath logging Strings
RLP.ValueResolver.warn.xPath.NoValues=xPath expression yielded no values
RLP.ValueResolver.warn.xPath.ExpressionErr=Error in provided xPath expression
Expand Down

0 comments on commit 2da6661

Please sign in to comment.