public static IStatus testConnection(String url, @Nullable String organization, @Nullable String username, @Nullable String password) { try { Builder builder = getConfigBuilderNoCredentials(url, organization); if (StringUtils.isNotBlank(username) || StringUtils.isNotBlank(password)) { builder.credentials(username, password); } WsHelper helper = new WsHelperImpl(); ValidationResult testConnection = helper.validateConnection(builder.build()); if (testConnection.success()) { return new Status(IStatus.OK, SonarLintCorePlugin.PLUGIN_ID, "Successfully connected!"); } else { return new Status(IStatus.ERROR, SonarLintCorePlugin.PLUGIN_ID, testConnection.message()); } } catch (Exception e) { if (e.getCause() instanceof UnknownHostException) { return new Status(IStatus.ERROR, SonarLintCorePlugin.PLUGIN_ID, "Unknown host: " + url); } SonarLintLogger.get().debug(e.getMessage(), e); return new Status(IStatus.ERROR, SonarLintCorePlugin.PLUGIN_ID, e.getMessage(), e); } }
static ValidationResult validateConnection(SonarLintWsClient client, @Nullable String organizationKey) { ServerVersionAndStatusChecker serverChecker = new ServerVersionAndStatusChecker(client); AuthenticationChecker authChecker = new AuthenticationChecker(client); try { ServerInfos serverStatus = serverChecker.checkVersionAndStatus(); ValidationResult validateCredentials = authChecker.validateCredentials(); if (validateCredentials.success() && organizationKey != null) { Version serverVersion = Version.create(serverStatus.getVersion()); if (serverVersion.compareToIgnoreQualifier(Version.create(MIN_VERSION_FOR_ORGANIZATIONS)) < 0) { return new DefaultValidationResult(false, "No organization support for this server version: " + serverStatus.getVersion()); } if (!fetchOrganization(client, organizationKey, new ProgressWrapper(null)).isPresent()) { return new DefaultValidationResult(false, "No organizations found for key: " + organizationKey); } } return validateCredentials; } catch (UnsupportedServerException e) { return new DefaultValidationResult(false, e.getMessage()); } catch (RuntimeException e) { throw SonarLintWrappedException.wrap(e); } }
private void checkConnection() throws CommitStepException { SonarQubeServer tmpServer = model.createServerWithoutOrganization(); ConnectionTestTask test = new ConnectionTestTask(tmpServer); ProgressManager.getInstance().run(test); ValidationResult r = test.result(); String msg = "Failed to connect to the server. Please check the configuration."; if (test.getException() != null) { if (test.getException().getMessage() != null) { msg = msg + " Error: " + test.getException().getMessage(); } throw new CommitStepException(msg); } else if (!r.success()) { throw new CommitStepException(msg + " Cause: " + r.message()); } }