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);
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations