Updated email format to actually send text plain emails without garbling html

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@99744 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-09-11 13:25:27 +00:00
parent 6e36acc3c5
commit 46e07fafeb
4 changed files with 94 additions and 13 deletions

View File

@ -48,4 +48,7 @@
<Changeset component="org.gcube.applicationsupportlayer.aslsocial.0-11-0" date="2014-07-01">
<Change>Implemented direct email sending through an external SMTP Server (if available in the infrastructure)</Change>
</Changeset>
<Changeset component="org.gcube.applicationsupportlayer.aslsocial.0-12-0" date="2014-09-12">
<Change>Updated email format to actually send text plain emails without garbling html</Change>
</Changeset>
</ReleaseNotes>

19
pom.xml
View File

@ -10,7 +10,7 @@
<groupId>org.gcube.applicationsupportlayer</groupId>
<artifactId>aslsocial</artifactId>
<version>0.11.0-SNAPSHOT</version>
<version>0.12.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Social Portal ASL Extension</name>
<description>
@ -44,7 +44,7 @@
<dependencies>
<dependency>
<groupId>org.gcube.common.portal</groupId>
<artifactId>portal-manager</artifactId>
<artifactId>portal-manager</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
@ -59,7 +59,7 @@
</dependency>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>social-networking-library</artifactId>
<artifactId>social-networking-library</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.dvos</groupId>
@ -83,19 +83,24 @@
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<artifactId>common-encryption</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-core</artifactId>
<scope>provided</scope>
<artifactId>storage-manager-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-wrapper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>
@ -114,7 +119,7 @@
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<artifactId>commons-net</artifactId>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -16,6 +16,11 @@ import javax.mail.internet.MimeMultipart;
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
import org.gcube.portal.databook.shared.Notification;
import org.gcube.portal.databook.shared.NotificationType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -90,9 +95,8 @@ public class EmailPlugin {
private static String getTextEmail(Notification notification2Save, String userFirstName, String portalURL, String email) {
String removedMarkup = notification2Save.getDescription().replaceAll("&amp;", "&");
removedMarkup = removedMarkup.replaceAll("&gt;", ">");
removedMarkup = removedMarkup.replaceAll("&lt;", "<");
String removedMarkup = convertHTML2Text(notification2Save.getDescription());
String sender = notification2Save.getSenderFullName();
if (notification2Save.getType() == NotificationType.DOCUMENT_WORKFLOW_STEP_REQUEST_TASK)
@ -117,6 +121,55 @@ public class EmailPlugin {
return body.toString();
}
/**
* Convert html into simple text
*
*/
protected static String convertHTML2Text(String html) {
if (html == null) {
return null;
}
String removedMarkup = html.replaceAll("&amp;", "&");
removedMarkup = removedMarkup.replaceAll("&gt;", ">");
removedMarkup = removedMarkup.replaceAll("&lt;", "<");
String text = removedMarkup;
try {
Document document = Jsoup.parse(removedMarkup);
Element body = document.body();
text = buildStringFromNode(body).toString();
}
catch (Exception e) {
_log.error("While converting HTML into text: " +e.getMessage());
return removedMarkup;
}
return text;
}
private static StringBuffer buildStringFromNode(Node node) {
StringBuffer buffer = new StringBuffer();
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
buffer.append(textNode.text().trim());
}
for (Node childNode : node.childNodes()) {
buffer.append(buildStringFromNode(childNode));
}
if (node instanceof Element) {
Element element = (Element) node;
String tagName = element.tagName();
if ("p".equals(tagName) || "br".equals(tagName)) {
buffer.append("\n");
}
}
return buffer;
}
private static String getActionLink(Notification notification2Save, String portalURL) {
StringBuilder actionLink = new StringBuilder("<a style=\"color:#3B5998; text-decoration:none\" target=\"_blank\" href=\"");

View File

@ -1,5 +1,11 @@
package org.gcube.applicationsupportlayer.social;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.applicationsupportlayer.social.mailing.EmailPlugin;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portal.databook.shared.Notification;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@ -28,11 +34,25 @@ public class AppTest
return new TestSuite( AppTest.class );
}
/**
* the current ASLSession
* @return the session
*/
private ASLSession getTestSession() {
ASLSession toReturn = SessionManager.getInstance().getASLSession("11", "test.user");
toReturn.setScope("/gcube");
toReturn.setUserFullName("Pico Pallino");
toReturn.setUserEmailAddress("test@test123.com");
toReturn.setGroupModelInfos("TheGroup", 123L);
return toReturn;
}
/**
* Rigourous Test :-)
*
*/
public void testApp()
{
public void testApp() {
assertTrue( true );
}
}