The Unified and Autonomous Development Platform
Refactoring restful API
robin shen0dcd8b7a3d67
diff --git a/core/src/main/java/com/gitplex/server/model/User.java b/core/src/main/java/com/gitplex/server/model/User.javaindex 3b66bcf465..4a615fe4c7 100644--- a/core/src/main/java/com/gitplex/server/model/User.java+++ b/core/src/main/java/com/gitplex/server/model/User.java@@ -116,7 +116,7 @@ public class User extends AbstractEntity implements AuthenticationInfo {return new Subject.Builder(securityManager).principals(asPrincipal(userId)).buildSubject();}- @Editable(name="User Name", order=100)+ @Editable(name="Login Name", order=100)@UserName@NotEmptypublic String getName() {diff --git a/rest/src/main/java/com/gitplex/server/rest/ProjectResource.java b/rest/src/main/java/com/gitplex/server/rest/ProjectResource.javaindex f9bce53ead..011e7dab2d 100644--- a/rest/src/main/java/com/gitplex/server/rest/ProjectResource.java+++ b/rest/src/main/java/com/gitplex/server/rest/ProjectResource.java@@ -1,5 +1,8 @@package com.gitplex.server.rest;+import java.util.ArrayList;+import java.util.Collection;+import javax.inject.Inject;import javax.inject.Singleton;import javax.ws.rs.Consumes;@@ -11,9 +14,11 @@ import javax.ws.rs.QueryParam;import javax.ws.rs.core.MediaType;import org.apache.shiro.authz.UnauthorizedException;+import org.hibernate.criterion.Restrictions;import com.gitplex.server.manager.ProjectManager;import com.gitplex.server.model.Project;+import com.gitplex.server.persistence.dao.EntityCriteria;import com.gitplex.server.rest.jersey.ValidQueryParams;import com.gitplex.server.security.SecurityUtils;@@ -30,24 +35,31 @@ public class ProjectResource {this.projectManager = projectManager;}- @Path("/{id}")+ @ValidQueryParams+ @GET+ public Collection<Project> query(@QueryParam("name") String projectName) {+ EntityCriteria<Project> criteria = projectManager.newCriteria();+ if (projectName != null)+ criteria.add(Restrictions.eq("name", projectName));++ Collection<Project> projects = new ArrayList<>();++ for (Project project: projectManager.findAll(criteria)) {+ if (SecurityUtils.canRead(project))+ projects.add(project);+ }++ return projects;+ }++ @Path("/{projectId}")@GET- public Project get(@PathParam("id") Long id) {- Project project = projectManager.load(id);-+ public Project get(@PathParam("projectId") Long projectId) {+ Project project = projectManager.load(projectId);if (!SecurityUtils.canRead(project))throw new UnauthorizedException("Unauthorized access to project " + project.getName());elsereturn project;}-- @ValidQueryParams- @GET- public Project query(@QueryParam("name") String name) {- Project project = projectManager.find(name);- if (project != null && !SecurityUtils.canRead(project))- throw new UnauthorizedException("Unauthorized access to project " + project.getName());- return project;- }-+}diff --git a/rest/src/main/java/com/gitplex/server/rest/PullRequestResource.java b/rest/src/main/java/com/gitplex/server/rest/PullRequestResource.javaindex df22aea6b0..6d1d6e2be5 100644--- a/rest/src/main/java/com/gitplex/server/rest/PullRequestResource.java+++ b/rest/src/main/java/com/gitplex/server/rest/PullRequestResource.java@@ -1,8 +1,8 @@package com.gitplex.server.rest;+import java.util.ArrayList;import java.util.Collection;import java.util.Date;-import java.util.List;import javax.inject.Inject;import javax.inject.Singleton;@@ -23,7 +23,7 @@ import com.gitplex.server.persistence.dao.EntityCriteria;import com.gitplex.server.rest.jersey.ValidQueryParams;import com.gitplex.server.security.SecurityUtils;-@Path("/pullrequests")+@Path("/pulls")@Consumes(MediaType.WILDCARD)@Produces(MediaType.APPLICATION_JSON)@Singleton@@ -40,24 +40,23 @@ public class PullRequestResource {this.pullRequestManager = pullRequestManager;}+ @Path("/{pullRequestId}")@GET- @Path("/{id}")- public PullRequest get(@PathParam("id") Long id) {- PullRequest request = pullRequestManager.load(id);-+ public PullRequest get(@PathParam("pullRequestId") Long pullRequestId) {+ PullRequest request = pullRequestManager.load(pullRequestId);if (!SecurityUtils.canRead(request.getTargetProject()))throw new UnauthorizedException();-return request;}@ValidQueryParams@GETpublic Collection<PullRequest> query(- @QueryParam("targetProjectId") Long targetProjectId, @QueryParam("targetBranch") String targetBranch,- @QueryParam("sourceProjectId") Long sourceProjectId, @QueryParam("sourceBranch") String sourceBranch,- @QueryParam("submitterId") Long submitterId, @QueryParam("status") String status,- @QueryParam("beginDate") Date beginDate, @QueryParam("endDate") Date endDate) {+ @QueryParam("targetProject") String targetProjectId, @QueryParam("targetBranch") String targetBranch,+ @QueryParam("sourceProject") String sourceProjectId, @QueryParam("sourceBranch") String sourceBranch,+ @QueryParam("number") Long number, @QueryParam("submitter") String submitterId,+ @QueryParam("status") String status, @QueryParam("beginDate") Date beginDate,+ @QueryParam("endDate") Date endDate) {EntityCriteria<PullRequest> criteria = EntityCriteria.of(PullRequest.class);@@ -71,6 +70,9 @@ public class PullRequestResource {if (sourceBranch != null)criteria.add(Restrictions.eq("sourceBranch", sourceBranch));+ if (number != null)+ criteria.add(Restrictions.eq("number", number));+if (OPEN.equalsIgnoreCase(status))criteria.add(PullRequest.CriterionHelper.ofOpen());else if (CLOSED.equalsIgnoreCase(status))@@ -83,11 +85,10 @@ public class PullRequestResource {if (endDate != null)criteria.add(Restrictions.le("submitDate", endDate));- List<PullRequest> requests = pullRequestManager.findAll(criteria);-- for (PullRequest request: requests) {- if (!SecurityUtils.canRead(request.getTarget().getProject()))- throw new UnauthorizedException("Unauthorized access to pull request " + request.getTarget() + "/" + request.getId());+ Collection<PullRequest> requests = new ArrayList<>();+ for (PullRequest request: pullRequestManager.findAll(criteria)) {+ if (SecurityUtils.canRead(request.getTarget().getProject()))+ requests.add(request);}return requests;}diff --git a/rest/src/main/java/com/gitplex/server/rest/ReviewResource.java b/rest/src/main/java/com/gitplex/server/rest/ReviewResource.javaindex c558bdcd80..d21b4e9979 100644--- a/rest/src/main/java/com/gitplex/server/rest/ReviewResource.java+++ b/rest/src/main/java/com/gitplex/server/rest/ReviewResource.java@@ -1,5 +1,8 @@package com.gitplex.server.rest;+import java.util.ArrayList;+import java.util.Collection;+import javax.inject.Inject;import javax.inject.Singleton;import javax.validation.Valid;@@ -11,14 +14,19 @@ import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;+import javax.ws.rs.QueryParam;import javax.ws.rs.core.MediaType;import org.apache.shiro.authz.UnauthorizedException;+import org.hibernate.criterion.Restrictions;import com.gitplex.server.manager.ReviewManager;import com.gitplex.server.manager.UserManager;import com.gitplex.server.model.Review;import com.gitplex.server.model.User;+import com.gitplex.server.persistence.annotation.Transactional;+import com.gitplex.server.persistence.dao.EntityCriteria;+import com.gitplex.server.rest.jersey.ValidQueryParams;import com.gitplex.server.security.SecurityUtils;@Path("/reviews")@@ -57,22 +65,33 @@ public class ReviewResource {throw new UnauthorizedException();}+ @ValidQueryParams+ @GET+ public Collection<Review> query(@QueryParam("pullRequest") Long pullRequestId, @QueryParam("user") Long userId,+ @QueryParam("commit") String commit) {+ EntityCriteria<Review> criteria = reviewManager.newCriteria();+ if (pullRequestId != null)+ criteria.add(Restrictions.eq("request.id", pullRequestId));+ if (userId != null)+ criteria.add(Restrictions.eq("user.id", userId));+ if (commit != null)+ criteria.add(Restrictions.eq("commit", commit));++ Collection<Review> reviews = new ArrayList<>();+ for (Review review: reviews) {+ if (SecurityUtils.canRead(review.getRequest().getTargetProject()))+ reviews.add(review);+ }+ return reviews;+ }++ @Transactional@POSTpublic Long save(@NotNull @Valid Review review) {- User user = userManager.getCurrent();- if (!SecurityUtils.canRead(review.getRequest().getTargetProject()) || user == null)+ if (!SecurityUtils.canRead(review.getRequest().getTargetProject())+ || !review.getUser().equals(userManager.getCurrent()) && !SecurityUtils.isAdministrator()) {throw new UnauthorizedException();-- if (review.isNew()) {- Review existingReview = reviewManager.find(review.getRequest(), user, review.getCommit());- if (existingReview != null) {- existingReview.setApproved(review.isApproved());- existingReview.setNote(review.getNote());- reviewManager.save(existingReview);- return existingReview.getId();- }- }- review.setUser(user);+ }reviewManager.save(review);return review.getId();}diff --git a/rest/src/main/java/com/gitplex/server/rest/UserResource.java b/rest/src/main/java/com/gitplex/server/rest/UserResource.javaindex 4965b5c4d7..9c56757dfe 100644--- a/rest/src/main/java/com/gitplex/server/rest/UserResource.java+++ b/rest/src/main/java/com/gitplex/server/rest/UserResource.java@@ -16,8 +16,8 @@ import org.apache.shiro.authz.UnauthorizedException;import org.hibernate.criterion.Restrictions;import org.hibernate.validator.constraints.Email;+import com.gitplex.server.manager.UserManager;import com.gitplex.server.model.User;-import com.gitplex.server.persistence.dao.Dao;import com.gitplex.server.persistence.dao.EntityCriteria;import com.gitplex.server.rest.jersey.ValidQueryParams;import com.gitplex.server.security.SecurityUtils;@@ -28,37 +28,32 @@ import com.gitplex.server.security.SecurityUtils;@Singletonpublic class UserResource {- private final Dao dao;+ private final UserManager userManager;@Inject- public UserResource(Dao dao) {- this.dao = dao;+ public UserResource(UserManager userManager) {+ this.userManager = userManager;}@ValidQueryParams@GET- public Collection<User> query(- @QueryParam("name") String name,- @Email @QueryParam("email") String email,- @QueryParam("fullName") String fullName) {+ public Collection<User> query(@QueryParam("name") String name, @Email @QueryParam("email") String email) {if (!SecurityUtils.canAccessPublic())throw new UnauthorizedException("Unauthorized access to user profiles");EntityCriteria<User> criteria = EntityCriteria.of(User.class);- if (name != null)- criteria.add(Restrictions.eq("name", name));+ if (name != null)+ criteria.add(Restrictions.eq("name", name));if (email != null)criteria.add(Restrictions.eq("email", email));- if (fullName != null)- criteria.add(Restrictions.eq("fullName", fullName));- return dao.findAll(criteria);+ return userManager.findAll(criteria);}@GET- @Path("/{id}")- public User get(@PathParam("id") Long id) {+ @Path("/{userId}")+ public User get(@PathParam("userId") Long userId) {if (!SecurityUtils.canAccessPublic())throw new UnauthorizedException("Unauthorized access to user profile");- return dao.load(User.class, id);+ return userManager.load(userId);}}diff --git a/rest/src/main/java/com/gitplex/server/rest/VerificationResource.java b/rest/src/main/java/com/gitplex/server/rest/VerificationResource.javanew file mode 100644index 0000000000..2444a8e61d--- /dev/null+++ b/rest/src/main/java/com/gitplex/server/rest/VerificationResource.java@@ -0,0 +1,73 @@+package com.gitplex.server.rest;++import java.util.Map;++import javax.inject.Inject;+import javax.inject.Singleton;+import javax.validation.Valid;+import javax.validation.constraints.NotNull;+import javax.ws.rs.Consumes;+import javax.ws.rs.GET;+import javax.ws.rs.POST;+import javax.ws.rs.Path;+import javax.ws.rs.PathParam;+import javax.ws.rs.Produces;+import javax.ws.rs.core.MediaType;++import org.apache.shiro.authz.UnauthorizedException;++import com.gitplex.server.manager.ProjectManager;+import com.gitplex.server.manager.VerificationManager;+import com.gitplex.server.model.Project;+import com.gitplex.server.security.SecurityUtils;+import com.gitplex.server.util.Verification;++@Path("/verifications")+@Consumes(MediaType.WILDCARD)+@Produces(MediaType.APPLICATION_JSON)+@Singleton+public class VerificationResource {++ private final VerificationManager verificationManager;++ private final ProjectManager projectManager;++ @Inject+ public VerificationResource(VerificationManager verificationManager, ProjectManager projectManager) {+ this.verificationManager = verificationManager;+ this.projectManager = projectManager;+ }++ @Path("/{projectId}/{commit}")+ @GET+ public Map<String, Verification> get(@PathParam("projectId") Long projectId, @PathParam("commit") String commit) {+ Project project = projectManager.load(projectId);+ if (!SecurityUtils.canRead(project))+ throw new UnauthorizedException();++ return verificationManager.getVerifications(project, commit);+ }++ @Path("/{projectId}/{commit}/{name}")+ @GET+ public Verification get(@PathParam("projectId") Long projectId, @PathParam("commit") String commit,+ @PathParam("name") String name) {+ Project project = projectManager.load(projectId);+ if (!SecurityUtils.canRead(project))+ throw new UnauthorizedException();++ return verificationManager.getVerifications(project, commit).get(name);+ }++ @Path("/{projectId}/{commit}/{name}")+ @POST+ public void save(@PathParam("projectId") Long projectId, @PathParam("commit") String commit,+ @PathParam("name") String name, @NotNull @Valid Verification verification) {+ Project project = projectManager.load(projectId);+ if (!SecurityUtils.canWrite(project))+ throw new UnauthorizedException();++ verificationManager.saveVerification(project, commit, name, verification);+ }++}