01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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: * Adds the header to the message
28: */
29: public class SetMimeHeaderHandler extends AbstractLogEnabled implements
30: MessageHandler, Configurable {
31:
32: /**
33: * The header name and value that needs to be added
34: */
35: private String headerName;
36: private String headerValue;
37:
38: /**
39: * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
40: */
41: public void configure(Configuration handlerConfiguration)
42: throws ConfigurationException {
43:
44: Configuration configuration = handlerConfiguration.getChild(
45: "headername", false);
46: if (configuration != null) {
47: setHeaderName(configuration.getValue());
48: }
49:
50: configuration = handlerConfiguration.getChild("headervalue",
51: false);
52: if (configuration != null) {
53: setHeaderValue(configuration.getValue());
54: }
55: }
56:
57: /**
58: * Set the header name
59: *
60: * @param headerName String which represent the header name
61: */
62: public void setHeaderName(String headerName) {
63: this .headerName = headerName;
64: }
65:
66: /**
67: * Set the header value
68: *
69: * @param headerValue String wich represetn the header value
70: */
71: public void setHeaderValue(String headerValue) {
72: this .headerValue = headerValue;
73: }
74:
75: /**
76: * Adds header to the message
77: * @see org.apache.james.smtpserver#onMessage(SMTPSession)
78: */
79: public void onMessage(SMTPSession session) {
80: try {
81: MimeMessage message = session.getMail().getMessage();
82:
83: //Set the header name and value (supplied at init time).
84: if (headerName != null) {
85: message.setHeader(headerName, headerValue);
86: message.saveChanges();
87: }
88:
89: } catch (javax.mail.MessagingException me) {
90: getLogger().error(me.getMessage());
91: }
92: }
93:
94: }
|