Skip to content

Commit

Permalink
DCM: always send user id and dataset id to DCM
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jun 14, 2016
1 parent f198957 commit c60ac56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import static edu.harvard.iq.dataverse.settings.SettingsServiceBean.Key.DataCaptureModuleUrl;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.json.JsonObjectBuilder;

@Stateless
@Named
Expand All @@ -29,26 +29,16 @@ public class DataCaptureModuleServiceBean implements Serializable {
* @throws Exception Throws an exception if Data Capture Module URL hasn't
* been configured or if the POST failed.
*/
public HttpResponse<JsonNode> requestRsyncScriptCreation(AuthenticatedUser user) throws Exception {
/**
* @todo Move this to a "util" class.
*
* @todo Send user id instead of "userIdentifier" ... may need to add
* more lookups by user database id to the "admin" API.
*
* @todo pass dataset id
*/
JsonObjectBuilder jsonObject = Json.createObjectBuilder();
jsonObject.add("userIdentifier", user.getAuthenticatedUserLookup().getPersistentUserId());
jsonObject.add("uid", user.getAuthenticatedUserLookup().getPersistentUserId());
String json = jsonObject.build().toString();
public HttpResponse<JsonNode> requestRsyncScriptCreation(AuthenticatedUser user, Dataset dataset, JsonObjectBuilder jab) throws Exception {
String dcmBaseUrl = settingsService.getValueForKey(DataCaptureModuleUrl);
if (dcmBaseUrl == null) {
throw new Exception("Problem POSTing JSON to Data Capture Module. The '" + DataCaptureModuleUrl + "' setting has not been configured.");
}
String jsonString = jab.build().toString();
logger.info("JSON to send to Data Capture Module: " + jsonString);
try {
HttpResponse<JsonNode> jsonResponse = Unirest.post(dcmBaseUrl + "/ur.py")
.body(json)
.body(jsonString)
.asJson();
return jsonResponse;
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.engine.command.exception.CommandExecutionException;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonObjectBuilder;

@RequiredPermissions(Permission.AddDataset)
public class RequestRsyncScriptCommand extends AbstractVoidCommand {
Expand Down Expand Up @@ -46,14 +48,18 @@ protected void executeImpl(CommandContext ctxt) throws CommandException {
*
* @todo make sure the error is logged to the actionlogrecord
*/
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());
jab.add("datasetId", dataset.getId());
try {
response = ctxt.dataCaptureModule().requestRsyncScriptCreation(au);
response = ctxt.dataCaptureModule().requestRsyncScriptCreation(au, dataset, jab);
} catch (Exception ex) {
throw new CommandException("Problem retrieving rsync script from Data Capture Module: " + ex.getLocalizedMessage(), this);
}
int statusCode = response.getStatus();
if (statusCode != 200) {
logger.info("Problem retrieving rsync script from Data Capture Module. Status code was: " + statusCode);
logger.info("Problem retrieving rsync script from Data Capture Module. Status code was " + statusCode + " and body was \'" + response.getBody() + "\'.");
}
String script = response.getBody().getObject().getString("script");
if (script == null || script.isEmpty()) {
Expand Down

0 comments on commit c60ac56

Please sign in to comment.