Search in sources :

Example 1 with DeleteOnCloseFileInputStream

use of org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream in project che by eclipse.

the class LocalVirtualFileSystem method getContent.

InputStream getContent(LocalVirtualFile virtualFile) throws ForbiddenException, ServerException {
    if (virtualFile.isFile()) {
        final PathLockFactory.PathLock lock = pathLockFactory.getLock(virtualFile.getPath(), false).acquire(WAIT_FOR_FILE_LOCK_TIMEOUT);
        File spoolFile = null;
        try {
            final File ioFile = virtualFile.toIoFile();
            final long fileLength = ioFile.length();
            if (fileLength <= MAX_BUFFER_SIZE) {
                return new ByteArrayInputStream(Files.toByteArray(ioFile));
            }
            // Copy this file to be able release the file lock before leave this method.
            spoolFile = File.createTempFile("spool_file", null);
            Files.copy(ioFile, spoolFile);
            return new DeleteOnCloseFileInputStream(spoolFile);
        } catch (IOException e) {
            if (spoolFile != null) {
                FileCleaner.addFile(spoolFile);
            }
            String errorMessage = String.format("Unable get content of '%s'", virtualFile.getPath());
            LOG.error(errorMessage + "\n" + e.getMessage(), e);
            throw new ServerException(errorMessage);
        } finally {
            lock.release();
        }
    } else {
        throw new ForbiddenException(String.format("Unable get content. Item '%s' is not a file", virtualFile.getPath()));
    }
}
Also used : PathLockFactory(org.eclipse.che.api.vfs.PathLockFactory) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DeleteOnCloseFileInputStream(org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 2 with DeleteOnCloseFileInputStream

use of org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream in project che by eclipse.

the class SubversionApi method exportPath.

/**
     * Perform an "svn export" based on the request.
     *
     * @param projectPath
     *         project path
     * @param path
     *         exported path
     * @param revision
     *         specified revision to export
     * @return Response which contains hyperlink with download url
     * @throws IOException
     *         if there is a problem executing the command
     * @throws ServerException
     *         if there is an exporting issue
     */
public Response exportPath(@NotNull final String projectPath, @NotNull final String path, String revision) throws IOException, ServerException, UnauthorizedException {
    final File project = new File(projectPath);
    final List<String> uArgs = defaultArgs();
    if (!isNullOrEmpty(revision)) {
        addOption(uArgs, "--revision", revision);
    }
    uArgs.add("--force");
    uArgs.add("export");
    File tempDir = null;
    File zip = null;
    try {
        tempDir = Files.createTempDir();
        final CommandLineResult result = runCommand(null, uArgs, project, Arrays.asList(path, tempDir.getAbsolutePath()));
        if (result.getExitCode() != 0) {
            LOG.warn("Svn export process finished with exit status {}", result.getExitCode());
            throw new ServerException("Export failed");
        }
        zip = new File(Files.createTempDir(), "export.zip");
        ZipUtils.zipDir(tempDir.getPath(), tempDir, zip, IoUtil.ANY_FILTER);
    } finally {
        if (tempDir != null) {
            IoUtil.deleteRecursive(tempDir);
        }
    }
    final Response.ResponseBuilder responseBuilder = Response.ok(new DeleteOnCloseFileInputStream(zip), MediaType.ZIP.toString()).lastModified(new Date(zip.lastModified())).header(HttpHeaders.CONTENT_LENGTH, Long.toString(zip.length())).header("Content-Disposition", "attachment; filename=\"export.zip\"");
    return responseBuilder.build();
}
Also used : ListResponse(org.eclipse.che.plugin.svn.shared.ListResponse) CLIOutputResponse(org.eclipse.che.plugin.svn.shared.CLIOutputResponse) Response(javax.ws.rs.core.Response) InfoResponse(org.eclipse.che.plugin.svn.shared.InfoResponse) CLIOutputWithRevisionResponse(org.eclipse.che.plugin.svn.shared.CLIOutputWithRevisionResponse) GetRevisionsResponse(org.eclipse.che.plugin.svn.shared.GetRevisionsResponse) ServerException(org.eclipse.che.api.core.ServerException) CommandLineResult(org.eclipse.che.plugin.svn.server.upstream.CommandLineResult) DeleteOnCloseFileInputStream(org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream) File(java.io.File) Date(java.util.Date)

Aggregations

File (java.io.File)2 ServerException (org.eclipse.che.api.core.ServerException)2 DeleteOnCloseFileInputStream (org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Response (javax.ws.rs.core.Response)1 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)1 PathLockFactory (org.eclipse.che.api.vfs.PathLockFactory)1 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)1 CommandLineResult (org.eclipse.che.plugin.svn.server.upstream.CommandLineResult)1 CLIOutputResponse (org.eclipse.che.plugin.svn.shared.CLIOutputResponse)1 CLIOutputWithRevisionResponse (org.eclipse.che.plugin.svn.shared.CLIOutputWithRevisionResponse)1 GetRevisionsResponse (org.eclipse.che.plugin.svn.shared.GetRevisionsResponse)1 InfoResponse (org.eclipse.che.plugin.svn.shared.InfoResponse)1 ListResponse (org.eclipse.che.plugin.svn.shared.ListResponse)1