Changes for file upload email confirmation

This commit is contained in:
hjk54 2010-03-02 21:59:15 +00:00
parent 5b83d1fcf9
commit a30c10fa54
6 changed files with 162 additions and 13 deletions

View file

@ -134,9 +134,6 @@ public class MailUsersServlet extends VitroHttpServlet {
webusername=webusername.trim(); webusername=webusername.trim();
deliveryfrom = webuseremail; deliveryfrom = webuseremail;
comments=comments.trim(); comments=comments.trim();
//Removed spam filtering code //Removed spam filtering code
StringBuffer msgBuf = new StringBuffer(); // contains the intro copy for the body of the email message StringBuffer msgBuf = new StringBuffer(); // contains the intro copy for the body of the email message

View file

@ -7,6 +7,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; 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.RDF;
import com.hp.hpl.jena.vocabulary.RDFS; 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.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; 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.EditN3Utils;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditSubmission; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditSubmission;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.Field; 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 * 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 " log.debug("File in multipart content request: field "
+ name + " with file name " + item.getName() + name + " with file name " + item.getName()
+ " detected."); + " detected.");
//Debug line
System.out.println("File in multipart content request: field "
+ name + " with file name " + item.getName()
+ " detected.");
} else { } else {
List<FileItem> itemList = new ArrayList<FileItem>(); List<FileItem> itemList = new ArrayList<FileItem>();
itemList.add(item); itemList.add(item);
@ -222,9 +222,9 @@ public class N3MultiPartUpload extends VitroHttpServlet {
&& editConfig.getObject().trim().length() > 0; && editConfig.getObject().trim().length() > 0;
log.debug(requestIsAnUpdate ? "request is an update for a file object" log.debug(requestIsAnUpdate ? "request is an update for a file object"
: "request is for a new 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 "); log.error("Editing an existing file resource is not supported by N3MultiPartUpload.java ");
request.setAttribute("errors", "Editing an existing file resource is not supported."); request.setAttribute("errors", "Editing an existing file resource is not supported.");
RequestDispatcher rd = request RequestDispatcher rd = request
@ -258,7 +258,10 @@ public class N3MultiPartUpload extends VitroHttpServlet {
+ fileItemKey + " and file " + fileItemKey + " and file "
+ fileItem.getName() + "\n" + fileItem.getName() + "\n"
+ requiredFieldAssertions.get(fileItemKey)); + requiredFieldAssertions.get(fileItemKey));
} }
//Save upload file name for use in email confirmation
uploadFileName = fileItem.getName();
} catch (Exception e) { } catch (Exception e) {
long t = System.currentTimeMillis(); long t = System.currentTimeMillis();
log.error("uplaod ticket " + t + " " + e.getMessage(), e); log.error("uplaod ticket " + t + " " + e.getMessage(), e);
@ -268,7 +271,6 @@ public class N3MultiPartUpload extends VitroHttpServlet {
} }
} }
} }
if ( ! saveFileToDiskSuccess) { if ( ! saveFileToDiskSuccess) {
if (errors != null && !errors.isEmpty()) { if (errors != null && !errors.isEmpty()) {
String form = editConfig.getFormUrl(); String form = editConfig.getFormUrl();
@ -322,7 +324,6 @@ public class N3MultiPartUpload extends VitroHttpServlet {
} }
} }
} }
if (postUploadSuccess) { if (postUploadSuccess) {
/* **** Save the models for all the files **** */ /* **** Save the models for all the files **** */
String editorUri = EditN3Utils.getEditorUri(request, String editorUri = EditN3Utils.getEditorUri(request,
@ -354,6 +355,14 @@ public class N3MultiPartUpload extends VitroHttpServlet {
.getRequestDispatcher("/edit/fileUploadError.jsp"); .getRequestDispatcher("/edit/fileUploadError.jsp");
rd.forward(request, resp); rd.forward(request, resp);
} else { } 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 RequestDispatcher rd = request
.getRequestDispatcher("/edit/postEditCleanUp.jsp"); .getRequestDispatcher("/edit/postEditCleanUp.jsp");
rd.forward(request, resp); rd.forward(request, resp);
@ -585,6 +594,45 @@ public class N3MultiPartUpload extends VitroHttpServlet {
return out; 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<String> deliverToArray = new ArrayList<String>();
deliverToArray.add(email);
//Compose body of message
StringBuffer msgBuf = new StringBuffer();
String lineSeparator = System.getProperty("line.separator");
msgBuf.setLength(0);
msgBuf.append("<html>" + lineSeparator );
msgBuf.append("<head>" + lineSeparator );
msgBuf.append("<style>a {text-decoration: none}</style>" + lineSeparator );
msgBuf.append("<title>" + deliveryFrom + "</title>" + lineSeparator );
msgBuf.append("</head>" + lineSeparator );
msgBuf.append("<body>" + lineSeparator );
String messageBody = "<h4>File has been uploaded to datastar";
//Include file name if it exists and is not empty
if(uploadFileName != null && uploadFileName != "") {
messageBody += ": " + uploadFileName;
}
messageBody += "</h4>";
msgBuf.append(messageBody + lineSeparator + "</body></html>");
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<String, List<String>> add, private boolean logAddRetract(String msg, Map<String, List<String>> add,
Map<String, List<String>> retract) { Map<String, List<String>> retract) {
log.debug(msg); log.debug(msg);

View file

@ -23,4 +23,6 @@ public interface UserDao {
public List<String> getIndividualsUserMayEditAs(String userURI); public List<String> getIndividualsUserMayEditAs(String userURI);
public List<String> getUserAccountEmails(); public List<String> getUserAccountEmails();
public String getUserEmailAddress(String userURI);
} }

View file

@ -59,4 +59,8 @@ public class UserDaoFiltering extends BaseFiltering implements UserDao{
return innerDao.getUserAccountEmails(); return innerDao.getUserAccountEmails();
} }
public String getUserEmailAddress(String userURI) {
return innerDao.getUserEmailAddress(userURI);
}
} }

View file

@ -282,13 +282,13 @@ public class UserDaoJena extends JenaBaseDao implements UserDao {
} }
//for a specific user account, get the email address //for a specific user account, get the email address
public String getUserEmailAddress (String userURI) { public String getUserEmailAddress(String userURI) {
OntModel ontModel = getOntModel(); OntModel ontModel = getOntModel();
OntModel baseModel = getOntModelSelector().getFullModel(); OntModel baseModel = getOntModelSelector().getFullModel();
ontModel.enterCriticalSection(Lock.READ); ontModel.enterCriticalSection(Lock.READ);
String swrcOntology = "http://swrc.ontoware.org/ontology#"; String swrcOntology = "http://swrc.ontoware.org/ontology#";
String emailProperty = swrcOntology + "email"; String emailProperty = swrcOntology + "email";
String personUri, emailValue = null; String personUri, emailValue = "";
try { try {
//Get person account associated with this email address //Get person account associated with this email address

View file

@ -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<String> 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<recipientCount; i++){
address[i] = new InternetAddress(deliverToArray.get(i));
}
msg.setRecipients( Message.RecipientType.TO, address );
}
// Set the subject and text
msg.setSubject( subject );
// add the multipart to the message
msg.setContent(messageText,"text/html");
// set the Date: header
msg.setSentDate( new Date() );
Transport.send( msg ); // try to send the message via smtp - catch error exceptions
} catch(Exception ex) {
System.out.println("Exception sending message :" + ex.getMessage());
}
}
public boolean isSmtpHostConfigured() {
if( smtpHost==null || smtpHost.equals("")) {
return false;
}
return true;
}
private String getSmtpHostFromProperties() {
String host = ConfigurationProperties.getProperty("Vitro.smtpHost");
if (host != null && !host.equals("")) {
//System.out.println("Found Vitro.smtpHost value is " + host);
//LOG.info("Found Vitro.smtpHost value of " + host);
} else {
System.out.println("No Vitro.smtpHost specified");
}
return (host != null && host.length() > 0) ? host : null;
}
}