Search in sources :

Example 71 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class FileAccessIO method getAuxObjectAsPath.

@Override
public Path getAuxObjectAsPath(String auxItemTag) throws IOException {
    if (auxItemTag == null || "".equals(auxItemTag)) {
        throw new IOException("Null or invalid Auxiliary Object Tag.");
    }
    String datasetDirectory = getDatasetDirectory();
    if (dvObject.getStorageIdentifier() == null || "".equals(dvObject.getStorageIdentifier())) {
        throw new IOException("Data Access: No local storage identifier defined for this datafile.");
    }
    Path auxPath = null;
    if (dvObject instanceof DataFile) {
        auxPath = Paths.get(datasetDirectory, dvObject.getStorageIdentifier() + "." + auxItemTag);
    } else if (dvObject instanceof Dataset) {
        auxPath = Paths.get(datasetDirectory, auxItemTag);
    } else if (dvObject instanceof Dataverse) {
    } else {
        throw new IOException("Aux path could not be generated for " + auxItemTag);
    }
    if (auxPath == null) {
        throw new IOException("Invalid Path location for the auxiliary file " + dvObject.getStorageIdentifier() + "." + auxItemTag);
    }
    return auxPath;
}
Also used : Path(java.nio.file.Path) DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) IOException(java.io.IOException) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Example 72 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class FileAccessIO method open.

@Override
public void open(DataAccessOption... options) throws IOException {
    DataFile dataFile;
    Dataset dataset;
    Dataverse dataverse = null;
    DataAccessRequest req = this.getRequest();
    if (isWriteAccessRequested(options)) {
        isWriteAccess = true;
        isReadAccess = false;
    } else {
        isWriteAccess = false;
        isReadAccess = true;
    }
    if (dvObject instanceof DataFile) {
        dataFile = this.getDataFile();
        if (req != null && req.getParameter("noVarHeader") != null) {
            this.setNoVarHeader(true);
        }
        if (dataFile.getStorageIdentifier() == null || "".equals(dataFile.getStorageIdentifier())) {
            throw new IOException("Data Access: No local storage identifier defined for this datafile.");
        }
        if (isReadAccess) {
            FileInputStream fin = openLocalFileAsInputStream();
            if (fin == null) {
                throw new IOException("Failed to open local file " + getStorageLocation());
            }
            this.setInputStream(fin);
            setChannel(fin.getChannel());
            this.setSize(getLocalFileSize());
            if (dataFile.getContentType() != null && dataFile.getContentType().equals("text/tab-separated-values") && dataFile.isTabularData() && dataFile.getDataTable() != null && (!this.noVarHeader())) {
                List<DataVariable> datavariables = dataFile.getDataTable().getDataVariables();
                String varHeaderLine = generateVariableHeader(datavariables);
                this.setVarHeader(varHeaderLine);
            }
        } else if (isWriteAccess) {
            // Creates a new directory as needed for a dataset.
            if (dataFile.getOwner().getFileSystemDirectory() != null && !Files.exists(dataFile.getOwner().getFileSystemDirectory())) {
                Files.createDirectories(dataFile.getOwner().getFileSystemDirectory());
            }
            FileOutputStream fout = openLocalFileAsOutputStream();
            if (fout == null) {
                throw new IOException("Failed to open local file " + getStorageLocation() + " for writing.");
            }
            this.setOutputStream(fout);
            setChannel(fout.getChannel());
        }
        this.setMimeType(dataFile.getContentType());
        try {
            this.setFileName(dataFile.getFileMetadata().getLabel());
        } catch (Exception ex) {
            this.setFileName("unknown");
        }
    } else if (dvObject instanceof Dataset) {
        // This case is for uploading a dataset related auxiliary file
        // e.g. image thumbnails/metadata exports
        // TODO: do we really need to do anything here? should we return the dataset directory?
        dataset = this.getDataset();
        if (isReadAccess) {
        // TODO: Not necessary for dataset as there is no files associated with this
        // FileInputStream fin = openLocalFileAsInputStream();
        // Path path= dataset.getFileSystemDirectory();
        // if (path == null) {
        // throw new IOException("Failed to locate Dataset"+dataset.getIdentifier());
        // }
        // 
        // this.setInputStream(fin);
        } else if (isWriteAccess) {
            // this checks whether a directory for a dataset exists
            if (dataset.getFileSystemDirectory() != null && !Files.exists(dataset.getFileSystemDirectory())) {
                Files.createDirectories(dataset.getFileSystemDirectory());
            }
            dataset.setStorageIdentifier("file://" + dataset.getAuthority() + dataset.getDoiSeparator() + dataset.getIdentifier());
        }
    } else if (dvObject instanceof Dataverse) {
        dataverse = this.getDataverse();
    } else {
        throw new IOException("Data Access: Invalid DvObject type");
    }
    // This "status" is a leftover from 3.6; we don't have a use for it
    // in 4.0 yet; and we may not need it at all.
    // -- L.A. 4.0.2
    this.setStatus(200);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) FileOutputStream(java.io.FileOutputStream) DataVariable(edu.harvard.iq.dataverse.datavariable.DataVariable) IOException(java.io.IOException) Dataverse(edu.harvard.iq.dataverse.Dataverse) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 73 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class S3AccessIO method generateTemporaryS3Url.

public String generateTemporaryS3Url() throws IOException {
    // A. 1 hour by default seems like an OK number. Making it configurable seems like a good idea too. -- L.A.
    if (s3 == null) {
        throw new IOException("ERROR: s3 not initialised. ");
    }
    if (dvObject instanceof DataFile) {
        key = getMainFileKey();
        java.util.Date expiration = new java.util.Date();
        long msec = expiration.getTime();
        msec += 1000 * getUrlExpirationMinutes();
        expiration.setTime(msec);
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
        // Default.
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);
        ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
        // responseHeaders.setContentDisposition("attachment; filename="+this.getDataFile().getDisplayName());
        // Encode the file name explicitly specifying the encoding as UTF-8:
        // (otherwise S3 may not like non-ASCII characters!)
        // Most browsers are happy with just "filename="+URLEncoder.encode(this.getDataFile().getDisplayName(), "UTF-8")
        // in the header. But Firefox appears to require that "UTF8" is
        // specified explicitly, as below:
        responseHeaders.setContentDisposition("attachment; filename*=UTF-8''" + URLEncoder.encode(this.getDataFile().getDisplayName(), "UTF-8"));
        // - without it, download will work, but Firefox will leave the special
        // characters in the file name encoded. For example, the file name
        // will look like "1976%E2%80%932016.txt" instead of "1976–2016.txt",
        // where the dash is the "long dash", represented by a 3-byte UTF8
        // character "\xE2\x80\x93"
        responseHeaders.setContentType(this.getDataFile().getContentType());
        generatePresignedUrlRequest.setResponseHeaders(responseHeaders);
        URL s = s3.generatePresignedUrl(generatePresignedUrlRequest);
        return s.toString();
    } else if (dvObject instanceof Dataset) {
        throw new IOException("Data Access: GenerateTemporaryS3Url: Invalid DvObject type : Dataset");
    } else if (dvObject instanceof Dataverse) {
        throw new IOException("Data Access: Invalid DvObject type : Dataverse");
    } else {
        throw new IOException("Data Access: Invalid DvObject type");
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) GeneratePresignedUrlRequest(com.amazonaws.services.s3.model.GeneratePresignedUrlRequest) Date(java.util.Date) Dataset(edu.harvard.iq.dataverse.Dataset) IOException(java.io.IOException) ResponseHeaderOverrides(com.amazonaws.services.s3.model.ResponseHeaderOverrides) Dataverse(edu.harvard.iq.dataverse.Dataverse) Date(java.util.Date) URL(java.net.URL)

Example 74 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class SwiftAccessIO method open.

@Override
public void open(DataAccessOption... options) throws IOException {
    DataAccessRequest req = this.getRequest();
    if (isWriteAccessRequested(options)) {
        isWriteAccess = true;
        isReadAccess = false;
    } else {
        isWriteAccess = false;
        isReadAccess = true;
    }
    if (dvObject instanceof DataFile) {
        DataFile dataFile = this.getDataFile();
        if (req != null && req.getParameter("noVarHeader") != null) {
            this.setNoVarHeader(true);
        }
        if (dataFile.getStorageIdentifier() == null || "".equals(dataFile.getStorageIdentifier())) {
            throw new IOException("Data Access: No local storage identifier defined for this datafile.");
        }
        if (isReadAccess) {
            InputStream fin = openSwiftFileAsInputStream();
            if (fin == null) {
                throw new IOException("Failed to open Swift file " + getStorageLocation());
            }
            this.setInputStream(fin);
            setChannel(Channels.newChannel(fin));
            if (dataFile.getContentType() != null && dataFile.getContentType().equals("text/tab-separated-values") && dataFile.isTabularData() && dataFile.getDataTable() != null && (!this.noVarHeader())) {
                List<DataVariable> datavariables = dataFile.getDataTable().getDataVariables();
                String varHeaderLine = generateVariableHeader(datavariables);
                this.setVarHeader(varHeaderLine);
            }
        } else if (isWriteAccess) {
            swiftFileObject = initializeSwiftFileObject(true);
        }
        this.setMimeType(dataFile.getContentType());
        try {
            this.setFileName(dataFile.getFileMetadata().getLabel());
        } catch (Exception ex) {
            this.setFileName("unknown");
        }
    } else if (dvObject instanceof Dataset) {
        // such as a dataset thumbnail or a metadata export
        if (isReadAccess) {
            // TODO: fix this
            InputStream fin = openSwiftFileAsInputStream();
            if (fin == null) {
                throw new IOException("Failed to open Swift file " + getStorageLocation());
            }
            this.setInputStream(fin);
        } else if (isWriteAccess) {
            swiftFileObject = initializeSwiftFileObject(true);
        }
    } else if (dvObject instanceof Dataverse) {
    } else {
        throw new IOException("Data Access: Invalid DvObject type");
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Dataset(edu.harvard.iq.dataverse.Dataset) DataVariable(edu.harvard.iq.dataverse.datavariable.DataVariable) IOException(java.io.IOException) Dataverse(edu.harvard.iq.dataverse.Dataverse) SignatureException(java.security.SignatureException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 75 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class DataverseUserPage method displayNotification.

public void displayNotification() {
    for (UserNotification userNotification : notificationsList) {
        switch(userNotification.getType()) {
            case ASSIGNROLE:
            case REVOKEROLE:
                // Can either be a dataverse or dataset, so search both
                Dataverse dataverse = dataverseService.find(userNotification.getObjectId());
                if (dataverse != null) {
                    userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), dataverse));
                    userNotification.setTheObject(dataverse);
                } else {
                    Dataset dataset = datasetService.find(userNotification.getObjectId());
                    if (dataset != null) {
                        userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), dataset));
                        userNotification.setTheObject(dataset);
                    } else {
                        DataFile datafile = fileService.find(userNotification.getObjectId());
                        userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), datafile));
                        userNotification.setTheObject(datafile);
                    }
                }
                break;
            case CREATEDV:
                userNotification.setTheObject(dataverseService.find(userNotification.getObjectId()));
                break;
            case REQUESTFILEACCESS:
                DataFile file = fileService.find(userNotification.getObjectId());
                userNotification.setTheObject(file.getOwner());
                break;
            case GRANTFILEACCESS:
            case REJECTFILEACCESS:
                userNotification.setTheObject(datasetService.find(userNotification.getObjectId()));
                break;
            case MAPLAYERUPDATED:
            case CREATEDS:
            case SUBMITTEDDS:
            case PUBLISHEDDS:
            case RETURNEDDS:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
            case MAPLAYERDELETEFAILED:
                userNotification.setTheObject(fileService.findFileMetadata(userNotification.getObjectId()));
                break;
            case CREATEACC:
                userNotification.setTheObject(userNotification.getUser());
                break;
            case CHECKSUMFAIL:
                userNotification.setTheObject(datasetService.find(userNotification.getObjectId()));
                break;
            case FILESYSTEMIMPORT:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
            case CHECKSUMIMPORT:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
        }
        userNotification.setDisplayAsRead(userNotification.isReadNotification());
        if (userNotification.isReadNotification() == false) {
            userNotification.setReadNotification(true);
            userNotificationService.save(userNotification);
        }
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) UserNotification(edu.harvard.iq.dataverse.UserNotification) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Aggregations

Dataverse (edu.harvard.iq.dataverse.Dataverse)94 Dataset (edu.harvard.iq.dataverse.Dataset)34 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)31 Test (org.junit.Test)27 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)22 DataFile (edu.harvard.iq.dataverse.DataFile)18 IOException (java.io.IOException)18 Path (javax.ws.rs.Path)16 JsonObject (javax.json.JsonObject)15 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)11 ArrayList (java.util.ArrayList)11 EJBException (javax.ejb.EJBException)11 JsonObjectBuilder (javax.json.JsonObjectBuilder)11 InputStream (java.io.InputStream)10 Date (java.util.Date)10 JsonArrayBuilder (javax.json.JsonArrayBuilder)10 POST (javax.ws.rs.POST)10 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)9 User (edu.harvard.iq.dataverse.authorization.users.User)9 SwordError (org.swordapp.server.SwordError)9