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

com.hp.octane.plugins.jetbrains.teamcity.configuration.TCConfigurationService Maven / Gradle / Ivy

/*
 *     Copyright 2017 Hewlett-Packard Development Company, L.P.
 *     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 com.hp.octane.plugins.jetbrains.teamcity.configuration;

import com.hp.octane.integrations.OctaneSDK;
import com.hp.octane.integrations.dto.configuration.OctaneConfiguration;
import com.hp.octane.integrations.dto.connectivity.OctaneResponse;
import com.hp.octane.plugins.jetbrains.teamcity.OctaneTeamCityPlugin;
import jetbrains.buildServer.serverSide.SBuildServer;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;

/**
 * Created by lazara.
 * Created by gadiel.
 */

public class TCConfigurationService {
	private static final Logger logger = LogManager.getLogger(TCConfigurationService.class);
	private static final String CONFIG_FILE = "octane-config.xml";

	@Autowired
	private SBuildServer buildServer;
	@Autowired
	private OctaneTeamCityPlugin octaneTeamCityPlugin;

	public String checkConfiguration(OctaneConfiguration octaneConfiguration) {
		String resultMessage;

		try {
			OctaneResponse result = OctaneSDK.getInstance().getConfigurationService().validateConfiguration(octaneConfiguration);
			if (result.getStatus() == HttpStatus.SC_OK) {
				resultMessage = "Connection succeeded";
			} else if (result.getStatus() == HttpStatus.SC_UNAUTHORIZED) {
				resultMessage = "Authentication failed";
			} else if (result.getStatus() == HttpStatus.SC_FORBIDDEN) {
				resultMessage = octaneConfiguration.getApiKey() + " not authorized to shared space " + octaneConfiguration.getSharedSpace();
			} else if (result.getStatus() == HttpStatus.SC_NOT_FOUND) {
				resultMessage = "Shared space " + octaneConfiguration.getSharedSpace() + " not exists";
			} else {
				resultMessage = "Validation failed for unknown reason; status code: " + result.getStatus();
			}
		} catch (IOException ioe) {
			resultMessage = "Connection failed: " + ioe.getMessage();
		}

		return resultMessage;
	}

	public OctaneConfigStructure readConfig() {
		OctaneConfigStructure result = null;
		try {
			JAXBContext context = JAXBContext.newInstance(OctaneConfigStructure.class);
			Unmarshaller un = context.createUnmarshaller();
			result = (OctaneConfigStructure) un.unmarshal(getConfigurationResource());
		} catch (JAXBException jaxbe) {
			logger.error("failed to read Octane configuration", jaxbe);
		}
		return result;
	}

	public void saveConfig(OctaneConfigStructure config) {
		try {
			JAXBContext context = JAXBContext.newInstance(OctaneConfigStructure.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(config, getConfigurationResource());
			OctaneSDK.getInstance().getConfigurationService().notifyChange();
		} catch (JAXBException jaxbe) {
			logger.error("failed to save Octane configuration", jaxbe);
		}
	}

	private File getConfigurationResource() {
		return new File(buildServer.getServerRootPath() + octaneTeamCityPlugin.getDescriptor().getPluginResourcesPath(CONFIG_FILE));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy