Line data Source code
1 : // Copyright (C) 2012 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.change; 16 : 17 : import com.google.gerrit.entities.HumanComment; 18 : import com.google.gerrit.extensions.registration.DynamicMap; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.ChildCollection; 21 : import com.google.gerrit.extensions.restapi.IdString; 22 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 23 : import com.google.gerrit.extensions.restapi.RestView; 24 : import com.google.gerrit.server.CommentsUtil; 25 : import com.google.gerrit.server.CurrentUser; 26 : import com.google.gerrit.server.change.DraftCommentResource; 27 : import com.google.gerrit.server.change.RevisionResource; 28 : import com.google.inject.Inject; 29 : import com.google.inject.Provider; 30 : import com.google.inject.Singleton; 31 : 32 : @Singleton 33 : public class DraftComments implements ChildCollection<RevisionResource, DraftCommentResource> { 34 : private final DynamicMap<RestView<DraftCommentResource>> views; 35 : private final Provider<CurrentUser> user; 36 : private final ListRevisionDrafts list; 37 : private final CommentsUtil commentsUtil; 38 : 39 : @Inject 40 : DraftComments( 41 : DynamicMap<RestView<DraftCommentResource>> views, 42 : Provider<CurrentUser> user, 43 : ListRevisionDrafts list, 44 145 : CommentsUtil commentsUtil) { 45 145 : this.views = views; 46 145 : this.user = user; 47 145 : this.list = list; 48 145 : this.commentsUtil = commentsUtil; 49 145 : } 50 : 51 : @Override 52 : public DynamicMap<RestView<DraftCommentResource>> views() { 53 1 : return views; 54 : } 55 : 56 : @Override 57 : public ListRevisionDrafts list() throws AuthException { 58 1 : checkIdentifiedUser(); 59 1 : return list; 60 : } 61 : 62 : @Override 63 : public DraftCommentResource parse(RevisionResource rev, IdString id) 64 : throws ResourceNotFoundException, AuthException { 65 18 : checkIdentifiedUser(); 66 18 : String uuid = id.get(); 67 : for (HumanComment c : 68 18 : commentsUtil.draftByPatchSetAuthor( 69 18 : rev.getPatchSet().id(), rev.getAccountId(), rev.getNotes())) { 70 18 : if (uuid.equals(c.key.uuid)) { 71 18 : return new DraftCommentResource(rev, c); 72 : } 73 7 : } 74 0 : throw new ResourceNotFoundException(id); 75 : } 76 : 77 : private void checkIdentifiedUser() throws AuthException { 78 18 : if (!user.get().isIdentifiedUser()) { 79 0 : throw new AuthException("drafts only available to authenticated users"); 80 : } 81 18 : } 82 : }