2 * Dumbster: a dummy SMTP server.
3 * Copyright (C) 2003, Jason Paul Kitchen
4 * lilnottsman@yahoo.com
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.
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.
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
20 package com.dumbster.smtp;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
29 * Container for a complete SMTP message - headers and message body.
31 public class SmtpMessage {
33 /** Headers: Map of List of String hashed on header name. */
37 private StringBuffer body;
40 * Constructor. Initializes headers Map and body buffer.
42 public SmtpMessage() {
43 headers = new HashMap();
44 body = new StringBuffer();
48 * Update the headers or body depending on the SmtpResponse object and line of input.
49 * @param response SmtpResponse object
50 * @param params remainder of input line after SMTP command has been removed
52 public void store(SmtpResponse response, String params) {
54 if (SmtpState.DATA_HDR == response.getNextState()) {
55 int headerNameEnd = params.indexOf(':');
56 if (headerNameEnd >= 0) {
57 String name = params.substring(0, headerNameEnd).trim();
58 String value = params.substring(headerNameEnd + 1).trim();
59 addHeader(name, value);
61 } else if (SmtpState.DATA_BODY == response.getNextState()) {
68 * Get an Iterator over the header names.
69 * @return an Iterator over the set of header names (String)
71 public Iterator getHeaderNames() {
72 return headers.keySet().iterator();
76 * Get the value(s) associated with the given header name.
77 * @param name header name
78 * @return value(s) associated with the header name
80 public String[] getHeaderValues(String name) {
81 List values = (List)headers.get(name);
85 return (String[])values.toArray(new String[0]);
90 * Get the first values associated with a given header name.
91 * @param name header name
92 * @return first value associated with the header name
94 public String getHeaderValue(String name) {
95 List values = (List)headers.get(name);
99 return (String)values.iterator().next();
104 * Get the message body.
105 * @return message body
107 public String getBody() {
108 return body.toString();
112 * Adds a header to the Map.
113 * @param name header name
114 * @param value header value
116 private void addHeader(String name, String value) {
117 List valueList = (List)headers.get(name);
118 if (valueList == null) {
119 valueList = new ArrayList();
120 headers.put(name, valueList);
122 valueList.add(value);
126 * String representation of the SmtpMessage.
129 public String toString() {
130 StringBuffer msg = new StringBuffer();
131 for (Iterator i = headers.keySet().iterator(); i.hasNext();) {
132 String name = (String)i.next();
133 List values = (List)headers.get(name);
134 for (Iterator j = values.iterator(); j.hasNext();) {
135 String value = (String)j.next();
145 return msg.toString();