use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project che-server by eclipse-che.
the class DevfileToApiExceptionMapperTest method shouldReturnBadrequestExceptionWhenCauseIsOtherException.
@Test
public void shouldReturnBadrequestExceptionWhenCauseIsOtherException() {
ScmItemNotFoundException itemNotFoundException = new ScmItemNotFoundException("unknown");
ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", itemNotFoundException));
assertTrue(exception instanceof BadRequestException);
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project che-server by eclipse-che.
the class DevfileToApiExceptionMapperTest method shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized.
@Test
public void shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized() {
ScmUnauthorizedException scmUnauthorizedException = new ScmUnauthorizedException("msg", "gitlab", "2.0", "http://gitlab.com/oauth/authenticate");
ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmUnauthorizedException));
assertTrue(exception instanceof UnauthorizedException);
assertEquals(((ExtendedError) exception.getServiceError()).getErrorCode(), 401);
assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().size(), 3);
assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_version"), "2.0");
assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_authentication_url"), "http://gitlab.com/oauth/authenticate");
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project che-server by eclipse-che.
the class URLFactoryBuilderTest method testShouldReturnV2WithDevworkspacesDisabled.
@Test
public void testShouldReturnV2WithDevworkspacesDisabled() throws ApiException, DevfileException {
String myLocation = "http://foo-location/";
Map<String, Object> devfileAsMap = Map.of("hello", "there", "how", "are", "you", "?");
JsonNode devfile = new ObjectNode(JsonNodeFactory.instance);
when(devfileParser.parseYamlRaw(anyString())).thenReturn(devfile);
when(devfileParser.convertYamlToMap(devfile)).thenReturn(devfileAsMap);
when(devfileVersionDetector.devfileMajorVersion(devfile)).thenReturn(2);
URLFactoryBuilder localUrlFactoryBuilder = new URLFactoryBuilder(defaultEditor, defaultPlugin, false, devfileParser, devfileVersionDetector);
FactoryMetaDto factory = localUrlFactoryBuilder.createFactoryFromDevfile(new DefaultFactoryUrl().withDevfileFileLocation(myLocation), s -> myLocation + ".list", emptyMap()).get();
assertNotNull(factory);
assertTrue(factory instanceof FactoryDevfileV2Dto);
assertEquals(((FactoryDevfileV2Dto) factory).getDevfile(), devfileAsMap);
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project che-server by eclipse-che.
the class URLFactoryBuilderTest method testDevfileV2.
@Test
public void testDevfileV2() throws ApiException, DevfileException {
String myLocation = "http://foo-location/";
Map<String, Object> devfileAsMap = Map.of("hello", "there", "how", "are", "you", "?");
JsonNode devfile = new ObjectNode(JsonNodeFactory.instance);
when(devfileParser.parseYamlRaw(anyString())).thenReturn(devfile);
when(devfileParser.convertYamlToMap(devfile)).thenReturn(devfileAsMap);
when(devfileVersionDetector.devfileMajorVersion(devfile)).thenReturn(2);
FactoryMetaDto factory = urlFactoryBuilder.createFactoryFromDevfile(new DefaultFactoryUrl().withDevfileFileLocation(myLocation), s -> myLocation + ".list", emptyMap()).get();
assertNotNull(factory);
assertNull(factory.getSource());
assertTrue(factory instanceof FactoryDevfileV2Dto);
assertEquals(((FactoryDevfileV2Dto) factory).getDevfile(), devfileAsMap);
}
use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException 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);
}
}
Aggregations