Rewrite /avatar/{id} as REST API /accounts/{id}/avatar
Implement avatar lookup as a REST API. GET on the /avatar URL will return the avatar as supplied by the avatar plugin. Change-Id: Ieb581558a8b8f7863dcb43ecea39b016dbdc696e
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.client;
|
||||
|
||||
import com.google.gerrit.client.rpc.RestApi;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gwt.event.dom.client.ErrorEvent;
|
||||
import com.google.gwt.event.dom.client.ErrorHandler;
|
||||
@@ -21,11 +22,8 @@ import com.google.gwt.user.client.ui.Image;
|
||||
|
||||
public class AvatarImage extends Image {
|
||||
|
||||
/**
|
||||
* An avatar image for the given account using a default size decided by the
|
||||
* avatar provider
|
||||
*/
|
||||
public AvatarImage(Account account) {
|
||||
/** A default sized avatar image. */
|
||||
public AvatarImage(Account.Id account) {
|
||||
this(account, 0);
|
||||
}
|
||||
|
||||
@@ -37,8 +35,8 @@ public class AvatarImage extends Image {
|
||||
* on the avatar provider. A size <= 0 indicates to let the provider
|
||||
* decide a default size.
|
||||
*/
|
||||
public AvatarImage(Account account, int size) {
|
||||
super("/avatar/" + account.getId() + (size > 0 ? ("?size=" + size) : ""));
|
||||
public AvatarImage(Account.Id account, int size) {
|
||||
super(url(account, size));
|
||||
|
||||
addErrorHandler(new ErrorHandler() {
|
||||
@Override
|
||||
@@ -49,4 +47,18 @@ public class AvatarImage extends Image {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static String url(Account.Id id, int size) {
|
||||
String u;
|
||||
if (Gerrit.isSignedIn() && id.equals(Gerrit.getUserAccount().getId())) {
|
||||
u = "self";
|
||||
} else {
|
||||
u = id.toString();
|
||||
}
|
||||
RestApi api = new RestApi("/accounts/" + u + "/avatar");
|
||||
if (size > 0) {
|
||||
api.addParameter("size", size);
|
||||
}
|
||||
return api.url();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class CurrentUserPopupPanel extends PluginSafePopupPanel {
|
||||
|
||||
public CurrentUserPopupPanel(Account account, boolean canLogOut) {
|
||||
super(/* auto hide */true, /* modal */false);
|
||||
avatar = new AvatarImage(account, 100);
|
||||
avatar = new AvatarImage(account.getId(), 100);
|
||||
setWidget(binder.createAndBindUi(this));
|
||||
// We must show and then hide this popup so that it is part of the DOM.
|
||||
// Otherwise the image does not get any events. Calling hide() would
|
||||
|
||||
@@ -274,10 +274,14 @@ public class RestApi {
|
||||
send(POST, cb);
|
||||
}
|
||||
|
||||
public String url() {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
public <T extends JavaScriptObject> void send(
|
||||
Method method,
|
||||
final AsyncCallback<T> cb) {
|
||||
RequestBuilder req = new RequestBuilder(method, url.toString());
|
||||
RequestBuilder req = new RequestBuilder(method, url());
|
||||
req.setHeader("Accept", JSON_TYPE);
|
||||
if (Gerrit.getAuthorization() != null) {
|
||||
req.setHeader("Authorization", Gerrit.getAuthorization());
|
||||
|
||||
@@ -18,7 +18,6 @@ import static com.google.inject.Scopes.SINGLETON;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
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.HostPageServlet;
|
||||
import com.google.gerrit.httpd.raw.LegacyGerritServlet;
|
||||
@@ -79,7 +78,6 @@ class UrlModule extends ServletModule {
|
||||
serve("/ssh_info").with(SshInfoServlet.class);
|
||||
serve("/static/*").with(StaticServlet.class);
|
||||
serve("/tools/*").with(ToolServlet.class);
|
||||
serve("/avatar/*").with(AvatarServlet.class);
|
||||
|
||||
serve("/Main.class").with(notFound());
|
||||
serve("/com/google/gerrit/launcher/*").with(notFound());
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
// 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,53 @@
|
||||
// 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.account;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.gerrit.extensions.registration.DynamicItem;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.gerrit.server.avatar.AvatarProvider;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.kohsuke.args4j.Option;
|
||||
|
||||
class GetAvatar implements RestReadView<AccountResource> {
|
||||
private final DynamicItem<AvatarProvider> avatarProvider;
|
||||
|
||||
@Option(name = "--size", usage = "size in pixels (height and width)")
|
||||
private int size;
|
||||
|
||||
@Inject
|
||||
GetAvatar(DynamicItem<AvatarProvider> avatarProvider) {
|
||||
this.avatarProvider = avatarProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response.Redirect apply(AccountResource rsrc)
|
||||
throws ResourceNotFoundException {
|
||||
AvatarProvider impl = avatarProvider.get();
|
||||
if (impl == null) {
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
String url = impl.getUrl(rsrc.getUser().getAccount(), size);
|
||||
if (Strings.isNullOrEmpty(url)) {
|
||||
throw new ResourceNotFoundException();
|
||||
} else {
|
||||
return Response.redirect(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ public class Module extends RestApiModule {
|
||||
DynamicMap.mapOf(binder(), ACCOUNT_KIND);
|
||||
DynamicMap.mapOf(binder(), CAPABILITY_KIND);
|
||||
|
||||
get(ACCOUNT_KIND, "avatar").to(GetAvatar.class);
|
||||
child(ACCOUNT_KIND, "capabilities").to(Capabilities.class);
|
||||
get(ACCOUNT_KIND, "groups").to(GetGroups.class);
|
||||
get(CAPABILITY_KIND).to(GetCapabilities.CheckOne.class);
|
||||
|
||||
@@ -19,7 +19,6 @@ import static com.google.inject.Scopes.SINGLETON;
|
||||
import com.google.gerrit.server.ApprovalsUtil;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.RequestCleanup;
|
||||
import com.google.gerrit.server.account.AccountControl;
|
||||
import com.google.gerrit.server.account.GroupMembers;
|
||||
import com.google.gerrit.server.account.PerformCreateGroup;
|
||||
import com.google.gerrit.server.account.PerformRenameGroup;
|
||||
|
||||
Reference in New Issue
Block a user