Search in sources :

Example 1 with Stream2StreamAO

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

the class TestDownloadWithTicket method uploadFileToIRODS.

private void uploadFileToIRODS(String path, File file) throws DataGridConnectionRefusedException, JargonException, IOException {
    IRODSFile targetFile = irodsServices.getIRODSFileFactory().instanceIRODSFile(path, file.getName());
    if (targetFile.exists()) {
        return;
    }
    Stream2StreamAO streamAO = irodsServices.getStream2StreamAO();
    InputStream inputStream = new FileInputStream(file);
    streamAO.transferStreamToFileUsingIOStreams(inputStream, (File) targetFile, 0, BUFFER_SIZE);
    inputStream.close();
    targetFile.close();
}
Also used : Stream2StreamAO(org.irods.jargon.core.pub.Stream2StreamAO) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) FileInputStream(java.io.FileInputStream)

Example 2 with Stream2StreamAO

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

the class UploadServiceImpl method upload.

@Override
public boolean upload(MultipartFile file, String targetPath, boolean computeCheckSum, boolean replicateFile, String replicationResc, String destResc, boolean overwrite) throws DataGridException {
    logger.info("upload()");
    if (file == null || file.isEmpty() || "".equals(targetPath) || targetPath == null || "".equals(destResc) || destResc == null) {
        logger.error("File could not be sent to the data grid.");
        return false;
    }
    logger.info("file:{}", file);
    logger.info("targetPath:{}", targetPath);
    logger.info("computeCheckSum:{}", computeCheckSum);
    logger.info("replicateFile:{}", replicateFile);
    logger.info("replicationResc:{}", replicationResc);
    logger.info("destResc:{}", destResc);
    logger.info("overwrite:{}", overwrite);
    InputStream inputStream;
    try {
        inputStream = file.getInputStream();
    } catch (IOException e) {
        logger.error("Could not get input stream from file: ", e.getMessage());
        throw new DataGridException("Could not get input stream from file.");
    }
    String defaultStorageResource = is.getDefaultStorageResource();
    logger.info("Setting default resource to {}", destResc);
    // Setting temporarily the defaultStorageResource for the logged user
    is.setDefaultStorageResource(destResc);
    boolean isFileUploaded;
    // Getting DataObjectAO in order to create the new file
    IRODSFileFactory irodsFileFactory = is.getIRODSFileFactory();
    Stream2StreamAO stream2StreamA0 = is.getStream2StreamAO();
    IRODSFile targetFile = null;
    try {
        String fileName = file.getOriginalFilename();
        if (fileName.isEmpty())
            fileName = file.getName();
        targetFile = irodsFileFactory.instanceIRODSFile(targetPath, fileName);
        logger.info("targetFile:{}", targetFile);
        // aborted.
        if (targetFile.exists() && !overwrite) {
            String msg = "File already exists. Not overwriting it.";
            logger.info(msg);
            throw new DataGridFileAlreadyExistsException(msg);
        }
        // Transfering file to iRODS filesystem
        stream2StreamA0.transferStreamToFileUsingIOStreams(inputStream, (File) targetFile, 0, BUFFER_SIZE);
        logger.info("transfer complete, compute checksum if required");
        // Computing a check sum for this file just uploaded to iRODS
        if (computeCheckSum)
            fos.computeChecksum(targetPath, fileName);
        // Replicating file into desired resource
        if (replicateFile)
            fos.replicateDataObject(targetFile.getPath(), replicationResc, false);
        if (configService.isUploadRulesEnabled()) {
            logger.info("applying upload rules");
            processUploadRules(targetPath, destResc, targetFile);
        }
        isFileUploaded = true;
    } catch (JargonException e) {
        fos.deleteDataObject(targetFile.getPath(), true);
        logger.error("Upload stream failed from Metalnx to the data grid. {}", e.getMessage());
        throw new DataGridException("Upload failed. Resource(s) might be full.");
    } catch (DataGridFileAlreadyExistsException e) {
        logger.warn("File already exists..will rethrow.");
        throw e;
    } catch (Throwable e) {
        logger.error("Exception in upload processing", e);
        throw new DataGridException("Could not upload due to system exception");
    } finally {
        try {
            // Closing streams opened
            inputStream.close();
        } catch (IOException e) {
            logger.error("Could close stream: ", e.getMessage());
        }
    }
    // Setting the default resource back to the original one.
    is.setDefaultStorageResource(defaultStorageResource);
    return isFileUploaded;
}
Also used : IRODSFileFactory(org.irods.jargon.core.pub.io.IRODSFileFactory) DataGridException(com.emc.metalnx.core.domain.exceptions.DataGridException) DataGridFileAlreadyExistsException(com.emc.metalnx.core.domain.exceptions.DataGridFileAlreadyExistsException) InputStream(java.io.InputStream) Stream2StreamAO(org.irods.jargon.core.pub.Stream2StreamAO) JargonException(org.irods.jargon.core.exception.JargonException) IOException(java.io.IOException) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile)

Example 3 with Stream2StreamAO

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

the class RuleDeploymentServiceImpl method deployRule.

@Override
public void deployRule(MultipartFile file) throws DataGridException, JargonException {
    logger.info("Deploying rule");
    if (file == null) {
        logger.error("File could not be sent to the data grid. Rule file is null.");
        throw new DataGridException("Rule file is null.");
    }
    if (!ruleCacheExists()) {
        logger.info("Rule cache does not exist. Creating one.");
        createRuleCache();
    }
    InputStream inputStream;
    try {
        inputStream = file.getInputStream();
    } catch (IOException e) {
        logger.error("Could not get input stream from rule file: ", e.getMessage());
        throw new DataGridException("Could not get input stream from ruleFile.");
    }
    // Getting DataObjectAO in order to create the new rule file
    IRODSFileFactory irodsFileFactory = irodsServices.getIRODSFileFactory();
    Stream2StreamAO stream2StreamA0 = irodsServices.getStream2StreamAO();
    IRODSFile targetFile = null;
    try {
        String ruleCacheDirPath = getRuleCachePath();
        String ruleName = file.getOriginalFilename().isEmpty() ? file.getName() : file.getOriginalFilename();
        targetFile = irodsFileFactory.instanceIRODSFile(ruleCacheDirPath, ruleName);
        stream2StreamA0.transferStreamToFileUsingIOStreams(inputStream, (File) targetFile, 0, BUFFER_SIZE);
        String resourceName = irodsServices.getDefaultStorageResource();
        Resource resc = irodsServices.getResourceAO().findByName(resourceName);
        String vaultPath = resc.getVaultPath();
        String host = resc.getLocation();
        String ruleVaultPath = String.format("%s/%s/%s", vaultPath, RULE_CACHE_DIR_NAME, ruleName);
        String ruleNameWithoutExtension = FilenameUtils.removeExtension(ruleName);
        ruleService.execDeploymentRule(host, ruleNameWithoutExtension, ruleVaultPath);
    } catch (JargonException e) {
        if (targetFile != null)
            fos.deleteDataObject(targetFile.getPath(), true);
        logger.error("Upload stream failed from Metalnx to the data grid. {}", e.getMessage());
        throw new DataGridException("Upload failed. Resource(s) might be full.");
    } finally {
        try {
            // Closing streams opened
            inputStream.close();
        } catch (IOException e) {
            logger.error("Could close stream: ", e.getMessage());
        }
    }
}
Also used : IRODSFileFactory(org.irods.jargon.core.pub.io.IRODSFileFactory) DataGridException(com.emc.metalnx.core.domain.exceptions.DataGridException) InputStream(java.io.InputStream) Stream2StreamAO(org.irods.jargon.core.pub.Stream2StreamAO) JargonException(org.irods.jargon.core.exception.JargonException) Resource(org.irods.jargon.core.pub.domain.Resource) IOException(java.io.IOException) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile)

Aggregations

InputStream (java.io.InputStream)3 Stream2StreamAO (org.irods.jargon.core.pub.Stream2StreamAO)3 IRODSFile (org.irods.jargon.core.pub.io.IRODSFile)3 DataGridException (com.emc.metalnx.core.domain.exceptions.DataGridException)2 IOException (java.io.IOException)2 JargonException (org.irods.jargon.core.exception.JargonException)2 IRODSFileFactory (org.irods.jargon.core.pub.io.IRODSFileFactory)2 DataGridFileAlreadyExistsException (com.emc.metalnx.core.domain.exceptions.DataGridFileAlreadyExistsException)1 FileInputStream (java.io.FileInputStream)1 Resource (org.irods.jargon.core.pub.domain.Resource)1