Search in sources :

Example 11 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException in project che-server by eclipse-che.

the class GitlabApiClient method executeRequest.

private <T> T executeRequest(HttpClient httpClient, HttpRequest request, Function<InputStream, T> bodyConverter) throws ScmBadRequestException, ScmItemNotFoundException, ScmCommunicationException {
    try {
        HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
        LOG.trace("executeRequest={} response {}", request, response.statusCode());
        if (response.statusCode() == 200) {
            return bodyConverter.apply(response.body());
        } else if (response.statusCode() == 204) {
            return null;
        } else {
            String body = CharStreams.toString(new InputStreamReader(response.body(), Charsets.UTF_8));
            switch(response.statusCode()) {
                case HTTP_BAD_REQUEST:
                    throw new ScmBadRequestException(body);
                case HTTP_NOT_FOUND:
                    throw new ScmItemNotFoundException(body);
                default:
                    throw new ScmCommunicationException("Unexpected status code " + response.statusCode() + " " + response.toString());
            }
        }
    } catch (IOException | InterruptedException | UncheckedIOException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Example 12 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException in project che-server by eclipse-che.

the class GithubPersonalAccessTokenFetcher method fetchPersonalAccessToken.

@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    OAuthToken oAuthToken;
    if (githubApiClient == null || !githubApiClient.isConnected(scmServerUrl)) {
        LOG.debug("not a  valid url {} for current fetcher ", scmServerUrl);
        return null;
    }
    try {
        oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME);
        // Find the user associated to the OAuth token by querying the GitHub API.
        GithubUser user = githubApiClient.getUser(oAuthToken.getToken());
        PersonalAccessToken token = new PersonalAccessToken(scmServerUrl, cheSubject.getUserId(), user.getLogin(), Long.toString(user.getId()), NameGenerator.generate(OAUTH_2_PREFIX, 5), NameGenerator.generate("id-", 5), oAuthToken.getToken());
        Optional<Boolean> valid = isValid(token);
        if (valid.isEmpty()) {
            throw new ScmCommunicationException("Unable to verify if current token is a valid GitHub token.  Token's scm-url needs to be '" + GithubApiClient.GITHUB_SERVER + "' and was '" + token.getScmProviderUrl() + "'");
        } else if (!valid.get()) {
            throw new ScmCommunicationException("Current token doesn't have the necessary privileges. Please make sure Che app scopes are correct and containing at least: " + DEFAULT_TOKEN_SCOPES.toString());
        }
        return token;
    } catch (UnauthorizedException e) {
        throw new ScmUnauthorizedException(cheSubject.getUserName() + " is not authorized in " + OAUTH_PROVIDER_NAME + " OAuth provider.", OAUTH_PROVIDER_NAME, "2.0", getLocalAuthenticateUrl());
    } catch (NotFoundException | ServerException | ForbiddenException | BadRequestException | ScmItemNotFoundException | ScmBadRequestException | ConflictException e) {
        LOG.error(e.getMessage());
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)

Example 13 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException in project che-server by eclipse-che.

the class GitlabOAuthTokenFetcher method fetchPersonalAccessToken.

@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    scmServerUrl = StringUtils.trimEnd(scmServerUrl, '/');
    GitlabApiClient gitlabApiClient = getApiClient(scmServerUrl);
    if (gitlabApiClient == null || !gitlabApiClient.isConnected(scmServerUrl)) {
        LOG.debug("not a  valid url {} for current fetcher ", scmServerUrl);
        return null;
    }
    if (oAuthAPI == null) {
        throw new ScmCommunicationException(format("OAuth 2 is not configured for SCM provider [%s]. For details, refer " + "the documentation in section of SCM providers configuration.", OAUTH_PROVIDER_NAME));
    }
    OAuthToken oAuthToken;
    try {
        oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME);
        GitlabUser user = gitlabApiClient.getUser(oAuthToken.getToken());
        PersonalAccessToken token = new PersonalAccessToken(scmServerUrl, cheSubject.getUserId(), user.getUsername(), Long.toString(user.getId()), NameGenerator.generate(OAUTH_2_PREFIX, 5), NameGenerator.generate("id-", 5), oAuthToken.getToken());
        Optional<Boolean> valid = isValid(token);
        if (valid.isEmpty() || !valid.get()) {
            throw new ScmCommunicationException("Current token doesn't have the necessary  privileges. Please make sure Che app scopes are correct and containing at least: " + DEFAULT_TOKEN_SCOPES.toString());
        }
        return token;
    } catch (UnauthorizedException e) {
        throw new ScmUnauthorizedException(cheSubject.getUserName() + " is not authorized in " + OAUTH_PROVIDER_NAME + " OAuth provider.", OAUTH_PROVIDER_NAME, "2.0", getLocalAuthenticateUrl());
    } catch (NotFoundException | ServerException | ForbiddenException | BadRequestException | ScmItemNotFoundException | ScmBadRequestException | ConflictException e) {
        LOG.warn(e.getMessage());
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)

Example 14 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException in project che-server by eclipse-che.

the class HttpBitbucketServerApiClient method executeRequest.

private <T> T executeRequest(HttpClient httpClient, HttpRequest request, Function<InputStream, T> bodyConverter) throws ScmBadRequestException, ScmItemNotFoundException, ScmCommunicationException, ScmUnauthorizedException {
    try {
        HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
        LOG.trace("executeRequest={} response {}", request, response.statusCode());
        if (response.statusCode() == 200) {
            return bodyConverter.apply(response.body());
        } else if (response.statusCode() == 204) {
            return null;
        } else {
            String body = CharStreams.toString(new InputStreamReader(response.body(), Charsets.UTF_8));
            switch(response.statusCode()) {
                case HTTP_UNAUTHORIZED:
                    throw buildScmUnauthorizedException();
                case HTTP_BAD_REQUEST:
                    throw new ScmBadRequestException(body);
                case HTTP_NOT_FOUND:
                    throw new ScmItemNotFoundException(body);
                default:
                    throw new ScmCommunicationException("Unexpected status code " + response.statusCode() + " " + response.toString());
            }
        }
    } catch (IOException | InterruptedException | UncheckedIOException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Example 15 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException in project che-server by eclipse-che.

the class HttpBitbucketServerApiClient method deletePersonalAccessTokens.

@Override
public void deletePersonalAccessTokens(String userSlug, Long tokenId) throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
    URI uri = serverUri.resolve("/rest/access-tokens/1.0/users/" + userSlug + "/" + tokenId);
    HttpRequest request = HttpRequest.newBuilder(uri).DELETE().headers(HttpHeaders.AUTHORIZATION, computeAuthorizationHeader("DELETE", uri.toString()), HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).timeout(DEFAULT_HTTP_TIMEOUT).build();
    try {
        LOG.trace("executeRequest={}", request);
        executeRequest(httpClient, request, inputStream -> {
            try {
                return OM.readValue(inputStream, String.class);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        });
    } catch (ScmBadRequestException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URI(java.net.URI) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Aggregations

ScmBadRequestException (org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)22 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)22 IOException (java.io.IOException)16 UncheckedIOException (java.io.UncheckedIOException)16 ScmItemNotFoundException (org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException)14 URI (java.net.URI)10 HttpRequest (java.net.http.HttpRequest)10 InputStream (java.io.InputStream)8 InputStreamReader (java.io.InputStreamReader)8 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)8 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)6 List (java.util.List)4 Optional (java.util.Optional)4 Set (java.util.Set)4 OAuthToken (org.eclipse.che.api.auth.shared.dto.OAuthToken)4 BadRequestException (org.eclipse.che.api.core.BadRequestException)4 ConflictException (org.eclipse.che.api.core.ConflictException)4 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)4 NotFoundException (org.eclipse.che.api.core.NotFoundException)4 ServerException (org.eclipse.che.api.core.ServerException)4