Search in sources :

Example 1 with UploadRequestResponse

use of edu.harvard.iq.dataverse.datacapturemodule.UploadRequestResponse 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)

Example 2 with UploadRequestResponse

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

the class RequestRsyncScriptCommandTest method setUp.

@Before
public void setUp() {
    testEngine = new TestDataverseEngine(new TestCommandContext() {

        @Override
        public DataCaptureModuleServiceBean dataCaptureModule() {
            return new DataCaptureModuleServiceBean() {

                @Override
                public UploadRequestResponse requestRsyncScriptCreation(String jsonString, String dcmBaseUrl) {
                    return new UploadRequestResponse(200, "myResponse");
                }

                @Override
                public ScriptRequestResponse retreiveRequestedRsyncScript(String datasetIdentifier, String dcmBaseUrl) {
                    int httpStatusCode = 200;
                    long userId = 123l;
                    String script = "theScript";
                    ScriptRequestResponse scriptRequestResponse = new ScriptRequestResponse(httpStatusCode, datasetIdentifier, userId, script);
                    return scriptRequestResponse;
                }
            };
        }

        @Override
        public SettingsServiceBean settings() {
            return new MockSettingsSvc();
        }
    });
}
Also used : ScriptRequestResponse(edu.harvard.iq.dataverse.datacapturemodule.ScriptRequestResponse) TestCommandContext(edu.harvard.iq.dataverse.engine.TestCommandContext) UploadRequestResponse(edu.harvard.iq.dataverse.datacapturemodule.UploadRequestResponse) DataCaptureModuleServiceBean(edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleServiceBean) TestDataverseEngine(edu.harvard.iq.dataverse.engine.TestDataverseEngine) Before(org.junit.Before)

Aggregations

ScriptRequestResponse (edu.harvard.iq.dataverse.datacapturemodule.ScriptRequestResponse)2 UploadRequestResponse (edu.harvard.iq.dataverse.datacapturemodule.UploadRequestResponse)2 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 DataCaptureModuleServiceBean (edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleServiceBean)1 TestCommandContext (edu.harvard.iq.dataverse.engine.TestCommandContext)1 TestDataverseEngine (edu.harvard.iq.dataverse.engine.TestDataverseEngine)1 PermissionException (edu.harvard.iq.dataverse.engine.command.exception.PermissionException)1 Before (org.junit.Before)1