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

com.marvelution.jira.plugins.sonar.rest.SonarServerRestResource 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.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.services.ResourceQuery;

import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
import com.marvelution.jira.plugins.sonar.rest.exceptions.InvalidServerException;
import com.marvelution.jira.plugins.sonar.rest.model.Resources;
import com.marvelution.jira.plugins.sonar.services.servers.SonarClientFactory;
import com.marvelution.jira.plugins.sonar.services.servers.SonarServerManager;
import com.sun.jersey.spi.resource.Singleton;

/**
 * @author Mark Rekveld
 * 
 * @since 2.4.0
 */
@Path("/server")
@Singleton
@AnonymousAllowed
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public class SonarServerRestResource {

	private final SonarServerManager serverManager;
	private final SonarClientFactory clientFactory;

	/**
	 * Constructor
	 *
	 * @param serverManager the {@link SonarServerManager} implementation
	 * @param clientFactory the {@link SonarClientFactory} implementation
	 */
	public SonarServerRestResource(SonarServerManager serverManager, SonarClientFactory clientFactory) {
		this.serverManager = serverManager;
		this.clientFactory = clientFactory;
	}

	/**
	 * Get all the resources on the given sonar server
	 * 
	 * @param serverId the id of the sonar server to get the resources from
	 * @return the resources
	 */
	@GET
	@Path("{serverId}/resources")
	public Resources getResources(@PathParam("serverId") int serverId) {
		if (serverManager.hasServer(serverId)) {
			Sonar sonar = clientFactory.create(serverManager.getServer(serverId));
			return new Resources(sonar.findAll(new ResourceQuery()));
		} else {
			throw new InvalidServerException("No server with id " + serverId);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy