Source Code Cross Referenced for SmtpMessage.java in  » Testing » Dumbster » com » dumbster » smtp » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Dumbster » com.dumbster.smtp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.