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.restapi.config; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.collect.Multimap; 20 : import com.google.gerrit.extensions.api.config.ConfigUpdateEntryInfo; 21 : import com.google.gerrit.extensions.common.Input; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import com.google.gerrit.extensions.restapi.RestModifyView; 25 : import com.google.gerrit.server.config.ConfigResource; 26 : import com.google.gerrit.server.config.ConfigUpdatedEvent.ConfigUpdateEntry; 27 : import com.google.gerrit.server.config.ConfigUpdatedEvent.UpdateResult; 28 : import com.google.gerrit.server.config.GerritServerConfigReloader; 29 : import com.google.gerrit.server.permissions.GlobalPermission; 30 : import com.google.gerrit.server.permissions.PermissionBackend; 31 : import com.google.gerrit.server.permissions.PermissionBackendException; 32 : import com.google.inject.Inject; 33 : import java.util.Collection; 34 : import java.util.Collections; 35 : import java.util.List; 36 : import java.util.Map; 37 : import java.util.stream.Collectors; 38 : 39 : public class ReloadConfig implements RestModifyView<ConfigResource, Input> { 40 : 41 : private GerritServerConfigReloader config; 42 : private PermissionBackend permissions; 43 : 44 : @Inject 45 1 : ReloadConfig(GerritServerConfigReloader config, PermissionBackend permissions) { 46 1 : this.config = config; 47 1 : this.permissions = permissions; 48 1 : } 49 : 50 : @Override 51 : public Response<Map<String, List<ConfigUpdateEntryInfo>>> apply( 52 : ConfigResource resource, Input input) throws RestApiException, PermissionBackendException { 53 1 : permissions.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER); 54 1 : Multimap<UpdateResult, ConfigUpdateEntry> updates = config.reloadConfig(); 55 1 : if (updates.isEmpty()) { 56 1 : return Response.ok(Collections.emptyMap()); 57 : } 58 0 : return Response.ok( 59 0 : updates.asMap().entrySet().stream() 60 0 : .collect( 61 0 : Collectors.toMap( 62 0 : e -> e.getKey().name().toLowerCase(), e -> toEntryInfos(e.getValue())))); 63 : } 64 : 65 : private static List<ConfigUpdateEntryInfo> toEntryInfos( 66 : Collection<ConfigUpdateEntry> updateEntries) { 67 0 : return updateEntries.stream() 68 0 : .map(ReloadConfig::toConfigUpdateEntryInfo) 69 0 : .collect(toImmutableList()); 70 : } 71 : 72 : private static ConfigUpdateEntryInfo toConfigUpdateEntryInfo(ConfigUpdateEntry e) { 73 0 : ConfigUpdateEntryInfo uei = new ConfigUpdateEntryInfo(); 74 0 : uei.configKey = e.key.toString(); 75 0 : uei.oldValue = e.oldVal; 76 0 : uei.newValue = e.newVal; 77 0 : return uei; 78 : } 79 : }