Skip to content

Commit

Permalink
follow -up on #11, transform noWhitespace(String) from FlattenerUtils…
Browse files Browse the repository at this point in the history
… into toVariableName, and use it also on headers and request params
  • Loading branch information
juanpablo-santos committed Jul 17, 2017
1 parent 13802b8 commit 8959796
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public static String filter(String string, String regexpFilter) {
return string.replaceAll(regexpFilter, "");
}

public static String noWhitespace(String mixedString) {
return mixedString.replaceAll("\\s", "_");
public static String toVariableName(String mixedString) {
if (mixedString == null) {
return null;
}
return mixedString.replaceAll("\\s", "_").replaceAll("-", "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.google.common.collect.Maps.newHashMap;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.filter;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.noWhitespace;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.toVariableName;

import java.util.List;
import java.util.Map;
Expand All @@ -26,7 +26,7 @@ public Map<String, String> flatternJson(String key, String regexFilter, Object r
flatternJson(key + "_" + entry.getKey(), regexFilter, entry.getValue()));
}
} else if (resolved != null) {
String noWhitespaces = noWhitespace(key);
String noWhitespaces = toVariableName(key);
resolvedVariables.put(noWhitespaces, filter(resolved.toString(), regexFilter));
}
return resolvedVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.of;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.filter;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.toVariableName;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.gwt.GenericHeaderVariable;

import com.google.common.base.Optional;
Expand All @@ -33,11 +33,11 @@ public Map<String, String> getRequestHeaders(
String headerValue = headerEnumeration.nextElement();
String regexpFilter = configuredVariable.get().getRegexpFilter();
String filteredValue = filter(headerValue, regexpFilter);
found.put(StringUtils.replace(headerName, "-", "_") + "_" + i, filteredValue);
found.put(toVariableName(headerName) + "_" + i, filteredValue);
boolean firstAndOnlyValue = i == 0 && !headerEnumeration.hasMoreElements();
if (firstAndOnlyValue) {
//Users will probably expect this variable for parameters that are never a list
found.put(StringUtils.replace(headerName, "-", "_"), filteredValue);
found.put(toVariableName(headerName), filteredValue);
}
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Optional.absent;
import static com.google.common.collect.Maps.newHashMap;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.filter;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.toVariableName;

import java.util.List;
import java.util.Map;
Expand All @@ -28,11 +29,11 @@ public Map<String, String> getRequestParameters(
String[] values = incomingParameterMap.get(requestParamName);
for (int i = 0; i < values.length; i++) {
String filteredValue = filter(values[i], configuredVariable.get().getRegexpFilter());
resolvedVariables.put(requestParamName + "_" + i, filteredValue);
resolvedVariables.put(toVariableName(requestParamName) + "_" + i, filteredValue);
boolean firstAndOnlyValue = i == 0 && values.length == 1;
if (firstAndOnlyValue) {
//Users will probably expect this variable for parameters that are never a list
resolvedVariables.put(requestParamName, filteredValue);
resolvedVariables.put(toVariableName(requestParamName), filteredValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.google.common.collect.Maps.newHashMap;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.filter;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.noWhitespace;
import static org.jenkinsci.plugins.gwt.resolvers.FlattenerUtils.toVariableName;

import java.util.Map;

Expand Down Expand Up @@ -31,15 +31,15 @@ private Map<String, String> flattenXmlNode(
String parentKey, String regexFilter, Node node, int level, boolean fromRootLevel) {
Map<String, String> resolvedVariables = newHashMap();
if (isXmlLeafNode(node)) {
String noWhitespaces = noWhitespace(parentKey);
String noWhitespaces = toVariableName(parentKey);
resolvedVariables.put(noWhitespaces, filter(node.getTextContent(), regexFilter));
} else {
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
Node childNode = node.getChildNodes().item(i);
String childKey =
expandKey(parentKey, level, fromRootLevel) + "_" + childNode.getNodeName();
if (isXmlLeafNode(childNode)) {
String noWhitespace = noWhitespace(childKey);
String noWhitespace = toVariableName(childKey);
resolvedVariables.put(noWhitespace, filter(childNode.getTextContent(), regexFilter));
} else {
//leafnode and text inside leafnode are 2 nodes, so /2 to keep counter in line
Expand Down

0 comments on commit 8959796

Please sign in to comment.