Search in sources :

Example 16 with ScmBadRequestException

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

the class HttpBitbucketServerApiClient method getUser.

@Override
public BitbucketUser getUser(Subject cheUser) throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException {
    try {
        // Since Bitbucket server API doesn't provide a way to get an account profile currently
        // authenticated user we will try to find it and by iterating over the list available to the
        // current user Bitbucket users and attempting to get their personal access tokens. To speed
        // up this process first of all we will search among users that contain(somewhere in Bitbucket
        // user
        // entity) Che's user username. At the second step, we will search against all visible(to the
        // current Che's user) bitbucket users that are not included in the first list.
        Set<String> usersByName = getUsers(cheUser.getUserName()).stream().map(BitbucketUser::getSlug).collect(Collectors.toSet());
        Optional<BitbucketUser> currentUser = findCurrentUser(usersByName);
        if (currentUser.isPresent()) {
            return currentUser.get();
        }
        Set<String> usersAllExceptByName = getUsers().stream().map(BitbucketUser::getSlug).filter(s -> !usersByName.contains(s)).collect(Collectors.toSet());
        currentUser = findCurrentUser(usersAllExceptByName);
        if (currentUser.isPresent()) {
            return currentUser.get();
        }
    } catch (ScmBadRequestException | ScmItemNotFoundException scmException) {
        throw new ScmCommunicationException(scmException.getMessage(), scmException);
    }
    throw new ScmItemNotFoundException("Current user not found. That is possible only if user are not authorized against " + serverUri);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HTTP_BAD_REQUEST(java.net.HttpURLConnection.HTTP_BAD_REQUEST) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) LoggerFactory(org.slf4j.LoggerFactory) Duration.ofSeconds(java.time.Duration.ofSeconds) Function(java.util.function.Function) OAuthAuthenticator(org.eclipse.che.security.oauth1.OAuthAuthenticator) HttpRequest(java.net.http.HttpRequest) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) HttpHeaders(com.google.common.net.HttpHeaders) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) CharStreams(com.google.common.io.CharStreams) Subject(org.eclipse.che.commons.subject.Subject) Duration(java.time.Duration) HttpClient(java.net.http.HttpClient) JavaType(com.fasterxml.jackson.databind.JavaType) URI(java.net.URI) LoggingUncaughtExceptionHandler(org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler) HttpResponse(java.net.http.HttpResponse) Charsets(com.google.common.base.Charsets) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) Logger(org.slf4j.Logger) HTTP_UNAUTHORIZED(java.net.HttpURLConnection.HTTP_UNAUTHORIZED) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) Set(java.util.Set) OAuthAuthenticationException(org.eclipse.che.security.oauth1.OAuthAuthenticationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Optional(java.util.Optional) HTTP_NOT_FOUND(java.net.HttpURLConnection.HTTP_NOT_FOUND) InputStream(java.io.InputStream) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Example 17 with ScmBadRequestException

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

the class HttpBitbucketServerApiClient method getUser.

@Override
public BitbucketUser getUser(String slug) throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
    URI uri = serverUri.resolve("/rest/api/1.0/users/" + slug);
    HttpRequest request = HttpRequest.newBuilder(uri).headers("Authorization", computeAuthorizationHeader("GET", uri.toString())).timeout(DEFAULT_HTTP_TIMEOUT).build();
    try {
        LOG.trace("executeRequest={}", request);
        return executeRequest(httpClient, request, inputStream -> {
            try {
                return OM.readValue(inputStream, BitbucketUser.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 18 with ScmBadRequestException

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

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 19 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException 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 20 with ScmBadRequestException

use of org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException 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)

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