From a30c10fa544646c0e97ba79bbace2035e4601e0f Mon Sep 17 00:00:00 2001 From: hjk54 Date: Tue, 2 Mar 2010 21:59:15 +0000 Subject: [PATCH] Changes for file upload email confirmation --- .../webapp/controller/MailUsersServlet.java | 3 - .../controller/edit/N3MultiPartUpload.java | 64 ++++++++++-- .../mannlib/vitro/webapp/dao/UserDao.java | 2 + .../dao/filtering/UserDaoFiltering.java | 4 + .../vitro/webapp/dao/jena/UserDaoJena.java | 4 +- .../mannlib/vitro/webapp/utils/MailUtil.java | 98 +++++++++++++++++++ 6 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java index f8b0f188d..8d8882c03 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/MailUsersServlet.java @@ -134,9 +134,6 @@ public class MailUsersServlet extends VitroHttpServlet { webusername=webusername.trim(); deliveryfrom = webuseremail; comments=comments.trim(); - - - //Removed spam filtering code StringBuffer msgBuf = new StringBuffer(); // contains the intro copy for the body of the email message diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java index c1bf33b15..78271ea88 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/N3MultiPartUpload.java @@ -7,6 +7,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.PrintWriter; import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; @@ -43,6 +44,8 @@ import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.vocabulary.RDF; import com.hp.hpl.jena.vocabulary.RDFS; +import edu.cornell.mannlib.vedit.beans.LoginFormBean; +import edu.cornell.mannlib.vitro.webapp.dao.UserDao; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; @@ -52,6 +55,7 @@ import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditN3Generator; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditN3Utils; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditSubmission; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.Field; +import edu.cornell.mannlib.vitro.webapp.utils.MailUtil; /** * Process a N3 form submission with a multipart data encoding. This follows a @@ -178,10 +182,6 @@ public class N3MultiPartUpload extends VitroHttpServlet { log.debug("File in multipart content request: field " + name + " with file name " + item.getName() + " detected."); - //Debug line - System.out.println("File in multipart content request: field " - + name + " with file name " + item.getName() - + " detected."); } else { List itemList = new ArrayList(); itemList.add(item); @@ -222,9 +222,9 @@ public class N3MultiPartUpload extends VitroHttpServlet { && editConfig.getObject().trim().length() > 0; log.debug(requestIsAnUpdate ? "request is an update for a file object" : "request is for a new file object"); - System.out.println("Request type, update or new: " + requestIsAnUpdate); /** *************************************************** */ - if (requestIsAnUpdate) {System.out.println("Currently existing file reosurce edit not supported"); + String uploadFileName = ""; + if (requestIsAnUpdate) {System.out.println("Currently existing file resource edit not supported"); log.error("Editing an existing file resource is not supported by N3MultiPartUpload.java "); request.setAttribute("errors", "Editing an existing file resource is not supported."); RequestDispatcher rd = request @@ -258,7 +258,10 @@ public class N3MultiPartUpload extends VitroHttpServlet { + fileItemKey + " and file " + fileItem.getName() + "\n" + requiredFieldAssertions.get(fileItemKey)); + } + //Save upload file name for use in email confirmation + uploadFileName = fileItem.getName(); } catch (Exception e) { long t = System.currentTimeMillis(); log.error("uplaod ticket " + t + " " + e.getMessage(), e); @@ -268,7 +271,6 @@ public class N3MultiPartUpload extends VitroHttpServlet { } } } - if ( ! saveFileToDiskSuccess) { if (errors != null && !errors.isEmpty()) { String form = editConfig.getFormUrl(); @@ -322,7 +324,6 @@ public class N3MultiPartUpload extends VitroHttpServlet { } } } - if (postUploadSuccess) { /* **** Save the models for all the files **** */ String editorUri = EditN3Utils.getEditorUri(request, @@ -354,6 +355,14 @@ public class N3MultiPartUpload extends VitroHttpServlet { .getRequestDispatcher("/edit/fileUploadError.jsp"); rd.forward(request, resp); } else { + //This is before the redirect occurs from postEditCleanUp + //Send out email confirmation here + try { + sendUserEmail(session, uploadFileName); + } catch(Exception ex) { + System.out.println("Problem with retrieving and/or sending email"); + } + RequestDispatcher rd = request .getRequestDispatcher("/edit/postEditCleanUp.jsp"); rd.forward(request, resp); @@ -584,6 +593,45 @@ public class N3MultiPartUpload extends VitroHttpServlet { } return out; } + + public void sendUserEmail(HttpSession session, String uploadFileName) { + LoginFormBean loginBean = (LoginFormBean) session.getAttribute("loginHandler"); + String userURI = loginBean.getUserURI(); + try{ + System.out.println("User URI is " + userURI); + UserDao uDao = getWebappDaoFactory().getUserDao(); + String email = uDao.getUserEmailAddress(userURI); + String deliveryFrom = "hjk54@cornell.edu";//TO DO: replace with email address to be used + //Now send message + MailUtil mu = new MailUtil(); + List deliverToArray = new ArrayList(); + deliverToArray.add(email); + + //Compose body of message + StringBuffer msgBuf = new StringBuffer(); + String lineSeparator = System.getProperty("line.separator"); + msgBuf.setLength(0); + msgBuf.append("" + lineSeparator ); + msgBuf.append("" + lineSeparator ); + msgBuf.append("" + lineSeparator ); + msgBuf.append("" + deliveryFrom + "" + lineSeparator ); + msgBuf.append("" + lineSeparator ); + msgBuf.append("" + lineSeparator ); + String messageBody = "

File has been uploaded to datastar"; + //Include file name if it exists and is not empty + if(uploadFileName != null && uploadFileName != "") { + messageBody += ": " + uploadFileName; + } + messageBody += "

"; + msgBuf.append(messageBody + lineSeparator + ""); + String messageText = msgBuf.toString(); + + //Send message + mu.sendMessage(messageText, "Datastar File Upload: Success", deliveryFrom, email, deliverToArray); + } catch(Exception ex) { + System.out.println("Error " + ex.getMessage()); + } + } private boolean logAddRetract(String msg, Map> add, Map> retract) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/UserDao.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/UserDao.java index f43a31831..ba40168c5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/UserDao.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/UserDao.java @@ -23,4 +23,6 @@ public interface UserDao { public List getIndividualsUserMayEditAs(String userURI); public List getUserAccountEmails(); + + public String getUserEmailAddress(String userURI); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/UserDaoFiltering.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/UserDaoFiltering.java index 785ca7d6b..49ddcb37b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/UserDaoFiltering.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/UserDaoFiltering.java @@ -58,5 +58,9 @@ public class UserDaoFiltering extends BaseFiltering implements UserDao{ public List getUserAccountEmails() { return innerDao.getUserAccountEmails(); } + + public String getUserEmailAddress(String userURI) { + return innerDao.getUserEmailAddress(userURI); + } } \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/UserDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/UserDaoJena.java index 22318f239..f331fdab7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/UserDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/UserDaoJena.java @@ -282,13 +282,13 @@ public class UserDaoJena extends JenaBaseDao implements UserDao { } //for a specific user account, get the email address - public String getUserEmailAddress (String userURI) { + public String getUserEmailAddress(String userURI) { OntModel ontModel = getOntModel(); OntModel baseModel = getOntModelSelector().getFullModel(); ontModel.enterCriticalSection(Lock.READ); String swrcOntology = "http://swrc.ontoware.org/ontology#"; String emailProperty = swrcOntology + "email"; - String personUri, emailValue = null; + String personUri, emailValue = ""; try { //Get person account associated with this email address diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java new file mode 100644 index 000000000..6fbd4fba4 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/MailUtil.java @@ -0,0 +1,98 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils; +import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Date; +import java.util.Properties; +import java.util.List; + +import javax.mail.Message; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import javax.servlet.ServletContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; + +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.ResourceFactory; + +import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import fedora.client.FedoraClient; + +public class MailUtil { + private String smtpHost = null; + public MailUtil(){ + smtpHost = getSmtpHostFromProperties(); + } + + public void sendMessage(String messageText, String subject, String from, String to, List deliverToArray) throws IOException{ + Properties props = System.getProperties(); + props.put("mail.smtp.host", smtpHost); + Session s = Session.getDefaultInstance(props,null); + + try{ + + int recipientCount = (deliverToArray == null) ? 0 : deliverToArray.size(); + if (recipientCount == 0) { + //log.error("recipientCount is 0 when DeliveryType specified as \""+formType+"\""); + throw new Error( + "To establish the Contact Us mail capability the system administrators must " + + "specify at least one email address in the current portal."); + } + + MimeMessage msg = new MimeMessage( s ); + // Set the from address + msg.setFrom( new InternetAddress( from )); + + // Set the recipient address + + if (recipientCount>0){ + InternetAddress[] address=new InternetAddress[recipientCount]; + for (int i=0; i 0) ? host : null; + } +}