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.change; 16 : 17 : import com.google.gerrit.entities.Account; 18 : import com.google.gerrit.entities.Address; 19 : import com.google.gerrit.extensions.registration.DynamicMap; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import com.google.gerrit.extensions.restapi.ChildCollection; 22 : import com.google.gerrit.extensions.restapi.IdString; 23 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 24 : import com.google.gerrit.extensions.restapi.RestView; 25 : import com.google.gerrit.server.account.AccountResolver; 26 : import com.google.gerrit.server.approval.ApprovalsUtil; 27 : import com.google.gerrit.server.change.ChangeResource; 28 : import com.google.gerrit.server.change.ReviewerResource; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Singleton; 31 : import java.io.IOException; 32 : import java.util.Collection; 33 : import org.eclipse.jgit.errors.ConfigInvalidException; 34 : 35 : @Singleton 36 : public class Reviewers implements ChildCollection<ChangeResource, ReviewerResource> { 37 : private final DynamicMap<RestView<ReviewerResource>> views; 38 : private final ApprovalsUtil approvalsUtil; 39 : private final ReviewerResource.Factory resourceFactory; 40 : private final ListReviewers list; 41 : private final AccountResolver accountResolver; 42 : 43 : @Inject 44 : Reviewers( 45 : ApprovalsUtil approvalsUtil, 46 : ReviewerResource.Factory resourceFactory, 47 : DynamicMap<RestView<ReviewerResource>> views, 48 : ListReviewers list, 49 145 : AccountResolver accountResolver) { 50 145 : this.approvalsUtil = approvalsUtil; 51 145 : this.resourceFactory = resourceFactory; 52 145 : this.views = views; 53 145 : this.list = list; 54 145 : this.accountResolver = accountResolver; 55 145 : } 56 : 57 : @Override 58 : public DynamicMap<RestView<ReviewerResource>> views() { 59 2 : return views; 60 : } 61 : 62 : @Override 63 : public RestView<ChangeResource> list() { 64 2 : return list; 65 : } 66 : 67 : @Override 68 : public ReviewerResource parse(ChangeResource rsrc, IdString id) 69 : throws ResourceNotFoundException, AuthException, IOException, ConfigInvalidException { 70 : try { 71 : 72 16 : AccountResolver.Result result = accountResolver.resolveIgnoreVisibility(id.get()); 73 16 : if (fetchAccountIds(rsrc).contains(result.asUniqueUser().getAccountId())) { 74 16 : return resourceFactory.create(rsrc, result.asUniqueUser().getAccountId()); 75 : } 76 7 : } catch (AccountResolver.UnresolvableAccountException e) { 77 7 : if (e.isSelf()) { 78 0 : throw new AuthException(e.getMessage(), e); 79 : } 80 1 : } 81 7 : Address address = Address.tryParse(id.get()); 82 7 : if (address != null && rsrc.getNotes().getReviewersByEmail().all().contains(address)) { 83 7 : return new ReviewerResource(rsrc, address); 84 : } 85 : 86 1 : throw new ResourceNotFoundException(id); 87 : } 88 : 89 : private Collection<Account.Id> fetchAccountIds(ChangeResource rsrc) { 90 16 : return approvalsUtil.getReviewers(rsrc.getNotes()).all(); 91 : } 92 : }