[CST-10632] Implement the consumer to enqueue outgoing LDN messages

This commit is contained in:
mohamed eskander
2023-11-20 20:02:45 +02:00
parent af0686d50f
commit 989d718b9b
17 changed files with 1224 additions and 13 deletions

View File

@@ -0,0 +1,189 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.ldn;
import static java.lang.String.format;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import org.apache.commons.collections4.CollectionUtils;
import org.dspace.app.ldn.factory.NotifyServiceFactory;
import org.dspace.app.ldn.service.LDNMessageService;
import org.dspace.app.ldn.service.NotifyPatternToTriggerService;
import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat;
import org.dspace.content.Bundle;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.I18nUtil;
import org.dspace.core.LDN;
import org.dspace.event.Consumer;
import org.dspace.event.Event;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.web.ContextUtil;
/**
* class for creating a new LDN Messages of installed item
*
* @author Mohamed Eskander (mohamed.eskander at 4science.com)
*/
public class LDNMessageConsumer implements Consumer {
private NotifyPatternToTriggerService notifyPatternToTriggerService;
private LDNMessageService ldnMessageService;
private ConfigurationService configurationService;
private ItemService itemService;
private BitstreamService bitstreamService;
@Override
public void initialize() throws Exception {
notifyPatternToTriggerService = NotifyServiceFactory.getInstance().getNotifyPatternToTriggerService();
ldnMessageService = NotifyServiceFactory.getInstance().getLDNMessageService();
configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
itemService = ContentServiceFactory.getInstance().getItemService();
bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
}
@Override
public void consume(Context context, Event event) throws Exception {
if (event.getSubjectType() != Constants.ITEM ||
event.getEventType() != Event.INSTALL) {
return;
}
createLDNMessages(context, (Item) event.getSubject(context));
}
private void createLDNMessages(Context context, Item item) throws SQLException {
List<NotifyPatternToTrigger> patternsToTrigger =
notifyPatternToTriggerService.findByItem(context, item);
patternsToTrigger.forEach(patternToTrigger -> {
try {
createLDNMessage(context, patternToTrigger);
} catch (SQLException e) {
throw new RuntimeException(e);
}
});
}
private void createLDNMessage(Context context, NotifyPatternToTrigger patternToTrigger)
throws SQLException {
LDN ldn = getLDNMessage(patternToTrigger.getPattern());
LDNMessageEntity ldnMessage =
ldnMessageService.create(context, format("urn:uuid:%s", UUID.randomUUID()));
ldnMessage.setObject(patternToTrigger.getItem());
ldnMessage.setTarget(patternToTrigger.getNotifyService());
ldnMessage.setQueueStatus(LDNMessageEntity.QUEUE_STATUS_QUEUED);
appendGeneratedMessage(ldn, ldnMessage, patternToTrigger.getPattern());
ldnMessageService.update(context, ldnMessage);
}
private LDN getLDNMessage(String pattern) {
try {
return LDN.getLDNMessage(I18nUtil.getLDNFilename(Locale.getDefault(), pattern));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void appendGeneratedMessage(LDN ldn, LDNMessageEntity ldnMessage, String pattern) {
Item item = (Item) ldnMessage.getObject();
ldn.addArgument(getUiUrl());
ldn.addArgument(configurationService.getProperty("ldn.notify.inbox"));
ldn.addArgument(configurationService.getProperty("dspace.name"));
ldn.addArgument(Objects.requireNonNullElse(ldnMessage.getTarget().getUrl(), ""));
ldn.addArgument(Objects.requireNonNullElse(ldnMessage.getTarget().getLdnUrl(), ""));
ldn.addArgument(getUiUrl() + "/handle/" + ldnMessage.getObject().getHandle());
ldn.addArgument(getIdentifierUri(item));
ldn.addArgument(generateBitstreamDownloadUrl(item));
ldn.addArgument(getBitstreamMimeType(findPrimaryBitstream(item)));
ldn.addArgument(ldnMessage.getID());
ldnMessage.setMessage(ldn.generateLDNMessage());
}
private String getUiUrl() {
return configurationService.getProperty("dspace.ui.url");
}
private String getIdentifierUri(Item item) {
return itemService.getMetadataByMetadataString(item, "dc.identifier.uri")
.stream()
.findFirst()
.map(MetadataValue::getValue)
.orElse("");
}
private String generateBitstreamDownloadUrl(Item item) {
String uiUrl = getUiUrl();
return findPrimaryBitstream(item)
.map(bs -> uiUrl + "/bitstreams/" + bs.getID() + "/download")
.orElse("");
}
private Optional<Bitstream> findPrimaryBitstream(Item item) {
List<Bundle> bundles = item.getBundles(Constants.CONTENT_BUNDLE_NAME);
return bundles.stream()
.findFirst()
.map(Bundle::getPrimaryBitstream)
.or(() -> bundles.stream()
.findFirst()
.flatMap(bundle -> CollectionUtils.isNotEmpty(bundle.getBitstreams())
? Optional.of(bundle.getBitstreams().get(0))
: Optional.empty()));
}
private String getBitstreamMimeType(Optional<Bitstream> bitstream) {
return bitstream.map(bs -> {
try {
Context context = ContextUtil.obtainCurrentRequestContext();
BitstreamFormat bitstreamFormat = bs.getFormat(context);
if (bitstreamFormat.getShortDescription().equals("Unknown")) {
return getUserFormatMimeType(bs);
}
return bitstreamFormat.getMIMEType();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}).orElse("");
}
private String getUserFormatMimeType(Bitstream bitstream) {
return bitstreamService.getMetadataFirstValue(bitstream,
"dc", "format", "mimetype", Item.ANY);
}
@Override
public void end(Context ctx) throws Exception {
}
@Override
public void finish(Context ctx) throws Exception {
}
}

View File

@@ -21,7 +21,8 @@ import org.dspace.app.ldn.processor.LDNProcessor;
*/
public class LDNRouter {
private Map<Set<String>, LDNProcessor> processors = new HashMap<>();
private Map<Set<String>, LDNProcessor> incomingProcessors = new HashMap<>();
private Map<Set<String>, LDNProcessor> outcomingProcessors = new HashMap<>();
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(LDNRouter.class);
/**
@@ -41,26 +42,50 @@ public class LDNRouter {
Set<String> ldnMessageTypeSet = new HashSet<String>();
ldnMessageTypeSet.add(ldnMessage.getActivityStreamType());
ldnMessageTypeSet.add(ldnMessage.getCoarNotifyType());
LDNProcessor processor = processors.get(ldnMessageTypeSet);
LDNProcessor processor = null;
if (ldnMessage.getTarget() == null) {
processor = incomingProcessors.get(ldnMessageTypeSet);
} else if (ldnMessage.getOrigin() == null) {
processor = outcomingProcessors.get(ldnMessageTypeSet);
}
return processor;
}
/**
* Get all routes.
* Get all incoming routes.
*
* @return Map<Set<String>, LDNProcessor>
*/
public Map<Set<String>, LDNProcessor> getProcessors() {
return processors;
public Map<Set<String>, LDNProcessor> getIncomingProcessors() {
return incomingProcessors;
}
/**
* Set all routes.
* Set all incoming routes.
*
* @param processors
* @param incomingProcessors
*/
public void setProcessors(Map<Set<String>, LDNProcessor> processors) {
this.processors = processors;
public void setIncomingProcessors(Map<Set<String>, LDNProcessor> incomingProcessors) {
this.incomingProcessors = incomingProcessors;
}
/**
* Get all outcoming routes.
*
* @return Map<Set<String>, LDNProcessor>
*/
public Map<Set<String>, LDNProcessor> getOutcomingProcessors() {
return outcomingProcessors;
}
/**
* Set all outcoming routes.
*
* @param outcomingProcessors
*/
public void setOutcomingProcessors(Map<Set<String>, LDNProcessor> outcomingProcessors) {
this.outcomingProcessors = outcomingProcessors;
}
}

View File

@@ -0,0 +1,65 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.ldn.action;
import static org.dspace.app.ldn.RdfMediaType.APPLICATION_JSON_LD;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.app.ldn.converter.JsonLdHttpMessageConverter;
import org.dspace.app.ldn.model.Notification;
import org.dspace.content.Item;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
/**
* Action to send LDN Message
*
* @author Mohamed Eskander (mohamed.eskander at 4science.com)
*/
public class SendLDNMessageAction implements LDNAction {
private static final Logger log = LogManager.getLogger(SendLDNMessageAction.class);
private final RestTemplate restTemplate;
public SendLDNMessageAction() {
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new JsonLdHttpMessageConverter());
}
@Override
public ActionStatus execute(Notification notification, Item item) throws Exception {
//TODO authorization with Bearer token should be supported.
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", APPLICATION_JSON_LD.toString());
HttpEntity<Notification> request = new HttpEntity<>(notification, headers);
log.info("Announcing notification {}", request);
// https://notify-inbox.info/inbox/ for test
ResponseEntity<String> response = restTemplate.postForEntity(
notification.getTarget().getInbox(),
request,
String.class
);
if (response.getStatusCode() == HttpStatus.ACCEPTED ||
response.getStatusCode() == HttpStatus.CREATED) {
return ActionStatus.CONTINUE;
}
return ActionStatus.ABORT;
}
}

View File

@@ -7,6 +7,7 @@
*/
package org.dspace.app.ldn.factory;
import org.dspace.app.ldn.service.LDNMessageService;
import org.dspace.app.ldn.service.NotifyPatternToTriggerService;
import org.dspace.app.ldn.service.NotifyService;
import org.dspace.app.ldn.service.NotifyServiceInboundPatternService;
@@ -26,6 +27,8 @@ public abstract class NotifyServiceFactory {
public abstract NotifyPatternToTriggerService getNotifyPatternToTriggerService();
public abstract LDNMessageService getLDNMessageService();
public static NotifyServiceFactory getInstance() {
return DSpaceServicesFactory.getInstance().getServiceManager().getServiceByName(
"notifyServiceFactory", NotifyServiceFactory.class);

View File

@@ -7,6 +7,7 @@
*/
package org.dspace.app.ldn.factory;
import org.dspace.app.ldn.service.LDNMessageService;
import org.dspace.app.ldn.service.NotifyPatternToTriggerService;
import org.dspace.app.ldn.service.NotifyService;
import org.dspace.app.ldn.service.NotifyServiceInboundPatternService;
@@ -29,6 +30,9 @@ public class NotifyServiceFactoryImpl extends NotifyServiceFactory {
@Autowired(required = true)
private NotifyPatternToTriggerService notifyPatternToTriggerService;
@Autowired(required = true)
private LDNMessageService ldnMessageService;
@Override
public NotifyService getNotifyService() {
return notifyService;
@@ -44,4 +48,9 @@ public class NotifyServiceFactoryImpl extends NotifyServiceFactory {
return notifyPatternToTriggerService;
}
@Override
public LDNMessageService getLDNMessageService() {
return ldnMessageService;
}
}

View File

@@ -33,6 +33,15 @@ public interface LDNMessageService {
*/
public LDNMessageEntity find(Context context, String id) throws SQLException;
/**
* find all ldn messages
*
* @param context the context
* @return all ldn messages by id
* @throws SQLException If something goes wrong in the database
*/
public List<LDNMessageEntity> findAll(Context context) throws SQLException;
/**
* Creates a new LDNMessage
*
@@ -106,4 +115,12 @@ public interface LDNMessageService {
* @throws SQLException if something goes wrong
*/
public NotifyServiceEntity findNotifyService(Context context, Service service) throws SQLException;
/**
* delete the provided ldn message
*
* @param context the context
* @param ldnMessage the ldn message
* @throws SQLException if something goes wrong
*/
public void delete(Context context, LDNMessageEntity ldnMessage) throws SQLException;
}

View File

@@ -69,6 +69,11 @@ public class LDNMessageServiceImpl implements LDNMessageService {
return ldnMessageDao.findByID(context, LDNMessageEntity.class, id);
}
@Override
public List<LDNMessageEntity> findAll(Context context) throws SQLException {
return ldnMessageDao.findAll(context, LDNMessageEntity.class);
}
@Override
public LDNMessageEntity create(Context context, String id) throws SQLException {
return ldnMessageDao.create(context, new LDNMessageEntity(id));
@@ -268,4 +273,10 @@ public class LDNMessageServiceImpl implements LDNMessageService {
}
return result;
}
@Override
public void delete(Context context, LDNMessageEntity ldnMessage) throws SQLException {
ldnMessageDao.delete(context, ldnMessage);
}
}

View File

@@ -377,6 +377,22 @@ public class I18nUtil {
return templateName;
}
/**
* Get the appropriate localized version of a ldn template according to language settings
*
* @param locale Locale for this request
* @param name String - base name of the ldn template
* @return templateName
* String - localized filename of a ldn template
*/
public static String getLDNFilename(Locale locale, String name) {
String templateFile =
DSpaceServicesFactory.getInstance().getConfigurationService().getProperty("dspace.dir") +
File.separator + "config" + File.separator + "ldn" + File.separator + name;
return getFilename(locale, templateFile, "");
}
/**
* Creates array of Locales from text list of locale-specifications.
* Used to parse lists in DSpace configuration properties.

View File

@@ -0,0 +1,212 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.core;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import javax.mail.MessagingException;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.resource.util.StringResourceRepository;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
/**
* Class representing an LDN message json
*
* @author Mohamed Eskander (mohamed.eskander at 4science.com)
*/
public class LDN {
/**
* The content of the ldn message
*/
private String content;
private String contentName;
/**
* The arguments to fill out
*/
private final List<Object> arguments;
private static final Logger LOG = LogManager.getLogger();
/** Velocity template settings. */
private static final String RESOURCE_REPOSITORY_NAME = "LDN";
private static final Properties VELOCITY_PROPERTIES = new Properties();
static {
VELOCITY_PROPERTIES.put(Velocity.RESOURCE_LOADERS, "string");
VELOCITY_PROPERTIES.put("resource.loader.string.description",
"Velocity StringResource loader");
VELOCITY_PROPERTIES.put("resource.loader.string.class",
StringResourceLoader.class.getName());
VELOCITY_PROPERTIES.put("resource.loader.string.repository.name",
RESOURCE_REPOSITORY_NAME);
VELOCITY_PROPERTIES.put("resource.loader.string.repository.static",
"false");
}
/** Velocity template for the message*/
private Template template;
/**
* Create a new ldn message.
*/
public LDN() {
arguments = new ArrayList<>(20);
template = null;
content = EMPTY;
}
/**
* Set the content of the message. Setting this also "resets" the message
* formatting - <code>addArgument</code> will start over. Comments and any
* "Subject:" line must be stripped.
*
* @param name a name for this message
* @param cnt the content of the message
*/
public void setContent(String name, String cnt) {
content = cnt;
contentName = name;
arguments.clear();
}
/**
* Fill out the next argument in the template
*
* @param arg the value for the next argument
*/
public void addArgument(Object arg) {
arguments.add(arg);
}
/**
* Generates the ldn message.
*
* @throws MessagingException if there was a problem sending the mail.
* @throws IOException if IO error
*/
public String generateLDNMessage() {
ConfigurationService config
= DSpaceServicesFactory.getInstance().getConfigurationService();
VelocityEngine templateEngine = new VelocityEngine();
templateEngine.init(VELOCITY_PROPERTIES);
VelocityContext vctx = new VelocityContext();
vctx.put("config", new LDN.UnmodifiableConfigurationService(config));
vctx.put("params", Collections.unmodifiableList(arguments));
if (null == template) {
if (StringUtils.isBlank(content)) {
LOG.error("template has no content");
throw new RuntimeException("template has no content");
}
// No template, so use a String of content.
StringResourceRepository repo = (StringResourceRepository)
templateEngine.getApplicationAttribute(RESOURCE_REPOSITORY_NAME);
repo.putStringResource(contentName, content);
// Turn content into a template.
template = templateEngine.getTemplate(contentName);
}
StringWriter writer = new StringWriter();
try {
template.merge(vctx, writer);
} catch (MethodInvocationException | ParseErrorException
| ResourceNotFoundException ex) {
LOG.error("Template not merged: {}", ex.getMessage());
throw new RuntimeException("Template not merged", ex);
}
return writer.toString();
}
/**
* Get the VTL template for a ldn message. The message is suitable
* for inserting values using Apache Velocity.
*
* @param ldnMessageFile
* full name for the ldn template, for example "/dspace/config/ldn/request-review".
*
* @return the ldn object, configured with body.
*
* @throws IOException if IO error,
* if the template couldn't be found, or there was some other
* error reading the template
*/
public static LDN getLDNMessage(String ldnMessageFile)
throws IOException {
StringBuilder contentBuffer = new StringBuilder();
try (
InputStream is = new FileInputStream(ldnMessageFile);
InputStreamReader ir = new InputStreamReader(is, "UTF-8");
BufferedReader reader = new BufferedReader(ir);
) {
boolean more = true;
while (more) {
String line = reader.readLine();
if (line == null) {
more = false;
} else {
contentBuffer.append(line);
contentBuffer.append("\n");
}
}
}
LDN ldn = new LDN();
ldn.setContent(ldnMessageFile, contentBuffer.toString());
return ldn;
}
/**
* Wrap ConfigurationService to prevent templates from modifying
* the configuration.
*/
public static class UnmodifiableConfigurationService {
private final ConfigurationService configurationService;
/**
* Swallow an instance of ConfigurationService.
*
* @param cs the real instance, to be wrapped.
*/
public UnmodifiableConfigurationService(ConfigurationService cs) {
configurationService = cs;
}
/**
* Look up a key in the actual ConfigurationService.
*
* @param key to be looked up in the DSpace configuration.
* @return whatever value ConfigurationService associates with {@code key}.
*/
public String get(String key) {
return configurationService.getProperty(key);
}
}
}