Line data Source code
1 : // Copyright (C) 2014 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.GeneralPreferencesInfo; 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 SetPreferences implements RestModifyView<ConfigResource, GeneralPreferencesInfo> { 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 149 : SetPreferences(Provider<MetaDataUpdate.User> metaDataUpdateFactory, AllUsersName allUsersName) { 47 149 : this.metaDataUpdateFactory = metaDataUpdateFactory; 48 149 : this.allUsersName = allUsersName; 49 149 : } 50 : 51 : @Override 52 : public Response<GeneralPreferencesInfo> apply(ConfigResource rsrc, GeneralPreferencesInfo input) 53 : throws BadRequestException, IOException, ConfigInvalidException { 54 3 : if (!hasSetFields(input)) { 55 1 : throw new BadRequestException("unsupported option"); 56 : } 57 2 : StoredPreferences.validateMy(input.my); 58 2 : try (MetaDataUpdate md = metaDataUpdateFactory.get().create(allUsersName)) { 59 2 : GeneralPreferencesInfo updatedPrefs = 60 2 : StoredPreferences.updateDefaultGeneralPreferences(md, input); 61 2 : return Response.ok(updatedPrefs); 62 : } 63 : } 64 : 65 : private static boolean hasSetFields(GeneralPreferencesInfo in) { 66 : try { 67 3 : for (Field field : in.getClass().getDeclaredFields()) { 68 3 : if (skipField(field)) { 69 3 : continue; 70 : } 71 3 : if (field.get(in) != null) { 72 2 : return true; 73 : } 74 : } 75 0 : } catch (IllegalAccessException e) { 76 0 : logger.atSevere().withCause(e).log("Unable to verify input"); 77 1 : } 78 1 : return false; 79 : } 80 : }