001: /*
002: * Dumbster - a dummy SMTP server
003: * Copyright 2004 Jason Paul Kitchen
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package com.dumbster.smtp;
018:
019: import java.util.Map;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.ArrayList;
024: import java.util.Set;
025:
026: /**
027: * Container for a complete SMTP message - headers and message body.
028: */
029: public class SmtpMessage {
030: /** Headers: Map of List of String hashed on header name. */
031: private Map headers;
032: /** Message body. */
033: private StringBuffer body;
034:
035: /**
036: * Constructor. Initializes headers Map and body buffer.
037: */
038: public SmtpMessage() {
039: headers = new HashMap(10);
040: body = new StringBuffer();
041: }
042:
043: /**
044: * Update the headers or body depending on the SmtpResponse object and line of input.
045: * @param response SmtpResponse object
046: * @param params remainder of input line after SMTP command has been removed
047: */
048: public void store(SmtpResponse response, String params) {
049: if (params != null) {
050: if (SmtpState.DATA_HDR.equals(response.getNextState())) {
051: int headerNameEnd = params.indexOf(':');
052: if (headerNameEnd >= 0) {
053: String name = params.substring(0, headerNameEnd)
054: .trim();
055: String value = params.substring(headerNameEnd + 1)
056: .trim();
057: addHeader(name, value);
058: }
059: } else if (SmtpState.DATA_BODY == response.getNextState()) {
060: body.append(params);
061: }
062: }
063: }
064:
065: /**
066: * Get an Iterator over the header names.
067: * @return an Iterator over the set of header names (String)
068: */
069: public Iterator getHeaderNames() {
070: Set nameSet = headers.keySet();
071: return nameSet.iterator();
072: }
073:
074: /**
075: * Get the value(s) associated with the given header name.
076: * @param name header name
077: * @return value(s) associated with the header name
078: */
079: public String[] getHeaderValues(String name) {
080: List values = (List) headers.get(name);
081: if (values == null) {
082: return new String[0];
083: } else {
084: return (String[]) values.toArray(new String[0]);
085: }
086: }
087:
088: /**
089: * Get the first values associated with a given header name.
090: * @param name header name
091: * @return first value associated with the header name
092: */
093: public String getHeaderValue(String name) {
094: List values = (List) headers.get(name);
095: if (values == null) {
096: return null;
097: } else {
098: Iterator iterator = values.iterator();
099: return (String) 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(1);
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: }
|