All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.jira.plugins.sonar.rest.SonarAssociationRestResource Maven / Gradle / Ivy

/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you 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.marvelution.jira.plugins.sonar.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.StringUtils;

import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.PermissionManager;
import com.atlassian.jira.security.Permissions;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.plugins.rest.common.security.AuthenticationContext;
import com.marvelution.jira.plugins.sonar.DefaultSonarAssociationImpl;
import com.marvelution.jira.plugins.sonar.rest.exceptions.InvalidAssociationException;
import com.marvelution.jira.plugins.sonar.rest.exceptions.NotAuthorizedException;
import com.marvelution.jira.plugins.sonar.service.SonarAssociationManager;
import com.opensymphony.user.User;
import com.sun.jersey.spi.resource.Singleton;

/**
 * REST API for Sonar Project Associations
 * 
 * @author Mark Rekveld
 */

@Path("/association")
@Singleton
public class SonarAssociationRestResource {

	private SonarAssociationManager associationManager;

	private UserUtil userUtil;

	private ProjectManager projectManager;

	private PermissionManager permissionManager;

	/**
	 * Constructor
	 * 
	 * @param associationManager the {@link SonarAssociationManager} implementation
	 * @param userUtil the {@link UserUtil} implementation
	 * @param projectManager the {@link ProjectManager} implementation
	 * @param permissionManager the {@link PermissionManager} implementation
	 */
	public SonarAssociationRestResource(SonarAssociationManager associationManager, UserUtil userUtil,
										ProjectManager projectManager, PermissionManager permissionManager) {
		this.associationManager = associationManager;
		this.userUtil = userUtil;
		this.projectManager = projectManager;
		this.permissionManager = permissionManager;
	}

	/**
	 * REST Endpoint to add a Sonar Association
	 * 
	 * @param projectId the ProjectId
	 * @param sonarServer the Sonar Server URL
	 * @param sonarProject the Sonar Project Key
	 * @param authenticationContext the {@link AuthenticationContext}
	 * @return ok {@link Response} in successful, forbidden {@link Response}
	 */
	@POST
	@Path("{projectId}")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response associateProject(@PathParam("projectId") Long projectId,
					@FormParam("sonarServer") String sonarServer, @FormParam("sonarProject") String sonarProject,
					@Context AuthenticationContext authenticationContext) {
		final Project project = projectManager.getProjectObj(projectId);
		final User user = userUtil.getUser(authenticationContext.getPrincipal().getName());
		if (project != null && user != null
			&& permissionManager.hasPermission(Permissions.PROJECT_ADMIN, project, user)) {
			if (StringUtils.isBlank(sonarServer)
				|| (!sonarServer.startsWith("http://") && !sonarServer.startsWith("http://"))) {
				throw new InvalidAssociationException("Invalid Sonar Server given");
			} else if (StringUtils.isBlank(sonarProject)) {
				throw new InvalidAssociationException("Invalid Sonar Project given");
			} else {
				associationManager.put(new DefaultSonarAssociationImpl(projectId, sonarServer, sonarProject));
				return Response.ok().build();
			}
		} else {
			throw new NotAuthorizedException("Access is denied");
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy