ComparableParameter(Map.Entry<String, String[]> parameter) throws UnsupportedEncodingException { this.parameter = parameter; String name = safeToString(parameter.getKey()); List<String> sortedValues = Arrays.asList(parameter.getValue()); Collections.sort(sortedValues); String value = StringUtils.join(sortedValues, ','); this.key = JwtUtil.percentEncode(name) + ' ' + JwtUtil.percentEncode(value); // ' ' is used because it comes before any character // that can appear in a percentEncoded string. }
private static String canonicalizeUri(CanonicalHttpRequest request) throws UnsupportedEncodingException { String path = StringUtils.defaultIfBlank(StringUtils.removeEnd(request.getRelativePath(), "/"), "/"); final String separatorAsString = String.valueOf(CANONICAL_REQUEST_PART_SEPARATOR); // If the separator is not URL encoded then the following URLs have the same query-string-hash: // https://djtest9.jira-dev.com/rest/api/2/project&a=b?x=y // https://djtest9.jira-dev.com/rest/api/2/project?a=b&x=y path = path.replaceAll(separatorAsString, JwtUtil.percentEncode(separatorAsString)); return path.startsWith("/") ? path : "/" + path; }
/** * Write a form-urlencoded document into the given stream, containing the * given sequence of name/parameter pairs. */ private static void percentEncode(Iterable<? extends Map.Entry<String, String[]>> parameters, OutputStream into) throws IOException { if (parameters != null) { boolean first = true; for (Map.Entry<String, String[]> parameter : parameters) { if (first) { first = false; } else { into.write(JwtUtil.QUERY_PARAMS_SEPARATOR); } into.write(JwtUtil.percentEncode(safeToString(parameter.getKey())).getBytes()); into.write('='); List<String> percentEncodedValues = new ArrayList<String>(parameter.getValue().length); for (String value : parameter.getValue()) { percentEncodedValues.add(JwtUtil.percentEncode(value)); } into.write(StringUtils.join(percentEncodedValues, ENCODED_PARAM_VALUE_SEPARATOR).getBytes()); } } }