Search in sources :

Example 91 with ServerException

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

the class LocalVirtualFileSystem method doCopy.

private void doCopy(LocalVirtualFile from, LocalVirtualFile to) throws ServerException {
    try {
        // First copy metadata (properties) for source. If we do in this way and fail cause to any i/o or other error client
        // will see error and may try to copy again. But if we successfully copy tree (or single file) and then fail to copy
        // metadata client may not try to copy again because copy destination already exists.
        final File fromMetadataFile = getMetadataIoFile(from.getPath());
        final File toMetadataFile = getMetadataIoFile(to.getPath());
        if (fromMetadataFile.exists()) {
            IoUtil.copy(fromMetadataFile, toMetadataFile, null);
        }
        IoUtil.copy(from.toIoFile(), to.toIoFile(), VFS_LOCK_FILTER);
    } catch (IOException e) {
        String errorMessage = String.format("Unable copy '%s' to '%s'", from, to);
        LOG.error(errorMessage + "\n" + e.getMessage(), e);
        throw new ServerException(errorMessage);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) IOException(java.io.IOException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 92 with ServerException

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

the class LocalVirtualFileSystem method doUnlock.

private void doUnlock(LocalVirtualFile virtualFile) throws ForbiddenException, ServerException {
    try {
        final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
        if (!fileLockIoFile.delete()) {
            if (fileLockIoFile.exists()) {
                throw new IOException(String.format("Unable delete lock file %s", fileLockIoFile));
            }
        }
        lockTokensCache.put(virtualFile.getPath(), NO_LOCK);
    } catch (IOException e) {
        String errorMessage = String.format("Unable unlock file '%s'", virtualFile.getPath());
        LOG.error(errorMessage + "\n" + e.getMessage(), e);
        throw new ServerException(errorMessage);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) IOException(java.io.IOException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 93 with ServerException

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

the class LocalVirtualFileSystem method doLock.

private String doLock(LocalVirtualFile virtualFile, long timeout) throws ConflictException, ServerException {
    try {
        if (NO_LOCK == lockTokensCache.get(virtualFile.getPath())) {
            final FileLock lock = createLock(timeout);
            final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
            fileLockIoFile.getParentFile().mkdirs();
            try (DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileLockIoFile)))) {
                locksSerializer.write(dos, lock);
            }
            lockTokensCache.put(virtualFile.getPath(), lock);
            return lock.getLockToken();
        }
        throw new ConflictException(String.format("Unable lock file '%s'. File already locked", virtualFile.getPath()));
    } catch (IOException | ExecutionException e) {
        String errorMessage = String.format("Unable lock file '%s'", virtualFile.getPath());
        if (e instanceof ExecutionException) {
            LOG.error(errorMessage + "\n" + e.getCause().getMessage(), e.getCause());
        } else {
            LOG.error(errorMessage + "\n" + e.getMessage(), e);
        }
        throw new ServerException(errorMessage);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 94 with ServerException

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

the class LocalVirtualFileSystem method doUpdateProperties.

private void doUpdateProperties(LocalVirtualFile virtualFile, Map<String, String> updates) throws ServerException {
    try {
        final Map<String, String> properties = getProperties(virtualFile);
        for (Map.Entry<String, String> entry : updates.entrySet()) {
            if (entry.getValue() == null) {
                properties.remove(entry.getKey());
            } else {
                properties.put(entry.getKey(), entry.getValue());
            }
        }
        final File metadataIoFile = getMetadataIoFile(virtualFile.getPath());
        if (properties.isEmpty()) {
            if (!metadataIoFile.delete()) {
                if (metadataIoFile.exists()) {
                    LOG.error("Unable delete metadata file {}", metadataIoFile);
                    throw new IOException(String.format("Unable update properties of item '%s'", virtualFile.getPath()));
                }
            }
        } else {
            metadataIoFile.getParentFile().mkdirs();
            try (DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(metadataIoFile)))) {
                metadataSerializer.write(dos, properties);
            }
        }
        metadataCache.put(virtualFile.getPath(), properties);
        if (!virtualFile.toIoFile().setLastModified(System.currentTimeMillis())) {
            LOG.warn("Unable to set timestamp to '{}'", virtualFile.toIoFile());
        }
    } catch (IOException e) {
        String errorMessage = String.format("Unable lock file '%s'", virtualFile.getPath());
        LOG.error(errorMessage + "\n" + e.getMessage(), e);
        throw new ServerException(errorMessage);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 95 with ServerException

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

the class LocalVirtualFileSystem method unzip.

void unzip(LocalVirtualFile parent, InputStream zipped, boolean overwrite, int stripNumber) throws ForbiddenException, ConflictException, ServerException {
    if (archiverFactory == null)
        throw new ServerException("VFS: Could not create zip archiver. Archiver Factory is not properly configured (is null)");
    if (parent.isFolder()) {
        extract(archiverFactory.createArchiver(parent, "zip"), zipped, overwrite, stripNumber);
        addInSearcher(parent);
    } else {
        throw new ForbiddenException(String.format("Unable import zip content. Item '%s' is not a folder", parent.getPath()));
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException)

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