Line data Source code
1 : // Copyright (C) 2018 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.api.accounts; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : 19 : import com.google.gerrit.extensions.api.accounts.EmailApi; 20 : import com.google.gerrit.extensions.common.EmailInfo; 21 : import com.google.gerrit.extensions.common.Input; 22 : import com.google.gerrit.extensions.restapi.IdString; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import com.google.gerrit.server.account.AccountResource; 25 : import com.google.gerrit.server.permissions.PermissionBackendException; 26 : import com.google.gerrit.server.restapi.account.DeleteEmail; 27 : import com.google.gerrit.server.restapi.account.EmailsCollection; 28 : import com.google.gerrit.server.restapi.account.GetEmail; 29 : import com.google.gerrit.server.restapi.account.PutPreferred; 30 : import com.google.inject.Inject; 31 : import com.google.inject.assistedinject.Assisted; 32 : 33 : public class EmailApiImpl implements EmailApi { 34 : interface Factory { 35 : EmailApiImpl create(AccountResource account, String email); 36 : } 37 : 38 : private final EmailsCollection emails; 39 : private final GetEmail get; 40 : private final DeleteEmail delete; 41 : private final PutPreferred putPreferred; 42 : private final AccountResource account; 43 : private final String email; 44 : 45 : @Inject 46 : EmailApiImpl( 47 : EmailsCollection emails, 48 : GetEmail get, 49 : DeleteEmail delete, 50 : PutPreferred putPreferred, 51 : @Assisted AccountResource account, 52 1 : @Assisted String email) { 53 1 : this.emails = emails; 54 1 : this.get = get; 55 1 : this.delete = delete; 56 1 : this.putPreferred = putPreferred; 57 1 : this.account = account; 58 1 : this.email = email; 59 1 : } 60 : 61 : @Override 62 : public EmailInfo get() throws RestApiException { 63 : try { 64 1 : return get.apply(resource()).value(); 65 1 : } catch (Exception e) { 66 1 : throw asRestApiException("Cannot read email", e); 67 : } 68 : } 69 : 70 : @Override 71 : public void delete() throws RestApiException { 72 : try { 73 1 : delete.apply(resource(), new Input()); 74 0 : } catch (Exception e) { 75 0 : throw asRestApiException("Cannot delete email", e); 76 1 : } 77 1 : } 78 : 79 : @Override 80 : public void setPreferred() throws RestApiException { 81 : try { 82 1 : putPreferred.apply(resource(), new Input()); 83 1 : } catch (Exception e) { 84 1 : throw asRestApiException(String.format("Cannot set %s as preferred email", email), e); 85 1 : } 86 1 : } 87 : 88 : private AccountResource.Email resource() throws RestApiException, PermissionBackendException { 89 1 : return emails.parse(account, IdString.fromDecoded(email)); 90 : } 91 : }