use of org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException in project che-server by eclipse-che.
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);
}
}
use of org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcher method fetchPersonalAccessToken.
@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheUser, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
if (!bitbucketServerApiClient.isConnected(scmServerUrl)) {
LOG.debug("not a valid url {} for current fetcher ", scmServerUrl);
return null;
}
final String tokenName = format(TOKEN_NAME_TEMPLATE, cheUser.getUserId(), apiEndpoint.getHost());
try {
BitbucketUser user = bitbucketServerApiClient.getUser(EnvironmentContext.getCurrent().getSubject());
LOG.debug("Current bitbucket user {} ", user);
// cleanup existed
List<BitbucketPersonalAccessToken> existingTokens = bitbucketServerApiClient.getPersonalAccessTokens(user.getSlug()).stream().filter(p -> p.getName().equals(tokenName)).collect(Collectors.toList());
for (BitbucketPersonalAccessToken existedToken : existingTokens) {
LOG.debug("Deleting existed che token {} {}", existedToken.getId(), existedToken.getName());
bitbucketServerApiClient.deletePersonalAccessTokens(user.getSlug(), existedToken.getId());
}
BitbucketPersonalAccessToken token = bitbucketServerApiClient.createPersonalAccessTokens(user.getSlug(), tokenName, DEFAULT_TOKEN_SCOPE);
LOG.debug("Token created = {} for {}", token.getId(), token.getUser());
return new PersonalAccessToken(scmServerUrl, EnvironmentContext.getCurrent().getSubject().getUserId(), user.getName(), valueOf(user.getId()), token.getName(), valueOf(token.getId()), token.getToken());
} catch (ScmBadRequestException | ScmItemNotFoundException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException in project che-server by eclipse-che.
the class HttpBitbucketServerApiClient method createPersonalAccessTokens.
@Override
public BitbucketPersonalAccessToken createPersonalAccessTokens(String userSlug, String tokenName, Set<String> permissions) throws ScmBadRequestException, ScmUnauthorizedException, ScmCommunicationException {
URI uri = serverUri.resolve("/rest/access-tokens/1.0/users/" + userSlug);
try {
HttpRequest request = HttpRequest.newBuilder(uri).PUT(HttpRequest.BodyPublishers.ofString(OM.writeValueAsString(new BitbucketPersonalAccessToken(tokenName, permissions)))).headers(HttpHeaders.AUTHORIZATION, computeAuthorizationHeader("PUT", uri.toString()), HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).timeout(DEFAULT_HTTP_TIMEOUT).build();
LOG.trace("executeRequest={}", request);
return executeRequest(httpClient, request, inputStream -> {
try {
return OM.readValue(inputStream, BitbucketPersonalAccessToken.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
} catch (ScmItemNotFoundException | JsonProcessingException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException in project devspaces-images by redhat-developer.
the class GitlabApiClient method executeRequest.
private <T> T executeRequest(HttpClient httpClient, HttpRequest request, Function<InputStream, T> bodyConverter) throws ScmBadRequestException, ScmItemNotFoundException, ScmCommunicationException {
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_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.ScmItemNotFoundException in project devspaces-images by redhat-developer.
the class GitlabOAuthTokenFetcher method fetchPersonalAccessToken.
@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
scmServerUrl = StringUtils.trimEnd(scmServerUrl, '/');
GitlabApiClient gitlabApiClient = getApiClient(scmServerUrl);
if (gitlabApiClient == null || !gitlabApiClient.isConnected(scmServerUrl)) {
LOG.debug("not a valid url {} for current fetcher ", scmServerUrl);
return null;
}
if (oAuthAPI == null) {
throw new ScmCommunicationException(format("OAuth 2 is not configured for SCM provider [%s]. For details, refer " + "the documentation in section of SCM providers configuration.", OAUTH_PROVIDER_NAME));
}
OAuthToken oAuthToken;
try {
oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME);
GitlabUser user = gitlabApiClient.getUser(oAuthToken.getToken());
PersonalAccessToken token = new PersonalAccessToken(scmServerUrl, cheSubject.getUserId(), user.getUsername(), Long.toString(user.getId()), NameGenerator.generate(OAUTH_2_PREFIX, 5), NameGenerator.generate("id-", 5), oAuthToken.getToken());
Optional<Boolean> valid = isValid(token);
if (valid.isEmpty() || !valid.get()) {
throw new ScmCommunicationException("Current token doesn't have the necessary privileges. Please make sure Che app scopes are correct and containing at least: " + DEFAULT_TOKEN_SCOPES.toString());
}
return token;
} catch (UnauthorizedException e) {
throw new ScmUnauthorizedException(cheSubject.getUserName() + " is not authorized in " + OAUTH_PROVIDER_NAME + " OAuth provider.", OAUTH_PROVIDER_NAME, "2.0", getLocalAuthenticateUrl());
} catch (NotFoundException | ServerException | ForbiddenException | BadRequestException | ScmItemNotFoundException | ScmBadRequestException | ConflictException e) {
LOG.warn(e.getMessage());
throw new ScmCommunicationException(e.getMessage(), e);
}
}
Aggregations