Search in sources :

Example 1 with GetHarvestingClientCommand

use of edu.harvard.iq.dataverse.engine.command.impl.GetHarvestingClientCommand in project dataverse by IQSS.

the class HarvestingClients method harvestingClient.

@GET
@Path("{nickName}")
public Response harvestingClient(@PathParam("nickName") String nickName, @QueryParam("key") String apiKey) throws IOException {
    HarvestingClient harvestingClient = null;
    try {
        harvestingClient = harvestingClientService.findByNickname(nickName);
    } catch (Exception ex) {
        logger.warning("Exception caught looking up harvesting client " + nickName + ": " + ex.getMessage());
        return error(Response.Status.BAD_REQUEST, "Internal error: failed to look up harvesting client " + nickName + ".");
    }
    if (harvestingClient == null) {
        return error(Response.Status.NOT_FOUND, "Harvesting client " + nickName + " not found.");
    }
    HarvestingClient retrievedHarvestingClient = null;
    try {
        // findUserOrDie() and execCommand() both throw WrappedResponse
        // exception, that already has a proper HTTP response in it.
        retrievedHarvestingClient = execCommand(new GetHarvestingClientCommand(createDataverseRequest(findUserOrDie()), harvestingClient));
        logger.info("retrieved Harvesting Client " + retrievedHarvestingClient.getName() + " with the GetHarvestingClient command.");
    } catch (WrappedResponse wr) {
        return wr.getResponse();
    } catch (Exception ex) {
        logger.warning("Unknown exception caught while executing GetHarvestingClientCommand: " + ex.getMessage());
        retrievedHarvestingClient = null;
    }
    if (retrievedHarvestingClient == null) {
        return error(Response.Status.BAD_REQUEST, "Internal error: failed to retrieve harvesting client " + nickName + ".");
    }
    try {
        return ok(harvestingConfigAsJson(retrievedHarvestingClient));
    } catch (Exception ex) {
        logger.warning("Unknown exception caught while trying to format harvesting client config as json: " + ex.getMessage());
        return error(Response.Status.BAD_REQUEST, "Internal error: failed to produce output for harvesting client " + nickName + ".");
    }
}
Also used : GetHarvestingClientCommand(edu.harvard.iq.dataverse.engine.command.impl.GetHarvestingClientCommand) 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 2 with GetHarvestingClientCommand

use of edu.harvard.iq.dataverse.engine.command.impl.GetHarvestingClientCommand 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)

Aggregations

GetHarvestingClientCommand (edu.harvard.iq.dataverse.engine.command.impl.GetHarvestingClientCommand)2 HarvestingClient (edu.harvard.iq.dataverse.harvest.client.HarvestingClient)2 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)2 IOException (java.io.IOException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1