Example usage for org.apache.commons.vfs2 FileObject createFolder

List of usage examples for org.apache.commons.vfs2 FileObject createFolder

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject createFolder.

Prototype

void createFolder() throws FileSystemException;

Source Link

Document

Creates this folder, if it does not exist.

Usage

From source file:org.obiba.opal.shell.commands.CopyCommand.java

/**
 * Resolves the output file based on the command parameter. Creates the necessary parent folders (when required).
 *
 * @return A FileObject representing the output file.
 * @throws FileSystemException/*from   www .j a  v  a  2  s.com*/
 */
private FileObject resolveOutputFileAndCreateParentFolders() throws FileSystemException {
    FileObject outputFile;
    outputFile = getFile(options.getOut());

    // Create the parent directory, if it doesn't already exist.
    FileObject directory = outputFile.getParent();
    if (directory != null) {
        directory.createFolder();
    }

    if (Strings.isNullOrEmpty(outputFile.getName().getExtension())) {
        outputFile.createFolder();
    }

    if ("xls".equals(outputFile.getName().getExtension())) {
        getShell().printf(
                "WARNING: Writing to an Excel 97 spreadsheet. These are limited to 256 columns and 65536 rows "
                        + "which may not be sufficient for writing large tables.\nUse an 'xlsx' extension to use Excel 2007 format "
                        + "which supports 16K columns.\n");
    }
    return outputFile;
}

From source file:org.obiba.opal.shell.commands.DecryptCommand.java

/**
 * Given the name/path of a directory, returns that directory (creating it if necessary).
 *
 * @param outputDirPath the name/path of the directory.
 * @return the directory, as a <code>FileObject</code> object (or <code>null</code> if the directory could not be
 * created.// w  ww  .  j a v a  2s  . c  om
 */
private FileObject getOutputDir(String outputDirPath) {
    try {
        FileObject outputDir = getFile(outputDirPath);
        outputDir.createFolder();
        return outputDir;
    } catch (FileSystemException e) {
        return null;
    }
}

From source file:org.obiba.opal.shell.commands.ImportCommand.java

private void archive(FileObject file) throws IOException {
    if (!options.isArchive()) {
        return;//from   w ww.  ja  v a  2  s.  c  om
    }

    String archivePath = options.getArchive();
    try {
        FileObject archiveDir = getFile(archivePath);
        if (archiveDir == null) {
            throw new IOException("Cannot archive file " + file.getName().getPath()
                    + ". Archive directory is null: " + archivePath);
        }
        archiveDir.createFolder();
        FileObject archiveFile = archiveDir.resolveFile(file.getName().getBaseName());
        file.moveTo(archiveFile);
    } catch (FileSystemException ex) {
        throw new IOException("Failed to archive file " + file.getName().getPath() + " to " + archivePath);
    }
}

From source file:org.obiba.opal.shell.commands.ReportCommand.java

private FileObject getReportOutput(ReportTemplate reportTemplate, Date reportDate) throws FileSystemException {
    String reportTemplateName = reportTemplate.getName();
    String reportFormat = reportTemplate.getFormat();
    String reportFileName = getReportFileName(reportTemplateName, reportFormat, reportDate);

    FileObject reportDir = getFile("/reports/" + reportTemplate.getProject() + "/" + reportTemplateName);
    reportDir.createFolder();

    return reportDir.resolveFile(reportFileName);
}

From source file:org.obiba.opal.shell.test.support.OpalFileSystemMockBuilder.java

public OpalFileSystemMockBuilder createFolder(String folderPath) throws FileSystemException {
    FileObject folder = fileMap.get(folderPath);
    folder.createFolder();

    expectationSetters = expectLastCall();

    return this;
}

From source file:org.obiba.opal.shell.web.ExportCommandOptionsDtoImpl.java

private String addFileExtensionIfMissing(String outputFilePath, String outputFileFormat) {
    String modifiedPath = outputFilePath;

    FileObject file = null;
    try {/*from  ww w  .  j  a  v  a  2 s.  co  m*/
        file = resolveFileInFileSystem(outputFilePath);

        // Add the extension if the file object is an existing file (FileType.FILE)
        // or a new file (FileType.IMAGINARY). We assume here that any "imaginary" file object
        // is a non-existent folder.
        if (file.getType() == FileType.FILE) {
            modifiedPath = addExtension(outputFileFormat, outputFilePath);

        } else if (file.getType() == FileType.IMAGINARY) {
            if ("xml".equals(outputFileFormat) && !outputFilePath.endsWith(".zip")) {
                modifiedPath = addExtension(outputFileFormat, outputFilePath);
            } else if ("csv".equals(outputFileFormat)) {
                // Create the directory
                file.createFolder();
            }
        }

    } catch (FileSystemException ex) {
        log.error("Unexpected file system exception in addFileExtensionIfMissing", ex);
    }

    return modifiedPath;
}

From source file:org.obiba.opal.web.FilesResource.java

@POST
@Path("/{path:.*}")
@Consumes("text/plain")
public Response createFolder(@PathParam("path") String path, String folderName, @Context UriInfo uriInfo)
        throws FileSystemException {
    if (folderName == null || folderName.trim().isEmpty())
        return Response.status(Status.BAD_REQUEST).build();

    String folderPath = getPathOfFileToWrite(path);
    FileObject folder = resolveFileInFileSystem(folderPath);
    Response folderResponse = validateFolder(folder, path);
    if (folderResponse != null)
        return folderResponse;

    FileObject file = folder.resolveFile(folderName);
    Response fileResponse = validateFile(file);
    if (fileResponse != null)
        return fileResponse;

    try {/*w ww .j a  v a2 s  .  c  o  m*/
        file.createFolder();
        Opal.FileDto dto = getBaseFolderBuilder(file).build();
        URI folderUri = uriInfo.getBaseUriBuilder().path(FilesResource.class).path(folderPath).path(folderName)
                .build();
        return Response.created(folderUri)//
                .header(AuthorizationInterceptor.ALT_PERMISSIONS,
                        new OpalPermissions(folderUri, AclAction.FILES_ALL))//
                .entity(dto).build();
    } catch (FileSystemException couldNotCreateTheFolder) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("cannotCreateFolderUnexpectedError")
                .build();
    }
}

From source file:org.obiba.opal.web.FilesResourceTest.java

@Test
public void testCreateFolder_FolderCreatedSuccessfully() throws FileSystemException, URISyntaxException {
    expect(fileObjectMock.getType()).andReturn(FileType.FOLDER).atLeastOnce();
    expect(fileObjectMock.exists()).andReturn(true).atLeastOnce();

    FileObject childFolderMock = createMock(FileObject.class);

    FileName fileNameMock = createMock(FileName.class);
    expect(fileNameMock.getBaseName()).andReturn("folder").atLeastOnce();
    expect(fileNameMock.getPath()).andReturn("folder1/folder").atLeastOnce();

    expect(childFolderMock.getName()).andReturn(fileNameMock).atLeastOnce();
    expect(childFolderMock.exists()).andReturn(false).atLeastOnce();

    FileContent mockContent = createMock(FileContent.class);
    expect(childFolderMock.getContent()).andReturn(mockContent).atLeastOnce();
    expect(mockContent.getLastModifiedTime()).andReturn((long) 1).atLeastOnce();

    childFolderMock.createFolder();
    FileObject parentFolderMock = createMock(FileObject.class);
    expect(childFolderMock.getParent()).andReturn(parentFolderMock).atLeastOnce();
    expect(childFolderMock.isReadable()).andReturn(true).atLeastOnce();
    expect(childFolderMock.isWriteable()).andReturn(true).atLeastOnce();
    expect(parentFolderMock.isWriteable()).andReturn(true).atLeastOnce();
    expect(fileObjectMock.resolveFile("folder")).andReturn(childFolderMock).atLeastOnce();
    expect(uriInfoMock.getBaseUriBuilder()).andReturn(UriBuilder.fromPath("/"));

    replay(fileObjectMock, uriInfoMock, parentFolderMock, childFolderMock, fileNameMock, mockContent);

    Response response = getFileResource().createFolder("folder1", "folder", uriInfoMock);
    assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode());
    verify(fileObjectMock, uriInfoMock, parentFolderMock, childFolderMock, fileNameMock, mockContent);
}

From source file:org.ow2.proactive.scheduler.smartproxy.SmartProxyImpl.java

@Override
protected void createFolder(String fUri) throws NotConnectedException, PermissionException {
    FileObject fo = null;
    try {//from  www.  j a  v  a2  s .c  om
        fo = jobTracker.resolveFile(fUri);
        fo.createFolder();
    } catch (FileSystemException e) {
        log.error("Error while creating folder: " + fo, e);
    }

    log.debug("Created remote folder: " + fUri);
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.RestDataspaceImpl.java

@POST
@Path("/{dataspace}/{path-name:.*}")
public Response create(@HeaderParam("sessionid") String sessionId, @PathParam("dataspace") String dataspacePath,
        @PathParam("path-name") String pathname, @FormParam("mimetype") String mimeType)
        throws NotConnectedRestException, PermissionRestException {

    checkPathParams(dataspacePath, pathname);
    Session session = checkSessionValidity(sessionId);
    try {/*w ww .jav a  2 s  .c  om*/
        FileObject fileObject = resolveFile(session, dataspacePath, pathname);

        if (mimeType.equals(org.ow2.proactive_grid_cloud_portal.common.FileType.FOLDER.getMimeType())) {
            logger.debug(String.format("Creating folder %s in %s", pathname, dataspacePath));
            fileObject.createFolder();
        } else if (mimeType.equals(org.ow2.proactive_grid_cloud_portal.common.FileType.FILE.getMimeType())) {
            logger.debug(String.format("Creating file %s in %s", pathname, dataspacePath));
            fileObject.createFile();
        } else {
            return serverErrorRes("Cannot create specified file since mimetype is not specified");
        }

        return Response.ok().build();
    } catch (FileSystemException e) {
        logger.error(String.format("Cannot create %s in %s", pathname, dataspacePath), e);
        throw rethrow(e);
    } catch (Throwable e) {
        logger.error(String.format("Cannot create %s in %s", pathname, dataspacePath), e);
        throw rethrow(e);
    }
}