Search in sources :

Example 41 with BoxAPIException

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

the class BoxFilesManager method downloadFile.

/**
     * Download a file.
     * 
     * @param fileId
     *            - the id of file.
     * @param output
     *            - the stream to which the file contents will be written.
     * @param rangeStart
     *            - the byte offset in file at which to start the download; if
     *            <code>null</code> the entire contents of file will be
     *            downloaded.
     * @param rangeEnd
     *            - the byte offset in file at which to stop the download; if
     *            <code>null</code> the entire contents of file will be
     *            downloaded.
     * @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.
     */
public OutputStream downloadFile(String fileId, OutputStream output, Long rangeStart, Long rangeEnd, ProgressListener listener) {
    try {
        LOG.debug("Downloading file(id=" + fileId + ")");
        if (fileId == null) {
            throw new IllegalArgumentException("Parameter 'fileId' can not be null");
        }
        if (output == null) {
            throw new IllegalArgumentException("Parameter 'output' can not be null");
        }
        BoxFile file = new BoxFile(boxConnection, fileId);
        if (listener != null) {
            if (rangeStart != null && rangeEnd != null) {
                file.downloadRange(output, rangeStart, rangeEnd, listener);
            } else {
                file.download(output, listener);
            }
        } else {
            if (rangeStart != null && rangeEnd != null) {
                file.downloadRange(output, rangeStart, rangeEnd);
            } else {
                file.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) BoxAPIException(com.box.sdk.BoxAPIException)

Example 42 with BoxAPIException

use of com.box.sdk.BoxAPIException 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 43 with BoxAPIException

use of com.box.sdk.BoxAPIException 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 44 with BoxAPIException

use of com.box.sdk.BoxAPIException 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 45 with BoxAPIException

use of com.box.sdk.BoxAPIException 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)

Aggregations

BoxAPIException (com.box.sdk.BoxAPIException)74 BoxFile (com.box.sdk.BoxFile)24 BoxFolder (com.box.sdk.BoxFolder)17 BoxUser (com.box.sdk.BoxUser)8 BoxTask (com.box.sdk.BoxTask)6 BoxCollaboration (com.box.sdk.BoxCollaboration)4 BoxComment (com.box.sdk.BoxComment)4 BoxGroup (com.box.sdk.BoxGroup)4 BoxFileVersion (com.box.sdk.BoxFileVersion)3 BoxGroupMembership (com.box.sdk.BoxGroupMembership)3 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)3 IOException (java.io.IOException)3 GeneralSecurityException (java.security.GeneralSecurityException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Test (org.junit.Test)3 BoxItem (com.box.sdk.BoxItem)2 BoxTaskAssignment (com.box.sdk.BoxTaskAssignment)2 IAccessTokenCache (com.box.sdk.IAccessTokenCache)2