(10);
+ arguments.clear();
+ recipients.clear();
+ attachments.clear();
+ moreAttachments.clear();
replyTo = null;
charset = null;
}
/**
- * Sends the email.
+ * Sends the email. If the template defines a Velocity context property
+ * named among the values of DSpace configuration property
+ * {@code mail.message.headers} then that name and its value will be added
+ * to the message's headers.
+ *
+ * "subject" is treated specially: if {@link setSubject()} has not been called,
+ * the value of any "subject" property will be used as if setSubject had
+ * been called with that value. Thus a template may define its subject, but
+ * the caller may override it.
*
* @throws MessagingException if there was a problem sending the mail.
* @throws IOException if IO error
*/
public void send() throws MessagingException, IOException {
- ConfigurationService config = DSpaceServicesFactory.getInstance().getConfigurationService();
+ ConfigurationService config
+ = DSpaceServicesFactory.getInstance().getConfigurationService();
// Get the mail configuration properties
String from = config.getProperty("mail.from.address");
@@ -279,20 +312,55 @@ public class Email {
i.next()));
}
- // Format the mail message
- Object[] args = arguments.toArray();
- String fullMessage = MessageFormat.format(content, args);
- Date date = new Date();
+ // Format the mail message body
+ VelocityEngine templateEngine = new VelocityEngine();
+ templateEngine.init(VELOCITY_PROPERTIES);
+ VelocityContext vctx = new VelocityContext();
+ vctx.put("config", new UnmodifiableConfigurationService(config));
+ vctx.put("params", Collections.unmodifiableList(arguments));
+
+ if (null == template) {
+ if (StringUtils.isBlank(content)) {
+ // No template and no content -- PANIC!!!
+ throw new MessagingException("Email has no body");
+ }
+ // 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();
+ template.merge(vctx, writer);
+ String fullMessage = writer.toString();
+
+ // Set some message header fields
+ Date date = new Date();
message.setSentDate(date);
message.setFrom(new InternetAddress(from));
- // Set the subject of the email (may contain parameters)
- String fullSubject = MessageFormat.format(subject, args);
+ // Get headers defined by the template.
+ for (String headerName : config.getArrayProperty("mail.message.headers")) {
+ String headerValue = (String) vctx.get(headerName);
+ if ("subject".equalsIgnoreCase(headerName)) {
+ if (null != subject) {
+ subject = headerValue;
+ }
+ } else if ("charset".equalsIgnoreCase(headerName)) {
+ charset = headerValue;
+ } else {
+ message.setHeader(headerName, headerValue);
+ }
+ }
+
+ // Set the subject of the email.
if (charset != null) {
- message.setSubject(fullSubject, charset);
+ message.setSubject(subject, charset);
} else {
- message.setSubject(fullSubject);
+ message.setSubject(subject);
}
// Add attachments
@@ -305,39 +373,33 @@ public class Email {
}
} else {
Multipart multipart = new MimeMultipart();
+
// create the first part of the email
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(fullMessage);
multipart.addBodyPart(messageBodyPart);
- if (!attachments.isEmpty()) {
- for (Iterator iter = attachments.iterator(); iter.hasNext(); ) {
- FileAttachment f = iter.next();
- // add the file
- messageBodyPart = new MimeBodyPart();
- messageBodyPart.setDataHandler(new DataHandler(
- new FileDataSource(f.file)));
- messageBodyPart.setFileName(f.name);
- multipart.addBodyPart(messageBodyPart);
- }
- message.setContent(multipart);
+
+ // Add file attachments
+ for (FileAttachment attachment : attachments) {
+ // add the file
+ messageBodyPart = new MimeBodyPart();
+ messageBodyPart.setDataHandler(new DataHandler(
+ new FileDataSource(attachment.file)));
+ messageBodyPart.setFileName(attachment.name);
+ multipart.addBodyPart(messageBodyPart);
}
- if (!moreAttachments.isEmpty()) {
- for (Iterator iter = moreAttachments.iterator(); iter.hasNext(); ) {
- InputStreamAttachment isa = iter.next();
- // add the stream
- messageBodyPart = new MimeBodyPart();
- messageBodyPart.setDataHandler(
- new DataHandler(new InputStreamDataSource(
- isa.name,
- isa.mimetype,
- isa.is)
- )
- );
- messageBodyPart.setFileName(isa.name);
- multipart.addBodyPart(messageBodyPart);
- }
- message.setContent(multipart);
+
+ // Add stream attachments
+ for (InputStreamAttachment attachment : moreAttachments) {
+ // add the stream
+ messageBodyPart = new MimeBodyPart();
+ messageBodyPart.setDataHandler(new DataHandler(
+ new InputStreamDataSource(attachment.name,attachment.mimetype,attachment.is)));
+ messageBodyPart.setFileName(attachment.name);
+ multipart.addBodyPart(messageBodyPart);
}
+
+ message.setContent(multipart);
}
if (replyTo != null) {
@@ -347,7 +409,7 @@ public class Email {
}
if (disabled) {
- StringBuffer text = new StringBuffer(
+ StringBuilder text = new StringBuilder(
"Message not sent due to mail.server.disabled:\n");
Enumeration headers = message.getAllHeaderLines();
@@ -365,19 +427,21 @@ public class Email {
text.append('\n').append(fullMessage);
- log.info(text);
+ LOG.info(text.toString());
} else {
Transport.send(message);
}
}
/**
- * Get the template for an email message. The message is suitable for
- * inserting values using java.text.MessageFormat
.
+ * Get the VTL template for an email message. The message is suitable
+ * for inserting values using Apache Velocity.
+ *
+ * @param emailFile
+ * full name for the email template, for example "/dspace/config/emails/register".
+ *
+ * @return the email object, configured with subject and body.
*
- * @param emailFile full name for the email template, for example "/dspace/config/emails/register".
- * @return the email object, with the content and subject filled out from
- * the template
* @throws IOException if IO error
* if the template couldn't be found, or there was some other
* error reading the template
@@ -385,55 +449,25 @@ public class Email {
public static Email getEmail(String emailFile)
throws IOException {
String charset = null;
- String subject = "";
StringBuilder contentBuffer = new StringBuilder();
- InputStream is = null;
- InputStreamReader ir = null;
- BufferedReader reader = null;
- try {
- is = new FileInputStream(emailFile);
- ir = new InputStreamReader(is, "UTF-8");
- reader = new BufferedReader(ir);
+ try (
+ InputStream is = new FileInputStream(emailFile);
+ 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 if (line.toLowerCase().startsWith("subject:")) {
- subject = line.substring(8).trim();
- } else if (line.toLowerCase().startsWith("charset:")) {
- charset = line.substring(8).trim();
- } else if (!line.startsWith("#")) {
+ } else {
contentBuffer.append(line);
contentBuffer.append("\n");
}
}
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException ioe) {
- // ignore
- }
- }
- if (ir != null) {
- try {
- ir.close();
- } catch (IOException ioe) {
- // ignore
- }
- }
- if (is != null) {
- try {
- is.close();
- } catch (IOException ioe) {
- // ignore
- }
- }
}
Email email = new Email();
- email.setSubject(subject);
- email.setContent(contentBuffer.toString());
+ email.setContent(emailFile, contentBuffer.toString());
if (charset != null) {
email.setCharset(charset);
}
@@ -452,24 +486,36 @@ public class Email {
/**
* Test method to send an email to check email server settings
*
- * @param args the command line arguments given
+ * @param args command line arguments. The first is the path to an email
+ * template file; the rest are the positional arguments for the
+ * template. If there are no arguments, a short, plain test
+ * message is sent.
*/
public static void main(String[] args) {
- ConfigurationService config = DSpaceServicesFactory.getInstance().getConfigurationService();
+ ConfigurationService config
+ = DSpaceServicesFactory.getInstance().getConfigurationService();
String to = config.getProperty("mail.admin");
String subject = "DSpace test email";
String server = config.getProperty("mail.server");
String url = config.getProperty("dspace.url");
- Email e = new Email();
- e.setSubject(subject);
- e.addRecipient(to);
- e.content = "This is a test email sent from DSpace: " + url;
- System.out.println("\nAbout to send test email:");
- System.out.println(" - To: " + to);
- System.out.println(" - Subject: " + subject);
- System.out.println(" - Server: " + server);
- boolean disabled = config.getBooleanProperty("mail.server.disabled", false);
+ Email message;
try {
+ if (args.length <= 0) {
+ message = new Email();
+ message.setContent("testing", "This is a test email sent from DSpace: " + url);
+ } else {
+ message = Email.getEmail(args[0]);
+ for (int i = 1; i < args.length; i++) {
+ message.addArgument(args[i]);
+ }
+ }
+ message.setSubject(subject);
+ message.addRecipient(to);
+ System.out.println("\nAbout to send test email:");
+ System.out.println(" - To: " + to);
+ System.out.println(" - Subject: " + subject);
+ System.out.println(" - Server: " + server);
+ boolean disabled = config.getBooleanProperty("mail.server.disabled", false);
if (disabled) {
System.err.println("\nError sending email:");
System.err.println(" - Error: cannot test email because mail.server.disabled is set to true");
@@ -478,16 +524,10 @@ public class Email {
System.exit(1);
return;
}
- e.send();
- } catch (MessagingException me) {
+ message.send();
+ } catch (MessagingException | IOException ex) {
System.err.println("\nError sending email:");
- System.err.println(" - Error: " + me);
- System.err.println("\nPlease see the DSpace documentation for assistance.\n");
- System.err.println("\n");
- System.exit(1);
- } catch (IOException e1) {
- System.err.println("\nError sending email:");
- System.err.println(" - Error: " + e1);
+ System.err.format(" - Error: %s%n", ex);
System.err.println("\nPlease see the DSpace documentation for assistance.\n");
System.err.println("\n");
System.exit(1);
@@ -532,9 +572,9 @@ public class Email {
* @author arnaldo
*/
public class InputStreamDataSource implements DataSource {
- private String name;
- private String contentType;
- private ByteArrayOutputStream baos;
+ private final String name;
+ private final String contentType;
+ private final ByteArrayOutputStream baos;
InputStreamDataSource(String name, String contentType, InputStream inputStream) throws IOException {
this.name = name;
@@ -568,4 +608,30 @@ public class Email {
}
}
+ /**
+ * Wrap ConfigurationService to prevent templates from modifying
+ * the configuration.
+ */
+ public 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);
+ }
+ }
}
diff --git a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java
index 9e8d04faf2..feff8fbda7 100644
--- a/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java
+++ b/dspace-api/src/main/java/org/dspace/handle/HandlePlugin.java
@@ -14,13 +14,14 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import net.cnri.util.StreamTable;
import net.handle.hdllib.Encoder;
import net.handle.hdllib.HandleException;
import net.handle.hdllib.HandleStorage;
import net.handle.hdllib.HandleValue;
import net.handle.hdllib.ScanCallback;
import net.handle.hdllib.Util;
-import net.handle.util.StreamTable;
+
import org.apache.logging.log4j.Logger;
import org.dspace.core.Context;
import org.dspace.handle.factory.HandleServiceFactory;
diff --git a/dspace-rest/pom.xml b/dspace-rest/pom.xml
index 4e5f898032..3c06a15027 100644
--- a/dspace-rest/pom.xml
+++ b/dspace-rest/pom.xml
@@ -4,8 +4,9 @@
dspace-rest
war
7.0-SNAPSHOT
- DSpace REST :: API and Implementation
- DSpace RESTful Web Services API
+ DSpace (Deprecated) REST Webapp
+ DSpace RESTful Web Services API. NOTE: this REST API is DEPRECATED.
+ Please consider using the REST API in the dspace-server-webapp instead!
http://demo.dspace.org
diff --git a/dspace-spring-rest/README.md b/dspace-server-webapp/README.md
similarity index 87%
rename from dspace-spring-rest/README.md
rename to dspace-server-webapp/README.md
index 2def91d522..e71039308a 100644
--- a/dspace-spring-rest/README.md
+++ b/dspace-server-webapp/README.md
@@ -1,5 +1,5 @@
-# DSpace7 REST Webapp
-> This is the new REST webapp for DSpace 7 build with Spring MVC + HATEOAS with a focus on the [JSON HAL format](http://stateless.co/hal_specification.html) ([formal specification](https://tools.ietf.org/html/draft-kelly-json-hal-08))
+# DSpace Server Webapp
+> This is the new server webapp for DSpace 7 built with Spring Boot, MVC + HATEOAS with a focus on the [JSON HAL format](http://stateless.co/hal_specification.html) ([formal specification](https://tools.ietf.org/html/draft-kelly-json-hal-08))
This webapp uses the following technologies:
- [Spring Boot](https://projects.spring.io/spring-boot/)
@@ -15,8 +15,8 @@ Check the infomation available on the DSpace Official Wiki page for the [DSpace
[DSpace 7 REST: Coding DSpace Objects](https://wiki.duraspace.org/display/DSPACE/DSpace+7+REST%3A+Coding+DSpace+Objects)
## How to run
-The only tested way right now is to run this webapp inside your IDE (Eclipse). Just create a new Tomcat 8 server and deploy the dspace-spring-rest maven module to it.
-> The *dspace.dir* is configured in the *dspace-spring-rest/src/main/resources/application.properties* file
+The only tested way right now is to run this webapp inside your IDE (Eclipse). Just create a new Tomcat 8 server and deploy the dspace-server-webapp maven module to it.
+> The *dspace.dir* is configured in the *dspace-server-webapp/src/main/resources/application.properties* file
[currently](src/main/resources/application.properties#L25)
> dspace.dir = d:/install/dspace7
diff --git a/dspace-spring-rest/pom.xml b/dspace-server-webapp/pom.xml
similarity index 99%
rename from dspace-spring-rest/pom.xml
rename to dspace-server-webapp/pom.xml
index 6b6fbb7911..0c872184ff 100644
--- a/dspace-spring-rest/pom.xml
+++ b/dspace-server-webapp/pom.xml
@@ -1,11 +1,11 @@
4.0.0
org.dspace
- dspace-spring-rest
+ dspace-server-webapp
war
- DSpace Spring Rest (Boot MVC + HATEOAS)
+ DSpace Server Webapp
- DSpace new Rest API
+ DSpace Server Webapp (Spring Boot)
-
- dspace-rdf
-
-
- rdf/pom.xml
-
-
-
- rdf
-
-
dspace-rest
@@ -62,47 +51,14 @@
- dspace-sword
+ dspace-server-webapp`
- sword/pom.xml
+ server/pom.xml
- sword
-
-
-
- dspace-swordv2
-
-
- swordv2/pom.xml
-
-
-
- swordv2
-
-
-
- dspace-oai
-
-
- oai/pom.xml
-
-
-
- oai
-
-
-
- dspace-spring-rest
-
-
- spring-rest/pom.xml
-
-
-
- spring-rest
+ server
diff --git a/dspace/modules/rest/pom.xml b/dspace/modules/rest/pom.xml
index 4b9c4a2a24..e6962b5da3 100644
--- a/dspace/modules/rest/pom.xml
+++ b/dspace/modules/rest/pom.xml
@@ -3,10 +3,11 @@
org.dspace.modules
rest
war
- DSpace REST :: Local Customizations
+ DSpace (Deprecated) REST Webapp :: Local Customizations
This project allows you to overlay your own local REST customizations
- on top of the default REST API provided with DSpace.
+ on top of the deprecated REST API provided with DSpace. NOTE: This REST API is DEPRECATED. Please consider
+ using the REST API in dspace-server-webapp instead.
diff --git a/dspace/modules/spring-rest/pom.xml b/dspace/modules/server/pom.xml
similarity index 94%
rename from dspace/modules/spring-rest/pom.xml
rename to dspace/modules/server/pom.xml
index b8d834a3f8..710867affd 100644
--- a/dspace/modules/spring-rest/pom.xml
+++ b/dspace/modules/server/pom.xml
@@ -1,13 +1,13 @@
4.0.0
org.dspace.modules
- spring-rest
+ server
war
- DSpace Spring Rest:: Local Customizations
- Overlay REST customizations.
-This is probably a temporary solution to the build problems. We like to investigate about
-the possibility to remove the overlays enable a more flexible extension mechanism.
-The use of web-fragment and spring mvc technology allow us to add request handlers
+ DSpace Server Webapp:: Local Customizations
+ Overlay customizations.
+This is probably a temporary solution to the build problems. We like to investigate about
+the possibility to remove the overlays enable a more flexible extension mechanism.
+The use of web-fragment and spring mvc technology allow us to add request handlers
just adding new jar in the classloader
@@ -101,7 +101,7 @@ just adding new jar in the classloader
org.dspace
- dspace-spring-rest
+ dspace-server-webapp
war
diff --git a/dspace/modules/spring-rest/src/main/webapp/.gitignore b/dspace/modules/server/src/main/webapp/.gitignore
similarity index 100%
rename from dspace/modules/spring-rest/src/main/webapp/.gitignore
rename to dspace/modules/server/src/main/webapp/.gitignore
diff --git a/dspace/pom.xml b/dspace/pom.xml
index bf46f313bf..c03dab19db 100644
--- a/dspace/pom.xml
+++ b/dspace/pom.xml
@@ -218,7 +218,7 @@
org.dspace
- dspace-spring-rest
+ dspace-server-webapp
war
compile
@@ -267,7 +267,7 @@
${project.parent.basedir}/dspace-rdf/src/main/java
${project.parent.basedir}/dspace-rest/src/main/java
${project.parent.basedir}/dspace-services/src/main/java
- ${project.parent.basedir}/dspace-spring-rest/src/main/java
+ ${project.parent.basedir}/dspace-server-webapp/src/main/java
${project.parent.basedir}/dspace-sword/src/main/java
${project.parent.basedir}/dspace-swordv2/src/main/java
diff --git a/pom.xml b/pom.xml
index 86d53c29f0..b8a8115aa6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -822,14 +822,14 @@
- dspace-spring-rest
+ dspace-server-webapp
- dspace-spring-rest/pom.xml
+ dspace-server-webapp/pom.xml
- dspace-spring-rest
+ dspace-server-webapp
@@ -854,7 +854,7 @@
dspace-services
dspace-sword
dspace-swordv2
- dspace-spring-rest
+ dspace-server-webapp
@@ -980,7 +980,7 @@
org.dspace
- dspace-spring-rest
+ dspace-server-webapp
7.0-SNAPSHOT
war
@@ -1104,7 +1104,18 @@
org.dspace
handle
- 6.2
+ 9.1.0.v20190416
+
+
+ org.eclipse.jetty.aggregate
+ jetty-all
+ 8.1.22.v20160922
+
+
+ javax.servlet
+ org.eclipse.jetty.orbit
+
+
org.dspace