Add plugin extension and servlet for avatar images
Avatar images are provided by a single plugin and requested from the /avatar/* URL. If no plugin is provided, a 404 is returned. Change-Id: Icfc35ce713b34381bb1afc3691dd7e84e54da86e
This commit is contained in:
committed by
Shawn Pearce
parent
30746a2981
commit
0694374875
@@ -18,6 +18,7 @@ import static com.google.inject.Scopes.SINGLETON;
|
|||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.gerrit.common.PageLinks;
|
import com.google.gerrit.common.PageLinks;
|
||||||
|
import com.google.gerrit.httpd.raw.AvatarServlet;
|
||||||
import com.google.gerrit.httpd.raw.CatServlet;
|
import com.google.gerrit.httpd.raw.CatServlet;
|
||||||
import com.google.gerrit.httpd.raw.HostPageServlet;
|
import com.google.gerrit.httpd.raw.HostPageServlet;
|
||||||
import com.google.gerrit.httpd.raw.LegacyGerritServlet;
|
import com.google.gerrit.httpd.raw.LegacyGerritServlet;
|
||||||
@@ -78,6 +79,7 @@ class UrlModule extends ServletModule {
|
|||||||
serve("/ssh_info").with(SshInfoServlet.class);
|
serve("/ssh_info").with(SshInfoServlet.class);
|
||||||
serve("/static/*").with(StaticServlet.class);
|
serve("/static/*").with(StaticServlet.class);
|
||||||
serve("/tools/*").with(ToolServlet.class);
|
serve("/tools/*").with(ToolServlet.class);
|
||||||
|
serve("/avatar/*").with(AvatarServlet.class);
|
||||||
|
|
||||||
serve("/Main.class").with(notFound());
|
serve("/Main.class").with(notFound());
|
||||||
serve("/com/google/gerrit/launcher/*").with(notFound());
|
serve("/com/google/gerrit/launcher/*").with(notFound());
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
// Copyright (C) 2013 The Android Open Source Project
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package com.google.gerrit.httpd.raw;
|
||||||
|
|
||||||
|
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||||
|
import com.google.gerrit.reviewdb.client.Account;
|
||||||
|
import com.google.gerrit.server.account.AccountResolver;
|
||||||
|
import com.google.gerrit.server.avatar.AvatarProvider;
|
||||||
|
import com.google.gwtorm.server.OrmException;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serves avatars based on the installed avatar plugin. If no plugin is
|
||||||
|
* installed, serves 404s.
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
public class AvatarServlet extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private static final Logger log = LoggerFactory
|
||||||
|
.getLogger(AvatarServlet.class);
|
||||||
|
|
||||||
|
private final AccountResolver accountResolver;
|
||||||
|
private final DynamicItem<AvatarProvider> avatarProviderItem;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
AvatarServlet(AccountResolver accountResolver,
|
||||||
|
DynamicItem<AvatarProvider> avatarProvider) {
|
||||||
|
this.accountResolver = accountResolver;
|
||||||
|
this.avatarProviderItem = avatarProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse rsp)
|
||||||
|
throws IOException {
|
||||||
|
AvatarProvider avatarProvider = avatarProviderItem.get();
|
||||||
|
if (avatarProvider == null) {
|
||||||
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String user = req.getPathInfo();
|
||||||
|
if (user == null) {
|
||||||
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Hack off leading '/'
|
||||||
|
user = user.substring(1);
|
||||||
|
|
||||||
|
final Account account;
|
||||||
|
try {
|
||||||
|
account = accountResolver.find(user);
|
||||||
|
} catch (OrmException e1) {
|
||||||
|
log.error("Exception while looking up avatar for user: " + user, e1);
|
||||||
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (account == null) {
|
||||||
|
// Account was not found.
|
||||||
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String size = req.getParameter("size");
|
||||||
|
int imageSize = 0;
|
||||||
|
if (size != null) {
|
||||||
|
try {
|
||||||
|
imageSize = Integer.parseInt(size);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// Ignore, keep size at 0.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final String url = avatarProvider.getUrl(account, imageSize);
|
||||||
|
if (url == null) {
|
||||||
|
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
rsp.sendRedirect(rsp.encodeRedirectURL(url));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
// Copyright (C) 2013 The Android Open Source Project
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package com.google.gerrit.server.avatar;
|
||||||
|
|
||||||
|
import com.google.gerrit.extensions.annotations.ExtensionPoint;
|
||||||
|
import com.google.gerrit.reviewdb.client.Account;
|
||||||
|
import com.google.gerrit.reviewdb.client.UserIdentity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide avatar URLs for specified user.
|
||||||
|
*
|
||||||
|
* Invoked by Gerrit when Avatar image requests are made.
|
||||||
|
*/
|
||||||
|
@ExtensionPoint
|
||||||
|
public interface AvatarProvider {
|
||||||
|
/**
|
||||||
|
* Get avatar URL.
|
||||||
|
*
|
||||||
|
* @param account The user for which to load an avatar image
|
||||||
|
* @param imageSize A requested image size, in pixels. An imageSize of 0
|
||||||
|
* indicates to use whatever default size the provider determines.
|
||||||
|
* AvatarProviders may ignore the requested image size. The web
|
||||||
|
* interface will resize any image to match imageSize, so ideally the
|
||||||
|
* provider should return an image sized correctly.
|
||||||
|
* @return a URL of an avatar image for the specified user. A return value of
|
||||||
|
* {@code null} is acceptable, and results in the server responding
|
||||||
|
* with a 404. This will hide the avatar image in the web UI.
|
||||||
|
*/
|
||||||
|
public String getUrl(Account account, int imageSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a URL for a user to modify their avatar image.
|
||||||
|
*
|
||||||
|
* @param user The user wishing to change their avatar image
|
||||||
|
* @return a URL the user should visit to modify their avatar, or null if
|
||||||
|
* modification is not possible.
|
||||||
|
*/
|
||||||
|
public String getChangeAvatarUrl(UserIdentity user);
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ import com.google.gerrit.audit.AuditModule;
|
|||||||
import com.google.gerrit.common.data.ApprovalTypes;
|
import com.google.gerrit.common.data.ApprovalTypes;
|
||||||
import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
|
import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
|
||||||
import com.google.gerrit.extensions.events.NewProjectCreatedListener;
|
import com.google.gerrit.extensions.events.NewProjectCreatedListener;
|
||||||
|
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||||
import com.google.gerrit.extensions.registration.DynamicMap;
|
import com.google.gerrit.extensions.registration.DynamicMap;
|
||||||
import com.google.gerrit.extensions.registration.DynamicSet;
|
import com.google.gerrit.extensions.registration.DynamicSet;
|
||||||
import com.google.gerrit.reviewdb.client.AuthType;
|
import com.google.gerrit.reviewdb.client.AuthType;
|
||||||
@@ -55,6 +56,7 @@ import com.google.gerrit.server.auth.AuthBackend;
|
|||||||
import com.google.gerrit.server.auth.InternalAuthBackend;
|
import com.google.gerrit.server.auth.InternalAuthBackend;
|
||||||
import com.google.gerrit.server.auth.UniversalAuthBackend;
|
import com.google.gerrit.server.auth.UniversalAuthBackend;
|
||||||
import com.google.gerrit.server.auth.ldap.LdapModule;
|
import com.google.gerrit.server.auth.ldap.LdapModule;
|
||||||
|
import com.google.gerrit.server.avatar.AvatarProvider;
|
||||||
import com.google.gerrit.server.cache.CacheRemovalListener;
|
import com.google.gerrit.server.cache.CacheRemovalListener;
|
||||||
import com.google.gerrit.server.events.EventFactory;
|
import com.google.gerrit.server.events.EventFactory;
|
||||||
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
|
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
|
||||||
@@ -209,10 +211,11 @@ public class GerritGlobalModule extends FactoryModule {
|
|||||||
DynamicSet.setOf(binder(), NewProjectCreatedListener.class);
|
DynamicSet.setOf(binder(), NewProjectCreatedListener.class);
|
||||||
DynamicSet.bind(binder(), GitReferenceUpdatedListener.class).to(ChangeCache.class);
|
DynamicSet.bind(binder(), GitReferenceUpdatedListener.class).to(ChangeCache.class);
|
||||||
DynamicSet.setOf(binder(), CommitValidationListener.class);
|
DynamicSet.setOf(binder(), CommitValidationListener.class);
|
||||||
factory(CommitValidators.Factory.class);
|
DynamicItem.itemOf(binder(), AvatarProvider.class);
|
||||||
|
|
||||||
bind(AnonymousUser.class);
|
bind(AnonymousUser.class);
|
||||||
|
|
||||||
|
factory(CommitValidators.Factory.class);
|
||||||
factory(NotesBranchUtil.Factory.class);
|
factory(NotesBranchUtil.Factory.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user