Search in sources :

Example 26 with ScmCommunicationException

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

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 27 with ScmCommunicationException

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

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 28 with ScmCommunicationException

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

the class HttpBitbucketServerApiClient method computeAuthorizationHeader.

private String computeAuthorizationHeader(String requestMethod, String requestUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    try {
        Subject subject = EnvironmentContext.getCurrent().getSubject();
        String authorizationHeader = authenticator.computeAuthorizationHeader(subject.getUserId(), requestMethod, requestUrl);
        if (Strings.isNullOrEmpty(authorizationHeader)) {
            throw buildScmUnauthorizedException();
        }
        return authorizationHeader;
    } catch (OAuthAuthenticationException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : OAuthAuthenticationException(org.eclipse.che.security.oauth1.OAuthAuthenticationException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) Subject(org.eclipse.che.commons.subject.Subject)

Example 29 with ScmCommunicationException

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

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)

Example 30 with ScmCommunicationException

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

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)

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