Search in sources :

Example 1 with ScmCommunicationException

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

the class GithubApiClient method executeRequest.

private <T> T executeRequest(HttpClient httpClient, HttpRequest request, Function<HttpResponse<InputStream>, T> responseConverter) throws ScmBadRequestException, ScmItemNotFoundException, ScmCommunicationException {
    try {
        HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
        LOG.trace("executeRequest={} response {}", request, response.statusCode());
        if (response.statusCode() == HTTP_OK) {
            return responseConverter.apply(response);
        } else if (response.statusCode() == HTTP_NO_CONTENT) {
            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 2 with ScmCommunicationException

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

the class GitlabApiClient method getTokenInfo.

public GitlabOauthTokenInfo getTokenInfo(String authenticationToken) throws ScmItemNotFoundException, ScmCommunicationException {
    final URI uri = serverUrl.resolve("/oauth/token/info");
    HttpRequest request = HttpRequest.newBuilder(uri).headers("Authorization", "Bearer " + authenticationToken).timeout(DEFAULT_HTTP_TIMEOUT).build();
    LOG.trace("executeRequest={}", request);
    try {
        return executeRequest(httpClient, request, inputStream -> {
            try {
                return OBJECT_MAPPER.readValue(inputStream, GitlabOauthTokenInfo.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)

Example 3 with ScmCommunicationException

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

the class BitbucketServerPersonalAccessTokenFetcher method fetchPersonalAccessToken.

@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheUser, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    if (!bitbucketServerApiClient.isConnected(scmServerUrl)) {
        LOG.debug("not a  valid url {} for current fetcher ", scmServerUrl);
        return null;
    }
    final String tokenName = format(TOKEN_NAME_TEMPLATE, cheUser.getUserId(), apiEndpoint.getHost());
    try {
        BitbucketUser user = bitbucketServerApiClient.getUser(EnvironmentContext.getCurrent().getSubject());
        LOG.debug("Current bitbucket user {} ", user);
        // cleanup existed
        List<BitbucketPersonalAccessToken> existingTokens = bitbucketServerApiClient.getPersonalAccessTokens(user.getSlug()).stream().filter(p -> p.getName().equals(tokenName)).collect(Collectors.toList());
        for (BitbucketPersonalAccessToken existedToken : existingTokens) {
            LOG.debug("Deleting existed che token {} {}", existedToken.getId(), existedToken.getName());
            bitbucketServerApiClient.deletePersonalAccessTokens(user.getSlug(), existedToken.getId());
        }
        BitbucketPersonalAccessToken token = bitbucketServerApiClient.createPersonalAccessTokens(user.getSlug(), tokenName, DEFAULT_TOKEN_SCOPE);
        LOG.debug("Token created = {} for {}", token.getId(), token.getUser());
        return new PersonalAccessToken(scmServerUrl, EnvironmentContext.getCurrent().getSubject().getUserId(), user.getName(), valueOf(user.getId()), token.getName(), valueOf(token.getId()), token.getToken());
    } catch (ScmBadRequestException | ScmItemNotFoundException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) URL(java.net.URL) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) BitbucketServerApiClient(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketServerApiClient) PersonalAccessTokenFetcher(org.eclipse.che.api.factory.server.scm.PersonalAccessTokenFetcher) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) Inject(javax.inject.Inject) List(java.util.List) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) String.valueOf(java.lang.String.valueOf) BitbucketUser(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) Subject(org.eclipse.che.commons.subject.Subject) Optional(java.util.Optional) Named(javax.inject.Named) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) BitbucketUser(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Example 4 with ScmCommunicationException

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

the class HttpBitbucketServerApiClient method getPersonalAccessToken.

@Override
public BitbucketPersonalAccessToken getPersonalAccessToken(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).headers("Authorization", computeAuthorizationHeader("GET", uri.toString()), HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).timeout(DEFAULT_HTTP_TIMEOUT).build();
    try {
        LOG.trace("executeRequest={}", request);
        return executeRequest(httpClient, request, inputStream -> {
            try {
                return OM.readValue(inputStream, BitbucketPersonalAccessToken.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)

Example 5 with ScmCommunicationException

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

the class HttpBitbucketServerApiClient method createPersonalAccessTokens.

@Override
public BitbucketPersonalAccessToken createPersonalAccessTokens(String userSlug, String tokenName, Set<String> permissions) throws ScmBadRequestException, ScmUnauthorizedException, ScmCommunicationException {
    URI uri = serverUri.resolve("/rest/access-tokens/1.0/users/" + userSlug);
    try {
        HttpRequest request = HttpRequest.newBuilder(uri).PUT(HttpRequest.BodyPublishers.ofString(OM.writeValueAsString(new BitbucketPersonalAccessToken(tokenName, permissions)))).headers(HttpHeaders.AUTHORIZATION, computeAuthorizationHeader("PUT", uri.toString()), HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).timeout(DEFAULT_HTTP_TIMEOUT).build();
        LOG.trace("executeRequest={}", request);
        return executeRequest(httpClient, request, inputStream -> {
            try {
                return OM.readValue(inputStream, BitbucketPersonalAccessToken.class);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        });
    } catch (ScmItemNotFoundException | JsonProcessingException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : HttpRequest(java.net.http.HttpRequest) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) 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) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)30 ScmBadRequestException (org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)22 IOException (java.io.IOException)20 UncheckedIOException (java.io.UncheckedIOException)18 ScmItemNotFoundException (org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException)16 URI (java.net.URI)12 HttpRequest (java.net.http.HttpRequest)12 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)10 InputStream (java.io.InputStream)8 InputStreamReader (java.io.InputStreamReader)8 ServerException (org.eclipse.che.api.core.ServerException)6 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)6 Subject (org.eclipse.che.commons.subject.Subject)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)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 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)4