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  
21  
22  package org.apache.james.smtpserver.core;
23  
24  import org.apache.avalon.framework.logger.AbstractLogEnabled;
25  import org.apache.avalon.framework.configuration.Configuration;
26  import org.apache.avalon.framework.configuration.Configurable;
27  import org.apache.avalon.framework.configuration.ConfigurationException;
28  import org.apache.james.smtpserver.MessageHandler;
29  import org.apache.james.smtpserver.SMTPSession;
30  
31  import javax.mail.internet.MimeMessage;
32  
33  
34  /**
35    * Adds the header to the message
36    */
37  public class SetMimeHeaderHandler
38      extends AbstractLogEnabled
39      implements MessageHandler, Configurable {
40  
41      /**
42       * The header name and value that needs to be added
43       */
44      private String headerName;
45      private String headerValue;
46  
47      /**
48       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
49       */
50      public void configure(Configuration handlerConfiguration) throws ConfigurationException {
51  
52          Configuration configuration = handlerConfiguration.getChild("headername", false);
53          if(configuration != null) {
54              setHeaderName(configuration.getValue());
55          }
56  
57          configuration = handlerConfiguration.getChild("headervalue", false);
58          if(configuration != null) {
59              setHeaderValue(configuration.getValue());
60          }
61      }
62      
63      /**
64       * Set the header name
65       * 
66       * @param headerName String which represent the header name
67       */
68      public void setHeaderName(String headerName) {
69          this.headerName = headerName;
70      }
71      
72      /**
73       * Set the header value
74       * 
75       * @param headerValue String wich represetn the header value
76       */
77      public void setHeaderValue(String headerValue) {
78          this.headerValue = headerValue;
79      }
80  
81      /**
82       * Adds header to the message
83       * @see org.apache.james.smtpserver#onMessage(SMTPSession)
84       */
85      public void onMessage(SMTPSession session) {
86          try {
87              MimeMessage message = session.getMail().getMessage ();
88  
89              //Set the header name and value (supplied at init time).
90              if(headerName != null) {
91                  message.setHeader(headerName, headerValue);
92                  message.saveChanges();
93              }
94  
95          } catch (javax.mail.MessagingException me) {
96              getLogger().error(me.getMessage());
97          }
98      }
99  
100 
101 
102 }