Search in sources :

Example 21 with DataFile

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

the class MediaResourceManagerImpl method deleteMediaResource.

@Override
public void deleteMediaResource(String uri, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(uri);
    String targetType = urlManager.getTargetType();
    String fileId = urlManager.getTargetIdentifier();
    if (targetType != null && fileId != null) {
        if ("file".equals(targetType)) {
            String fileIdString = urlManager.getTargetIdentifier();
            if (fileIdString != null) {
                Long fileIdLong;
                try {
                    fileIdLong = Long.valueOf(fileIdString);
                } catch (NumberFormatException ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "File id must be a number, not '" + fileIdString + "'. URL was: " + uri);
                }
                if (fileIdLong != null) {
                    logger.fine("preparing to delete file id " + fileIdLong);
                    DataFile fileToDelete = dataFileService.find(fileIdLong);
                    if (fileToDelete != null) {
                        Dataset dataset = fileToDelete.getOwner();
                        Dataset datasetThatOwnsFile = fileToDelete.getOwner();
                        Dataverse dataverseThatOwnsFile = datasetThatOwnsFile.getOwner();
                        /**
                         * @todo it would be nice to have this check higher
                         * up. Do we really need the file ID? Should the
                         * last argument to isUserAllowedOn be changed from
                         * "dataset" to "fileToDelete"?
                         */
                        UpdateDatasetCommand updateDatasetCommand = new UpdateDatasetCommand(dataset, dvReq, fileToDelete);
                        if (!permissionService.isUserAllowedOn(user, updateDatasetCommand, dataset)) {
                            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "User " + user.getDisplayInfo().getTitle() + " is not authorized to modify " + dataverseThatOwnsFile.getAlias());
                        }
                        try {
                            commandEngine.submit(updateDatasetCommand);
                        } catch (CommandException ex) {
                            throw SwordUtil.throwSpecialSwordErrorWithoutStackTrace(UriRegistry.ERROR_BAD_REQUEST, "Could not delete file: " + ex);
                        }
                    } else {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to find file id " + fileIdLong + " from URL: " + uri);
                    }
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to find file id in URL: " + uri);
                }
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not file file to delete in URL: " + uri);
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unsupported file type found in URL: " + uri);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Target or identifer not specified in URL: " + uri);
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DataFile(edu.harvard.iq.dataverse.DataFile) SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Example 22 with DataFile

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

the class StatementManagerImpl method getStatement.

@Override
public Statement getStatement(String editUri, Map<String, String> map, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordServerException, SwordError, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(editUri);
    String globalId = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("study") && globalId != null) {
        logger.fine("request for sword statement by user " + user.getDisplayInfo().getTitle());
        Dataset dataset = datasetService.findByGlobalId(globalId);
        if (dataset == null) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "couldn't find dataset with global ID of " + globalId);
        }
        Dataverse dvThatOwnsDataset = dataset.getOwner();
        if (!permissionService.isUserAllowedOn(user, new GetDraftDatasetVersionCommand(dvReq, dataset), dataset)) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to view dataset with global ID " + globalId);
        }
        String feedUri = urlManager.getHostnamePlusBaseUrlPath(editUri) + "/edit/study/" + dataset.getGlobalId();
        String author = dataset.getLatestVersion().getAuthorsStr();
        String title = dataset.getLatestVersion().getTitle();
        // in the statement, the element is called "updated"
        Date lastUpdatedFinal = new Date();
        Date lastUpdateTime = dataset.getLatestVersion().getLastUpdateTime();
        if (lastUpdateTime != null) {
            lastUpdatedFinal = lastUpdateTime;
        } else {
            logger.info("lastUpdateTime was null, trying createtime");
            Date createtime = dataset.getLatestVersion().getCreateTime();
            if (createtime != null) {
                lastUpdatedFinal = createtime;
            } else {
                logger.info("creatime was null, using \"now\"");
                lastUpdatedFinal = new Date();
            }
        }
        AtomDate atomDate = new AtomDate(lastUpdatedFinal);
        String datedUpdated = atomDate.toString();
        Statement statement = new AtomStatement(feedUri, author, title, datedUpdated);
        Map<String, String> states = new HashMap<>();
        states.put("latestVersionState", dataset.getLatestVersion().getVersionState().toString());
        Boolean isMinorUpdate = dataset.getLatestVersion().isMinorUpdate();
        states.put("isMinorUpdate", isMinorUpdate.toString());
        if (dataset.isLocked()) {
            states.put("locked", "true");
            states.put("lockedDetail", dataset.getLocks().stream().map(l -> l.getInfo()).collect(joining(",")));
            Optional<DatasetLock> earliestLock = dataset.getLocks().stream().min((l1, l2) -> (int) Math.signum(l1.getStartTime().getTime() - l2.getStartTime().getTime()));
            states.put("lockedStartTime", earliestLock.get().getStartTime().toString());
        } else {
            states.put("locked", "false");
        }
        statement.setStates(states);
        List<FileMetadata> fileMetadatas = dataset.getLatestVersion().getFileMetadatas();
        for (FileMetadata fileMetadata : fileMetadatas) {
            DataFile dataFile = fileMetadata.getDataFile();
            // We are exposing the filename for informational purposes. The file id is what you
            // actually operate on to delete a file, etc.
            // 
            // Replace spaces to avoid IRISyntaxException
            String fileNameFinal = fileMetadata.getLabel().replace(' ', '_');
            String fileUrlString = urlManager.getHostnamePlusBaseUrlPath(editUri) + "/edit-media/file/" + dataFile.getId() + "/" + fileNameFinal;
            IRI fileUrl;
            try {
                fileUrl = new IRI(fileUrlString);
            } catch (IRISyntaxException ex) {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Invalid URL for file ( " + fileUrlString + " ) resulted in " + ex.getMessage());
            }
            ResourcePart resourcePart = new ResourcePart(fileUrl.toString());
            // default to something that doesn't throw a org.apache.abdera.util.MimeTypeParseException
            String finalFileFormat = "application/octet-stream";
            String contentType = dataFile.getContentType();
            if (contentType != null) {
                finalFileFormat = contentType;
            }
            resourcePart.setMediaType(finalFileFormat);
            /**
             * @todo: Why are properties set on a ResourcePart not exposed
             * when you GET a Statement? Asked about this at
             * http://www.mail-archive.com/sword-app-tech@lists.sourceforge.net/msg00394.html
             */
            // Map<String, String> properties = new HashMap<String, String>();
            // properties.put("filename", studyFile.getFileName());
            // properties.put("category", studyFile.getLatestCategory());
            // properties.put("originalFileType", studyFile.getOriginalFileType());
            // properties.put("id", studyFile.getId().toString());
            // properties.put("UNF", studyFile.getUnf());
            // resourcePart.setProperties(properties);
            statement.addResource(resourcePart);
        /**
         * @todo it's been noted at
         * https://github.com/IQSS/dataverse/issues/892#issuecomment-54159284
         * that at the file level the "updated" date is always "now",
         * which seems to be set here:
         * https://github.com/swordapp/JavaServer2.0/blob/sword2-server-1.0/src/main/java/org/swordapp/server/AtomStatement.java#L70
         */
        }
        return statement;
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not determine target type or identifier from URL: " + editUri);
    }
}
Also used : IRI(org.apache.abdera.i18n.iri.IRI) ResourcePart(org.swordapp.server.ResourcePart) SwordError(org.swordapp.server.SwordError) AtomStatement(org.swordapp.server.AtomStatement) HashMap(java.util.HashMap) Dataset(edu.harvard.iq.dataverse.Dataset) AtomStatement(org.swordapp.server.AtomStatement) Statement(org.swordapp.server.Statement) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) Date(java.util.Date) AtomDate(org.apache.abdera.model.AtomDate) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DataFile(edu.harvard.iq.dataverse.DataFile) AtomDate(org.apache.abdera.model.AtomDate) GetDraftDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetDraftDatasetVersionCommand) DatasetLock(edu.harvard.iq.dataverse.DatasetLock) IRISyntaxException(org.apache.abdera.i18n.iri.IRISyntaxException)

Example 23 with DataFile

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

the class Access method fileCardImage.

@Path("fileCardImage/{fileId}")
@GET
@Produces({ "image/png" })
public InputStream fileCardImage(@PathParam("fileId") Long fileId, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    DataFile df = dataFileService.find(fileId);
    if (df == null) {
        logger.warning("Preview: datafile service could not locate a DataFile object for id " + fileId + "!");
        return null;
    }
    StorageIO<DataFile> thumbnailDataAccess = null;
    try {
        StorageIO<DataFile> dataAccess = df.getStorageIO();
        if (dataAccess != null) {
            // && dataAccess.isLocalFile()) {
            dataAccess.open();
            if ("application/pdf".equalsIgnoreCase(df.getContentType()) || df.isImage() || "application/zipped-shapefile".equalsIgnoreCase(df.getContentType())) {
                thumbnailDataAccess = ImageThumbConverter.getImageThumbnailAsInputStream(dataAccess, 48);
            }
        }
    } catch (IOException ioEx) {
        return null;
    }
    if (thumbnailDataAccess != null && thumbnailDataAccess.getInputStream() != null) {
        return thumbnailDataAccess.getInputStream();
    }
    return null;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 24 with DataFile

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

the class Admin method getDatasetThumbnailMetadata.

/**
 * This method is used in API tests, called from UtilIt.java.
 */
@GET
@Path("datasets/thumbnailMetadata/{id}")
public Response getDatasetThumbnailMetadata(@PathParam("id") Long idSupplied) {
    Dataset dataset = datasetSvc.find(idSupplied);
    if (dataset == null) {
        return error(Response.Status.NOT_FOUND, "Could not find dataset based on id supplied: " + idSupplied + ".");
    }
    JsonObjectBuilder data = Json.createObjectBuilder();
    DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
    data.add("isUseGenericThumbnail", dataset.isUseGenericThumbnail());
    data.add("datasetLogoPresent", DatasetUtil.isDatasetLogoPresent(dataset));
    if (datasetThumbnail != null) {
        data.add("datasetThumbnailBase64image", datasetThumbnail.getBase64image());
        DataFile dataFile = datasetThumbnail.getDataFile();
        if (dataFile != null) {
            /**
             * @todo Change this from a String to a long.
             */
            data.add("dataFileId", dataFile.getId().toString());
        }
    }
    return ok(data);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DatasetThumbnail(edu.harvard.iq.dataverse.dataset.DatasetThumbnail) Dataset(edu.harvard.iq.dataverse.Dataset) JsonObjectBuilder(javax.json.JsonObjectBuilder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 25 with DataFile

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

the class JsonPrinterTest method testGetFileCategories.

@Test
public void testGetFileCategories() {
    FileMetadata fmd = new FileMetadata();
    DatasetVersion dsVersion = new DatasetVersion();
    DataFile dataFile = new DataFile();
    List<DataFileTag> dataFileTags = new ArrayList<>();
    DataFileTag tag = new DataFileTag();
    tag.setTypeByLabel("Survey");
    dataFileTags.add(tag);
    dataFile.setTags(dataFileTags);
    fmd.setDatasetVersion(dsVersion);
    fmd.setDataFile(dataFile);
    List<DataFileCategory> fileCategories = new ArrayList<>();
    DataFileCategory dataFileCategory = new DataFileCategory();
    dataFileCategory.setName("Data");
    fileCategories.add(dataFileCategory);
    fmd.setCategories(fileCategories);
    JsonObjectBuilder job = JsonPrinter.json(fmd);
    assertNotNull(job);
    JsonObject jsonObject = job.build();
    System.out.println("json: " + jsonObject);
    assertEquals("", jsonObject.getString("description"));
    assertEquals("", jsonObject.getString("label"));
    assertEquals("Data", jsonObject.getJsonArray("categories").getString(0));
    assertEquals("", jsonObject.getJsonObject("dataFile").getString("filename"));
    assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("filesize"));
    assertEquals("UNKNOWN", jsonObject.getJsonObject("dataFile").getString("originalFormatLabel"));
    assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("rootDataFileId"));
    assertEquals("Survey", jsonObject.getJsonObject("dataFile").getJsonArray("tabularTags").getString(0));
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder) DataFileTag(edu.harvard.iq.dataverse.DataFileTag) Test(org.junit.Test)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)111 Dataset (edu.harvard.iq.dataverse.Dataset)39 IOException (java.io.IOException)39 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)30 ArrayList (java.util.ArrayList)25 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)20 File (java.io.File)20 FileNotFoundException (java.io.FileNotFoundException)18 Path (javax.ws.rs.Path)18 Dataverse (edu.harvard.iq.dataverse.Dataverse)17 FileInputStream (java.io.FileInputStream)16 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 Date (java.util.Date)13 GET (javax.ws.rs.GET)13 Test (org.junit.Test)13 Timestamp (java.sql.Timestamp)11 InputStream (java.io.InputStream)10 DataVariable (edu.harvard.iq.dataverse.datavariable.DataVariable)8 FileOutputStream (java.io.FileOutputStream)8