advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Review: James: The Java Apache Mail Enterprise Server:

Mailets and Matchers

Download and Installation

One of the neat things about James is its use of mailets and matchers:
  • A mailet is a Java class that does something with a mail message, like adding a footer, forwarding the message, notifying the postmaster about the receipt of the message, or whatever else a mail server may want to do.
  • A matcher is a Java class that says "Yes, apply this mailet's instructions to that message for these recipients," and "No, don't apply this mailet's instructions to that message for those other recipients."

How Matchers and Mailets Work Together

To clarify this Matcher/Mailet business a bit further:
  • Each mailet contains a method named service. A typical service method looks like this:
    
        public void service(Mail mail) throws MessagingException {
            MimeMessage mimeMessage = mail.getMessage();
    
            // Do things with the message
    
            mimeMessage.saveChanges();
        }
    
    The MimeMessage class belongs to the javax.mail.internet package, and the Mail class is part of the org.apache.mailet package.
  • Each matcher contains a method named match. A typical match method looks like this:
    
        public Collection match(Mail mail) {
            if ( /* something or other is true */ ) {
                return mail.getRecipients();
            } else {
                return null;
            }
        }
    
Despite this simplified explanation, an invocation of the match method doesn't return "yes" or "no." Instead, the match method returns a list of recipients for which a mailet should be processed.

Take, for instance, an AddFooter mailet that pastes the words "FOR YOUR EYES ONLY" at the bottom a message body. A particular message may have recipients Alan, Barry, Carol, and Diane. So the skeletal match method above may return a collection containing Alan, Barry, Carol, and Diane. This collection tells James to add "FOR YOUR EYES ONLY" to the copies sent to all four recipients.

On the other hand, the match method above may return null. In that case, James adds "FOR YOUR EYES ONLY" to none of the message's recipients. Of course, if you rewrite the match method, you may return a collection containing only Barry and Carol, or some other bunch from the message's original list of recipients.

How do you associate a matcher with a mailet? The James config.xml file contains mailet tags. Here's a nice mailet tag:


<mailet match="SizeGreaterThan=1k" class="AddFooter">
    <text>Warning: Attachments may contain viruses!</text>
</mailet>
The tag uses a matcher named SizeGreaterThan and a mailet named AddFooter. Both of these classes (the SizeGreaterThan matcher and the AddFooter mailet) are provided as part of the James distribution. When you write a mailet tag, you normally supply parameters to both the mailet and the matcher.
  • A parameter to a mailet is an XML element. For example, in the tag above, the text element tells the AddFooter mailet what text the footer must contain.
  • A parameter to a matcher is buried inside the mailet tag's match attribute. In the tag above, the match attribute specifies the name of the matcher class (SizeGreaterThan) and the 1k (1 kilobyte) parameter value. If a message has size greater than 1 kilobyte, then James adds a Warning: Attachments may contain viruses! footer at the bottom of the message.

Matcher and Mailet Classes

To give James a test run, and as an excuse to write a matcher and a mailet, we dreamt up a fairly frivolous scenario. Here's how it goes:

We're strong advocates of Java technology, and thus, we are dismayed by any mention of .NET. So, when we receive e-mail messages containing the characters ".NET," we want to blot out those characters and replace them with some alternate text. What we really need is a ReplacePlainText mailet. The mailet looks for all occurrences of a certain string, and replaces such occurrences with other (less upsetting) text.

There's only one exception. We have a mutual friend named Manny, who works with both Java and .NET. (Manny's ongoing effort is to port portions of the Java API to the .NET platform.)

Manny has many e-mail addresses, each of the form Manny@some-domain-or-other. Our goal is to allow mail from Manny@anything-at-all (and no other mail) to display the word .NET. So our SenderIsNot matcher class takes an incoming e-mail message.

  • If the sender isn't Manny@some-domain-or-other, then the matcher returns all the message's recipients. That is, the matcher tells James to apply the ReplacePlainText mailet to every recipient's copy of the message.
  • If the sender is Manny@some-domain-or-other, then the matcher returns null. This tells James to apply the ReplacePlainText mailet to none of the recipients' copies.
The matcher and mailet classes are in Listings Listing 1 and 2 respectively. The XML tag to make James use the matcher with the mailet is in Listing 3 .

Both the matcher and the mailet contain init methods. In each init method, the Java program reads parameters from the mailet tag.

  • The matcher's init method calls getCondition, which reads the word Manny from Listing 3 .
  • The mailet's init method calls getInitParameter, which reads the strings \.NET and !^#% from Listing 3 .
Inside the mailet tag, the match attribute's value takes the form

match="matcher-class-name=parameters"
The getCondition method reads the parameters string, which in Listing 3 is the word Manny. When a mail message arrives, James calls the match method in Listing 1 . If the message's sender is Manny, the match method says, "Don't apply the mailet to any of the message's recipients." If the message's sender isn't Manny, the match method says "Apply the mailet to all of the message's recipients."

By the way, some matchers have what you'd normally think of as several parameters. For instance, I may want to allow the word .NET to appear in messages from three people named Manny, Moe, and Jack. In that case I'd stuff all three names into one delimited string.


match="SenderIsNot=Manny,Moe,Jack"
Then I'd write business logic inside the match method to parse the string and use the three names appropriately.

So, assume that Fred@dotnetlover.com sends me an e-mail message containing the word .NET. Then the match method in Listing 1 tells James to apply the mailet's logic to this message. In response, James calls the service method of Listing 2 , and replaces all occurrences of .NET with the string !^#%. (In Listing 3 , the backslash before .NET ensures that the replaceAll method doesn't mistake the dot for a regular expression's "any character" symbol.)

Click here for the full sample code.

A Good Choice

James is an efficient, robust e-mail server with an architecture that's ripe for future expansion. Upcoming releases of James may support instant messaging, new e-mail protocols, and other exciting features. James's plug-in design goes hand-in-hand with Java portability, making James a good choice for an enterprise-level e-mail server.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: It's Not Just a Codename Anymore
Microsoft Shines Silverlight on Eclipse
OpenOffice Hits 3.0: Can It Challenge Microsoft?
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft

Intel Sees Fewer Power Cords in Your Future
F# 101
Use Explicit Conversion Functions to Avert Reckless Implicit Conversions
Polyglot Programming: Building Solutions by Composing Languages
Automated testing for .NET by Ben Hall
"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES