Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
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.QueryParam;
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 org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;
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.AnonymousAllowed;
import com.atlassian.plugins.rest.common.security.AuthenticationContext;
import com.marvelution.jira.plugins.sonar.DefaultSonarAssociationImpl;
import com.marvelution.jira.plugins.sonar.SonarPluginHelper;
import com.marvelution.jira.plugins.sonar.rest.exceptions.InvalidAssociationException;
import com.marvelution.jira.plugins.sonar.rest.exceptions.NotAuthorizedException;
import com.marvelution.jira.plugins.sonar.rest.model.SonarAssociationConfigurationResource;
import com.marvelution.jira.plugins.sonar.rest.model.SonarResource;
import com.marvelution.jira.plugins.sonar.service.SonarAssociation;
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 componentId the componentId
* @param sonarServer the Sonar Server URL
* @param sonarProject the Sonar Project Key
* @param authenticationContext the {@link AuthenticationContext}
* @return ok {@link Response} if successful, forbidden {@link Response} otherwise
*/
@POST
@Path("add")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response addProjectAssociation(@FormParam("projectId") Long projectId,
@FormParam("componentId") Long componentId, @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)) {
validateSonarProperties(sonarServer, sonarProject);
associationManager.put(new DefaultSonarAssociationImpl(projectId, componentId, sonarServer, sonarProject));
return Response.ok().build();
} else {
throw new NotAuthorizedException("Access is denied");
}
}
/**
* REST Endpoint to update a Sonar Association
*
* @param associationId the {@link SonarAssociation} Id to update
* @param sonarServer the new Sonar Server host url
* @param sonarProject the new Sonar Project Resource
* @param authenticationContext the {@link AuthenticationContext}
* @return ok {@link Response} if successful, forbidden {@link Response} otherwise
*/
@POST
@Path("edit/{associationId}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateProjectAssociation(@PathParam("associationId") Long associationId,
@FormParam("sonarServer") String sonarServer, @FormParam("sonarProject") String sonarProject,
@Context AuthenticationContext authenticationContext) {
final SonarAssociation association = associationManager.getSonarAssociationById(associationId);
if (association == null) {
throw new InvalidAssociationException("Invalid Association Id given");
} else {
final Project project = projectManager.getProjectObj(association.getProjectId());
final User user = userUtil.getUser(authenticationContext.getPrincipal().getName());
if (user != null && permissionManager.hasPermission(Permissions.PROJECT_ADMIN, project, user)) {
validateSonarProperties(sonarServer, sonarProject);
association.setSonarServer(sonarServer);
association.setSonarProject(sonarProject);
associationManager.put(association);
return Response.ok().build();
} else {
throw new NotAuthorizedException("Access is denied");
}
}
}
/**
* Get the current Configuration for a {@link SonarAssociation}
*
* @param associationId the {@link SonarAssociation} Id
* @return configuration and available resources wrapped in a {@link Response} object
*/
@GET
@Path("get/currentConfiguration")
@Produces(MediaType.APPLICATION_JSON)
public Response getAssociationCurrentConfiguration(@QueryParam("associationId") Long associationId) {
final SonarAssociation association = associationManager.getSonarAssociationById(associationId);
if (association != null) {
final List resources =
SonarPluginHelper.getSonarClientForAssociation(association).findAll(new ResourceQuery());
final SonarAssociationConfigurationResource config =
new SonarAssociationConfigurationResource(association.getAssociationId(), association.getProjectId(),
association.getComponentId(), association.getSonarServer(), association.getSonarProject());
for (Resource resource : resources) {
config.getAvailableResources().add(new SonarResource(resource.getName(), resource.getKey()));
}
return Response.ok(config).build();
} else {
throw new InvalidAssociationException("Invalid Association Id given");
}
}
/**
* Get the Sonar project name from the Sonar Server
*
* @param associationId the Association Id to get the project name for
* @return the project name in a {@link Response} object
*/
@GET
@AnonymousAllowed
@Path("get/projectName")
@Produces(MediaType.TEXT_PLAIN)
public Response getSonarProjectName(@QueryParam("associationId") Long associationId) {
final SonarAssociation association = associationManager.getSonarAssociationById(associationId);
String resourceName = "UNKNOWN";
if (association != null) {
final Resource resource = SonarPluginHelper.getSonarClientForAssociation(association).find(
new ResourceQuery(association.getSonarProject()));
if (StringUtils.isNotBlank(resource.getName())) {
resourceName = resource.getName();
}
}
return Response.ok(resourceName).build();
}
/**
* Method to validate the given Sonar Server and Sonar Resource
* Will throw an {@link InvalidAssociationException} in case of errors
*
* @param sonarServer the Sonar Server url
* @param sonarProject the Sonar Project Resource
*/
private void validateSonarProperties(String sonarServer, String sonarProject) {
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");
}
}
}