Fixed bug 24549: Regular expression errors when sending emails
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Tue, 20 Aug 2013 13:36:57 +0200
changeset 20999 b09efdf35533
parent 20998 8d3f43817c4f
child 21000 ad00c4ddb9a1
Fixed bug 24549: Regular expression errors when sending emails

Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of characters: '$' and '\'
src/org/openbravo/erpCommon/utility/reporting/Report.java
src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java
--- a/src/org/openbravo/erpCommon/utility/reporting/Report.java	Tue Aug 20 09:37:09 2013 +0200
+++ b/src/org/openbravo/erpCommon/utility/reporting/Report.java	Tue Aug 20 13:36:57 2013 +0200
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.util.Date;
 import java.util.UUID;
+import java.util.regex.Matcher;
 
 import javax.servlet.ServletException;
 
@@ -146,17 +147,22 @@
     // Generate the target report filename
     final String dateStamp = Utility.formatDate(new Date(), "yyyyMMdd-HHmmss");
     String reportFilename = templateInfo.getReportFilename();
-    reportFilename = reportFilename.replaceAll("@our_ref@", _OurReference);
-    reportFilename = reportFilename.replaceAll("@cus_ref@", _CusReference);
-    reportFilename = reportFilename.replaceAll("@cus_nam@", _ContactName);
-    reportFilename = reportFilename.replaceAll("@bp_nam@", _BPartnerName);
-    reportFilename = reportFilename.replaceAll("@doc_date@", _DocDate);
-    reportFilename = reportFilename.replaceAll("@doc_nextduedate@", _MinDueDate);
-    reportFilename = reportFilename.replaceAll("@doc_lastduedate@", _MaxDueDate);
-    // reportFilename = reportFilename.replaceAll("@doc_desc@", _DocDescription); // Too long
+    reportFilename = reportFilename
+        .replaceAll("@our_ref@", Matcher.quoteReplacement(_OurReference));
+    reportFilename = reportFilename
+        .replaceAll("@cus_ref@", Matcher.quoteReplacement(_CusReference));
+    reportFilename = reportFilename.replaceAll("@cus_nam@", Matcher.quoteReplacement(_ContactName));
+    reportFilename = reportFilename.replaceAll("@bp_nam@", Matcher.quoteReplacement(_BPartnerName));
+    reportFilename = reportFilename.replaceAll("@doc_date@", Matcher.quoteReplacement(_DocDate));
+    reportFilename = reportFilename.replaceAll("@doc_nextduedate@",
+        Matcher.quoteReplacement(_MinDueDate));
+    reportFilename = reportFilename.replaceAll("@doc_lastduedate@",
+        Matcher.quoteReplacement(_MaxDueDate));
+    // reportFilename = reportFilename.replaceAll("@doc_desc@",
+    // Matcher.quoteReplacement(_DocDescription)); // Too long
     if (checkSalesOrder.equalsIgnoreCase("y")
         && !_DocumentType.toString().equalsIgnoreCase("PAYMENT")) {
-      reportFilename = reportFilename.replaceAll("@sal_nam@", _SalNam);
+      reportFilename = reportFilename.replaceAll("@sal_nam@", Matcher.quoteReplacement(_SalNam));
     } else {
       reportFilename = reportFilename.replaceAll("@sal_nam@", "");
     }
--- a/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Tue Aug 20 09:37:09 2013 +0200
+++ b/src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java	Tue Aug 20 13:36:57 2013 +0200
@@ -29,6 +29,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Vector;
+import java.util.regex.Matcher;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -694,25 +695,33 @@
 
     // Replace special tags
 
-    emailSubject = emailSubject.replaceAll("@cus_ref@", cusReference);
-    emailSubject = emailSubject.replaceAll("@our_ref@", ourReference);
-    emailSubject = emailSubject.replaceAll("@cus_nam@", toName);
-    emailSubject = emailSubject.replaceAll("@sal_nam@", replyToName);
-    emailSubject = emailSubject.replaceAll("@bp_nam@", report.getBPName());
-    emailSubject = emailSubject.replaceAll("@doc_date@", report.getDocDate());
-    emailSubject = emailSubject.replaceAll("@doc_nextduedate@", report.getMinDueDate());
-    emailSubject = emailSubject.replaceAll("@doc_lastduedate@", report.getMaxDueDate());
-    emailSubject = emailSubject.replaceAll("@doc_desc@", report.getDocDescription());
+    emailSubject = emailSubject.replaceAll("@cus_ref@", Matcher.quoteReplacement(cusReference));
+    emailSubject = emailSubject.replaceAll("@our_ref@", Matcher.quoteReplacement(ourReference));
+    emailSubject = emailSubject.replaceAll("@cus_nam@", Matcher.quoteReplacement(toName));
+    emailSubject = emailSubject.replaceAll("@sal_nam@", Matcher.quoteReplacement(replyToName));
+    emailSubject = emailSubject
+        .replaceAll("@bp_nam@", Matcher.quoteReplacement(report.getBPName()));
+    emailSubject = emailSubject.replaceAll("@doc_date@",
+        Matcher.quoteReplacement(report.getDocDate()));
+    emailSubject = emailSubject.replaceAll("@doc_nextduedate@",
+        Matcher.quoteReplacement(report.getMinDueDate()));
+    emailSubject = emailSubject.replaceAll("@doc_lastduedate@",
+        Matcher.quoteReplacement(report.getMaxDueDate()));
+    emailSubject = emailSubject.replaceAll("@doc_desc@",
+        Matcher.quoteReplacement(report.getDocDescription()));
 
-    emailBody = emailBody.replaceAll("@cus_ref@", cusReference);
-    emailBody = emailBody.replaceAll("@our_ref@", ourReference);
-    emailBody = emailBody.replaceAll("@cus_nam@", toName);
-    emailBody = emailBody.replaceAll("@sal_nam@", replyToName);
-    emailBody = emailBody.replaceAll("@bp_nam@", report.getBPName());
-    emailBody = emailBody.replaceAll("@doc_date@", report.getDocDate());
-    emailBody = emailBody.replaceAll("@doc_nextduedate@", report.getMinDueDate());
-    emailBody = emailBody.replaceAll("@doc_lastduedate@", report.getMaxDueDate());
-    emailBody = emailBody.replaceAll("@doc_desc@", report.getDocDescription());
+    emailBody = emailBody.replaceAll("@cus_ref@", Matcher.quoteReplacement(cusReference));
+    emailBody = emailBody.replaceAll("@our_ref@", Matcher.quoteReplacement(ourReference));
+    emailBody = emailBody.replaceAll("@cus_nam@", Matcher.quoteReplacement(toName));
+    emailBody = emailBody.replaceAll("@sal_nam@", Matcher.quoteReplacement(replyToName));
+    emailBody = emailBody.replaceAll("@bp_nam@", Matcher.quoteReplacement(report.getBPName()));
+    emailBody = emailBody.replaceAll("@doc_date@", Matcher.quoteReplacement(report.getDocDate()));
+    emailBody = emailBody.replaceAll("@doc_nextduedate@",
+        Matcher.quoteReplacement(report.getMinDueDate()));
+    emailBody = emailBody.replaceAll("@doc_lastduedate@",
+        Matcher.quoteReplacement(report.getMaxDueDate()));
+    emailBody = emailBody.replaceAll("@doc_desc@",
+        Matcher.quoteReplacement(report.getDocDescription()));
 
     String host = null;
     boolean auth = true;