Line data Source code
1 : // Copyright (C) 2012 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.gerrit.extensions.registration.DynamicMap; 18 : import com.google.gerrit.extensions.restapi.AuthException; 19 : import com.google.gerrit.extensions.restapi.IdString; 20 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 21 : import com.google.gerrit.extensions.restapi.RestCollection; 22 : import com.google.gerrit.extensions.restapi.RestView; 23 : import com.google.gerrit.extensions.restapi.TopLevelResource; 24 : import com.google.gerrit.server.account.AccountResolver; 25 : import com.google.gerrit.server.account.AccountResolver.UnresolvableAccountException; 26 : import com.google.gerrit.server.account.AccountResource; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Provider; 29 : import com.google.inject.Singleton; 30 : import java.io.IOException; 31 : import org.eclipse.jgit.errors.ConfigInvalidException; 32 : 33 : @Singleton 34 : public class AccountsCollection implements RestCollection<TopLevelResource, AccountResource> { 35 : private final AccountResolver accountResolver; 36 : private final Provider<QueryAccounts> list; 37 : private final DynamicMap<RestView<AccountResource>> views; 38 : 39 : @Inject 40 : public AccountsCollection( 41 : AccountResolver accountResolver, 42 : Provider<QueryAccounts> list, 43 149 : DynamicMap<RestView<AccountResource>> views) { 44 149 : this.accountResolver = accountResolver; 45 149 : this.list = list; 46 149 : this.views = views; 47 149 : } 48 : 49 : @Override 50 : public AccountResource parse(TopLevelResource root, IdString id) 51 : throws ResourceNotFoundException, AuthException, IOException, ConfigInvalidException { 52 : try { 53 32 : return new AccountResource(accountResolver.resolve(id.get()).asUniqueUser()); 54 11 : } catch (UnresolvableAccountException e) { 55 11 : if (e.isSelf()) { 56 : // Must be authenticated to use 'me' or 'self'. 57 1 : throw new AuthException(e.getMessage(), e); 58 : } 59 11 : throw new ResourceNotFoundException(e.getMessage(), e); 60 : } 61 : } 62 : 63 : @Override 64 : public RestView<TopLevelResource> list() throws ResourceNotFoundException { 65 2 : return list.get(); 66 : } 67 : 68 : @Override 69 : public DynamicMap<RestView<AccountResource>> views() { 70 6 : return views; 71 : } 72 : }