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.gpg.server.GpgKey.GPG_KEY_KIND; 18 : import static com.google.gerrit.server.account.AccountResource.ACCOUNT_KIND; 19 : 20 : import com.google.gerrit.extensions.api.accounts.GpgKeyApi; 21 : import com.google.gerrit.extensions.common.GpgKeyInfo; 22 : import com.google.gerrit.extensions.common.PushCertificateInfo; 23 : import com.google.gerrit.extensions.registration.DynamicMap; 24 : import com.google.gerrit.extensions.restapi.IdString; 25 : import com.google.gerrit.extensions.restapi.NotImplementedException; 26 : import com.google.gerrit.extensions.restapi.RestApiModule; 27 : import com.google.gerrit.gpg.server.DeleteGpgKey; 28 : import com.google.gerrit.gpg.server.GpgKeys; 29 : import com.google.gerrit.gpg.server.PostGpgKeys; 30 : import com.google.gerrit.server.IdentifiedUser; 31 : import com.google.gerrit.server.account.AccountResource; 32 : import com.google.gerrit.server.account.GpgApiAdapter; 33 : import java.util.List; 34 : import java.util.Map; 35 : 36 : public class GpgApiModule extends RestApiModule { 37 : private final boolean enabled; 38 : 39 152 : public GpgApiModule(boolean enabled) { 40 152 : this.enabled = enabled; 41 152 : } 42 : 43 : @Override 44 : protected void configure() { 45 152 : if (!enabled) { 46 151 : bind(GpgApiAdapter.class).to(NoGpgApi.class); 47 151 : return; 48 : } 49 7 : bind(GpgApiAdapter.class).to(GpgApiAdapterImpl.class); 50 7 : factory(GpgKeyApiImpl.Factory.class); 51 : 52 7 : DynamicMap.mapOf(binder(), GPG_KEY_KIND); 53 : 54 7 : child(ACCOUNT_KIND, "gpgkeys").to(GpgKeys.class); 55 7 : post(ACCOUNT_KIND, "gpgkeys").to(PostGpgKeys.class); 56 7 : get(GPG_KEY_KIND).to(GpgKeys.Get.class); 57 7 : delete(GPG_KEY_KIND).to(DeleteGpgKey.class); 58 7 : } 59 : 60 : private static class NoGpgApi implements GpgApiAdapter { 61 : private static final String MSG = "GPG key APIs disabled"; 62 : 63 : @Override 64 : public boolean isEnabled() { 65 102 : return false; 66 : } 67 : 68 : @Override 69 : public Map<String, GpgKeyInfo> listGpgKeys(AccountResource account) { 70 0 : throw new NotImplementedException(MSG); 71 : } 72 : 73 : @Override 74 : public Map<String, GpgKeyInfo> putGpgKeys( 75 : AccountResource account, List<String> add, List<String> delete) { 76 0 : throw new NotImplementedException(MSG); 77 : } 78 : 79 : @Override 80 : public GpgKeyApi gpgKey(AccountResource account, IdString idStr) { 81 0 : throw new NotImplementedException(MSG); 82 : } 83 : 84 : @Override 85 : public PushCertificateInfo checkPushCertificate(String certStr, IdentifiedUser expectedUser) { 86 0 : throw new NotImplementedException(MSG); 87 : } 88 : } 89 : }