Search in sources :

Example 41 with NotFoundException

use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.

the class ProjectManager method moveTo.

/**
     * Moves item to the new path
     *
     * @param itemPath path to the item
     * @param newParentPath path of new parent
     * @param newName new item's name
     * @param overwrite whether existed (if any) item should be overwritten
     *
     * @return new item
     * @throws ServerException
     * @throws NotFoundException
     * @throws ConflictException
     * @throws ForbiddenException
     */
public VirtualFileEntry moveTo(String itemPath, String newParentPath, String newName, boolean overwrite) throws ServerException, NotFoundException, ConflictException, ForbiddenException {
    final VirtualFile oldItem = vfs.getRoot().getChild(Path.of(itemPath));
    if (oldItem == null) {
        throw new NotFoundException("Item not found " + itemPath);
    }
    final VirtualFile newParent;
    if (newParentPath == null) {
        // rename only
        newParent = oldItem.getParent();
    } else {
        newParent = vfs.getRoot().getChild(Path.of(newParentPath));
    }
    if (newParent == null) {
        throw new NotFoundException("New parent not found " + newParentPath);
    }
    // TODO lock token ?
    final VirtualFile newItem = oldItem.moveTo(newParent, newName, overwrite, null);
    final RegisteredProject owner = projectRegistry.getParentProject(newItem.getPath().toString());
    if (owner == null) {
        throw new NotFoundException("Parent project not found " + newItem.getPath().toString());
    }
    final VirtualFileEntry move;
    if (newItem.isFile()) {
        move = new FileEntry(newItem, projectRegistry);
    } else {
        move = new FolderEntry(newItem, projectRegistry);
    }
    if (move.isProject()) {
        final RegisteredProject project = projectRegistry.getProject(itemPath);
        NewProjectConfig projectConfig = new NewProjectConfigImpl(newItem.getPath().toString(), project.getType(), project.getMixins(), newName, project.getDescription(), project.getAttributes(), null, project.getSource());
        if (move instanceof FolderEntry) {
            projectRegistry.removeProjects(project.getPath());
            updateProject(projectConfig);
        }
    }
    return move;
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) NotFoundException(org.eclipse.che.api.core.NotFoundException) NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig)

Example 42 with NotFoundException

use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.

the class ProjectManager method doImportProject.

/** Note: Use {@link FileWatcherManager#suspend()} and {@link FileWatcherManager#resume()} while importing source code */
private RegisteredProject doImportProject(String path, SourceStorage sourceStorage, boolean rewrite, LineConsumerFactory lineConsumerFactory) throws ServerException, IOException, ForbiddenException, UnauthorizedException, ConflictException, NotFoundException {
    final ProjectImporter importer = importers.getImporter(sourceStorage.getType());
    if (importer == null) {
        throw new NotFoundException(format("Unable import sources project from '%s'. Sources type '%s' is not supported.", sourceStorage.getLocation(), sourceStorage.getType()));
    }
    String normalizePath = (path.startsWith("/")) ? path : "/".concat(path);
    FolderEntry folder = asFolder(normalizePath);
    if (folder != null && !rewrite) {
        throw new ConflictException(format("Project %s already exists ", path));
    }
    if (folder == null) {
        folder = getProjectsRoot().createFolder(normalizePath);
    }
    try {
        importer.importSources(folder, sourceStorage, lineConsumerFactory);
    } catch (final Exception e) {
        folder.remove();
        throw e;
    }
    final String name = folder.getPath().getName();
    for (ProjectConfig project : workspaceProjectsHolder.getProjects()) {
        if (normalizePath.equals(project.getPath())) {
            // TODO Needed for factory project importing with keepDir. It needs to find more appropriate solution
            List<String> innerProjects = projectRegistry.getProjects(normalizePath);
            for (String innerProject : innerProjects) {
                RegisteredProject registeredProject = projectRegistry.getProject(innerProject);
                projectRegistry.putProject(registeredProject, asFolder(registeredProject.getPath()), true, false);
            }
            RegisteredProject rp = projectRegistry.putProject(project, folder, true, false);
            workspaceProjectsHolder.sync(projectRegistry);
            return rp;
        }
    }
    RegisteredProject rp = projectRegistry.putProject(new NewProjectConfigImpl(normalizePath, name, BaseProjectType.ID, sourceStorage), folder, true, false);
    workspaceProjectsHolder.sync(projectRegistry);
    return rp;
}
Also used : ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ConflictException(org.eclipse.che.api.core.ConflictException) NotFoundException(org.eclipse.che.api.core.NotFoundException) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ConflictException(org.eclipse.che.api.core.ConflictException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ProjectImporter(org.eclipse.che.api.project.server.importer.ProjectImporter)

Example 43 with NotFoundException

use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.

the class ProjectManager method copyTo.

/**
     * Copies item to new path with
     *
     * @param itemPath path to item to copy
     * @param newParentPath path where the item should be copied to
     * @param newName new item name
     * @param overwrite whether existed (if any) item should be overwritten
     *
     * @return new item
     * @throws ServerException
     * @throws NotFoundException
     * @throws ConflictException
     * @throws ForbiddenException
     */
public VirtualFileEntry copyTo(String itemPath, String newParentPath, String newName, boolean overwrite) throws ServerException, NotFoundException, ConflictException, ForbiddenException {
    VirtualFile oldItem = vfs.getRoot().getChild(Path.of(itemPath));
    if (oldItem == null) {
        throw new NotFoundException("Item not found " + itemPath);
    }
    VirtualFile newParent = vfs.getRoot().getChild(Path.of(newParentPath));
    if (newParent == null) {
        throw new NotFoundException("New parent not found " + newParentPath);
    }
    final VirtualFile newItem = oldItem.copyTo(newParent, newName, overwrite);
    final RegisteredProject owner = projectRegistry.getParentProject(newItem.getPath().toString());
    if (owner == null) {
        throw new NotFoundException("Parent project not found " + newItem.getPath().toString());
    }
    final VirtualFileEntry copy;
    if (newItem.isFile()) {
        copy = new FileEntry(newItem, projectRegistry);
    } else {
        copy = new FolderEntry(newItem, projectRegistry);
    }
    if (copy.isProject()) {
        projectRegistry.getProject(copy.getProject()).getTypes();
    // fire event
    }
    return copy;
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Example 44 with NotFoundException

use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.

the class MachineProviderImpl method startService.

@Override
public Instance startService(String namespace, String workspaceId, String envName, String machineName, boolean isDev, String networkName, CheServiceImpl service, LineConsumer machineLogger) throws ServerException {
    // copy to not affect/be affected by changes in origin
    service = new CheServiceImpl(service);
    ProgressLineFormatterImpl progressLineFormatter = new ProgressLineFormatterImpl();
    ProgressMonitor progressMonitor = currentProgressStatus -> {
        try {
            machineLogger.writeLine(progressLineFormatter.format(currentProgressStatus));
        } catch (IOException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    };
    String container = null;
    try {
        String image = prepareImage(machineName, service, progressMonitor);
        container = createContainer(workspaceId, machineName, isDev, image, networkName, service);
        connectContainerToAdditionalNetworks(container, service);
        docker.startContainer(StartContainerParams.create(container));
        readContainerLogsInSeparateThread(container, workspaceId, service.getId(), machineLogger);
        DockerNode node = dockerMachineFactory.createNode(workspaceId, container);
        dockerInstanceStopDetector.startDetection(container, service.getId(), workspaceId);
        final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
        MachineImpl machine = new MachineImpl(MachineConfigImpl.builder().setDev(isDev).setName(machineName).setType("docker").setLimits(new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"))).setSource(new MachineSourceImpl(service.getBuild() != null ? "context" : "image").setLocation(service.getBuild() != null ? service.getBuild().getContext() : service.getImage())).build(), service.getId(), workspaceId, envName, userId, MachineStatus.RUNNING, null);
        return dockerMachineFactory.createInstance(machine, container, image, node, machineLogger);
    } catch (SourceNotFoundException e) {
        throw e;
    } catch (RuntimeException | ServerException | NotFoundException | IOException e) {
        cleanUpContainer(container);
        throw new ServerException(e.getLocalizedMessage(), e);
    }
}
Also used : ConnectContainer(org.eclipse.che.plugin.docker.client.json.network.ConnectContainer) RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) Arrays(java.util.Arrays) MachineStatus(org.eclipse.che.api.core.model.machine.MachineStatus) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) DockerConnectorProvider(org.eclipse.che.plugin.docker.client.DockerConnectorProvider) Volume(org.eclipse.che.plugin.docker.client.json.Volume) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) NetworkingConfig(org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig) LoggingUncaughtExceptionHandler(org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler) FileCleaner(org.eclipse.che.api.core.util.FileCleaner) Collectors.toSet(java.util.stream.Collectors.toSet) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) GetContainerLogsParams(org.eclipse.che.plugin.docker.client.params.GetContainerLogsParams) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) Set(java.util.Set) Nullable(org.eclipse.che.commons.annotation.Nullable) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) WindowsPathEscaper(org.eclipse.che.commons.lang.os.WindowsPathEscaper) ConnectContainerToNetworkParams(org.eclipse.che.plugin.docker.client.params.network.ConnectContainerToNetworkParams) List(java.util.List) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Pattern(java.util.regex.Pattern) UserSpecificDockerRegistryCredentialsProvider(org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) EndpointConfig(org.eclipse.che.plugin.docker.client.json.network.EndpointConfig) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) Size(org.eclipse.che.commons.lang.Size) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) ArrayList(java.util.ArrayList) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) LATEST_TAG(org.eclipse.che.plugin.docker.machine.DockerInstance.LATEST_TAG) CreateNetworkParams(org.eclipse.che.plugin.docker.client.params.network.CreateNetworkParams) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) SocketTimeoutException(java.net.SocketTimeoutException) MachineInstanceProvider(org.eclipse.che.api.environment.server.MachineInstanceProvider) Collections.singletonMap(java.util.Collections.singletonMap) Named(javax.inject.Named) Instance(org.eclipse.che.api.machine.server.spi.Instance) ExecutorService(java.util.concurrent.ExecutorService) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) Files(java.nio.file.Files) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) FileWriter(java.io.FileWriter) MoreObjects(com.google.common.base.MoreObjects) ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) IOException(java.io.IOException) SystemInfo(org.eclipse.che.api.core.util.SystemInfo) RemoveNetworkParams(org.eclipse.che.plugin.docker.client.params.RemoveNetworkParams) NotFoundException(org.eclipse.che.api.core.NotFoundException) File(java.io.File) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DockerNode(org.eclipse.che.plugin.docker.machine.node.DockerNode) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) ServerException(org.eclipse.che.api.core.ServerException) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) ProgressLineFormatterImpl(org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) NetworkNotFoundException(org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ContainerNotFoundException(org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException) MachineLimitsImpl(org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl) IOException(java.io.IOException) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor)

Example 45 with NotFoundException

use of org.eclipse.che.api.core.NotFoundException in project che by eclipse.

the class DockerAbandonedResourcesCleaner method cleanContainers.

/**
     * Cleans up CHE docker containers which don't tracked by API any more.
     */
@VisibleForTesting
void cleanContainers() {
    List<String> activeContainers = new ArrayList<>();
    try {
        for (ContainerListEntry container : dockerConnector.listContainers()) {
            String containerName = container.getNames()[0];
            Optional<ContainerNameInfo> optional = nameGenerator.parse(containerName);
            if (optional.isPresent()) {
                try {
                    // container is orphaned if not found exception is thrown
                    environmentEngine.getMachine(optional.get().getWorkspaceId(), optional.get().getMachineId());
                    activeContainers.add(containerName);
                } catch (NotFoundException e) {
                    cleanUpContainer(container);
                } catch (Exception e) {
                    LOG.error(format("Failed to check activity for container with name '%s'. Cause: %s", containerName, e.getLocalizedMessage()), e);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("Failed to get list docker containers", e);
    } catch (Exception e) {
        LOG.error("Failed to clean up inactive containers", e);
    }
    LOG.info("List containers registered in the api: " + activeContainers);
}
Also used : ContainerNameInfo(org.eclipse.che.plugin.docker.machine.DockerContainerNameGenerator.ContainerNameInfo) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) ArrayList(java.util.ArrayList) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

NotFoundException (org.eclipse.che.api.core.NotFoundException)72 ServerException (org.eclipse.che.api.core.ServerException)29 ConflictException (org.eclipse.che.api.core.ConflictException)19 Transactional (com.google.inject.persist.Transactional)16 IOException (java.io.IOException)13 EntityManager (javax.persistence.EntityManager)13 Path (javax.ws.rs.Path)13 Test (org.testng.annotations.Test)12 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)11 Produces (javax.ws.rs.Produces)10 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 ArrayList (java.util.ArrayList)9 BadRequestException (org.eclipse.che.api.core.BadRequestException)9 Instance (org.eclipse.che.api.machine.server.spi.Instance)9 GET (javax.ws.rs.GET)7 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)7 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)6 MachineImpl (org.eclipse.che.api.machine.server.model.impl.MachineImpl)6 SnapshotImpl (org.eclipse.che.api.machine.server.model.impl.SnapshotImpl)6