use of edu.harvard.iq.dataverse.engine.command.DataverseRequest in project dataverse by IQSS.
the class HarvestingClients method startHarvestingJob.
// TODO:
// add a @DELETE method
// (there is already a DeleteHarvestingClient command)
// Methods for managing harvesting runs (jobs):
// This POST starts a new harvesting run:
@POST
@Path("{nickName}/run")
public Response startHarvestingJob(@PathParam("nickName") String clientNickname, @QueryParam("key") String apiKey) throws IOException {
try {
AuthenticatedUser authenticatedUser = null;
try {
authenticatedUser = findAuthenticatedUserOrDie();
} catch (WrappedResponse wr) {
return error(Response.Status.UNAUTHORIZED, "Authentication required to use this API method");
}
if (authenticatedUser == null || !authenticatedUser.isSuperuser()) {
return error(Response.Status.FORBIDDEN, "Only the Dataverse Admin user can run harvesting jobs");
}
HarvestingClient harvestingClient = harvestingClientService.findByNickname(clientNickname);
if (harvestingClient == null) {
return error(Response.Status.NOT_FOUND, "No such dataverse: " + clientNickname);
}
DataverseRequest dataverseRequest = createDataverseRequest(authenticatedUser);
harvesterService.doAsyncHarvest(dataverseRequest, harvestingClient);
} catch (Exception e) {
return this.error(Response.Status.BAD_REQUEST, "Exception thrown when running harvesting client\"" + clientNickname + "\" via REST API; " + e.getMessage());
}
return this.accepted();
}
use of edu.harvard.iq.dataverse.engine.command.DataverseRequest in project dataverse by IQSS.
the class HarvestingClients method harvestingClients.
/*
* /api/harvest/clients
* and
* /api/harvest/clients/{nickname}
* will, by default, return a JSON record with the information about the
* configured remote archives.
* optionally, plain text output may be provided as well.
*/
@GET
@Path("/")
public Response harvestingClients(@QueryParam("key") String apiKey) throws IOException {
List<HarvestingClient> harvestingClients = null;
try {
harvestingClients = harvestingClientService.getAllHarvestingClients();
} catch (Exception ex) {
return error(Response.Status.INTERNAL_SERVER_ERROR, "Caught an exception looking up configured harvesting clients; " + ex.getMessage());
}
if (harvestingClients == null) {
// returning an empty list:
return ok(jsonObjectBuilder().add("harvestingClients", ""));
}
JsonArrayBuilder hcArr = Json.createArrayBuilder();
for (HarvestingClient harvestingClient : harvestingClients) {
// We already have this harvestingClient - wny do we need to
// execute this "Get HarvestingClients Client Command" in order to get it,
// again? - the purpose of the command is to run the request through
// the Authorization system, to verify that they actually have
// the permission to view this harvesting client config. -- L.A. 4.4
HarvestingClient retrievedHarvestingClient = null;
try {
DataverseRequest req = createDataverseRequest(findUserOrDie());
retrievedHarvestingClient = execCommand(new GetHarvestingClientCommand(req, harvestingClient));
} catch (Exception ex) {
// Don't do anything.
// We'll just skip this one - since this means the user isn't
// authorized to view this client configuration.
}
if (retrievedHarvestingClient != null) {
hcArr.add(harvestingConfigAsJson(retrievedHarvestingClient));
}
}
return ok(jsonObjectBuilder().add("harvestingClients", hcArr));
}
use of edu.harvard.iq.dataverse.engine.command.DataverseRequest in project dataverse by IQSS.
the class HarvestingClients method modifyHarvestingClient.
@PUT
@Path("{nickName}")
public Response modifyHarvestingClient(String jsonBody, @PathParam("nickName") String nickName, @QueryParam("key") String apiKey) throws IOException, JsonParseException {
HarvestingClient harvestingClient = null;
try {
harvestingClient = harvestingClientService.findByNickname(nickName);
} catch (Exception ex) {
// We don't care what happened; we'll just assume we couldn't find it.
harvestingClient = null;
}
if (harvestingClient == null) {
return error(Response.Status.NOT_FOUND, "Harvesting client " + nickName + " not found.");
}
String ownerDataverseAlias = harvestingClient.getDataverse().getAlias();
try (StringReader rdr = new StringReader(jsonBody)) {
DataverseRequest req = createDataverseRequest(findUserOrDie());
JsonObject json = Json.createReader(rdr).readObject();
String newDataverseAlias = jsonParser().parseHarvestingClient(json, harvestingClient);
if (newDataverseAlias != null && !newDataverseAlias.equals("") && !newDataverseAlias.equals(ownerDataverseAlias)) {
return error(Response.Status.BAD_REQUEST, "Bad \"dataverseAlias\" supplied. Harvesting client " + nickName + " belongs to the dataverse " + ownerDataverseAlias);
}
HarvestingClient managedHarvestingClient = execCommand(new UpdateHarvestingClientCommand(req, harvestingClient));
return created("/datasets/" + nickName, harvestingConfigAsJson(managedHarvestingClient));
} catch (JsonParseException ex) {
return error(Response.Status.BAD_REQUEST, "Error parsing harvesting client: " + ex.getMessage());
} catch (WrappedResponse ex) {
return ex.getResponse();
}
}
use of edu.harvard.iq.dataverse.engine.command.DataverseRequest in project dataverse by IQSS.
the class BatchImport method postImport.
/**
* Import a new Dataset with DDI xml data posted in the request
*
* @param body the xml
* @param parentIdtf the dataverse to import into (id or alias)
* @param apiKey user's api key
* @return import status (including id of the dataset created)
*/
@POST
@Path("import")
public Response postImport(String body, @QueryParam("dv") String parentIdtf, @QueryParam("key") String apiKey) {
DataverseRequest dataverseRequest;
try {
dataverseRequest = createDataverseRequest(findAuthenticatedUserOrDie());
} catch (WrappedResponse wr) {
return wr.getResponse();
}
if (parentIdtf == null) {
parentIdtf = "root";
}
Dataverse owner = findDataverse(parentIdtf);
if (owner == null) {
return error(Response.Status.NOT_FOUND, "Can't find dataverse with identifier='" + parentIdtf + "'");
}
try {
// Cleanup log isn't needed for ImportType == NEW. We don't do any data cleanup in this mode.
PrintWriter cleanupLog = null;
// Since this is a single input from a POST, there is no file that we are reading from.
String filename = null;
JsonObjectBuilder status = importService.doImport(dataverseRequest, owner, body, filename, ImportType.NEW, cleanupLog);
return this.ok(status);
} catch (ImportException | IOException e) {
return this.error(Response.Status.BAD_REQUEST, e.getMessage());
}
}
use of edu.harvard.iq.dataverse.engine.command.DataverseRequest in project dataverse by IQSS.
the class HarvestingClientsPage method runHarvest.
public void runHarvest(HarvestingClient harvestingClient) {
try {
DataverseRequest dataverseRequest = new DataverseRequest(session.getUser(), (HttpServletRequest) null);
harvesterService.doAsyncHarvest(dataverseRequest, harvestingClient);
} catch (Exception ex) {
String failMessage = "Sorry, harvest could not be started for the selected harvesting client configuration (unknown server error).";
JH.addMessage(FacesMessage.SEVERITY_FATAL, failMessage);
return;
}
String successMessage = JH.localize("harvestclients.actions.runharvest.success");
successMessage = successMessage.replace("{0}", harvestingClient.getName());
JsfHelper.addSuccessMessage(successMessage);
// it has already been updated with the "inprogress" setting)
try {
Thread.sleep(500L);
} catch (Exception e) {
}
configuredHarvestingClients = harvestingClientService.getAllHarvestingClients();
}
Aggregations