use of edu.harvard.iq.dataverse.harvest.client.HarvestingClient in project dataverse by IQSS.
the class HarvestingClients method createHarvestingClient.
@POST
@Path("{nickName}")
public Response createHarvestingClient(String jsonBody, @PathParam("nickName") String nickName, @QueryParam("key") String apiKey) throws IOException, JsonParseException {
try (StringReader rdr = new StringReader(jsonBody)) {
JsonObject json = Json.createReader(rdr).readObject();
HarvestingClient harvestingClient = new HarvestingClient();
// TODO: check that it doesn't exist yet...
harvestingClient.setName(nickName);
String dataverseAlias = jsonParser().parseHarvestingClient(json, harvestingClient);
Dataverse ownerDataverse = dataverseService.findByAlias(dataverseAlias);
if (ownerDataverse == null) {
return error(Response.Status.BAD_REQUEST, "No such dataverse: " + dataverseAlias);
}
harvestingClient.setDataverse(ownerDataverse);
if (ownerDataverse.getHarvestingClientConfigs() == null) {
ownerDataverse.setHarvestingClientConfigs(new ArrayList<>());
}
ownerDataverse.getHarvestingClientConfigs().add(harvestingClient);
DataverseRequest req = createDataverseRequest(findUserOrDie());
HarvestingClient managedHarvestingClient = execCommand(new CreateHarvestingClientCommand(req, harvestingClient));
return created("/harvest/clients/" + 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.harvest.client.HarvestingClient 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 + ".");
}
}
use of edu.harvard.iq.dataverse.harvest.client.HarvestingClient in project dataverse by IQSS.
the class HarvestingClientsPage method saveClient.
// this saves an existing client that the user has edited:
public void saveClient(ActionEvent ae) {
HarvestingClient harvestingClient = getSelectedClient();
if (harvestingClient == null) {
// TODO:
// tell the user somehow that the client cannot be saved, and advise
// them to save the settings they have entered.
// as of now - we will show an error message, but only after the
// edit form has been closed.
}
// nickname is not editable for existing clients:
// harvestingClient.setName(newNickname);
harvestingClient.setHarvestingUrl(newHarvestingUrl);
harvestingClient.setHarvestingSet(newOaiSet);
harvestingClient.setMetadataPrefix(newMetadataFormat);
harvestingClient.setHarvestStyle(newHarvestingStyle);
if (isNewHarvestingScheduled()) {
harvestingClient.setScheduled(true);
if (isNewHarvestingScheduledWeekly()) {
harvestingClient.setSchedulePeriod(HarvestingClient.SCHEDULE_PERIOD_WEEKLY);
if (getWeekDayNumber() == null) {
// create a "week day is required..." error message, etc.
// but we may be better off not even giving them an opportunity
// to leave the field blank - ?
}
harvestingClient.setScheduleDayOfWeek(getWeekDayNumber());
} else {
harvestingClient.setSchedulePeriod(HarvestingClient.SCHEDULE_PERIOD_DAILY);
}
if (getHourOfDay() == null) {
// see the comment above, about the day of week. same here.
}
harvestingClient.setScheduleHourOfDay(getHourOfDay());
} else {
harvestingClient.setScheduled(false);
}
try {
harvestingClient = engineService.submit(new UpdateHarvestingClientCommand(dvRequestService.getDataverseRequest(), harvestingClient));
configuredHarvestingClients = harvestingClientService.getAllHarvestingClients();
if (!harvestingClient.isScheduled()) {
dataverseTimerService.removeHarvestTimer(harvestingClient);
}
JsfHelper.addSuccessMessage("Succesfully updated harvesting client " + harvestingClient.getName());
} catch (CommandException ex) {
logger.log(Level.WARNING, "Failed to save harvesting client", ex);
JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, "Failed to save harvesting client", ex.getMessage());
} catch (Exception ex) {
JH.addMessage(FacesMessage.SEVERITY_FATAL, "Failed to save harvesting client (reason unknown).");
logger.log(Level.SEVERE, "Failed to save harvesting client (reason unknown)." + ex.getMessage(), ex);
}
setPageMode(PageMode.VIEW);
}
use of edu.harvard.iq.dataverse.harvest.client.HarvestingClient in project dataverse by IQSS.
the class HarvestingClientsPage method createClient.
public void createClient(ActionEvent ae) {
// will be set as type OAI by default
HarvestingClient newHarvestingClient = new HarvestingClient();
newHarvestingClient.setName(newNickname);
if (getSelectedDestinationDataverse() == null) {
JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, "Failed to create a new Harvesting Client configuration: no destination dataverse selected.");
}
newHarvestingClient.setDataverse(getSelectedDestinationDataverse());
if (getSelectedDestinationDataverse().getHarvestingClientConfigs() == null) {
getSelectedDestinationDataverse().setHarvestingClientConfigs(new ArrayList<>());
}
getSelectedDestinationDataverse().getHarvestingClientConfigs().add(newHarvestingClient);
newHarvestingClient.setHarvestingUrl(newHarvestingUrl);
if (!StringUtils.isEmpty(newOaiSet)) {
newHarvestingClient.setHarvestingSet(newOaiSet);
}
newHarvestingClient.setMetadataPrefix(newMetadataFormat);
newHarvestingClient.setHarvestStyle(newHarvestingStyle);
if (isNewHarvestingScheduled()) {
newHarvestingClient.setScheduled(true);
if (isNewHarvestingScheduledWeekly()) {
newHarvestingClient.setSchedulePeriod(HarvestingClient.SCHEDULE_PERIOD_WEEKLY);
if (getWeekDayNumber() == null) {
// create a "week day is required..." error message, etc.
// but we may be better off not even giving them an opportunity
// to leave the field blank - ?
}
newHarvestingClient.setScheduleDayOfWeek(getWeekDayNumber());
} else {
newHarvestingClient.setSchedulePeriod(HarvestingClient.SCHEDULE_PERIOD_DAILY);
}
if (getHourOfDay() == null) {
// see the comment above, about the day of week. same here.
}
newHarvestingClient.setScheduleHourOfDay(getHourOfDay());
}
// make default archive url (used to generate links pointing back to the
// archival sources, when harvested datasets are displayed in search results),
// from the harvesting url:
newHarvestingClient.setArchiveUrl(makeDefaultArchiveUrl());
// set default description - they can customize it as they see fit:
newHarvestingClient.setArchiveDescription(JH.localize("harvestclients.viewEditDialog.archiveDescription.default.generic"));
try {
newHarvestingClient = engineService.submit(new CreateHarvestingClientCommand(dvRequestService.getDataverseRequest(), newHarvestingClient));
configuredHarvestingClients = harvestingClientService.getAllHarvestingClients();
// NO, we no longer create timers here. It is the job of the Mother Timer!
// dataverseTimerService.createHarvestTimer(newHarvestingClient);
String successMessage = JH.localize("harvestclients.newClientDialog.success");
successMessage = successMessage.replace("{0}", newHarvestingClient.getName());
JsfHelper.addSuccessMessage(successMessage);
}/* TODO: (?) add a dedicated "NameAlreadyExists" exception for the
create client command?
catch ( CreateHarvestingClientCommand.NicknameAlreadyExistsException naee ) {
FacesContext.getCurrentInstance().addMessage(newHarvestingClient.getName(),
new FacesMessage( FacesMessage.SEVERITY_ERROR, naee.getMessage(), null));
}*/
catch (CommandException ex) {
logger.log(Level.WARNING, "Harvesting client creation command failed", ex);
JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, "Harvesting Client creation command failed.", ex.getMessage());
} catch (Exception ex) {
JH.addMessage(FacesMessage.SEVERITY_FATAL, "Harvesting client creation failed (reason unknown).");
logger.log(Level.SEVERE, "Harvesting client creation failed (reason unknown)." + ex.getMessage(), ex);
}
setPageMode(PageMode.VIEW);
}
use of edu.harvard.iq.dataverse.harvest.client.HarvestingClient in project dataverse by IQSS.
the class DataverseTimerServiceBean method handleTimeout.
/**
* This method is called whenever an EJB Timer goes off.
* Check to see if this is a Harvest Timer, and if it is
* Run the harvest for the given (scheduled) dataverse
* @param timer
*/
@Timeout
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void handleTimeout(javax.ejb.Timer timer) {
if (!systemConfig.isTimerServer()) {
// logger.info("I am not the timer server! - bailing out of handleTimeout()");
Logger.getLogger(DataverseTimerServiceBean.class.getName()).log(Level.WARNING, null, "I am not the timer server! - but handleTimeout() got called. Please investigate!");
}
try {
logger.log(Level.INFO, "Handling timeout on " + InetAddress.getLocalHost().getCanonicalHostName());
} catch (UnknownHostException ex) {
Logger.getLogger(DataverseTimerServiceBean.class.getName()).log(Level.SEVERE, null, ex);
}
if (timer.getInfo() instanceof MotherTimerInfo) {
logger.info("Behold! I am the Master Timer, king of all timers! I'm here to create all the lesser timers!");
removeHarvestTimers();
for (HarvestingClient client : harvestingClientService.getAllHarvestingClients()) {
createHarvestTimer(client);
}
} else if (timer.getInfo() instanceof HarvestTimerInfo) {
HarvestTimerInfo info = (HarvestTimerInfo) timer.getInfo();
try {
logger.log(Level.INFO, "running a harvesting client: id=" + info.getHarvestingClientId());
// Timer batch jobs are run by the main Admin user.
// TODO: revisit how we retrieve the superuser here.
// Should it be configurable somewhere, which superuser
// runs these jobs? Should there be a central mechanism for obtaining
// the "major", builtin superuser for this Dataverse instance?
// -- L.A. 4.5, Aug. 2016
// getAuthenticatedUser("admin");
AuthenticatedUser adminUser = authSvc.getAdminUser();
if (adminUser == null) {
logger.info("Scheduled harvest: failed to locate the admin user! Exiting.");
throw new IOException("Scheduled harvest: failed to locate the admin user");
}
logger.info("found admin user " + adminUser.getName());
DataverseRequest dataverseRequest = new DataverseRequest(adminUser, (HttpServletRequest) null);
harvesterService.doHarvest(dataverseRequest, info.getHarvestingClientId());
} catch (Throwable e) {
// Harvester Service should be handling any error notifications,
// if/when things go wrong.
// (TODO: -- verify this logic; harvesterService may still be able
// to throw an IOException, if it could not run the harvest at all,
// or could not for whatever reason modify the database record...
// in this case we should, probably, log the error and try to send
// a mail notification. -- L.A. 4.4)
// dataverseService.setHarvestResult(info.getHarvestingDataverseId(), harvesterService.HARVEST_RESULT_FAILED);
// mailService.sendHarvestErrorNotification(dataverseService.find().getSystemEmail(), dataverseService.find().getName());
logException(e, logger);
}
} else if (timer.getInfo() instanceof ExportTimerInfo) {
try {
ExportTimerInfo info = (ExportTimerInfo) timer.getInfo();
logger.info("Timer Service: Running a scheduled export job.");
// try to export all unexported datasets:
datasetService.exportAll();
// and update all oai sets:
oaiSetService.exportAllSets();
} catch (Throwable e) {
logException(e, logger);
}
}
}
Aggregations