import java.util.Locale;
import java.util.Map;
import java.util.Vector;
+import java.util.Collections;
import mir.config.MirPropertiesConfiguration;
import mir.generator.Generator;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
-import org.apache.struts.util.MessageResources;
/**
}
catch (Throwable t) {
logger.fatal("Cannot load configuration: " + t.toString());
-
+
throw new RuntimeException("Cannot load configuration: " + t.toString());
}
}
-
+
public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
if (aSession.getAttribute("initialRequest") == null) {
aSession.setAttribute("initialRequest", "no");
subsequentRequest(aRequest, aSession, aResponse);
}
}
-
+
protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
initializeSession(aRequest, aSession);
makeInitialResponse(aRequest, aSession, aResponse);
}
-
+
protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
/* do nothing for now */
-
+
}
-
+
protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
/* if you do not supply an aid to this handler, it should return an error page */
/* if you supply a non-functioning/non-published aid to this handler, it should return an error page, but at a
else {
aSession.setAttribute("email.aid",articleID);
aResponse.setResponseValue("errors", null);
-
- String mail_language = configuration.getString("Mir.Login.DefaultLanguage", "en");
+
+ String mail_language = configuration.getString("Mir.Login.DefaultLanguage", "en");
aResponse.setResponseValue("mail_language",mail_language);
aResponse.setResponseValue("mail_to","");
aResponse.setResponseValue("mail_from","");
aResponse.setResponseValue("mail_from_name","");
aResponse.setResponseValue("mail_comment","");
-
+
aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.email.PrepareTemplate"));
-
+
}
}
-
+
protected boolean shouldSendMail(Request aRequest, Session aSession, Response aResponse,List aValidationErrors) throws SessionExc, SessionFailure{
if (validate(aRequest,aSession,aResponse,aValidationErrors)){
String to=aRequest.getParameter("mail_to");
|| to.indexOf('\n') != -1
|| to.indexOf('\r') != -1
|| to.indexOf(',') != -1) {
- throw new SessionExc("Invalid to address"); // we might want to see this in a log, so it is not a validation error
+ throw new SessionExc("Invalid to address"); // we might want to see this in a log, so it is not a validation error
}
else return true; // go for it
}
else{
return false; //validation failed, but not in a potentially abusive way
}
-
+
}
-
-
+
+
protected boolean validate(Request aRequest, Session aSession, Response aResponse,List aValidationErrors) throws SessionExc, SessionFailure{
-
+
if (ValidationHelper.testFieldEntered(aRequest, "mail_to", "validationerror.missing", aValidationErrors))
aResponse.setResponseValue("mail_to",aRequest.getParameter("mail_to"));
if (ValidationHelper.testFieldEntered(aRequest, "mail_from", "validationerror.missing", aValidationErrors))
}
else {
try {
- ModuleContent contentModule = new ModuleContent(DatabaseContent.getInstance());
+ ModuleContent contentModule = new ModuleContent();
EntityContent contentEnt = (EntityContent) contentModule.getById(aid);
-
+
Map articleData = new HashMap();
articleData.put("article", MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("content", contentEnt));
articleData.put("languagecode", language);
Map responseData = GeneratorHelper.makeBasicGenerationData(new Locale[] {new Locale(language,""),new Locale(configuration.getString("Mir.Admin.FallbackLanguage", "en"), "")},"bundles.open","bundles.open");
responseData.put("data",articleData);
-
+
String emailAnArticleTemplate = configuration.getString("Localizer.OpenSession.email.MailTemplate");
-
+
Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(emailAnArticleTemplate);
-
+
StringWriter theEmailStringWriter = new StringWriter();
PrintWriter theEmailPrintWriter = new PrintWriter(theEmailStringWriter);
generator.generate(theEmailPrintWriter, responseData, logger);
-
+
theEmailStringWriter.close();
theText = theEmailStringWriter.toString();
}
protected String getExtraEmailHeaders(Request aRequest,String to,String from) throws SessionExc {
-
+
String headers = "To: " + to + "\nReply-To: "+ from+"\n";;
if (configuration.getString("Localizer.OpenSession.email.includeSenderIP","no").equals("yes"))
headers= headers+"X-Originating-IP: "+ ((HTTPRequestAdapter)aRequest).getRequest().getRemoteAddr() + "\n";
-
+
return headers;
}
protected String interpolateComment(String emailText,String comment,String from_name,String language) throws SessionExc{
- if (comment != null) {
- MessageResources messages = MessageResources.getMessageResources("bundles.open");
- String commentTextToInsert=messages.getMessage(new Locale(language,""),"email.comment.intro", from_name)+"\n";
-
- try {
- emailText=StringRoutines.performRegularExpressionReplacement(emailText,"!COMMENT!",commentTextToInsert);
- }
- catch (Throwable e){
- throw new SessionExc("Problem doing regular expression replacement :" + e.toString());
- }
+ if (comment != null) {
+ String commentTextToInsert =
+ MirGlobal.getBundleFactory().getBundle("etc/bundles/open", new String[] { language }).
+ getValue("email.comment.intro" , Collections.singletonList(from_name)) + "\n";
+
+ try {
+ emailText = StringRoutines.performRegularExpressionReplacement(emailText, "!COMMENT!", commentTextToInsert);
}
- else{
- try {
- emailText=StringRoutines.performRegularExpressionReplacement(emailText,"!COMMENT!","");
- }
- catch (Throwable e){
- throw new SessionExc("Problem doing regular expression replacement " + e.toString());
- }
+ catch (Throwable e) {
+ throw new SessionExc("Problem doing regular expression replacement :" + e.toString());
+ }
+ }
+ else {
+ try {
+ emailText = StringRoutines.performRegularExpressionReplacement(emailText, "!COMMENT!", "");
+ }
+ catch (Throwable e) {
+ throw new SessionExc("Problem doing regular expression replacement " + e.toString());
}
- return emailText;
+ }
+ return emailText;
}
-
+
protected boolean doTheSMTP(String aMessage,String aTo,String aFrom) throws SessionExc{
SMTPClient client=new SMTPClient();
try {
int reply;
client.connect(configuration.getString("ServletModule.OpenIndy.SMTPServer"));
reply = client.getReplyCode();
-
+
if (!SMTPReply.isPositiveCompletion(reply)) {
client.disconnect();
throw new SessionExc("SMTP server refused connection.");
}
}
throw new SessionExc(e.getMessage());
- }
+ }
}
-
+
protected boolean sendMail(Request aRequest,Session aSession,Response aResponse) throws SessionExc,SessionFailure {
- String aid=aRequest.getParameter("mail_aid");
String to=aRequest.getParameter("mail_to");
String from=aRequest.getParameter("mail_from");
String from_name=aRequest.getParameter("mail_from_name");
String language=aRequest.getParameter("mail_language");
String comment=aRequest.getParameter("mail_comment");
-
+
String theEmailText=getEmailText((String) aSession.getAttribute("email.aid"),language);
String headers=getExtraEmailHeaders(aRequest,to,from);
theEmailText=interpolateComment(theEmailText,comment,from_name,language);
- String message=headers+"\n"+theEmailText;
+ String message=headers+theEmailText; // the space between headers and content is in the template
return doTheSMTP(message,to,from);
-
+
}
protected void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
List validationErrors = new Vector();
if (shouldSendMail(aRequest,aSession,aResponse,validationErrors)){
-
+
sendMail(aRequest,aSession,aResponse);
aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.email.DoneTemplate"));
}
else {
- aResponse.setResponseValue("mail_comment",aRequest.getParameter("mail_comment")); //everything else is required
+ aResponse.setResponseValue("mail_comment",aRequest.getParameter("mail_comment")); //everything else is required
aResponse.setResponseValue("errors",validationErrors);
aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.email.PrepareTemplate"));
}
-
+
}
-
+
/*
String mail_language = aRequest.getParameter("mail_language");
if (mail_language == null)
- mail_language = configuration.getString("Mir.Login.DefaultLanguage", "en");
-
+ mail_language = configuration.getString("Mir.Login.DefaultLanguage", "en");
+
aResponse.setResponseValue("mail_language",mail_language);
*/