Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.account; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.extensions.registration.DynamicItem; 19 : import com.google.gerrit.extensions.restapi.CacheControl; 20 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestReadView; 23 : import com.google.gerrit.server.account.AccountResource; 24 : import com.google.gerrit.server.avatar.AvatarProvider; 25 : import com.google.inject.Inject; 26 : import java.util.concurrent.TimeUnit; 27 : import org.kohsuke.args4j.Option; 28 : 29 : /** 30 : * REST endpoint to get the avatar image of an account. 31 : * 32 : * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/avatar} requests. 33 : * 34 : * <p>Avatar images are only available if an {@link AvatarProvider} plugin is installed. 35 : */ 36 : public class GetAvatar implements RestReadView<AccountResource> { 37 : private final DynamicItem<AvatarProvider> avatarProvider; 38 : 39 : private int size; 40 : 41 : @Option( 42 : name = "--size", 43 : aliases = {"-s"}, 44 : usage = "recommended size in pixels, height and width") 45 : public void setSize(int s) { 46 0 : size = s; 47 0 : } 48 : 49 : @Inject 50 32 : GetAvatar(DynamicItem<AvatarProvider> avatarProvider) { 51 32 : this.avatarProvider = avatarProvider; 52 32 : } 53 : 54 : @Override 55 : public Response.Redirect apply(AccountResource rsrc) throws ResourceNotFoundException { 56 0 : AvatarProvider impl = avatarProvider.get(); 57 0 : if (impl == null) { 58 0 : throw new ResourceNotFoundException().caching(CacheControl.PUBLIC(1, TimeUnit.DAYS)); 59 : } 60 : 61 0 : String url = impl.getUrl(rsrc.getUser(), size); 62 0 : if (Strings.isNullOrEmpty(url)) { 63 0 : throw new ResourceNotFoundException().caching(CacheControl.PUBLIC(1, TimeUnit.HOURS)); 64 : } 65 0 : return Response.redirect(url); 66 : } 67 : }