Search in sources :

Example 6 with HarvestingClient

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);
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient)

Example 7 with HarvestingClient

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();
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 8 with HarvestingClient

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));
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) GetHarvestingClientCommand(edu.harvard.iq.dataverse.engine.command.impl.GetHarvestingClientCommand) JsonArrayBuilder(javax.json.JsonArrayBuilder) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) IOException(java.io.IOException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with HarvestingClient

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();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) UpdateHarvestingClientCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateHarvestingClientCommand) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) IOException(java.io.IOException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Aggregations

HarvestingClient (edu.harvard.iq.dataverse.harvest.client.HarvestingClient)9 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)5 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)5 IOException (java.io.IOException)5 Path (javax.ws.rs.Path)5 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2 CreateHarvestingClientCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateHarvestingClientCommand)2 GetHarvestingClientCommand (edu.harvard.iq.dataverse.engine.command.impl.GetHarvestingClientCommand)2 UpdateHarvestingClientCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateHarvestingClientCommand)2 StringReader (java.io.StringReader)2 JsonObject (javax.json.JsonObject)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 DataFile (edu.harvard.iq.dataverse.DataFile)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)1 HarvestTimerInfo (edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo)1 UnknownHostException (java.net.UnknownHostException)1 Timeout (javax.ejb.Timeout)1