Search in sources :

Example 1 with StackIcon

use of org.eclipse.che.api.workspace.server.stack.image.StackIcon in project che by eclipse.

the class StackLoader method setIconData.

private void setIconData(StackImpl stack, Path stackIconFolderPath) {
    StackIcon stackIcon = stack.getStackIcon();
    if (stackIcon == null) {
        return;
    }
    try {
        Path stackIconPath = stackIconFolderPath.resolve(stackIcon.getName());
        if (Files.exists(stackIconPath) && Files.isRegularFile(stackIconPath)) {
            stackIcon = new StackIcon(stackIcon.getName(), stackIcon.getMediaType(), Files.readAllBytes(stackIconPath));
            stack.setStackIcon(stackIcon);
        } else {
            throw new IOException("Stack icon is not a file or doesn't exist by path: " + stackIconPath);
        }
    } catch (IOException e) {
        stack.setStackIcon(null);
        LOG.error(format("Failed to load stack icon data for the stack with id '%s'", stack.getId()), e);
    }
}
Also used : Path(java.nio.file.Path) StackIcon(org.eclipse.che.api.workspace.server.stack.image.StackIcon) IOException(java.io.IOException)

Example 2 with StackIcon

use of org.eclipse.che.api.workspace.server.stack.image.StackIcon in project che by eclipse.

the class StackService method uploadIcon.

@POST
@Path("/{id}/icon")
@Consumes(MULTIPART_FORM_DATA)
@Produces(TEXT_PLAIN)
@GenerateLink(rel = LINK_REL_UPLOAD_ICON)
@ApiOperation(value = "Upload icon for required stack", notes = "This operation can be performed only by authorized stack owner")
@ApiResponses({ @ApiResponse(code = 200, message = "Image was successfully uploaded"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have access upload image for stack with required id"), @ApiResponse(code = 404, message = "The stack doesn't exist"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public Response uploadIcon(@ApiParam("The image for stack") final Iterator<FileItem> formData, @ApiParam("The stack id") @PathParam("id") final String id) throws NotFoundException, ServerException, BadRequestException, ForbiddenException, ConflictException {
    if (formData.hasNext()) {
        FileItem fileItem = formData.next();
        StackIcon stackIcon = new StackIcon(fileItem.getName(), fileItem.getContentType(), fileItem.get());
        StackImpl stack = stackDao.getById(id);
        stack.setStackIcon(stackIcon);
        stackDao.update(stack);
    }
    return Response.ok().build();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) StackImpl(org.eclipse.che.api.workspace.server.model.impl.stack.StackImpl) StackIcon(org.eclipse.che.api.workspace.server.stack.image.StackIcon) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with StackIcon

use of org.eclipse.che.api.workspace.server.stack.image.StackIcon in project che by eclipse.

the class StackService method asStackDto.

private StackDto asStackDto(StackImpl stack) {
    final UriBuilder builder = getServiceContext().getServiceUriBuilder();
    List<Link> links = new ArrayList<>();
    final Link removeLink = LinksHelper.createLink("DELETE", builder.clone().path(getClass(), "removeStack").build(stack.getId()).toString(), LINK_REL_REMOVE_STACK);
    final Link getLink = LinksHelper.createLink("GET", builder.clone().path(getClass(), "getStack").build(stack.getId()).toString(), APPLICATION_JSON, LINK_REL_GET_STACK_BY_ID);
    links.add(removeLink);
    links.add(getLink);
    StackIcon stackIcon = stack.getStackIcon();
    if (stackIcon != null) {
        Link deleteIcon = LinksHelper.createLink("DELETE", builder.clone().path(getClass(), "removeIcon").build(stack.getId()).toString(), stackIcon.getMediaType(), LINK_REL_DELETE_ICON);
        Link getIconLink = LinksHelper.createLink("GET", builder.clone().path(getClass(), "getIcon").build(stack.getId()).toString(), stackIcon.getMediaType(), LINK_REL_GET_ICON);
        links.add(deleteIcon);
        links.add(getIconLink);
    }
    return asDto(stack).withLinks(links);
}
Also used : ArrayList(java.util.ArrayList) StackIcon(org.eclipse.che.api.workspace.server.stack.image.StackIcon) UriBuilder(javax.ws.rs.core.UriBuilder) Link(org.eclipse.che.api.core.rest.shared.dto.Link) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink)

Example 4 with StackIcon

use of org.eclipse.che.api.workspace.server.stack.image.StackIcon in project che by eclipse.

the class StackService method getIcon.

@GET
@Path("/{id}/icon")
@Produces("image/*")
@GenerateLink(rel = LINK_REL_GET_ICON)
@ApiOperation(value = "Get icon by stack id", notes = "This operation can be performed only by authorized user", response = byte[].class)
@ApiResponses({ @ApiResponse(code = 200, message = "The response contains requested image entity"), @ApiResponse(code = 403, message = "The user does not have access to get image entity"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public Response getIcon(@ApiParam("The stack id") @PathParam("id") final String id) throws NotFoundException, ServerException, BadRequestException {
    StackImpl stack = stackDao.getById(id);
    if (stack == null) {
        throw new NotFoundException("Stack with id '" + id + "' was not found.");
    }
    StackIcon image = stack.getStackIcon();
    if (image == null) {
        throw new NotFoundException("Image for stack with id '" + id + "' was not found.");
    }
    return Response.ok(image.getData(), image.getMediaType()).build();
}
Also used : StackImpl(org.eclipse.che.api.workspace.server.model.impl.stack.StackImpl) NotFoundException(org.eclipse.che.api.core.NotFoundException) StackIcon(org.eclipse.che.api.workspace.server.stack.image.StackIcon) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with StackIcon

use of org.eclipse.che.api.workspace.server.stack.image.StackIcon in project che by eclipse.

the class StackServiceTest method setUp.

@BeforeMethod
public void setUp() throws NoSuchFieldException, IllegalAccessException {
    byte[] fileContent = STACK_ID.getBytes();
    stackIcon = new StackIcon(ICON_MEDIA_TYPE, "image/svg+xml", fileContent);
    componentsImpl = singletonList(new StackComponentImpl(COMPONENT_NAME, COMPONENT_VERSION));
    stackSourceImpl = new StackSourceImpl(SOURCE_TYPE, SOURCE_ORIGIN);
    CommandImpl command = new CommandImpl(COMMAND_NAME, COMMAND_LINE, COMMAND_TYPE);
    EnvironmentImpl environment = new EnvironmentImpl(null, null);
    WorkspaceConfigImpl workspaceConfig = WorkspaceConfigImpl.builder().setName(WORKSPACE_CONFIG_NAME).setDefaultEnv(DEF_ENVIRONMENT_NAME).setCommands(singletonList(command)).setEnvironments(singletonMap(ENVIRONMENT_NAME, environment)).build();
    stackSourceDto = newDto(StackSourceDto.class).withType(SOURCE_TYPE).withOrigin(SOURCE_ORIGIN);
    StackComponentDto stackComponentDto = newDto(StackComponentDto.class).withName(COMPONENT_NAME).withVersion(COMPONENT_VERSION);
    componentsDto = singletonList(stackComponentDto);
    stackDto = DtoFactory.getInstance().createDto(StackDto.class).withId(STACK_ID).withName(NAME).withDescription(DESCRIPTION).withScope(SCOPE).withCreator(CREATOR).withTags(tags).withSource(stackSourceDto).withComponents(componentsDto);
    stackImpl = StackImpl.builder().setId(STACK_ID).setName(NAME).setDescription(DESCRIPTION).setScope(SCOPE).setCreator(CREATOR).setTags(tags).setSource(stackSourceImpl).setComponents(componentsImpl).setWorkspaceConfig(workspaceConfig).setStackIcon(stackIcon).build();
    foreignStack = StackImpl.builder().setId(STACK_ID).setName(NAME).setDescription(DESCRIPTION).setScope(SCOPE).setCreator(FOREIGN_CREATOR).setTags(tags).setSource(stackSourceImpl).setComponents(componentsImpl).setWorkspaceConfig(workspaceConfig).setStackIcon(stackIcon).build();
    when(uriInfo.getBaseUriBuilder()).thenReturn(new UriBuilderImpl());
    final Field uriField = service.getClass().getSuperclass().getDeclaredField("uriInfo");
    uriField.setAccessible(true);
    uriField.set(service, uriInfo);
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) Field(java.lang.reflect.Field) StackComponentImpl(org.eclipse.che.api.workspace.server.model.impl.stack.StackComponentImpl) StackComponentDto(org.eclipse.che.api.workspace.shared.dto.stack.StackComponentDto) StackDto(org.eclipse.che.api.workspace.shared.dto.stack.StackDto) StackIcon(org.eclipse.che.api.workspace.server.stack.image.StackIcon) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) StackSourceImpl(org.eclipse.che.api.workspace.server.model.impl.stack.StackSourceImpl) StackSourceDto(org.eclipse.che.api.workspace.shared.dto.stack.StackSourceDto) UriBuilderImpl(org.everrest.core.impl.uri.UriBuilderImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

StackIcon (org.eclipse.che.api.workspace.server.stack.image.StackIcon)7 StackImpl (org.eclipse.che.api.workspace.server.model.impl.stack.StackImpl)4 GenerateLink (org.eclipse.che.api.core.rest.annotations.GenerateLink)3 StackComponentImpl (org.eclipse.che.api.workspace.server.model.impl.stack.StackComponentImpl)3 StackSourceImpl (org.eclipse.che.api.workspace.server.model.impl.stack.StackSourceImpl)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 FileItem (org.apache.commons.fileupload.FileItem)1 NotFoundException (org.eclipse.che.api.core.NotFoundException)1