Line data Source code
1 : // Copyright (C) 2015 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.gpg.api; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : 19 : import com.google.gerrit.extensions.api.accounts.GpgKeyApi; 20 : import com.google.gerrit.extensions.common.GpgKeyInfo; 21 : import com.google.gerrit.extensions.common.Input; 22 : import com.google.gerrit.extensions.restapi.RestApiException; 23 : import com.google.gerrit.gpg.server.DeleteGpgKey; 24 : import com.google.gerrit.gpg.server.GpgKey; 25 : import com.google.gerrit.gpg.server.GpgKeys; 26 : import com.google.inject.Inject; 27 : import com.google.inject.assistedinject.Assisted; 28 : import java.io.IOException; 29 : import org.bouncycastle.openpgp.PGPException; 30 : import org.eclipse.jgit.errors.ConfigInvalidException; 31 : 32 : public class GpgKeyApiImpl implements GpgKeyApi { 33 : public interface Factory { 34 : GpgKeyApiImpl create(GpgKey rsrc); 35 : } 36 : 37 : private final GpgKeys.Get get; 38 : private final DeleteGpgKey delete; 39 : private final GpgKey rsrc; 40 : 41 : @Inject 42 1 : GpgKeyApiImpl(GpgKeys.Get get, DeleteGpgKey delete, @Assisted GpgKey rsrc) { 43 1 : this.get = get; 44 1 : this.delete = delete; 45 1 : this.rsrc = rsrc; 46 1 : } 47 : 48 : @Override 49 : public GpgKeyInfo get() throws RestApiException { 50 : try { 51 1 : return get.apply(rsrc).value(); 52 0 : } catch (Exception e) { 53 0 : throw asRestApiException("Cannot get GPG key", e); 54 : } 55 : } 56 : 57 : @Override 58 : public void delete() throws RestApiException { 59 : try { 60 1 : delete.apply(rsrc, new Input()); 61 0 : } catch (PGPException | IOException | ConfigInvalidException e) { 62 0 : throw asRestApiException("Cannot delete GPG key", e); 63 1 : } 64 1 : } 65 : }