View Javadoc

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