Search in sources :

Example 1 with OctaneConnectivityException

use of com.hp.octane.integrations.exceptions.OctaneConnectivityException in project octane-gitlab-service by MicroFocus.

the class Application method main.

public static void main(String[] args) throws GeneralSecurityException, IOException {
    if (args.length > 0 && args[0].equals("encrypt")) {
        if (args.length == 1) {
            System.out.println("Usage: java -jar octane-gitlab-service-<version>.jar encrypt <password>");
            return;
        } else {
            String tokenToEncrypt = args[1];
            String encryptedToken = encrypt(tokenToEncrypt);
            System.out.println("Encrypted token: " + PasswordEncryption.PREFIX + encryptedToken);
            return;
        }
    }
    ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
    context.registerShutdownHook();
    OctaneServices octaneServices = context.getBean("octaneServices", OctaneServices.class);
    try {
        if (octaneServices.getGitLabService().isCleanUpOnly()) {
            System.out.println("clean-up webhooks from Gitlab server process is finished. Stopping the service");
            context.close();
            return;
        }
        tryToConnectToOctane(octaneServices);
        OctaneSDK.addClient(octaneServices.getOctaneConfiguration(), OctaneServices.class);
        MergeRequestHistoryHandler mrHistoryHandler = context.getBean(MergeRequestHistoryHandler.class);
        mrHistoryHandler.executeFirstScan();
        mrHistoryHandler.startListening();
        System.out.println("Connection to Octane was successful. gitlab application is ready...");
    } catch (IllegalArgumentException | OctaneConnectivityException r) {
        log.warn("Connection to Octane failed: " + r.getMessage());
        System.out.println("Connection to Octane failed: " + r.getMessage());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException) OctaneServices(com.microfocus.octane.gitlab.services.OctaneServices) MergeRequestHistoryHandler(com.microfocus.octane.gitlab.api.MergeRequestHistoryHandler)

Example 2 with OctaneConnectivityException

use of com.hp.octane.integrations.exceptions.OctaneConnectivityException in project octane-gitlab-service by MicroFocus.

the class Application method tryToConnectToOctane.

private static void tryToConnectToOctane(OctaneServices octaneServices) throws OctaneConnectivityException {
    OctaneConfiguration octaneConfiguration = octaneServices.getOctaneConfiguration();
    if (StringUtils.isEmpty(octaneConfiguration.getUrl())) {
        throw new IllegalArgumentException("Location URL is missing");
    }
    if (StringUtils.isEmpty(octaneConfiguration.getClient())) {
        throw new IllegalArgumentException("Client ID is missing");
    }
    if (StringUtils.isEmpty(octaneConfiguration.getSecret())) {
        throw new IllegalArgumentException("Client Secret is missing");
    }
    octaneServices.getGitLabApiWrapper().getGitLabApi().getSecretToken();
    try {
        OctaneSDK.testOctaneConfigurationAndFetchAvailableWorkspaces(octaneConfiguration.getUrl(), octaneConfiguration.getSharedSpace(), octaneConfiguration.getClient(), octaneConfiguration.getSecret(), OctaneServices.class);
    } catch (OctaneConnectivityException e) {
        throw new IllegalArgumentException(e.getErrorMessageVal());
    } catch (Exception e) {
        throw new IllegalArgumentException("Unexpected exception :" + e.getMessage());
    }
}
Also used : OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException) OctaneConfiguration(com.hp.octane.integrations.OctaneConfiguration) IOException(java.io.IOException) OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException) GeneralSecurityException(java.security.GeneralSecurityException)

Example 3 with OctaneConnectivityException

use of com.hp.octane.integrations.exceptions.OctaneConnectivityException in project octane-ci-java-sdk by MicroFocus.

the class OctaneSDK method testOctaneConfigurationAndFetchAvailableWorkspaces.

/**
 * This method allows to test Octane configuration prior to creating full functioning Octane client (use case - test connection in UI)
 * In case of failed configuration , IllegalArgumentException is thrown
 *
 * @param octaneServerUrl base Octane server URL
 * @param sharedSpaceId   shared space ID
 * @param client          client / api key
 * @param secret          secret / api secret
 * @param pluginServicesClass class that extends CIPluginServices
 * @return   List of available workspaces
 * @throws IOException in case of basic connectivity failure
 */
public static List<Entity> testOctaneConfigurationAndFetchAvailableWorkspaces(String octaneServerUrl, String sharedSpaceId, String client, String secret, Class<? extends CIPluginServices> pluginServicesClass) throws IOException {
    // instance ID is a MUST parameter but not needed for configuration validation, therefore RANDOM value provided
    OctaneConfiguration configuration = OctaneConfiguration.create(UUID.randomUUID().toString(), octaneServerUrl, sharedSpaceId);
    configuration.setSecret(secret);
    configuration.setClient(client);
    if (pluginServicesClass == null) {
        throw new IllegalArgumentException("plugin services provider is invalid");
    }
    CIPluginServices pluginServices;
    try {
        pluginServices = pluginServicesClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalArgumentException("failed to instantiate plugin services '" + pluginServicesClass.getSimpleName() + "'", e);
    }
    SDKServicesConfigurer configurer = new SDKServicesConfigurer(configuration, pluginServices);
    RestService restService = RestService.newInstance(configurer);
    ConfigurationService configurationService = ConfigurationService.newInstance(configurer, restService);
    OctaneConnectivityStatus octaneConnectivityStatus = configurationService.validateConfigurationAndGetConnectivityStatus();
    if (!CIPluginSDKUtils.isSdkSupported(octaneConnectivityStatus)) {
        throw new OctaneConnectivityException(0, OctaneConnectivityException.UNSUPPORTED_SDK_VERSION_KEY, OctaneConnectivityException.UNSUPPORTED_SDK_VERSION_MESSAGE);
    }
    try {
        EntitiesService entitiesService = EntitiesService.newInstance(configurer, restService);
        List<Entity> workspaces = entitiesService.getEntities(null, /*no workspace*/
        EntityConstants.Workspaces.COLLECTION_NAME, null, /*no conditions*/
        Arrays.asList(EntityConstants.Base.NAME_FIELD));
        return workspaces;
    } catch (Exception e) {
        logger.error(configuration.getLocationForLog() + "Failed to fetch workspaces in testOctaneConfigurationAndFetchAvailableWorkspaces : " + e.getMessage());
        return null;
    }
}
Also used : OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException) Entity(com.hp.octane.integrations.dto.entities.Entity) IOException(java.io.IOException) OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException) OctaneConnectivityStatus(com.hp.octane.integrations.dto.general.OctaneConnectivityStatus) ConfigurationService(com.hp.octane.integrations.services.configuration.ConfigurationService) EntitiesService(com.hp.octane.integrations.services.entities.EntitiesService) RestService(com.hp.octane.integrations.services.rest.RestService)

Example 4 with OctaneConnectivityException

use of com.hp.octane.integrations.exceptions.OctaneConnectivityException in project octane-ci-java-sdk by MicroFocus.

the class ConfigurationServiceImpl method validateConfigurationAndGetConnectivityStatus.

@Override
public OctaneConnectivityStatus validateConfigurationAndGetConnectivityStatus() throws IOException {
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(configurer.octaneConfiguration.getUrl() + RestService.SHARED_SPACE_INTERNAL_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + CONNECTIVITY_STATUS_URL);
    OctaneResponse response = restService.obtainOctaneRestClient().execute(request, configurer.octaneConfiguration);
    if (response.getStatus() == 401) {
        throw new OctaneConnectivityException(response.getStatus(), OctaneConnectivityException.AUTHENTICATION_FAILURE_KEY, OctaneConnectivityException.AUTHENTICATION_FAILURE_MESSAGE);
    } else if (response.getStatus() == 403) {
        throw new OctaneConnectivityException(response.getStatus(), OctaneConnectivityException.AUTHORIZATION_FAILURE_KEY, OctaneConnectivityException.AUTHORIZATION_FAILURE_MESSAGE);
    } else if (response.getStatus() == 404) {
        throw new OctaneConnectivityException(response.getStatus(), OctaneConnectivityException.CONN_SHARED_SPACE_INVALID_KEY, OctaneConnectivityException.CONN_SHARED_SPACE_INVALID_MESSAGE);
    } else if (response.getStatus() == 200) {
        OctaneConnectivityStatus octaneConnectivityStatus = DTOFactory.getInstance().dtoFromJson(response.getBody(), OctaneConnectivityStatus.class);
        return octaneConnectivityStatus;
    } else {
        throw new OctaneConnectivityException(response.getStatus(), OctaneConnectivityException.UNEXPECTED_FAILURE_KEY, OctaneConnectivityException.UNEXPECTED_FAILURE_MESSAGE + ": " + response.getStatus());
    }
}
Also used : OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException) OctaneConnectivityStatus(com.hp.octane.integrations.dto.general.OctaneConnectivityStatus) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest)

Aggregations

OctaneConnectivityException (com.hp.octane.integrations.exceptions.OctaneConnectivityException)4 OctaneConnectivityStatus (com.hp.octane.integrations.dto.general.OctaneConnectivityStatus)2 IOException (java.io.IOException)2 OctaneConfiguration (com.hp.octane.integrations.OctaneConfiguration)1 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)1 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)1 Entity (com.hp.octane.integrations.dto.entities.Entity)1 ConfigurationService (com.hp.octane.integrations.services.configuration.ConfigurationService)1 EntitiesService (com.hp.octane.integrations.services.entities.EntitiesService)1 RestService (com.hp.octane.integrations.services.rest.RestService)1 MergeRequestHistoryHandler (com.microfocus.octane.gitlab.api.MergeRequestHistoryHandler)1 OctaneServices (com.microfocus.octane.gitlab.services.OctaneServices)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1