use of edu.harvard.iq.dataverse.harvest.client.HarvestingClient in project dataverse by IQSS.
the class DeleteHarvestingClientCommand method executeImpl.
@Override
public void executeImpl(CommandContext ctxt) throws CommandException {
if (harvestingClient == null) {
throw new IllegalCommandException("DeleteHarvestingClientCommand: attempted to execute with null harvesting client; dataverse: " + motherDataverse.getAlias(), this);
}
HarvestingClient merged = ctxt.em().merge(harvestingClient);
for (DataFile harvestedFile : ctxt.files().findHarvestedFilesByClient(merged)) {
DataFile mergedFile = ctxt.em().merge(harvestedFile);
ctxt.em().remove(mergedFile);
harvestedFile = null;
}
ctxt.em().remove(merged);
}
use of edu.harvard.iq.dataverse.harvest.client.HarvestingClient 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.harvest.client.HarvestingClient 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.harvest.client.HarvestingClient 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();
}
}
Aggregations