From cb4c556ae70704f9178a3604d2949b2006e4ba0e Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Mon, 22 Jun 2015 13:56:57 +0000 Subject: [PATCH] Initial import. git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/invites-common-library@115462 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 26 +++ .project | 23 +++ .settings/org.eclipse.core.resources.prefs | 4 + .settings/org.eclipse.jdt.core.prefs | 5 + .settings/org.eclipse.m2e.core.prefs | 4 + distro/INSTALL | 1 + distro/LICENSE | 8 + distro/MAINTAINERS | 2 + distro/README | 7 + distro/changelog.xml | 6 + distro/descriptor.xml | 48 +++++ distro/profile.xml | 25 +++ distro/svnpath.txt | 1 + pom.xml | 171 +++++++++++++++++ .../gcube/portal/invites/InvitesManager.java | 177 ++++++++++++++++++ .../org/gcube/portal/invites/AppTest.java | 38 ++++ 16 files changed, 546 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 distro/INSTALL create mode 100644 distro/LICENSE create mode 100644 distro/MAINTAINERS create mode 100644 distro/README create mode 100644 distro/changelog.xml create mode 100644 distro/descriptor.xml create mode 100644 distro/profile.xml create mode 100644 distro/svnpath.txt create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/portal/invites/InvitesManager.java create mode 100644 src/test/java/org/gcube/portal/invites/AppTest.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4214a97 --- /dev/null +++ b/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..6a6d8c2 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + invites-common-library + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ec4300d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/distro/INSTALL b/distro/INSTALL new file mode 100644 index 0000000..4af1832 --- /dev/null +++ b/distro/INSTALL @@ -0,0 +1 @@ +None \ No newline at end of file diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..bd0c2b4 --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1,8 @@ +gCube System - License +------------------------------------------------------------ + +The gCube/gCore software is licensed as Free Open Source software conveying to +the EUPL (http://ec.europa.eu/idabc/eupl). +The software and documentation is provided by its authors/distributors "as is" +and no expressed or implied warranty is given for its use, quality or fitness +for a particular case. diff --git a/distro/MAINTAINERS b/distro/MAINTAINERS new file mode 100644 index 0000000..d0b4c39 --- /dev/null +++ b/distro/MAINTAINERS @@ -0,0 +1,2 @@ +* Massimiliano Assante (massimiliano.assante@isti.cnr.it), CNR Pisa, + Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..8487208 --- /dev/null +++ b/distro/README @@ -0,0 +1,7 @@ +The gCube System +------------------------------------------------------------ + +This work is partially funded by the European Commission in the +context of the iMarine project (www.i-marine.eu), under FP7 Programme. + + diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..717a786 --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,6 @@ + + + First Release + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..c468f13 --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,48 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + INSTALL + MAINTAINERS + changelog.xml + + 755 + true + + + target/apidocs + /${artifactId}/doc/api + true + 755 + + + + + ${distroDirectory}/profile.xml + ./ + true + + + target/${build.finalName}.jar + /${artifactId} + + + ${distroDirectory}/svnpath.txt + /${artifactId} + true + + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..3a8d912 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,25 @@ + + + + Library + + ${description} + Portal + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + diff --git a/distro/svnpath.txt b/distro/svnpath.txt new file mode 100644 index 0000000..edacb04 --- /dev/null +++ b/distro/svnpath.txt @@ -0,0 +1 @@ +${scm.url} \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1680c67 --- /dev/null +++ b/pom.xml @@ -0,0 +1,171 @@ + + + + maven-parent + org.gcube.tools + 1.0.0 + + + + 4.0.0 + + org.gcube.portal + invites-common-library + 0.1.0-SNAPSHOT + jar + + gCube Invites Common Library is a common library containing shared code for Inviting external users to portal VREs. + + invites-common-library + + scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId} + scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId} + http://svn.d4science.research-infrastructures.eu/gcube/trunk/portal/${project.artifactId} + + + distro + 1.7 + 1.7 + UTF-8 + UTF-8 + + + + + org.gcube.distribution + maven-portal-bom + LATEST + pom + import + + + + + + + org.jsoup + jsoup + 1.6.2 + + + org.gcube.applicationsupportlayer + aslcore + provided + + + com.sun.mail + javax.mail + provided + + + com.liferay.portal + portal-service + provided + + + junit + junit + 4.8.1 + test + + + javax.portlet + portlet-api + provided + + + org.slf4j + slf4j-log4j12 + + + org.slf4j + slf4j-api + + + + + + + maven-compiler-plugin + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.2 + + + + test-jar + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12 + + true + + + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + + copy-profile + install + + copy-resources + + + target + + + ${distroDirectory} + true + + profile.xml + + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.2 + + + ${distroDirectory}/descriptor.xml + + + + fully.qualified.MainClass + + + + + + servicearchive + install + + single + + + + + + + diff --git a/src/main/java/org/gcube/portal/invites/InvitesManager.java b/src/main/java/org/gcube/portal/invites/InvitesManager.java new file mode 100644 index 0000000..daeed01 --- /dev/null +++ b/src/main/java/org/gcube/portal/invites/InvitesManager.java @@ -0,0 +1,177 @@ +package org.gcube.portal.invites; + +import java.util.Properties; + +import javax.mail.Message; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.gcube.application.framework.core.session.ASLSession; +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; + +/** + * + * @author Massimiliano Assante + * + */ +public class InvitesManager { + private static final Logger _log = LoggerFactory.getLogger(InvitesManager.class); + private final static String MAIL_SERVICE_HOST = "localhost"; + private final static String MAIL_SERVICE_PORT = "25"; + + private static InvitesManager instance; + + private InvitesManager() { } + + public static InvitesManager getInstance(){ + instance = new InvitesManager(); + return instance; + } + + + public Boolean sendInviteEmail( + ASLSession aslSession, + String portalSenderEmail, + String portalURL, + String name, + String lastName, + String email, + String vreDescription) { + + String vreName = aslSession.getGroupName(); + String fromFullName = aslSession.getUserFullName(); + + + Properties props = System.getProperties(); + Session session = null; + props.put("mail.smtp.host", MAIL_SERVICE_HOST); + props.put("mail.smtp.port", MAIL_SERVICE_PORT); + session = Session.getDefaultInstance(props); + session.setDebug(true); + + try { + MimeMessage message = new MimeMessage(session); + message.setHeader("Content-Type", "text/plain; charset=UTF-8"); + // Set From: header field of the header. + message.setFrom(new InternetAddress(portalSenderEmail, fromFullName)); + message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); + message.addRecipient(Message.RecipientType.CC, new InternetAddress(aslSession.getUserEmailAddress())); + + // Set Subject: header field + message.setSubject("Join me on " + vreName); + + // Now set the actual message + message.setText(getTextEmail(aslSession, portalURL, name, lastName, email, fromFullName, vreName, vreDescription)); + + // Send message + Transport.send(message); + _log.debug("Sent message successfully to " + email ); + } catch (Exception mex) { + mex.printStackTrace(); + _log.error("Sent message ERROR to " + email ); + return false; + } + return true; + } + + /** + * + * @param aslSession + * @param portalUrl the url of the portal + * @param name the name of the invited person + * @param lastName the last name of the invited person + * @param email the email address of the invite person + * @param fromFullName who is inviting + * @param vreName the name of the environment where you are inviting the person + * @param vreDescription the description of the environment where you are inviting the person + * @return the email text + */ + private String getTextEmail( + ASLSession aslSession, + String portalUrl, + String name, + String lastName, + String email, + String fromFullName, + String vreName, + String vreDescription) { + + long organizationId = aslSession.getGroupId(); + + StringBuilder body = new StringBuilder(); + + body.append("Dear " + name) + .append(",\n") + .append(fromFullName).append(" has invited you to " + vreName + ", you can find a brief description below:") + .append("\n") + .append("\n").append(convertHTML2Text(vreDescription)) + .append("\n\n") + .append("To accept the invite just follow this link: " + portalUrl + "/group/data-e-infrastructure-gateway/join-new?orgid="+organizationId) + .append("\n\n") + .append("Please note: if you do not have an account yet, sign up first: " + portalUrl + "/web/guest/home?p_p_id=58&_58_struts_action=%2Flogin%2Fcreate_account") + .append("\n\n\n\n") + .append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain") + .append("information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. " + + "If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, " + + "you are strictly prohibited from disclosing, distributing, copying, or in any way using this message.") + .append("If you have received this communication in error, please notify the and destroy and delete any copies you may have received."); + + return body.toString(); + + } + + /** + * Convert html into simple text + * + */ + protected static String convertHTML2Text(String html) { + if (html == null) { + return null; + } + String removedMarkup = html.replaceAll("&", "&"); + removedMarkup = removedMarkup.replaceAll(">", ">"); + removedMarkup = removedMarkup.replaceAll("<", "<"); + 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) || "div".equals(tagName) || "h1".equals(tagName) || "h2".equals(tagName) || "h3".equals(tagName) || "h4".equals(tagName)) { + buffer.append("\n"); + } + } + + return buffer; + } +} diff --git a/src/test/java/org/gcube/portal/invites/AppTest.java b/src/test/java/org/gcube/portal/invites/AppTest.java new file mode 100644 index 0000000..d826c31 --- /dev/null +++ b/src/test/java/org/gcube/portal/invites/AppTest.java @@ -0,0 +1,38 @@ +package org.gcube.portal.invites; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}