Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/xpn/xwiki/test/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 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   * General Public License for more details.
15   *
16   * You should have received a copy of the GNU 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.xpn.xwiki.test.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    /** Headers: Map of List of String hashed on header name. */
29    private Map headers;
30    /** Message body. */
31    private StringBuffer body;
32  
33    /**
34     * Constructor. Initializes headers Map and body buffer.
35     */
36    public SmtpMessage() {
37      headers = new HashMap();
38      body = new StringBuffer();
39    }
40  
41    /**
42     * Update the headers or body depending on the SmtpResponse object and line of input.
43     * @param response SmtpResponse object
44     * @param params remainder of input line after SMTP command has been removed
45     */
46    public void store(SmtpResponse response, String params) {
47      if (params != null) {
48        if (SmtpState.DATA_HDR == response.getNextState()) {
49          int headerNameEnd = params.indexOf(':');
50          if (headerNameEnd >= 0) {
51            String name = params.substring(0, headerNameEnd).trim();
52            String value = params.substring(headerNameEnd+1).trim();
53            addHeader(name, value);
54          }
55        } else if (SmtpState.DATA_BODY == response.getNextState()) {
56          body.append(params);
57          body.append("\n");
58        }
59      }
60    }
61  
62    /**
63     * Get an Iterator over the header names.
64     * @return an Iterator over the set of header names (String)
65     */
66    public Iterator getHeaderNames() {
67      return headers.keySet().iterator();
68    }
69  
70    /**
71     * Get the value(s) associated with the given header name.
72     * @param name header name
73     * @return value(s) associated with the header name
74     */
75    public String[] getHeaderValues(String name) {
76      List values = (List)headers.get(name);
77      if (values == null) {
78        return new String[0];
79      } else {
80        return (String[])values.toArray(new String[0]);
81      }
82    }
83  
84    /**
85     * Get the first values associated with a given header name.
86     * @param name header name
87     * @return first value associated with the header name
88     */
89    public String getHeaderValue(String name) {
90      List values = (List)headers.get(name);
91      if (values == null) {
92        return null;
93      } else {
94        return (String)values.iterator().next();
95      }
96    }
97  
98    /**
99     * Get the message body.
100    * @return message body
101    */
102   public String getBody() {
103     return body.toString();
104   }
105 
106   /**
107    * Adds a header to the Map.
108    * @param name header name
109    * @param value header value
110    */
111   private void addHeader(String name, String value) {
112     List valueList = (List)headers.get(name);
113     if (valueList == null) {
114       valueList = new ArrayList();
115       headers.put(name, valueList);
116     }
117     valueList.add(value);
118   }
119 
120   /**
121    * String representation of the SmtpMessage.
122    * @return a String
123    */
124   public String toString() {
125     StringBuffer msg = new StringBuffer();
126     for(Iterator i = headers.keySet().iterator(); i.hasNext();) {
127       String name = (String)i.next();
128       List values = (List)headers.get(name);
129       for(Iterator j = values.iterator(); j.hasNext();) {
130         String value = (String)j.next();
131         msg.append(name);
132         msg.append(": ");
133         msg.append(value);
134         msg.append('\n');
135       }
136     }
137     msg.append('\n');
138     msg.append(body);
139     msg.append('\n');
140     return msg.toString();
141   }
142 }