Search in sources :

Example 16 with ScmCommunicationException

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

the class DevfileToApiExceptionMapperTest method shouldReturnServerExceptionWhenCauseIsCommunicationException.

@Test
public void shouldReturnServerExceptionWhenCauseIsCommunicationException() {
    ScmCommunicationException communicationException = new ScmCommunicationException("unknown");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", communicationException));
    assertTrue(exception instanceof ServerException);
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 17 with ScmCommunicationException

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

the class AuthorizingFileContentProvider method fetchContent.

@Override
public String fetchContent(String fileURL) throws IOException, DevfileException {
    final String requestURL = formatUrl(fileURL);
    try {
        Optional<PersonalAccessToken> token = personalAccessTokenManager.get(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
        if (token.isPresent()) {
            PersonalAccessToken personalAccessToken = token.get();
            String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
            gitCredentialManager.createOrReplace(personalAccessToken);
            return content;
        } else {
            try {
                return urlFetcher.fetch(requestURL);
            } catch (IOException exception) {
                if (exception instanceof SSLException) {
                    ScmCommunicationException cause = new ScmCommunicationException(String.format("Failed to fetch a content from URL %s due to TLS key misconfiguration. Please refer to the docs about how to correctly import it. ", requestURL));
                    throw new DevfileException(exception.getMessage(), cause);
                } else if (exception instanceof FileNotFoundException) {
                    if (isPublicRepository(remoteFactoryUrl)) {
                        // for public repo-s return 404 as-is
                        throw exception;
                    }
                }
                // unable to determine exact cause, so let's just try to authorize...
                try {
                    PersonalAccessToken personalAccessToken = personalAccessTokenManager.fetchAndSave(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
                    String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
                    gitCredentialManager.createOrReplace(personalAccessToken);
                    return content;
                } catch (ScmUnauthorizedException | UnknownScmProviderException e) {
                    throw new DevfileException(e.getMessage(), e);
                } catch (ScmCommunicationException e) {
                    throw new IOException(String.format("Failed to fetch a content from URL %s. Make sure the URL" + " is correct. For private repository, make sure authentication is configured." + " Additionally, if you're using " + " relative form, make sure the referenced file are actually stored" + " relative to the devfile on the same host," + " or try to specify URL in absolute form. The current attempt to authenticate" + " request, failed with the following error message: %s", fileURL, e.getMessage()), e);
                }
            }
        }
    } catch (ScmConfigurationPersistenceException | UnsatisfiedScmPreconditionException | ScmUnauthorizedException | ScmCommunicationException e) {
        throw new DevfileException(e.getMessage(), e);
    }
}
Also used : ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) UnsatisfiedScmPreconditionException(org.eclipse.che.api.factory.server.scm.exception.UnsatisfiedScmPreconditionException) ScmConfigurationPersistenceException(org.eclipse.che.api.factory.server.scm.exception.ScmConfigurationPersistenceException)

Example 18 with ScmCommunicationException

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

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

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

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

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