Search in sources :

Example 31 with ServerException

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());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ServerException(org.eclipse.che.api.core.ServerException) Test(org.testng.annotations.Test)

Example 32 with ServerException

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;
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException)

Example 33 with ServerException

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;
}
Also used : URLEncodedUtils(org.eclipse.che.commons.lang.URLEncodedUtils) Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) FactoryBuilder(org.eclipse.che.api.factory.server.builder.FactoryBuilder) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) ApiOperation(io.swagger.annotations.ApiOperation) QueryParam(javax.ws.rs.QueryParam) Service(org.eclipse.che.api.core.rest.Service) PreferenceManager(org.eclipse.che.api.user.server.PreferenceManager) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) DELETE(javax.ws.rs.DELETE) WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ImmutableSet(com.google.common.collect.ImmutableSet) Context(javax.ws.rs.core.Context) Predicate(java.util.function.Predicate) Set(java.util.Set) Pair(org.eclipse.che.commons.lang.Pair) AuthorDto(org.eclipse.che.api.factory.shared.dto.AuthorDto) List(java.util.List) BadRequestException(org.eclipse.che.api.core.BadRequestException) Response(javax.ws.rs.core.Response) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) UriInfo(javax.ws.rs.core.UriInfo) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) FactoryLinksHelper.createLinks(org.eclipse.che.api.factory.server.FactoryLinksHelper.createLinks) PathParam(javax.ws.rs.PathParam) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GET(javax.ws.rs.GET) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ApiException(org.eclipse.che.api.core.ApiException) User(org.eclipse.che.api.core.model.user.User) ConflictException(org.eclipse.che.api.core.ConflictException) MULTIPART_FORM_DATA(javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA) Api(io.swagger.annotations.Api) DtoFactory(org.eclipse.che.dto.server.DtoFactory) CONTENT_DISPOSITION(javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) Iterator(java.util.Iterator) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItem(org.apache.commons.fileupload.FileItem) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) Factory(org.eclipse.che.api.core.model.factory.Factory) Collectors.toList(java.util.stream.Collectors.toList) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) ServerException(org.eclipse.che.api.core.ServerException) ApiResponse(io.swagger.annotations.ApiResponse) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl) PUT(javax.ws.rs.PUT) UserManager(org.eclipse.che.api.user.server.UserManager) Collections(java.util.Collections) InputStream(java.io.InputStream) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) ArrayList(java.util.ArrayList) LoggerFactory(org.slf4j.LoggerFactory) DtoFactory(org.eclipse.che.dto.server.DtoFactory) Factory(org.eclipse.che.api.core.model.factory.Factory) Pair(org.eclipse.che.commons.lang.Pair) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 34 with ServerException

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");
    }
}
Also used : AuthorDto(org.eclipse.che.api.factory.shared.dto.AuthorDto) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) User(org.eclipse.che.api.core.model.user.User) ServerException(org.eclipse.che.api.core.ServerException) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Example 35 with ServerException

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);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) ServerException(org.eclipse.che.api.core.ServerException) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) NotFoundException(org.eclipse.che.api.core.NotFoundException) Transactional(com.google.inject.persist.Transactional)

Aggregations

ServerException (org.eclipse.che.api.core.ServerException)143 IOException (java.io.IOException)51 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)37 NotFoundException (org.eclipse.che.api.core.NotFoundException)37 ConflictException (org.eclipse.che.api.core.ConflictException)32 File (java.io.File)22 Test (org.testng.annotations.Test)20 ArrayList (java.util.ArrayList)19 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)19 List (java.util.List)15 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)15 Map (java.util.Map)13 Transactional (com.google.inject.persist.Transactional)12 BadRequestException (org.eclipse.che.api.core.BadRequestException)12 InputStream (java.io.InputStream)11 Unlocker (org.eclipse.che.commons.lang.concurrent.Unlocker)10 String.format (java.lang.String.format)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 HashMap (java.util.HashMap)8