Move remaining REST handlers into restapi subpackage

This moves most Collections and all RestViews for
{change,group,account,access} into server/restapi/ . These have to be
done a single change and end up in a single java_library as these four
groups use each other's code.

Resources are left in place, to maintain compatibility for plugins.

Some Json classes (ActionJson, ChangeJson, GroupJson) have many
dependencies on the server library, so they are left in the server
package.

Revisisons (the Collection for RevisionResource) is left in place, as
it is used in ActionJson

This reduces the giant server package from ~1200 to ~900 files.

Change-Id: Id0d9030ad2845d636875e6139a08243f8b624338
This commit is contained in:
Han-Wen Nienhuys
2017-12-21 15:34:39 +01:00
parent 7a223792d9
commit 36d5aac207
361 changed files with 1229 additions and 747 deletions

View File

@@ -0,0 +1,97 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.restapi.project;
import static java.util.stream.Collectors.toList;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.permissions.ProjectPermission;
import com.google.gerrit.server.project.ChildProjects;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectJson;
import com.google.gerrit.server.project.ProjectResource;
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.kohsuke.args4j.Option;
public class ListChildProjects implements RestReadView<ProjectResource> {
@Option(name = "--recursive", usage = "to list child projects recursively")
private boolean recursive;
private final ProjectCache projectCache;
private final PermissionBackend permissionBackend;
private final Provider<CurrentUser> user;
private final AllProjectsName allProjects;
private final ProjectJson json;
private final ChildProjects childProjects;
@Inject
ListChildProjects(
ProjectCache projectCache,
PermissionBackend permissionBackend,
Provider<CurrentUser> user,
AllProjectsName allProjectsName,
ProjectJson json,
ChildProjects childProjects) {
this.projectCache = projectCache;
this.permissionBackend = permissionBackend;
this.user = user;
this.allProjects = allProjectsName;
this.json = json;
this.childProjects = childProjects;
}
public void setRecursive(boolean recursive) {
this.recursive = recursive;
}
@Override
public List<ProjectInfo> apply(ProjectResource rsrc) throws PermissionBackendException {
if (recursive) {
return childProjects.list(rsrc.getNameKey());
}
return directChildProjects(rsrc.getNameKey());
}
private List<ProjectInfo> directChildProjects(Project.NameKey parent)
throws PermissionBackendException {
Map<Project.NameKey, Project> children = new HashMap<>();
for (Project.NameKey name : projectCache.all()) {
ProjectState c = projectCache.get(name);
if (c != null && parent.equals(c.getProject().getParent(allProjects))) {
children.put(c.getNameKey(), c.getProject());
}
}
return permissionBackend
.user(user)
.filter(ProjectPermission.ACCESS, children.keySet())
.stream()
.sorted()
.map((p) -> json.format(children.get(p)))
.collect(toList());
}
}