Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new Functions#urlEncode(String) method for encoding URLs in Jelly views #4278

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -726,6 +728,20 @@ public static String encode(String s) {
return Util.encode(s);
}

/**
* Shortcut function for calling {@link URLEncoder#encode(String,String)} (with UTF-8 encoding).<br>
* Useful for encoding URL query parameters in jelly code (as in {@code "...?param=${h.urlEncode(something)}"}).
*
* @since TODO
*/
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new Error(e); // impossible
}
}

public static String escape(String s) {
return Util.escape(s);
}
Expand Down
6 changes: 0 additions & 6 deletions core/src/main/java/hudson/search/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import hudson.util.EditDistance;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -68,10 +66,6 @@
* @see SearchableModelObject
*/
public class Search implements StaplerProxy {
@Restricted(NoExternalUse.class) // used from stapler views only
public static String encodeQuery(String query) throws UnsupportedEncodingException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether it's safe to remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say it is because of NoExternalUse, and a Github search in the jenkinsci org doesn't reveal more usage than what I've replaced. But let's ask @stephenc, who introduced this method in the first place, in 5d92057.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's @Restricted and has been from the beginning. If anyone is using it, that's a bug because they've messed with their tooling. That's why that annotation exists.

return URLEncoder.encode(query, "UTF-8");
}

public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
List<Ancestor> l = req.getAncestors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ THE SOFTWARE.
<ol>
<j:forEach var="i" items="${items}">
<li id="item_${i.path}">
<a href="?q=${it.encodeQuery(i.path)}">${i.path}</a>
<a href="?q=${h.urlEncode(i.path)}">${i.path}</a>
</li>
</j:forEach>
</ol>
<j:if test="${items.hasMoreResults()}">
<j:set var="max" value="${request.hasParameter('max')?request.getParameter('max'):100}"/>
<em>result has been truncated, <a href="?q=${it.encodeQuery(q)}&amp;max=${max+100}">see 100 more</a></em>
<em>result has been truncated, <a href="?q=${h.urlEncode(q)}&amp;max=${max+100}">see 100 more</a></em>
</j:if>
</j:otherwise>
</j:choose>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ THE SOFTWARE.
<d:tag name="relationship">
<j:if test="${lhs.fingerprintConfigured and rhs.fingerprintConfigured}">
<st:nbsp/>
<a href="${rootURL}/projectRelationship?lhs=${lhs.name}&amp;rhs=${rhs.name}">
<a href="${rootURL}/projectRelationship?lhs=${h.urlEncode(lhs.name)}&amp;rhs=${h.urlEncode(rhs.name)}">
<l:icon class="icon-fingerprint icon-sm"/>
</a>
</j:if>
Expand Down