Line data Source code
1 : // Copyright (C) 2013 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.group; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.entities.AccountGroup; 19 : import com.google.gerrit.entities.GroupDescription; 20 : import com.google.gerrit.exceptions.NoSuchGroupException; 21 : import com.google.gerrit.extensions.common.DescriptionInput; 22 : import com.google.gerrit.extensions.restapi.AuthException; 23 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestModifyView; 26 : import com.google.gerrit.server.UserInitiated; 27 : import com.google.gerrit.server.group.GroupResource; 28 : import com.google.gerrit.server.group.db.GroupDelta; 29 : import com.google.gerrit.server.group.db.GroupsUpdate; 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.util.Objects; 35 : import org.eclipse.jgit.errors.ConfigInvalidException; 36 : 37 : @Singleton 38 : public class PutDescription implements RestModifyView<GroupResource, DescriptionInput> { 39 : private final Provider<GroupsUpdate> groupsUpdateProvider; 40 : 41 : @Inject 42 146 : PutDescription(@UserInitiated Provider<GroupsUpdate> groupsUpdateProvider) { 43 146 : this.groupsUpdateProvider = groupsUpdateProvider; 44 146 : } 45 : 46 : @Override 47 : public Response<String> apply(GroupResource resource, DescriptionInput input) 48 : throws AuthException, NotInternalGroupException, ResourceNotFoundException, IOException, 49 : ConfigInvalidException { 50 4 : if (input == null) { 51 1 : input = new DescriptionInput(); // Delete would set description to null. 52 : } 53 : 54 4 : GroupDescription.Internal internalGroup = 55 4 : resource.asInternalGroup().orElseThrow(NotInternalGroupException::new); 56 4 : if (!resource.getControl().isOwner()) { 57 0 : throw new AuthException("Not group owner"); 58 : } 59 : 60 4 : String currentDescription = Strings.nullToEmpty(internalGroup.getDescription()); 61 4 : String newDescription = Strings.nullToEmpty(input.description); 62 4 : if (!Objects.equals(currentDescription, newDescription)) { 63 3 : AccountGroup.UUID groupUuid = internalGroup.getGroupUUID(); 64 3 : GroupDelta groupDelta = GroupDelta.builder().setDescription(newDescription).build(); 65 : try { 66 3 : groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta); 67 0 : } catch (NoSuchGroupException e) { 68 0 : throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e); 69 3 : } 70 : } 71 : 72 4 : return Strings.isNullOrEmpty(input.description) 73 2 : ? Response.none() 74 3 : : Response.ok(input.description); 75 : } 76 : }