You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2004/04/16 22:59:14 UTC

cvs commit: james-server/src/java/org/apache/james/transport/mailets CommandListservProcessor.java

noel        2004/04/16 13:59:14

  Modified:    src/java/org/apache/james/transport/mailets Tag:
                        branch_2_1_fcs CommandListservProcessor.java
  Log:
  Fix JAMES-239.  Replaced the normalizeSubject code with the code from GenericListserv.  Also changed to use the AbstractRedirect.changeSubject() utility method, which handles character encodings.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.1.2.6   +65 -41    james-server/src/java/org/apache/james/transport/mailets/Attic/CommandListservProcessor.java
  
  Index: CommandListservProcessor.java
  ===================================================================
  RCS file: /home/cvs/james-server/src/java/org/apache/james/transport/mailets/Attic/CommandListservProcessor.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- CommandListservProcessor.java	15 Mar 2004 03:54:19 -0000	1.1.2.5
  +++ CommandListservProcessor.java	16 Apr 2004 20:59:14 -0000	1.1.2.6
  @@ -27,7 +27,6 @@
   import org.apache.mailet.Mail;
   import org.apache.mailet.MailAddress;
   import org.apache.mailet.MailetException;
  -import org.apache.oro.text.regex.*;
   
   import javax.mail.MessagingException;
   import javax.mail.internet.MimeMessage;
  @@ -139,11 +138,6 @@
       protected String listName;
   
       /**
  -     * For matching
  -     */
  -    protected Pattern pattern;
  -
  -    /**
        * The list serv manager
        */
       protected ICommandListservManager commandListservManager;
  @@ -174,8 +168,6 @@
               initializeResources();
               //init user repos
               initUsersRepository();
  -            //init regexp
  -            initRegExp();
           } catch (Exception e) {
               throw new MessagingException(e.getMessage(), e);
           }
  @@ -252,7 +244,7 @@
                   subj = "";
               }
               subj = normalizeSubject(subj, prefix);
  -            message.setSubject(subj);
  +            AbstractRedirect.changeSubject(message, subj);
           }
       }
   
  @@ -413,44 +405,76 @@
       }
   
       /**
  -     * init the regexp
  -     */
  -    protected void initRegExp() throws Exception {
  -        StringBuffer regExp = new StringBuffer();
  -        if (autoBracket) {
  -            regExp.append("\\[");
  -        }
  -        if (subjectPrefix != null) {
  -            regExp.append(subjectPrefix);
  -        }
  -        if (autoBracket) {
  -            regExp.append("\\]");
  -        }
  -        if (subjectPrefix != null) {
  -            regExp.append("|");
  +     * <p>This takes the subject string and reduces (normailzes) it.
  +     * Multiple "Re:" entries are reduced to one, and capitalized.  The
  +     * prefix is always moved/placed at the beginning of the line, and
  +     * extra blanks are reduced, so that the output is always of the
  +     * form:</p>
  +     * <code>
  +     * &lt;prefix&gt; + &lt;one-optional-"Re:"*gt; + &lt;remaining subject&gt;
  +     * </code>
  +     * <p>I have done extensive testing of this routine with a standalone
  +     * driver, and am leaving the commented out debug messages so that
  +     * when someone decides to enhance this method, it can be yanked it
  +     * from this file, embedded it with a test driver, and the comments
  +     * enabled.</p>
  +     */
  +    static private String normalizeSubject(final String subj, final String prefix) {
  +        // JDK IMPLEMENTATION NOTE!  When we require JDK 1.4+, all
  +        // occurrences of subject.toString.().indexOf(...) can be
  +        // replaced by subject.indexOf(...).
  +
  +        StringBuffer subject = new StringBuffer(subj);
  +        int prefixLength = prefix.length();
  +
  +        // System.err.println("In:  " + subject);
  +
  +        // If the "prefix" is not at the beginning the subject line, remove it
  +        int index = subject.toString().indexOf(prefix);
  +        if (index != 0) {
  +            // System.err.println("(p) index: " + index + ", subject: " + subject);
  +            if (index > 0) {
  +                subject.delete(index, index + prefixLength);
  +            }
  +            subject.insert(0, prefix); // insert prefix at the front
           }
  -        regExp.append("re:");
   
  -        pattern = new Perl5Compiler().compile(regExp.toString(), Perl5Compiler.CASE_INSENSITIVE_MASK);
  -    }
  +        // Replace Re: with RE:
  +        String match = "Re:";
  +        index = subject.toString().indexOf(match, prefixLength);
   
  -    protected String normalizeSubject(String subject, String prefix) {
  -        if (subject == null) {
  -            subject = "";
  +        while(index > -1) {
  +            // System.err.println("(a) index: " + index + ", subject: " + subject);
  +            subject.replace(index, index + match.length(), "RE:");
  +            index = subject.toString().indexOf(match, prefixLength);
  +            // System.err.println("(b) index: " + index + ", subject: " + subject);
           }
   
  -        boolean hasMatch = (subject.indexOf(subjectPrefix) != -1);
  -        String result = Util.substitute(new Perl5Matcher(),
  -                pattern,
  -                new StringSubstitution(""),
  -                subject,
  -                Util.SUBSTITUTE_ALL).trim();
  +        // Reduce them to one at the beginning
  +        match ="RE:";
  +        int indexRE = subject.toString().indexOf(match, prefixLength) + match.length();
  +        index = subject.toString().indexOf(match, indexRE);
  +        while(index > 0) {
  +            // System.err.println("(c) index: " + index + ", subject: " + subject);
  +            subject.delete(index, index + match.length());
  +            index = subject.toString().indexOf(match, indexRE);
  +            // System.err.println("(d) index: " + index + ", subject: " + subject);
  +        }
   
  -        if (hasMatch) {
  -            return prefix + " RE: " + result;
  -        } else {
  -            return prefix + " " + result;
  +        // Reduce blanks
  +        match = "  ";
  +        index = subject.toString().indexOf(match, prefixLength);
  +        while(index > -1) {
  +            // System.err.println("(e) index: " + index + ", subject: " + subject);
  +            subject.replace(index, index + match.length(), " ");
  +            index = subject.toString().indexOf(match, prefixLength);
  +            // System.err.println("(f) index: " + index + ", subject: " + subject);
           }
  +
  +
  +        // System.err.println("Out: " + subject);
  +
  +        return subject.toString();
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org