Search in sources :

Example 41 with DataverseRequest

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();
}
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 42 with DataverseRequest

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));
}
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 43 with DataverseRequest

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();
    }
}
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)

Example 44 with DataverseRequest

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());
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) ImportException(edu.harvard.iq.dataverse.api.imports.ImportException) IOException(java.io.IOException) JsonObjectBuilder(javax.json.JsonObjectBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse) PrintWriter(java.io.PrintWriter) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 45 with DataverseRequest

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();
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException)

Aggregations

DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)57 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)22 Dataverse (edu.harvard.iq.dataverse.Dataverse)21 Test (org.junit.Test)18 Dataset (edu.harvard.iq.dataverse.Dataset)15 Path (javax.ws.rs.Path)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 SwordError (org.swordapp.server.SwordError)10 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)6 IOException (java.io.IOException)6 POST (javax.ws.rs.POST)6 DataFile (edu.harvard.iq.dataverse.DataFile)5 User (edu.harvard.iq.dataverse.authorization.users.User)5 HarvestingClient (edu.harvard.iq.dataverse.harvest.client.HarvestingClient)5 JsonObject (javax.json.JsonObject)5 JsonObjectBuilder (javax.json.JsonObjectBuilder)5 DepositReceipt (org.swordapp.server.DepositReceipt)5 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)4