Search in sources :

Example 1 with DataCaptureModuleException

use of edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleException in project dataverse by IQSS.

the class RequestRsyncScriptCommand method execute.

@Override
public ScriptRequestResponse execute(CommandContext ctxt) throws CommandException {
    if (request == null) {
        throw new PermissionException("DataverseRequest cannot be null.", this, Collections.singleton(Permission.AddDataset), dataset);
    }
    String dcmBaseUrl = ctxt.settings().getValueForKey(DataCaptureModuleUrl);
    if (dcmBaseUrl == null) {
        throw new RuntimeException(DataCaptureModuleUrl + " is null!");
    }
    User user = request.getUser();
    if (!(user instanceof AuthenticatedUser)) {
        /**
         * @todo get Permission.AddDataset from above somehow rather than
         * duplicating it here.
         */
        throw new PermissionException("This command can only be called by an AuthenticatedUser, not " + user, this, Collections.singleton(Permission.AddDataset), dataset);
    }
    // We need an AuthenticatedUser so we can pass its database id to the DCM.
    AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
    String errorPreamble = "User id " + authenticatedUser.getId() + " had a problem retrieving rsync script for dataset id " + dataset.getId() + " from Data Capture Module.";
    String jsonString = DataCaptureModuleUtil.generateJsonForUploadRequest(authenticatedUser, dataset).toString();
    UploadRequestResponse uploadRequestResponse = null;
    try {
        uploadRequestResponse = ctxt.dataCaptureModule().requestRsyncScriptCreation(jsonString, dcmBaseUrl + DataCaptureModuleServiceBean.uploadRequestPath);
    } catch (DataCaptureModuleException ex) {
        throw new RuntimeException("Problem making upload request to Data Capture Module:  " + DataCaptureModuleUtil.getMessageFromException(ex));
    }
    int statusCode = uploadRequestResponse.getHttpStatusCode();
    String response = uploadRequestResponse.getResponse();
    if (statusCode != 200) {
        throw new RuntimeException("When making the upload request, rather than 200 the status code was " + statusCode + ". The body was \'" + response + "\'. We cannot proceed. Returning.");
    }
    long millisecondsToSleep = DataCaptureModuleServiceBean.millisecondsToSleepBetweenUploadRequestAndScriptRequestCalls;
    logger.fine("Message from Data Caputure Module upload request endpoint: " + response + ". Sleeping " + millisecondsToSleep + " milliseconds before making rsync script request.");
    try {
        Thread.sleep(millisecondsToSleep);
    } catch (InterruptedException ex) {
        throw new RuntimeException(errorPreamble + " Unable to wait " + millisecondsToSleep + " milliseconds: " + ex.getLocalizedMessage());
    }
    ScriptRequestResponse scriptRequestResponse = null;
    try {
        scriptRequestResponse = ctxt.dataCaptureModule().retreiveRequestedRsyncScript(dataset.getIdentifier(), dcmBaseUrl + DataCaptureModuleServiceBean.scriptRequestPath);
    } catch (DataCaptureModuleException ex) {
        throw new RuntimeException("Problem making script request to Data Capture Module:  " + DataCaptureModuleUtil.getMessageFromException(ex));
    }
    String script = scriptRequestResponse.getScript();
    if (script == null || script.isEmpty()) {
        logger.warning("There was a problem getting the script. DCM returned status code: " + scriptRequestResponse.getHttpStatusCode());
    }
    logger.fine("script for dataset " + dataset.getId() + ": " + script);
    return scriptRequestResponse;
}
Also used : PermissionException(edu.harvard.iq.dataverse.engine.command.exception.PermissionException) ScriptRequestResponse(edu.harvard.iq.dataverse.datacapturemodule.ScriptRequestResponse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) UploadRequestResponse(edu.harvard.iq.dataverse.datacapturemodule.UploadRequestResponse) DataCaptureModuleException(edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)

Aggregations

AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 User (edu.harvard.iq.dataverse.authorization.users.User)1 DataCaptureModuleException (edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleException)1 ScriptRequestResponse (edu.harvard.iq.dataverse.datacapturemodule.ScriptRequestResponse)1 UploadRequestResponse (edu.harvard.iq.dataverse.datacapturemodule.UploadRequestResponse)1 PermissionException (edu.harvard.iq.dataverse.engine.command.exception.PermissionException)1