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

com.marvelution.jira.plugins.sonar.rest.SonarLayoutRestResource 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.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBElement;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

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.rest.exceptions.NotAuthorizedException;
import com.marvelution.jira.plugins.sonar.rest.model.ColumnResource;
import com.marvelution.jira.plugins.sonar.rest.model.ColumnsResource;
import com.marvelution.jira.plugins.sonar.service.SonarPanelLayoutManager;
import com.opensymphony.user.User;
import com.sun.jersey.spi.resource.Singleton;

/**
 * REST API for Sonar Layouts
 * 
 * @author Mark Rekveld
 */

@Path("/layout")
@Singleton
public class SonarLayoutRestResource {

	private final Logger logger = Logger.getLogger(SonarLayoutRestResource.class);

	private SonarPanelLayoutManager panelLayoutManager;

	private UserUtil userUtil;

	private PermissionManager permissionManager;

	/**
	 * Constructor
	 *
	 * @param panelLayoutManager the {@link SonarPanelLayoutManager} implementation
	 * @param userUtil the {@link UserUtil} implementation
	 * @param permissionManager the {@link PermissionManager} implementation
	 */
	public SonarLayoutRestResource(SonarPanelLayoutManager panelLayoutManager, UserUtil userUtil,
										PermissionManager permissionManager) {
		this.panelLayoutManager = panelLayoutManager;
		this.userUtil = userUtil;
		this.permissionManager = permissionManager;
	}

	/**
	 * REST Endpoint to store the system layout
	 * 
	 * @param columns {@link ColumnsResource}
	 * @param authenticationContext the {@link AuthenticationContext}
	 * @return ok {@link Response} in successful, forbidden {@link Response}
	 */
	@POST
	@Path("/saveSystemLayout")
	@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
	public Response storeSystemLayout(JAXBElement columns,
					@Context AuthenticationContext authenticationContext) {
		final User user = userUtil.getUser(authenticationContext.getPrincipal().getName());
		if (user != null && permissionManager.hasPermission(Permissions.ADMINISTER, user)) {
			for (ColumnResource column : columns.getValue().getColumns()) {
				logger.trace("Storing column [" + column.getColumnId() + "] with gadgets ["
					+ StringUtils.join(column.getGadgets(), ',') + "]");
				panelLayoutManager.put(column.getColumnId(), column.getGadgets());
			}
			return Response.ok().build();
		} else {
			logger.error("Deteceted unauthorized access to the saveSystemLayout API");
			throw new NotAuthorizedException("Access is denied");
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy