View Javadoc

1   /************************************************************************
2    * Copyright (c) 1999-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.smtpserver;
19  
20  import org.apache.avalon.framework.logger.AbstractLogEnabled;
21  import org.apache.avalon.framework.configuration.Configuration;
22  import org.apache.avalon.framework.configuration.Configurable;
23  import org.apache.avalon.framework.configuration.ConfigurationException;
24  import javax.mail.internet.MimeMessage;
25  
26  
27  /***
28    * Adds the header to the message
29    */
30  public class SetMimeHeaderHandler
31      extends AbstractLogEnabled
32      implements MessageHandler, Configurable {
33  
34      /***
35       * The header name and value that needs to be added
36       */
37      private String headerName;
38      private String headerValue;
39  
40      /***
41       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
42       */
43      public void configure(Configuration handlerConfiguration) throws ConfigurationException {
44  
45          Configuration configuration = handlerConfiguration.getChild("headername", false);
46          if(configuration != null) {
47              setHeaderName(configuration.getValue());
48          }
49  
50          configuration = handlerConfiguration.getChild("headervalue", false);
51          if(configuration != null) {
52              setHeaderValue(configuration.getValue());
53          }
54      }
55      
56      /***
57       * Set the header name
58       * 
59       * @param headerName String which represent the header name
60       */
61      public void setHeaderName(String headerName) {
62          this.headerName = headerName;
63      }
64      
65      /***
66       * Set the header value
67       * 
68       * @param headerValue String wich represetn the header value
69       */
70      public void setHeaderValue(String headerValue) {
71          this.headerValue = headerValue;
72      }
73  
74      /***
75       * Adds header to the message
76       * @see org.apache.james.smtpserver#onMessage(SMTPSession)
77       */
78      public void onMessage(SMTPSession session) {
79          try {
80              MimeMessage message = session.getMail().getMessage ();
81  
82              //Set the header name and value (supplied at init time).
83              if(headerName != null) {
84                  message.setHeader(headerName, headerValue);
85                  message.saveChanges();
86              }
87  
88          } catch (javax.mail.MessagingException me) {
89              getLogger().error(me.getMessage());
90          }
91      }
92  
93  
94  
95  }