Skip to content

Commit

Permalink
[FIXED JENKINS-18118] Introduced UserAvatarResolver.resolveOrNull to …
Browse files Browse the repository at this point in the history
…properly produce fallback icon URL.
  • Loading branch information
jglick committed May 28, 2013
1 parent 94a3b43 commit 8549e7b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion changelog.html
Expand Up @@ -55,7 +55,9 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=>
<li class=bug>
User icon in People broken if Jenkins root URL unconfigured.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-18118">issue 18118</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/model/View.java
Expand Up @@ -792,7 +792,7 @@ public AsynchPeople(View parent) {
UserInfo info = users.get(user);
if (info == null) {
UserInfo userInfo = new UserInfo(user, p, build.getTimestamp());
userInfo.avatar = UserAvatarResolver.resolve(user, iconSize);
userInfo.avatar = UserAvatarResolver.resolveOrNull(user, iconSize);
synchronized (this) {
users.put(user, userInfo);
modified.add(user);
Expand Down Expand Up @@ -824,7 +824,7 @@ public AsynchPeople(View parent) {
}
if (!users.containsKey(u)) {
UserInfo userInfo = new UserInfo(u, null, null);
userInfo.avatar = UserAvatarResolver.resolve(u, iconSize);
userInfo.avatar = UserAvatarResolver.resolveOrNull(u, iconSize);
synchronized (this) {
users.put(u, userInfo);
modified.add(u);
Expand All @@ -842,7 +842,7 @@ public AsynchPeople(View parent) {
accumulate("id", u.getId()).
accumulate("fullName", u.getFullName()).
accumulate("url", u.getUrl()).
accumulate("avatar", i.avatar).
accumulate("avatar", i.avatar != null ? i.avatar : Stapler.getCurrentRequest().getContextPath() + Functions.getResourcePath() + "/images/" + iconSize + "/user.png").
accumulate("timeSortKey", i.getTimeSortKey()).
accumulate("lastChangeTimeString", i.getLastChangeTimeString());
AbstractProject<?,?> p = i.getProject();
Expand Down
14 changes: 12 additions & 2 deletions core/src/main/java/hudson/tasks/UserAvatarResolver.java
Expand Up @@ -33,6 +33,7 @@
import hudson.ExtensionPoint;
import hudson.Functions;
import hudson.model.User;
import javax.annotation.CheckForNull;

/**
* Infers avatar image URLs for users
Expand Down Expand Up @@ -78,13 +79,22 @@ public abstract class UserAvatarResolver implements ExtensionPoint {
public abstract String findAvatarFor(User u, int width, int height);

/**
* Resolve an avatar image URL string for the user
* Resolve an avatar image URL string for the user.
* Note that this method must be called from an HTTP request to be reliable; else use {@link #resolveOrNull}.
* @param u user
* @param avatarSize the preferred image size, "[width]x[height]"
* @return a URL string for a user Avatar image.
*/
public static String resolve(User u, String avatarSize) {
String avatar = resolveOrNull(u, avatarSize);
return avatar != null ? avatar : Jenkins.getInstance().getRootUrl() + Functions.getResourcePath() + "/images/" + avatarSize + "/user.png";
}

/**
* Like {@link #resolve} but returns null rather than a fallback URL in case there is no special avatar.
* @since 1.518
*/
public static @CheckForNull String resolveOrNull(User u, String avatarSize) {
Matcher matcher = iconSizeRegex.matcher(avatarSize);
if (matcher.matches() && matcher.groupCount() == 2) {
int width = Integer.parseInt(matcher.group(1));
Expand All @@ -97,7 +107,7 @@ public static String resolve(User u, String avatarSize) {
LOGGER.warning(String.format("Could not split up the avatar size (%s) into a width and height.", avatarSize));
}

return Jenkins.getInstance().getRootUrl() + Functions.getResourcePath() + "/images/" + avatarSize + "/user.png";
return null;
}

/**
Expand Down

0 comments on commit 8549e7b

Please sign in to comment.