OSDN Git Service

pom.xml の小修正
[spring-ext/ozacc-mail.git] / src / test / java / com / dumbster / smtp / SmtpMessage.java
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.ArrayList;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 /**
29  * Container for a complete SMTP message - headers and message body.
30  */
31 public class SmtpMessage {
32
33         /** Headers: Map of List of String hashed on header name. */
34         private Map headers;
35
36         /** Message body. */
37         private StringBuffer body;
38
39         /**
40          * Constructor. Initializes headers Map and body buffer.
41          */
42         public SmtpMessage() {
43                 headers = new HashMap();
44                 body = new StringBuffer();
45         }
46
47         /**
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
51          */
52         public void store(SmtpResponse response, String params) {
53                 if (params != null) {
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);
60                                 }
61                         } else if (SmtpState.DATA_BODY == response.getNextState()) {
62                                 body.append(params);
63                         }
64                 }
65         }
66
67         /**
68          * Get an Iterator over the header names.
69          * @return an Iterator over the set of header names (String)
70          */
71         public Iterator getHeaderNames() {
72                 return headers.keySet().iterator();
73         }
74
75         /**
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
79          */
80         public String[] getHeaderValues(String name) {
81                 List values = (List)headers.get(name);
82                 if (values == null) {
83                         return new String[0];
84                 } else {
85                         return (String[])values.toArray(new String[0]);
86                 }
87         }
88
89         /**
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
93          */
94         public String getHeaderValue(String name) {
95                 List values = (List)headers.get(name);
96                 if (values == null) {
97                         return null;
98                 } else {
99                         return (String)values.iterator().next();
100                 }
101         }
102
103         /**
104          * Get the message body.
105          * @return message body
106          */
107         public String getBody() {
108                 return body.toString();
109         }
110
111         /**
112          * Adds a header to the Map.
113          * @param name header name
114          * @param value header value
115          */
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);
121                 }
122                 valueList.add(value);
123         }
124
125         /**
126          * String representation of the SmtpMessage.
127          * @return a String
128          */
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();
136                                 msg.append(name);
137                                 msg.append(": ");
138                                 msg.append(value);
139                                 msg.append('\n');
140                         }
141                 }
142                 msg.append('\n');
143                 msg.append(body);
144                 msg.append('\n');
145                 return msg.toString();
146         }
147 }