Search in sources :

Example 16 with BoxFile

use of com.box.sdk.BoxFile in project camel by apache.

the class BoxFilesManager method getDownloadURL.

/**
     * Get an expiring URL for downloading a file directly from Box. This can be
     * user, for example, for sending as a redirect to a browser to cause the
     * browser to download the file directly from Box.
     * 
     * @param fileId
     *            - the id of file.
     * @return The temporary download URL
     */
public URL getDownloadURL(String fileId) {
    try {
        LOG.debug("Getting download URL for file(id=" + fileId + ")");
        if (fileId == null) {
            throw new IllegalArgumentException("Parameter 'fileId' can not be null");
        }
        BoxFile file = new BoxFile(boxConnection, fileId);
        return file.getDownloadURL();
    } catch (BoxAPIException e) {
        throw new RuntimeException(String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e);
    }
}
Also used : BoxFile(com.box.sdk.BoxFile) BoxAPIException(com.box.sdk.BoxAPIException)

Example 17 with BoxFile

use of com.box.sdk.BoxFile in project camel by apache.

the class BoxFilesManager method getFileVersions.

/**
     * Get any previous versions of file.
     * 
     * @param fileId
     *            - the id of file.
     * @return The list of previous file versions.
     */
public Collection<BoxFileVersion> getFileVersions(String fileId) {
    try {
        LOG.debug("Getting versions of file(id=" + fileId + ")");
        if (fileId == null) {
            throw new IllegalArgumentException("Parameter 'fileId' can not be null");
        }
        BoxFile file = new BoxFile(boxConnection, fileId);
        return file.getVersions();
    } catch (BoxAPIException e) {
        throw new RuntimeException(String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e);
    }
}
Also used : BoxFile(com.box.sdk.BoxFile) BoxAPIException(com.box.sdk.BoxAPIException)

Example 18 with BoxFile

use of com.box.sdk.BoxFile in project camel by apache.

the class BoxFilesManager method downloadPreviousFileVersion.

/**
     * Download a previous version of file.
     * 
     * @param fileId
     *            - the id of file.
     * @param version
     *            - the version of file to download; initial version of file has
     *            value of <code>0</code>, second version of file is
     *            <code>1</code> and so on.
     * @param output
     *            - the stream to which the version contents will be written.
     * @param listener
     *            - a listener for monitoring the download's progress; if
     *            <code>null</code> the download's progress will not be
     *            monitored.
     * @return The stream containing the contents of the downloaded file
     *         version.
     */
public OutputStream downloadPreviousFileVersion(String fileId, Integer version, OutputStream output, ProgressListener listener) {
    try {
        LOG.debug("Downloading file(id=" + fileId + ", version=" + version + ")");
        if (fileId == null) {
            throw new IllegalArgumentException("Parameter 'fileId' can not be null");
        }
        if (version == null) {
            throw new IllegalArgumentException("Parameter 'version' can not be null");
        }
        if (output == null) {
            throw new IllegalArgumentException("Parameter 'output' can not be null");
        }
        BoxFile file = new BoxFile(boxConnection, fileId);
        List<BoxFileVersion> fileVersions = (List<BoxFileVersion>) file.getVersions();
        BoxFileVersion fileVersion = fileVersions.get(version);
        if (listener != null) {
            fileVersion.download(output, listener);
        } else {
            fileVersion.download(output);
        }
        return output;
    } catch (BoxAPIException e) {
        throw new RuntimeException(String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e);
    }
}
Also used : BoxFile(com.box.sdk.BoxFile) List(java.util.List) BoxAPIException(com.box.sdk.BoxAPIException) BoxFileVersion(com.box.sdk.BoxFileVersion)

Example 19 with BoxFile

use of com.box.sdk.BoxFile in project camel by apache.

the class BoxFilesManager method createFileMetadata.

/**
     * Create metadata for file in either the global properties template or the
     * specified template type.
     * 
     * @param fileId
     *            - the id of the file to create metadata for.
     * @param metadata
     *            - the new metadata values.
     * @param typeName
     *            - the metadata template type name; if <code>null</code> the
     *            global properties template type is used.
     * @return The metadata returned from the server.
     */
public Metadata createFileMetadata(String fileId, Metadata metadata, String typeName) {
    try {
        LOG.debug("Creating metadata for file(id=" + fileId + ")");
        if (fileId == null) {
            throw new IllegalArgumentException("Parameter 'fileId' can not be null");
        }
        if (metadata == null) {
            throw new IllegalArgumentException("Parameter 'metadata' can not be null");
        }
        BoxFile file = new BoxFile(boxConnection, fileId);
        if (typeName != null) {
            return file.createMetadata(typeName, metadata);
        } else {
            return file.createMetadata(metadata);
        }
    } catch (BoxAPIException e) {
        throw new RuntimeException(String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e);
    }
}
Also used : BoxFile(com.box.sdk.BoxFile) BoxAPIException(com.box.sdk.BoxAPIException)

Example 20 with BoxFile

use of com.box.sdk.BoxFile in project camel by apache.

the class BoxFilesManager method copyFile.

/**
     * Copy file to destination folder while optionally giving it a new name.
     * 
     * @param fileId
     *            - the id of file to copy.
     * @param destinationFolderId
     *            - the id of the destination folder.
     * @param newName
     *            - the new name for copied file; if <code>newName</code> is
     *            <code>null</code>, the copied file has same name as the
     *            original.
     * @return The copied file.
     */
public BoxFile copyFile(String fileId, String destinationFolderId, String newName) {
    try {
        LOG.debug("Copying file(id=" + fileId + ") to destination_folder(id=" + destinationFolderId + ")" + (newName == null ? "" : " with new name '" + newName + "'"));
        if (fileId == null) {
            throw new IllegalArgumentException("Parameter 'fileId' can not be null");
        }
        if (destinationFolderId == null) {
            throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null");
        }
        BoxFile fileToCopy = new BoxFile(boxConnection, fileId);
        BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId);
        if (newName == null) {
            return fileToCopy.copy(destinationFolder).getResource();
        } else {
            return fileToCopy.copy(destinationFolder, newName).getResource();
        }
    } catch (BoxAPIException e) {
        throw new RuntimeException(String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e);
    }
}
Also used : BoxFile(com.box.sdk.BoxFile) BoxAPIException(com.box.sdk.BoxAPIException) BoxFolder(com.box.sdk.BoxFolder)

Aggregations

BoxAPIException (com.box.sdk.BoxAPIException)24 BoxFile (com.box.sdk.BoxFile)24 BoxFileVersion (com.box.sdk.BoxFileVersion)3 List (java.util.List)3 BoxFolder (com.box.sdk.BoxFolder)2 BoxTask (com.box.sdk.BoxTask)1