Java tutorial
/* * Copyright 2000-2014 JetBrains s.r.o. * * 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 jetbrains.buildServer.projectPush; import com.google.common.net.HttpHeaders; import jetbrains.buildServer.controllers.AuthorizationInterceptor; import jetbrains.buildServer.controllers.BaseController; import jetbrains.buildServer.projectPush.helpers.AuthHelper; import jetbrains.buildServer.projectPush.helpers.RESTApiHelper; import jetbrains.buildServer.projectPush.helpers.RolesHelper; import jetbrains.buildServer.serverSide.ProjectManager; import jetbrains.buildServer.serverSide.SProject; import jetbrains.buildServer.serverSide.auth.Role; import jetbrains.buildServer.serverSide.auth.RoleScope; import jetbrains.buildServer.serverSide.identifiers.ProjectIdentifiersManager; import jetbrains.buildServer.users.SUser; import jetbrains.buildServer.web.openapi.WebControllerManager; import jetbrains.buildServer.web.util.SessionUser; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.http.HttpStatus; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; /** * @author Evgeniy.Koshkin */ public class PostProjectToSandboxController extends BaseController { private static final String PATH = "/app/push/sandbox/projects"; @NotNull private final AuthHelper myAuthHelper; @NotNull private final RolesHelper myRolesHelper; @NotNull private final ProjectPushSettingsStorage mySettings; @NotNull private final ProjectManager myProjectManager; @NotNull private final ProjectIdentifiersManager myProjectIdentifiersManager; public PostProjectToSandboxController(@NotNull WebControllerManager webControllerManager, @NotNull AuthHelper authHelper, @NotNull AuthorizationInterceptor authInterceptor, @NotNull ProjectPushSettingsStorage settings, @NotNull ProjectManager projectManager, @NotNull ProjectIdentifiersManager projectIdentifiersManager, @NotNull RolesHelper rolesHelper) { myAuthHelper = authHelper; mySettings = settings; myProjectManager = projectManager; myProjectIdentifiersManager = projectIdentifiersManager; myRolesHelper = rolesHelper; webControllerManager.registerController(PATH, this); authInterceptor.addPathNotRequiringAuth(PATH); } @Nullable @Override protected ModelAndView doHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws Exception { if (!isPost(request)) { response.sendError(HttpStatus.METHOD_NOT_ALLOWED.value()); return null; } final StringBuilder stringBuffer = new StringBuilder(); try { String line; BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) stringBuffer.append(line); } catch (Exception e) { response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage()); return null; } final String projectName = stringBuffer.toString(); if (projectName.isEmpty()) { response.sendError(HttpStatus.BAD_REQUEST.value(), "Project name is empty."); return null; } if (mySettings.isDisabled()) { response.sendError(HttpStatus.FORBIDDEN.value(), "Sandbox disabled."); return null; } SUser user; user = SessionUser.getUser(request); if (user == null) { user = myAuthHelper.getAuthenticatedUser(request, response); } if (user == null) return null; final Role projectAdminRole = myRolesHelper.findProjectAdminRole(); if (projectAdminRole == null) { response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Failed to locate Project Admin role on the server."); return null; } final SProject project; try { final String sandboxProjectId = mySettings.getSandboxProjectId(); final SProject sandboxProject = myProjectManager.findProjectByExternalId(sandboxProjectId); if (sandboxProject == null) { response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Failed to locate sandbox project by ID " + sandboxProjectId); return null; } if (sandboxProject.findProjectByName(projectName) != null) { response.sendError(HttpStatus.CONFLICT.value(), "Project with name " + projectName + " already exists."); return null; } project = sandboxProject.createProject( myProjectIdentifiersManager.generateNewExternalId(null, projectName, null), projectName); project.persist(); } catch (Exception e) { response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()); return null; } try { myRolesHelper.addRole(user, RoleScope.projectScope(project.getProjectId()), projectAdminRole); } catch (Throwable throwable) { response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), throwable.getMessage()); return null; } response.setStatus(HttpStatus.CREATED.value()); response.setHeader(HttpHeaders.LOCATION, RESTApiHelper.getProjectURI(project)); return null; } }