1   /*
2    * Dumbster: a dummy SMTP server.
3    * Copyright (C) 2003, Jason Paul Kitchen
4    * lilnottsman@yahoo.com
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   */
20  package com.dumbster.smtp;
21  
22  import java.util.*;
23  
24  /***
25   * Container for a complete SMTP message - headers and message body.
26   */
27  public class SmtpMessage {
28  
29  	/*** Headers: Map of List of String hashed on header name. */
30  	private Map headers;
31  
32  	/*** Message body. */
33  	private StringBuffer body;
34  
35  	/***
36  	 * Constructor. Initializes headers Map and body buffer.
37  	 */
38  	public SmtpMessage() {
39  		headers = new HashMap();
40  		body = new StringBuffer();
41  	}
42  
43  	/***
44  	 * Update the headers or body depending on the SmtpResponse object and line of input.
45  	 * @param response SmtpResponse object
46  	 * @param params remainder of input line after SMTP command has been removed
47  	 */
48  	public void store(SmtpResponse response, String params) {
49  		if (params != null) {
50  			if (SmtpState.DATA_HDR == response.getNextState()) {
51  				int headerNameEnd = params.indexOf(':');
52  				if (headerNameEnd >= 0) {
53  					String name = params.substring(0, headerNameEnd).trim();
54  					String value = params.substring(headerNameEnd + 1).trim();
55  					addHeader(name, value);
56  				}
57  			} else if (SmtpState.DATA_BODY == response.getNextState()) {
58  				body.append(params);
59  			}
60  		}
61  	}
62  
63  	/***
64  	 * Get an Iterator over the header names.
65  	 * @return an Iterator over the set of header names (String)
66  	 */
67  	public Iterator getHeaderNames() {
68  		return headers.keySet().iterator();
69  	}
70  
71  	/***
72  	 * Get the value(s) associated with the given header name.
73  	 * @param name header name
74  	 * @return value(s) associated with the header name
75  	 */
76  	public String[] getHeaderValues(String name) {
77  		List values = (List)headers.get(name);
78  		if (values == null) {
79  			return new String[0];
80  		} else {
81  			return (String[])values.toArray(new String[0]);
82  		}
83  	}
84  
85  	/***
86  	 * Get the first values associated with a given header name.
87  	 * @param name header name
88  	 * @return first value associated with the header name
89  	 */
90  	public String getHeaderValue(String name) {
91  		List values = (List)headers.get(name);
92  		if (values == null) {
93  			return null;
94  		} else {
95  			return (String)values.iterator().next();
96  		}
97  	}
98  
99  	/***
100 	 * Get the message body.
101 	 * @return message body
102 	 */
103 	public String getBody() {
104 		return body.toString();
105 	}
106 
107 	/***
108 	 * Adds a header to the Map.
109 	 * @param name header name
110 	 * @param value header value
111 	 */
112 	private void addHeader(String name, String value) {
113 		List valueList = (List)headers.get(name);
114 		if (valueList == null) {
115 			valueList = new ArrayList();
116 			headers.put(name, valueList);
117 		}
118 		valueList.add(value);
119 	}
120 
121 	/***
122 	 * String representation of the SmtpMessage.
123 	 * @return a String
124 	 */
125 	public String toString() {
126 		StringBuffer msg = new StringBuffer();
127 		for (Iterator i = headers.keySet().iterator(); i.hasNext();) {
128 			String name = (String)i.next();
129 			List values = (List)headers.get(name);
130 			for (Iterator j = values.iterator(); j.hasNext();) {
131 				String value = (String)j.next();
132 				msg.append(name);
133 				msg.append(": ");
134 				msg.append(value);
135 				msg.append('\n');
136 			}
137 		}
138 		msg.append('\n');
139 		msg.append(body);
140 		msg.append('\n');
141 		return msg.toString();
142 	}
143 }