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.project; 16 : 17 : import com.google.gerrit.extensions.registration.DynamicMap; 18 : import com.google.gerrit.extensions.restapi.AuthException; 19 : import com.google.gerrit.extensions.restapi.ChildCollection; 20 : import com.google.gerrit.extensions.restapi.IdString; 21 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 22 : import com.google.gerrit.extensions.restapi.RestApiException; 23 : import com.google.gerrit.extensions.restapi.RestView; 24 : import com.google.gerrit.extensions.restapi.TopLevelResource; 25 : import com.google.gerrit.server.permissions.PermissionBackendException; 26 : import com.google.gerrit.server.project.ChildProjectResource; 27 : import com.google.gerrit.server.project.ProjectResource; 28 : import com.google.gerrit.server.project.ProjectState; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Provider; 31 : import com.google.inject.Singleton; 32 : import java.io.IOException; 33 : 34 : @Singleton 35 : public class ChildProjectsCollection 36 : implements ChildCollection<ProjectResource, ChildProjectResource> { 37 : private final Provider<ListChildProjects> list; 38 : private final ProjectsCollection projectsCollection; 39 : private final DynamicMap<RestView<ChildProjectResource>> views; 40 : 41 : @Inject 42 : ChildProjectsCollection( 43 : Provider<ListChildProjects> list, 44 : ProjectsCollection projectsCollection, 45 146 : DynamicMap<RestView<ChildProjectResource>> views) { 46 146 : this.list = list; 47 146 : this.projectsCollection = projectsCollection; 48 146 : this.views = views; 49 146 : } 50 : 51 : @Override 52 : public ListChildProjects list() throws ResourceNotFoundException, AuthException { 53 2 : return list.get(); 54 : } 55 : 56 : @Override 57 : public ChildProjectResource parse(ProjectResource parent, IdString id) 58 : throws RestApiException, IOException, PermissionBackendException { 59 2 : parent.getProjectState().checkStatePermitsRead(); 60 2 : ProjectResource p = projectsCollection.parse(TopLevelResource.INSTANCE, id); 61 2 : for (ProjectState pp : p.getProjectState().parents()) { 62 2 : if (parent.getNameKey().equals(pp.getProject().getNameKey())) { 63 2 : return new ChildProjectResource(parent, p.getProjectState()); 64 : } 65 1 : } 66 1 : throw new ResourceNotFoundException(id); 67 : } 68 : 69 : @Override 70 : public DynamicMap<RestView<ChildProjectResource>> views() { 71 1 : return views; 72 : } 73 : }