Search in sources :

Example 31 with Dataset

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

the class MediaResourceManagerImpl method replaceOrAddFiles.

DepositReceipt replaceOrAddFiles(String uri, Deposit deposit, AuthCredentials authCredentials, SwordConfiguration swordConfiguration, boolean shouldReplace) throws SwordError, SwordAuthException, SwordServerException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(uri);
    String globalId = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("study") && globalId != null) {
        logger.fine("looking up dataset with globalId " + globalId);
        Dataset dataset = datasetService.findByGlobalId(globalId);
        if (dataset == null) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataset with global ID of " + globalId);
        }
        UpdateDatasetCommand updateDatasetCommand = new UpdateDatasetCommand(dataset, dvReq);
        if (!permissionService.isUserAllowedOn(user, updateDatasetCommand, dataset)) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to modify dataset with global ID " + dataset.getGlobalId());
        }
        // -------------------------------------
        if (DataCaptureModuleUtil.rsyncSupportEnabled(settingsSvc.getValueForKey(SettingsServiceBean.Key.UploadMethods))) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, SettingsServiceBean.Key.UploadMethods + " contains " + SystemConfig.FileUploadMethods.RSYNC + ". Please use rsync file upload.");
        }
        /**
         * @todo decide if we want non zip files to work. Technically, now
         * that we're letting ingestService.createDataFiles unpack the zip
         * for us, the following *does* work:
         *
         * curl--data-binary @path/to/trees.png -H "Content-Disposition:
         * filename=trees.png" -H "Content-Type: image/png" -H "Packaging:
         * http://purl.org/net/sword/package/SimpleZip"
         *
         * We *might* want to continue to force API users to only upload zip
         * files so that some day we can support a including a file or files
         * that contain the metadata (i.e. description) for each file in the
         * zip: https://github.com/IQSS/dataverse/issues/723
         */
        if (!deposit.getPackaging().equals(UriRegistry.PACKAGE_SIMPLE_ZIP)) {
            throw new SwordError(UriRegistry.ERROR_CONTENT, 415, "Package format " + UriRegistry.PACKAGE_SIMPLE_ZIP + " is required but format specified in 'Packaging' HTTP header was " + deposit.getPackaging());
        }
        String uploadedZipFilename = deposit.getFilename();
        DatasetVersion editVersion = dataset.getEditVersion();
        if (deposit.getInputStream() == null) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Deposit input stream was null.");
        }
        int bytesAvailableInInputStream = 0;
        try {
            bytesAvailableInInputStream = deposit.getInputStream().available();
        } catch (IOException ex) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not determine number of bytes available in input stream: " + ex);
        }
        if (bytesAvailableInInputStream == 0) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Bytes available in input stream was " + bytesAvailableInInputStream + ". Please check the file you are attempting to deposit.");
        }
        /**
         * @todo Think about if we should instead pass in "application/zip"
         * rather than letting ingestService.createDataFiles() guess the
         * contentType by passing it "null". See also the note above about
         * SimpleZip vs. other contentTypes.
         */
        String guessContentTypeForMe = null;
        List<DataFile> dataFiles = new ArrayList<>();
        try {
            try {
                dataFiles = FileUtil.createDataFiles(editVersion, deposit.getInputStream(), uploadedZipFilename, guessContentTypeForMe, systemConfig);
            } catch (EJBException ex) {
                Throwable cause = ex.getCause();
                if (cause != null) {
                    if (cause instanceof IllegalArgumentException) {
                        /**
                         * @todo should be safe to remove this catch of
                         * EJBException and IllegalArgumentException once
                         * this ticket is resolved:
                         *
                         * IllegalArgumentException: MALFORMED when
                         * uploading certain zip files
                         * https://github.com/IQSS/dataverse/issues/1021
                         */
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Exception caught calling ingestService.createDataFiles. Problem with zip file, perhaps: " + cause);
                    } else {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Exception caught calling ingestService.createDataFiles: " + cause);
                    }
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Exception caught calling ingestService.createDataFiles. No cause: " + ex.getMessage());
                }
            }
        /*TODO: L.A. 4.6! catch (FileExceedsMaxSizeException ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Exception caught calling ingestService.createDataFiles: " + ex.getMessage());
                    //Logger.getLogger(MediaResourceManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
                }*/
        } catch (IOException ex) {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to add file(s) to dataset: " + ex.getMessage());
        }
        if (!dataFiles.isEmpty()) {
            Set<ConstraintViolation> constraintViolations = editVersion.validate();
            if (constraintViolations.size() > 0) {
                ConstraintViolation violation = constraintViolations.iterator().next();
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to add file(s) to dataset: " + violation.getMessage() + " The invalid value was \"" + violation.getInvalidValue() + "\".");
            } else {
                ingestService.addFiles(editVersion, dataFiles);
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "No files to add to dataset. Perhaps the zip file was empty.");
        }
        try {
            dataset = commandEngine.submit(updateDatasetCommand);
        } catch (CommandException ex) {
            throw returnEarly("Couldn't update dataset " + ex);
        } catch (EJBException ex) {
            /**
             * @todo stop bothering to catch an EJBException once this has
             * been implemented:
             *
             * Have commands catch ConstraintViolationException and turn
             * them into something that inherits from CommandException ·
             * https://github.com/IQSS/dataverse/issues/1009
             */
            Throwable cause = ex;
            StringBuilder sb = new StringBuilder();
            sb.append(ex.getLocalizedMessage());
            while (cause.getCause() != null) {
                cause = cause.getCause();
                sb.append(cause + " ");
                if (cause instanceof ConstraintViolationException) {
                    ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                    for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                        sb.append(" Invalid value \"").append(violation.getInvalidValue()).append("\" for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                    }
                }
            }
            throw returnEarly("EJBException: " + sb.toString());
        }
        ingestService.startIngestJobs(dataset, user);
        ReceiptGenerator receiptGenerator = new ReceiptGenerator();
        String baseUrl = urlManager.getHostnamePlusBaseUrlPath(uri);
        DepositReceipt depositReceipt = receiptGenerator.createDatasetReceipt(baseUrl, dataset);
        return depositReceipt;
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unable to determine target type or identifier from URL: " + uri);
    }
}
Also used : SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) IOException(java.io.IOException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DataFile(edu.harvard.iq.dataverse.DataFile) DepositReceipt(org.swordapp.server.DepositReceipt) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) EJBException(javax.ejb.EJBException) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Example 32 with Dataset

use of edu.harvard.iq.dataverse.Dataset 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 33 with Dataset

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

the class MediaResourceManagerImpl method getMediaResourceRepresentation.

@Override
public MediaResource getMediaResourceRepresentation(String uri, Map<String, String> map, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(uri);
    String globalId = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("study") && globalId != null) {
        logger.fine("looking up dataset with globalId " + globalId);
        Dataset dataset = datasetService.findByGlobalId(globalId);
        if (dataset != null) {
            /**
             * @todo: support downloading of files (SWORD 2.0 Profile 6.4. -
             * Retrieving the content)
             * http://swordapp.github.io/SWORDv2-Profile/SWORDProfile.html#protocoloperations_retrievingcontent
             *
             * This ticket is mostly about terms of use:
             * https://github.com/IQSS/dataverse/issues/183
             */
            boolean getMediaResourceRepresentationSupported = false;
            if (getMediaResourceRepresentationSupported) {
                Dataverse dvThatOwnsDataset = dataset.getOwner();
                /**
                 * @todo Add Dataverse 4 style permission check here. Is
                 * there a Command we use for downloading files as zip?
                 */
                boolean authorized = false;
                if (authorized) {
                    /**
                     * @todo Zip file download is being implemented in
                     * https://github.com/IQSS/dataverse/issues/338
                     */
                    InputStream fixmeInputStream = new ByteArrayInputStream("FIXME: replace with zip of all dataset files".getBytes());
                    String contentType = "application/zip";
                    String packaging = UriRegistry.PACKAGE_SIMPLE_ZIP;
                    boolean isPackaged = true;
                    MediaResource mediaResource = new MediaResource(fixmeInputStream, contentType, packaging, isPackaged);
                    return mediaResource;
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to get a media resource representation of the dataset with global ID " + dataset.getGlobalId());
                }
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Downloading files via the SWORD-based Dataverse Data Deposit API is not (yet) supported: https://github.com/IQSS/dataverse/issues/183");
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't find dataset with global ID of " + globalId);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't dermine target type or identifier from URL: " + uri);
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaResource(org.swordapp.server.MediaResource) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Example 34 with Dataset

use of edu.harvard.iq.dataverse.Dataset 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 35 with Dataset

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

the class CollectionDepositManagerImpl method createNew.

@Override
public DepositReceipt createNew(String collectionUri, Deposit deposit, AuthCredentials authCredentials, SwordConfiguration config) throws SwordError, SwordServerException, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, request);
    urlManager.processUrl(collectionUri);
    String dvAlias = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("dataverse") && dvAlias != null) {
        logger.log(Level.FINE, "attempting deposit into this dataverse alias: {0}", dvAlias);
        Dataverse dvThatWillOwnDataset = dataverseService.findByAlias(dvAlias);
        if (dvThatWillOwnDataset != null) {
            logger.log(Level.FINE, "multipart: {0}", deposit.isMultipart());
            logger.log(Level.FINE, "binary only: {0}", deposit.isBinaryOnly());
            logger.log(Level.FINE, "entry only: {0}", deposit.isEntryOnly());
            logger.log(Level.FINE, "in progress: {0}", deposit.isInProgress());
            logger.log(Level.FINE, "metadata relevant: {0}", deposit.isMetadataRelevant());
            if (deposit.isEntryOnly()) {
                // do a sanity check on the XML received
                try {
                    SwordEntry swordEntry = deposit.getSwordEntry();
                    logger.log(Level.FINE, "deposit XML received by createNew():\n{0}", swordEntry.toString());
                } catch (ParseException ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Can not create dataset due to malformed Atom entry: " + ex);
                }
                Dataset dataset = new Dataset();
                dataset.setOwner(dvThatWillOwnDataset);
                String nonNullDefaultIfKeyNotFound = "";
                String protocol = settingsService.getValueForKey(SettingsServiceBean.Key.Protocol, nonNullDefaultIfKeyNotFound);
                String authority = settingsService.getValueForKey(SettingsServiceBean.Key.Authority, nonNullDefaultIfKeyNotFound);
                String separator = settingsService.getValueForKey(SettingsServiceBean.Key.DoiSeparator, nonNullDefaultIfKeyNotFound);
                dataset.setProtocol(protocol);
                dataset.setAuthority(authority);
                dataset.setDoiSeparator(separator);
                // Wait until the create command before actually getting an identifier
                // dataset.setIdentifier(datasetService.generateDatasetIdentifier(protocol, authority, separator));
                logger.log(Level.FINE, "DS Deposit identifier: {0}", dataset.getIdentifier());
                CreateDatasetCommand createDatasetCommand = new CreateDatasetCommand(dataset, dvReq, false);
                if (!permissionService.isUserAllowedOn(user, createDatasetCommand, dataset)) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to create a dataset in this dataverse.");
                }
                DatasetVersion newDatasetVersion = dataset.getEditVersion();
                String foreignFormat = SwordUtil.DCTERMS;
                try {
                    importGenericService.importXML(deposit.getSwordEntry().toString(), foreignFormat, newDatasetVersion);
                } catch (Exception ex) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "problem calling importXML: " + ex);
                }
                swordService.addDatasetContact(newDatasetVersion, user);
                swordService.addDatasetDepositor(newDatasetVersion, user);
                swordService.addDatasetSubjectIfMissing(newDatasetVersion);
                swordService.setDatasetLicenseAndTermsOfUse(newDatasetVersion, deposit.getSwordEntry());
                Dataset createdDataset = null;
                try {
                    createdDataset = engineSvc.submit(createDatasetCommand);
                } catch (EJBException | CommandException ex) {
                    Throwable cause = ex;
                    StringBuilder sb = new StringBuilder();
                    sb.append(ex.getLocalizedMessage());
                    while (cause.getCause() != null) {
                        cause = cause.getCause();
                        /**
                         * @todo move this ConstraintViolationException
                         * check to CreateDatasetCommand. Can be triggered
                         * if you don't call dataset.setIdentifier() or if
                         * you feed it date format we don't like. Once this
                         * is done we should be able to drop EJBException
                         * from the catch above and only catch
                         * CommandException
                         *
                         * See also Have commands catch
                         * ConstraintViolationException and turn them into
                         * something that inherits from CommandException ·
                         * Issue #1009 · IQSS/dataverse -
                         * https://github.com/IQSS/dataverse/issues/1009
                         */
                        if (cause instanceof ConstraintViolationException) {
                            ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                            for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                                sb.append(" Invalid value: '").append(violation.getInvalidValue()).append("' for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
                            }
                        }
                    }
                    logger.info(sb.toString());
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't create dataset: " + sb.toString());
                }
                if (createdDataset != null) {
                    ReceiptGenerator receiptGenerator = new ReceiptGenerator();
                    String baseUrl = urlManager.getHostnamePlusBaseUrlPath(collectionUri);
                    DepositReceipt depositReceipt = receiptGenerator.createDatasetReceipt(baseUrl, createdDataset);
                    return depositReceipt;
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Problem creating dataset. Null returned.");
                }
            } else if (deposit.isBinaryOnly()) {
                // curl --insecure -s --data-binary "@example.zip" -H "Content-Disposition: filename=example.zip" -H "Content-Type: application/zip" https://sword:sword@localhost:8181/dvn/api/data-deposit/v1/swordv2/collection/dataverse/sword/
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Binary deposit to the collection IRI via POST is not supported. Please POST an Atom entry instead.");
            } else if (deposit.isMultipart()) {
                // "Yeah, multipart is critically broken across all implementations" -- http://www.mail-archive.com/sword-app-tech@lists.sourceforge.net/msg00327.html
                throw new UnsupportedOperationException("Not yet implemented");
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "expected deposit types are isEntryOnly, isBinaryOnly, and isMultiPart");
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataverse: " + dvAlias);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not determine target type or identifier from URL: " + collectionUri);
    }
}
Also used : SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) CreateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) SwordAuthException(org.swordapp.server.SwordAuthException) SwordServerException(org.swordapp.server.SwordServerException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) EJBException(javax.ejb.EJBException) ConstraintViolationException(javax.validation.ConstraintViolationException) ParseException(org.apache.abdera.parser.ParseException) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DepositReceipt(org.swordapp.server.DepositReceipt) ConstraintViolation(javax.validation.ConstraintViolation) SwordEntry(org.swordapp.server.SwordEntry) ConstraintViolationException(javax.validation.ConstraintViolationException) ParseException(org.apache.abdera.parser.ParseException) EJBException(javax.ejb.EJBException)

Aggregations

Dataset (edu.harvard.iq.dataverse.Dataset)142 Test (org.junit.Test)53 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)44 DataFile (edu.harvard.iq.dataverse.DataFile)41 Dataverse (edu.harvard.iq.dataverse.Dataverse)34 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)27 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)23 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)22 Path (javax.ws.rs.Path)22 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)17 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)17 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)16 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)16 Date (java.util.Date)16 JsonObjectBuilder (javax.json.JsonObjectBuilder)16 JsonObject (javax.json.JsonObject)15 DvObject (edu.harvard.iq.dataverse.DvObject)14 RoleAssignee (edu.harvard.iq.dataverse.authorization.RoleAssignee)14 PrivateUrlUser (edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser)14