diff --git a/src/main/java/edu/harvard/iq/dataverse/datacapturemodule/DataCaptureModuleServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/datacapturemodule/DataCaptureModuleServiceBean.java index 93b1aa17df2..a862cc8ed62 100644 --- a/src/main/java/edu/harvard/iq/dataverse/datacapturemodule/DataCaptureModuleServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/datacapturemodule/DataCaptureModuleServiceBean.java @@ -36,10 +36,21 @@ public HttpResponse requestRsyncScriptCreation(AuthenticatedUser user, } String jsonString = jab.build().toString(); logger.fine("JSON to send to Data Capture Module: " + jsonString); - HttpResponse jsonResponse = Unirest.post(dcmBaseUrl + "/ur.py") + HttpResponse uploadRequest = Unirest.post(dcmBaseUrl + "/ur.py") .body(jsonString) .asJson(); - return jsonResponse; + return uploadRequest; + } + + public HttpResponse retreiveRequestedRsyncScript(AuthenticatedUser user, Dataset dataset) throws Exception { + String dcmBaseUrl = settingsService.getValueForKey(DataCaptureModuleUrl); + if (dcmBaseUrl == null) { + throw new Exception("Problem GETing JSON to Data Capture Module for dataset " + dataset.getId() + " The '" + DataCaptureModuleUrl + "' setting has not been configured."); + } + HttpResponse scriptRequest = Unirest + .get(dcmBaseUrl + "/sr.py/" + dataset.getId()) + .asJson(); + return scriptRequest; } } diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RequestRsyncScriptCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RequestRsyncScriptCommand.java index 3ea3a301001..6ab04f54c99 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RequestRsyncScriptCommand.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RequestRsyncScriptCommand.java @@ -54,6 +54,9 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt } AuthenticatedUser au = (AuthenticatedUser) user; HttpResponse response; + /** + * @todo Refactor this building of JSON to make it testable. + */ JsonObjectBuilder jab = Json.createObjectBuilder(); // The general rule should be to always pass the user id and dataset id to the DCM. jab.add("userId", au.getId()); @@ -65,6 +68,10 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt throw new RuntimeException(errorPreamble + ex.getLocalizedMessage(), ex); } int statusCode = response.getStatus(); + /** + * @todo Since we're creating something, maybe a 201 response would be + * more appropriate. + */ if (statusCode != 200) { /** * @todo is the body too big to fit in the actionlogrecord? The @@ -73,12 +80,29 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt */ throw new RuntimeException(errorPreamble + "Rather than 200 the status code was " + statusCode + ". The body was \'" + response.getBody() + "\'."); } + String message = response.getBody().getObject().getString("status"); + logger.info("Message from Data Caputure Module upload request endpoint: " + message); /** * @todo Don't expect to get the script from ur.py (upload request). Go * fetch it from sr.py (script request) after a minute or so. (Cron runs - * every minute.) Wait 90 seconds to be safe. Note that it's possible - * for a different user id to call ur.py vs. sr.py + * every minute.) Wait 90 seconds to be safe. */ + long millisecondsToSleep = 0; + try { + Thread.sleep(millisecondsToSleep); + } catch (InterruptedException ex) { + throw new RuntimeException(errorPreamble + "Unable to wait " + millisecondsToSleep + " milliseconds: " + ex.getLocalizedMessage()); + } + try { + response = ctxt.dataCaptureModule().retreiveRequestedRsyncScript(au, dataset); + } catch (Exception ex) { + throw new RuntimeException(errorPreamble + "Problem retrieving rsync script: " + ex.getLocalizedMessage()); + } + statusCode = response.getStatus(); + if (statusCode != 200) { + throw new RuntimeException(errorPreamble + "Rather than 200 the status code was " + statusCode + ". The body was \'" + response.getBody() + "\'."); + } + long datasetId = response.getBody().getObject().getLong("datasetId"); String script = response.getBody().getObject().getString("script"); if (script == null || script.isEmpty()) { throw new RuntimeException(errorPreamble + "The script was null or empty."); @@ -87,8 +111,7 @@ protected void executeImpl(CommandContext ctxt) throws PermissionException, Runt * @todo Put this in the database somewhere. Will I be able to query the * DCM at any time and GET the script again, based on an id? */ - logger.info("script: " + script); - + logger.info("script for dataset " + datasetId + ": " + script); } }