use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class WorkspaceManagerTest method shouldRemoveTemporaryWorkspaceAfterStartFailed.
@Test
public void shouldRemoveTemporaryWorkspaceAfterStartFailed() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
workspace.setTemporary(true);
mockRuntime(workspace, RUNNING);
doThrow(new ServerException("")).when(runtimes).stop(workspace.getId());
workspaceManager.stopWorkspace(workspace.getId());
captureRunAsyncCallsAndRunSynchronously();
verify(workspaceDao).remove(workspace.getId());
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class FactoryManager method getFactorySnippet.
/**
* Gets factory snippet by factory id and snippet type.
* If snippet type is not set, "url" type will be used as default.
*
* @param factoryId
* id of factory
* @param snippetType
* type of snippet
* @param baseUri
* URI from which will be created snippet
* @return snippet content or null when snippet type not found.
* @throws NotFoundException
* when factory with specified id doesn't not found
* @throws ServerException
* when any server error occurs during snippet creation
*/
public String getFactorySnippet(String factoryId, String snippetType, URI baseUri) throws NotFoundException, ServerException {
requireNonNull(factoryId);
final String baseUrl = UriBuilder.fromUri(baseUri).replacePath("").build().toString();
switch(firstNonNull(snippetType, URL_SNIPPET_TYPE)) {
case URL_SNIPPET_TYPE:
return UriBuilder.fromUri(baseUri).replacePath("factory").queryParam("id", factoryId).build().toString();
case HTML_SNIPPET_TYPE:
return SnippetGenerator.generateHtmlSnippet(baseUrl, factoryId);
case IFRAME_SNIPPET_TYPE:
return SnippetGenerator.generateiFrameSnippet(baseUrl, factoryId);
case MARKDOWN_SNIPPET_TYPE:
final Set<FactoryImage> images = getFactoryImages(factoryId);
final String imageId = (images.size() > 0) ? images.iterator().next().getName() : null;
try {
return SnippetGenerator.generateMarkdownSnippet(baseUrl, getById(factoryId), imageId);
} catch (IllegalArgumentException e) {
throw new ServerException(e.getLocalizedMessage());
}
default:
// when the specified type is not supported
return null;
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class FactoryService method getFactoryByAttribute.
@GET
@Path("/find")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get factory by attribute, " + "the attribute must match one of the Factory model fields with type 'String', " + "e.g. (factory.name, factory.creator.name)", notes = "If specify more than one value for a single query parameter then will be taken the first one")
@ApiResponses({ @ApiResponse(code = 200, message = "Response contains list requested factories"), @ApiResponse(code = 400, message = "When query does not contain at least one attribute to search for"), @ApiResponse(code = 500, message = "Internal server error") })
public List<FactoryDto> getFactoryByAttribute(@DefaultValue("0") @QueryParam("skipCount") Integer skipCount, @DefaultValue("30") @QueryParam("maxItems") Integer maxItems, @Context UriInfo uriInfo) throws BadRequestException, ServerException {
final Set<String> skip = ImmutableSet.of("token", "skipCount", "maxItems");
final List<Pair<String, String>> query = URLEncodedUtils.parse(uriInfo.getRequestUri()).entrySet().stream().filter(param -> !skip.contains(param.getKey()) && !param.getValue().isEmpty()).map(entry -> Pair.of(entry.getKey(), entry.getValue().iterator().next())).collect(toList());
checkArgument(!query.isEmpty(), "Query must contain at least one attribute");
final List<FactoryDto> factories = new ArrayList<>();
for (Factory factory : factoryManager.getByAttribute(maxItems, skipCount, query)) {
factories.add(injectLinks(asDto(factory), null));
}
return factories;
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class FactoryService method processDefaults.
/**
* Checks the current user if it is not temporary then
* adds to the factory creator information and time of creation
*/
private void processDefaults(FactoryDto factory) throws ForbiddenException {
try {
final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
final User user = userManager.getById(userId);
if (user == null || parseBoolean(preferenceManager.find(userId).get("temporary"))) {
throw new ForbiddenException("Current user is not allowed to use this method.");
}
factory.setCreator(DtoFactory.newDto(AuthorDto.class).withUserId(userId).withName(user.getName()).withEmail(user.getEmail()).withCreated(System.currentTimeMillis()));
} catch (NotFoundException | ServerException ex) {
throw new ForbiddenException("Current user is not allowed to use this method");
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class JpaRecipeDao method getById.
@Override
@Transactional
public RecipeImpl getById(String id) throws NotFoundException, ServerException {
requireNonNull(id);
try {
final EntityManager manager = managerProvider.get();
final RecipeImpl recipe = manager.find(RecipeImpl.class, id);
if (recipe == null) {
throw new NotFoundException(format("Recipe with id '%s' doesn't exist", id));
}
return recipe;
} catch (RuntimeException ex) {
throw new ServerException(ex.getLocalizedMessage(), ex);
}
}
Aggregations