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