Line data Source code
1 : // Copyright (C) 2016 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.gerrit.server.config.ConfigUtil.skipField; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.common.data.GlobalCapability; 21 : import com.google.gerrit.extensions.annotations.RequiresCapability; 22 : import com.google.gerrit.extensions.client.DiffPreferencesInfo; 23 : import com.google.gerrit.extensions.restapi.BadRequestException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestModifyView; 26 : import com.google.gerrit.server.account.StoredPreferences; 27 : import com.google.gerrit.server.config.AllUsersName; 28 : import com.google.gerrit.server.config.ConfigResource; 29 : import com.google.gerrit.server.git.meta.MetaDataUpdate; 30 : import com.google.inject.Inject; 31 : import com.google.inject.Provider; 32 : import com.google.inject.Singleton; 33 : import java.io.IOException; 34 : import java.lang.reflect.Field; 35 : import org.eclipse.jgit.errors.ConfigInvalidException; 36 : 37 : @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) 38 : @Singleton 39 : public class SetDiffPreferences implements RestModifyView<ConfigResource, DiffPreferencesInfo> { 40 149 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 41 : 42 : private final Provider<MetaDataUpdate.User> metaDataUpdateFactory; 43 : private final AllUsersName allUsersName; 44 : 45 : @Inject 46 : SetDiffPreferences( 47 149 : Provider<MetaDataUpdate.User> metaDataUpdateFactory, AllUsersName allUsersName) { 48 149 : this.metaDataUpdateFactory = metaDataUpdateFactory; 49 149 : this.allUsersName = allUsersName; 50 149 : } 51 : 52 : @Override 53 : public Response<DiffPreferencesInfo> apply( 54 : ConfigResource configResource, DiffPreferencesInfo input) 55 : throws BadRequestException, IOException, ConfigInvalidException { 56 3 : if (input == null) { 57 0 : throw new BadRequestException("input must be provided"); 58 : } 59 3 : if (!hasSetFields(input)) { 60 1 : throw new BadRequestException("unsupported option"); 61 : } 62 : 63 2 : try (MetaDataUpdate md = metaDataUpdateFactory.get().create(allUsersName)) { 64 2 : DiffPreferencesInfo updatedPrefs = StoredPreferences.updateDefaultDiffPreferences(md, input); 65 2 : return Response.ok(updatedPrefs); 66 : } 67 : } 68 : 69 : private static boolean hasSetFields(DiffPreferencesInfo in) { 70 : try { 71 3 : for (Field field : in.getClass().getDeclaredFields()) { 72 3 : if (skipField(field)) { 73 3 : continue; 74 : } 75 3 : if (field.get(in) != null) { 76 2 : return true; 77 : } 78 : } 79 0 : } catch (IllegalAccessException e) { 80 0 : logger.atWarning().withCause(e).log("Unable to verify input"); 81 1 : } 82 1 : return false; 83 : } 84 : }