Search in sources :

Example 11 with IRODSFile

use of org.irods.jargon.core.pub.io.IRODSFile 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 12 with IRODSFile

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

the class SpecQueryServiceImplTest method decorateChildren.

public static void decorateChildren(final IRODSFile file, final CollectionAO collectionAO, final DataObjectAO dataObjectAO) throws Exception {
    IRODSFile childIrods;
    int i = 0;
    AvuData dataToAdd;
    for (File child : file.listFiles()) {
        childIrods = (IRODSFile) child;
        if (childIrods.isDirectory()) {
            if (i % 2 == 0) {
                dataToAdd = AvuData.instance(COLL_AVU_ATTR1, COLL_AVU_VAL1, "");
            } else {
                dataToAdd = AvuData.instance(COLL_AVU_ATTR2, COLL_AVU_VAL2, "");
            }
            collectionAO.addAVUMetadata(childIrods.getAbsolutePath(), dataToAdd);
            decorateChildren(childIrods, collectionAO, dataObjectAO);
        } else {
            if (i % 2 == 0) {
                dataToAdd = AvuData.instance(DATA_AVU_ATTR1, DATA_AVU_VAL1, "");
            } else {
                dataToAdd = AvuData.instance(DATA_AVU_ATTR2, DATA_AVU_VAL2, "");
            }
            dataObjectAO.addAVUMetadata(childIrods.getAbsolutePath(), dataToAdd);
        }
    }
}
Also used : AvuData(org.irods.jargon.core.pub.domain.AvuData) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) File(java.io.File)

Example 13 with IRODSFile

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

the class PermissionsServiceImpl method canLoggedUserModifyPermissionOnPath.

@Override
public boolean canLoggedUserModifyPermissionOnPath(String path) throws DataGridConnectionRefusedException {
    String userName = irodsServices.getCurrentUser();
    final IRODSFileFactory irodsFileFactory = irodsServices.getIRODSFileFactory();
    final IRODSFileSystemAO irodsFileSystemAO = irodsServices.getIRODSFileSystemAO();
    try {
        int resultingPermission;
        final IRODSFile fileObj = irodsFileFactory.instanceIRODSFile(path);
        if (irodsFileSystemAO.isDirectory(fileObj)) {
            resultingPermission = irodsFileSystemAO.getDirectoryPermissionsForGivenUser(fileObj, userName);
        } else {
            resultingPermission = irodsFileSystemAO.getFilePermissionsForGivenUser(fileObj, userName);
        }
        return resultingPermission > FilePermissionEnum.WRITE.getPermissionNumericValue();
    } catch (final Exception e) {
        logger.error("Could not get permissions for current user: {}", e.getMessage());
    }
    return false;
}
Also used : IRODSFileFactory(org.irods.jargon.core.pub.io.IRODSFileFactory) IRODSFileSystemAO(org.irods.jargon.core.pub.IRODSFileSystemAO) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) DataGridConnectionRefusedException(com.emc.metalnx.core.domain.exceptions.DataGridConnectionRefusedException) FileNotFoundException(org.irods.jargon.core.exception.FileNotFoundException) JargonException(org.irods.jargon.core.exception.JargonException) DataGridException(com.emc.metalnx.core.domain.exceptions.DataGridException)

Example 14 with IRODSFile

use of org.irods.jargon.core.pub.io.IRODSFile 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 15 with IRODSFile

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

the class TicketServiceImpl method create.

@Override
public String create(DataGridTicket dgTicket) throws DataGridConnectionRefusedException, DataGridTicketException {
    logger.info("Create ticket");
    if (dgTicket == null) {
        logger.info("Could not create ticket: Null ticket provided.");
        throw new DataGridTicketException("Could not create ticket: null ticket provided.");
    }
    if (dgTicket.getPath().isEmpty()) {
        logger.info("Could not create ticket: Ticket with no path.");
        throw new DataGridTicketException("Could not create ticket: path is empty");
    }
    TicketCreateModeEnum ticketType = TicketCreateModeEnum.UNKNOWN;
    if (dgTicket.getType() == DataGridTicket.TicketType.READ)
        ticketType = TicketCreateModeEnum.READ;
    if (dgTicket.getType() == DataGridTicket.TicketType.WRITE)
        ticketType = TicketCreateModeEnum.WRITE;
    String path = dgTicket.getPath();
    int idxOfSeparator = path.lastIndexOf(GRID_FILE_SEPARATOR);
    String parentPath = path.substring(0, idxOfSeparator);
    String item = path.substring(idxOfSeparator + 1, path.length());
    String ticketString = "";
    try {
        IRODSFile irodsFile = irodsServices.getIRODSFileFactory().instanceIRODSFile(parentPath, item);
        TicketAdminService tas = irodsServices.getTicketAdminService();
        ticketString = tas.createTicket(ticketType, irodsFile, dgTicket.getTicketString());
        // set ticket string created by the grid
        dgTicket.setTicketString(ticketString);
        modify(dgTicket);
    } catch (JargonException e) {
        logger.error("Could not create a ticket: {}", e);
        throw new DataGridTicketException(e.getMessage());
    }
    return ticketString;
}
Also used : TicketCreateModeEnum(org.irods.jargon.ticket.packinstr.TicketCreateModeEnum) JargonException(org.irods.jargon.core.exception.JargonException) DataGridTicketException(com.emc.metalnx.core.domain.exceptions.DataGridTicketException) IRODSFile(org.irods.jargon.core.pub.io.IRODSFile) TicketAdminService(org.irods.jargon.ticket.TicketAdminService)

Aggregations

IRODSFile (org.irods.jargon.core.pub.io.IRODSFile)28 IRODSFileFactory (org.irods.jargon.core.pub.io.IRODSFileFactory)23 JargonException (org.irods.jargon.core.exception.JargonException)19 CollectionAO (org.irods.jargon.core.pub.CollectionAO)7 IRODSFileSystemAO (org.irods.jargon.core.pub.IRODSFileSystemAO)7 DataGridException (com.emc.metalnx.core.domain.exceptions.DataGridException)6 DataObjectAO (org.irods.jargon.core.pub.DataObjectAO)6 DataGridConnectionRefusedException (com.emc.metalnx.core.domain.exceptions.DataGridConnectionRefusedException)4 File (java.io.File)4 IOException (java.io.IOException)4 DataTransferOperations (org.irods.jargon.core.pub.DataTransferOperations)4 InputStream (java.io.InputStream)3 Stream2StreamAO (org.irods.jargon.core.pub.Stream2StreamAO)3 DataGridChecksumException (com.emc.metalnx.core.domain.exceptions.DataGridChecksumException)2 DataGridTicketInvalidUserException (com.emc.metalnx.core.domain.exceptions.DataGridTicketInvalidUserException)2 IRODSAccount (org.irods.jargon.core.connection.IRODSAccount)2 IRODSFileInputStream (org.irods.jargon.core.pub.io.IRODSFileInputStream)2 IRODSTestSetupUtilities (org.irods.jargon.testutils.IRODSTestSetupUtilities)2 TestingPropertiesHelper (org.irods.jargon.testutils.TestingPropertiesHelper)2 ScratchFileUtils (org.irods.jargon.testutils.filemanip.ScratchFileUtils)2