File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\tasks\UserAvatarResolver.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, Erik Ramfelt
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.tasks;
25
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import jenkins.model.Jenkins;
31 import hudson.Extension;
32 import hudson.ExtensionList;
33 import hudson.ExtensionPoint;
34 import hudson.Functions;
35 import hudson.model.User;
36
37 /**
38 * Infers avatar image URLs for users
39 *
40 * <p>
41 * This is an extension point of Jenkins. Plugins that contribute a new implementation
42 * of this class should put {@link Extension} on your implementation class, like this:
43 *
44 * <pre>
45 * &#64;Extension
46 * class MyUserAvatarResolver extends {@link UserAvatarResolver} {
47 * ...
48 * }
49 * </pre>
50 *
51 * @author Erik Ramfelt
52 * @since 1.434
53 */
54 public abstract class UserAvatarResolver implements ExtensionPoint {
55
56 /** Regex pattern for splitting up the icon size string that is used in jelly pages. */
57 static Pattern iconSizeRegex = Pattern.compile("(\\d+)x(\\d+)");
58
59 /**
60 * Finds an avatar image URL string for a user.
61 *
62 * <p>
63 * This method is called when a web page is going to show an avatar for a {@link User}.
64 *
65 * <p>
66 * When multiple resolvers are installed, they are consulted in order and
67 * the search will be over when an avatar is found by someone.
68 *
69 * <p>
70 * Since {@link UserAvatarResolver} is singleton, this method can be invoked concurrently
71 * from multiple threads.
72 * @param u the user
73 * @param width the preferred width of the avatar
74 * @param height the preferred height of the avatar.
75 *
76 * @return null if the inference failed.
77 */
78 public abstract String findAvatarFor(User u, int width, int height);
79
80 /**
81 * Resolve an avatar image URL string for the user
82 * @param u user
83 * @param avatarSize the preferred image size, "[width]x[height]"
84 * @return a URL string for a user Avatar image.
85 */
86 public static String resolve(User u, String avatarSize) {
87
88 Matcher matcher = iconSizeRegex.matcher(avatarSize);
89 if (matcher.matches() && matcher.groupCount() == 2) {
90 int width = Integer.parseInt(matcher.group(1));
91 int height = Integer.parseInt(matcher.group(2));
92 for (UserAvatarResolver r : all()) {
93 String name = r.findAvatarFor(u, width, height);
94 if(name!=null) return name;
95 }
96 } else {
97 LOGGER.warning(String.format("Could not split up the avatar size (%s) into a width and height.", avatarSize));
98 }
99
100 return Jenkins.getInstance().getRootUrl() + Functions.getResourcePath() + "/images/" + avatarSize + "/user.png";
101 }
102
103 /**
104 * Returns all the registered {@link UserAvatarResolver} descriptors.
105 */
106 public static ExtensionList<UserAvatarResolver> all() {
107 return Jenkins.getInstance().getExtensionList(UserAvatarResolver.class);
108 }
109
110 private static final Logger LOGGER = Logger.getLogger(UserAvatarResolver.class.getName());
111 }
112