Issue 4235 - changes from review

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-04-14 21:34:26 +10:00
parent 4bc32e314b
commit 0de5976651
3 changed files with 23 additions and 15 deletions

View File

@ -434,8 +434,8 @@ public class OpenIdAuthenticator extends LoginAuthenticator
String redirectUri = URIUtil.addPaths(request.getContextPath(), _errorPage);
if (message != null)
{
String query = URIUtil.combineQueryParams(ERROR_PARAMETER + "=" + UrlEncoded.encodeString(message), _errorQuery);
redirectUri = URIUtil.addPaths(request.getContextPath(), _errorPath) + "?" + query;
String query = URIUtil.addQueries(ERROR_PARAMETER + "=" + UrlEncoded.encodeString(message), _errorQuery);
redirectUri = URIUtil.addPathQuery(URIUtil.addPaths(request.getContextPath(), _errorPath), query);
baseResponse.sendRedirect(getRedirectCode(baseRequest.getHttpVersion()), redirectUri);
}

View File

@ -714,6 +714,20 @@ public class URIUtil
return buf.toString();
}
/** Add a path and a query string
* @param path The path which may already contain contain a query
* @param query The query string or null if no query to be added
* @return The path with any non null query added after a '?' or '&amp;' as appropriate.
*/
public static String addPathQuery(String path, String query)
{
if (query == null)
return path;
if (path.indexOf('?') >= 0)
return path + '&' + query;
return path + '?' + query;
}
/**
* Given a URI, attempt to get the last segment.
* <p>
@ -1312,19 +1326,13 @@ public class URIUtil
* @param query2 the second query string.
* @return the combination of the two query strings.
*/
public static String combineQueryParams(String query1, String query2)
public static String addQueries(String query1, String query2)
{
StringBuilder queryBuilder = new StringBuilder();
if (!StringUtil.isEmpty(query1))
{
queryBuilder.append(query1);
if (!StringUtil.isEmpty(query2))
queryBuilder.append("&");
}
if (!StringUtil.isEmpty(query2))
queryBuilder.append(query2);
return queryBuilder.toString();
if (StringUtil.isEmpty(query1))
return query2;
if (StringUtil.isEmpty(query2))
return query1;
return query1 + '&' + query2;
}
public static URI getJarSource(URI uri)

View File

@ -730,6 +730,6 @@ public class URIUtilTest
@MethodSource("addQueryParameterSource")
public void testAddQueryParam(String param1, String param2, Matcher<String> matcher)
{
assertThat(URIUtil.combineQueryParams(param1, param2), matcher);
assertThat(URIUtil.addQueries(param1, param2), matcher);
}
}