Search in sources :

Example 1 with IRODSFileInputStream

use of org.irods.jargon.core.pub.io.IRODSFileInputStream in project metalnx-web by irods-contrib.

the class PreviewServiceImpl method filePreview.

@Override
public boolean filePreview(String path, String mimeType, HttpServletResponse response) {
    logger.info("getting file preview  for {} ::" + path + " and mimetype :: " + mimeType);
    boolean isCopySuccessFul = true;
    IRODSFileInputStream irodsFileInputStream = null;
    IRODSFile irodsFile = null;
    try {
        IRODSFileFactory irodsFileFactory = irodsServices.getIRODSFileFactory();
        irodsFile = irodsFileFactory.instanceIRODSFile(path);
        irodsFileInputStream = irodsFileFactory.instanceIRODSFileInputStream(irodsFile);
        response.setContentType(mimeType);
        FileCopyUtils.copy(irodsFileInputStream, response.getOutputStream());
    } catch (IOException | JargonException | DataGridConnectionRefusedException e) {
        e.printStackTrace();
        isCopySuccessFul = false;
    } finally {
        try {
            if (irodsFileInputStream != null)
                irodsFileInputStream.close();
            if (irodsFile != null)
                irodsFile.close();
        } catch (Exception e) {
            logger.error("Could not close stream(s): ", e.getMessage());
        }
    }
    return isCopySuccessFul;
}
Also used : IRODSFileFactory(org.irods.jargon.core.pub.io.IRODSFileFactory) DataGridConnectionRefusedException(com.emc.metalnx.core.domain.exceptions.DataGridConnectionRefusedException) IRODSFileInputStream(org.irods.jargon.core.pub.io.IRODSFileInputStream) JargonException(org.irods.jargon.core.exception.JargonException) IOException(java.io.IOException) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) DataGridException(com.emc.metalnx.core.domain.exceptions.DataGridException) IOException(java.io.IOException) DataGridConnectionRefusedException(com.emc.metalnx.core.domain.exceptions.DataGridConnectionRefusedException) JargonException(org.irods.jargon.core.exception.JargonException)

Example 2 with IRODSFileInputStream

use of org.irods.jargon.core.pub.io.IRODSFileInputStream in project metalnx-web by irods-contrib.

the class FileOperationServiceImpl method copyFileIntoHttpResponse.

/**
 * Copies a buffered input stream from a file to a HTTP response for
 * downloading.
 *
 * @param path
 *            path to the file in iRODS to be added to the HTTP response
 * @param response
 *            HTTP response to let the user download the file
 * @return True, if the file was successfully added to the HTTP response. False,
 *         otherwise.
 * @throws DataGridConnectionRefusedException
 *             is Metalnx cannot connect to the data grid
 */
private boolean copyFileIntoHttpResponse(String path, HttpServletResponse response) throws DataGridConnectionRefusedException {
    boolean isCopySuccessFul = true;
    IRODSFileInputStream irodsFileInputStream = null;
    IRODSFile irodsFile = null;
    logger.debug("Trying to copy path stream {} to user", path);
    try {
        String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
        logger.debug("The filename is [{}]", fileName);
        logger.debug("Initiating iRodsFileFactory");
        IRODSFileFactory irodsFileFactory = irodsServices.getIRODSFileFactory();
        logger.debug("Getting iRodsFileFactory instance for {}", path);
        irodsFile = irodsFileFactory.instanceIRODSFile(path);
        logger.debug("Creating stream from {}", irodsFile);
        irodsFileInputStream = irodsFileFactory.instanceIRODSFileInputStream(irodsFile);
        // set file mime type
        response.setContentType(CONTENT_TYPE);
        response.setHeader("Content-Disposition", String.format(HEADER_FORMAT, fileName));
        response.setContentLength((int) irodsFile.length());
        FileCopyUtils.copy(irodsFileInputStream, response.getOutputStream());
    } catch (IOException e) {
        logger.error("Could not put the file in the Http response ", e);
        isCopySuccessFul = false;
    } catch (JargonException e) {
        logger.error("Could not copy file in the Http response: ", e.getMessage());
        isCopySuccessFul = false;
    } finally {
        try {
            if (irodsFileInputStream != null)
                irodsFileInputStream.close();
            if (irodsFile != null)
                irodsFile.close();
        } catch (Exception e) {
            logger.error("Could not close stream(s): ", e.getMessage());
        }
    }
    return isCopySuccessFul;
}
Also used : IRODSFileFactory(org.irods.jargon.core.pub.io.IRODSFileFactory) IRODSFileInputStream(org.irods.jargon.core.pub.io.IRODSFileInputStream) JargonException(org.irods.jargon.core.exception.JargonException) IOException(java.io.IOException) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) DataGridChecksumException(com.emc.metalnx.core.domain.exceptions.DataGridChecksumException) DataGridConnectionRefusedException(com.emc.metalnx.core.domain.exceptions.DataGridConnectionRefusedException) DataNotFoundException(org.irods.jargon.core.exception.DataNotFoundException) JargonException(org.irods.jargon.core.exception.JargonException) DataGridReplicateException(com.emc.metalnx.core.domain.exceptions.DataGridReplicateException) DataGridRuleException(com.emc.metalnx.core.domain.exceptions.DataGridRuleException) DataGridException(com.emc.metalnx.core.domain.exceptions.DataGridException) IOException(java.io.IOException)

Aggregations

DataGridConnectionRefusedException (com.emc.metalnx.core.domain.exceptions.DataGridConnectionRefusedException)2 DataGridException (com.emc.metalnx.core.domain.exceptions.DataGridException)2 IOException (java.io.IOException)2 JargonException (org.irods.jargon.core.exception.JargonException)2 IRODSFile (org.irods.jargon.core.pub.io.IRODSFile)2 IRODSFileFactory (org.irods.jargon.core.pub.io.IRODSFileFactory)2 IRODSFileInputStream (org.irods.jargon.core.pub.io.IRODSFileInputStream)2 DataGridChecksumException (com.emc.metalnx.core.domain.exceptions.DataGridChecksumException)1 DataGridReplicateException (com.emc.metalnx.core.domain.exceptions.DataGridReplicateException)1 DataGridRuleException (com.emc.metalnx.core.domain.exceptions.DataGridRuleException)1 DataNotFoundException (org.irods.jargon.core.exception.DataNotFoundException)1