Source Code Cross Referenced for InternetHeaders.java in  » Library » mime-pull » org » jvnet » mimepull » 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 » Library » mime pull » org.jvnet.mimepull 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common Development
008:         * and Distribution License("CDDL") (collectively, the "License").  You
009:         * may not use this file except in compliance with the License. You can obtain
010:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
012:         * language governing permissions and limitations under the License.
013:         *
014:         * When distributing the software, include this License Header Notice in each
015:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016:         * Sun designates this particular file as subject to the "Classpath" exception
017:         * as provided by Sun in the GPL Version 2 section of the License file that
018:         * accompanied this code.  If applicable, add the following below the License
019:         * Header, with the fields enclosed by brackets [] replaced by your own
020:         * identifying information: "Portions Copyrighted [year]
021:         * [name of copyright owner]"
022:         *
023:         * Contributor(s):
024:         *
025:         * If you wish your version of this file to be governed by only the CDDL or
026:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
027:         * elects to include this software in this distribution under the [CDDL or GPL
028:         * Version 2] license."  If you don't indicate a single choice of license, a
029:         * recipient has the option to distribute your version of this file under
030:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
031:         * its licensees as provided above.  However, if you add GPL Version 2 code
032:         * and therefore, elected the GPL Version 2 license, then the option applies
033:         * only if the new code is made subject to such option by the copyright
034:         * holder.
035:         */
036:        package org.jvnet.mimepull;
037:
038:        import java.io.IOException;
039:        import java.util.NoSuchElementException;
040:        import java.util.List;
041:
042:        /**
043:         * InternetHeaders is a utility class that manages RFC822 style
044:         * headers. Given an RFC822 format message stream, it reads lines
045:         * until the blank line that indicates end of header. The input stream
046:         * is positioned at the start of the body. The lines are stored
047:         * within the object and can be extracted as either Strings or
048:         * {@link Header} objects. <p>
049:         * <p/>
050:         * This class is mostly intended for service providers. MimeMessage
051:         * and MimeBody use this class for holding their headers. <p>
052:         * <p/>
053:         * <hr> <strong>A note on RFC822 and MIME headers</strong><p>
054:         * <p/>
055:         * RFC822 and MIME header fields <strong>must</strong> contain only
056:         * US-ASCII characters. If a header contains non US-ASCII characters,
057:         * it must be encoded as per the rules in RFC 2047. The MimeUtility
058:         * class provided in this package can be used to to achieve this.
059:         * Callers of the <code>setHeader</code>, <code>addHeader</code>, and
060:         * <code>addHeaderLine</code> methods are responsible for enforcing
061:         * the MIME requirements for the specified headers.  In addition, these
062:         * header fields must be folded (wrapped) before being sent if they
063:         * exceed the line length limitation for the transport (1000 bytes for
064:         * SMTP).  Received headers may have been folded.  The application is
065:         * responsible for folding and unfolding headers as appropriate. <p>
066:         *
067:         * @author John Mani
068:         * @author Bill Shannon
069:         */
070:        final class InternetHeaders {
071:
072:            private final FinalArrayList<hdr> headers = new FinalArrayList<hdr>();
073:
074:            /**
075:             * Read and parse the given RFC822 message stream till the
076:             * blank line separating the header from the body. Store the
077:             * header lines inside this InternetHeaders object. <p>
078:             * <p/>
079:             * Note that the header lines are added into this InternetHeaders
080:             * object, so any existing headers in this object will not be
081:             * affected.
082:             *
083:             * @param	lis RFC822 input stream
084:             */
085:            InternetHeaders(MIMEParser.LineInputStream lis) {
086:                // Read header lines until a blank line. It is valid
087:                // to have BodyParts with no header lines.
088:                String line;
089:                String prevline = null; // the previous header line, as a string
090:                // a buffer to accumulate the header in, when we know it's needed
091:                StringBuffer lineBuffer = new StringBuffer();
092:
093:                try {
094:                    //while ((line = lis.readLine()) != null) {
095:                    do {
096:                        line = lis.readLine();
097:                        if (line != null
098:                                && (line.startsWith(" ") || line
099:                                        .startsWith("\t"))) {
100:                            // continuation of header
101:                            if (prevline != null) {
102:                                lineBuffer.append(prevline);
103:                                prevline = null;
104:                            }
105:                            lineBuffer.append("\r\n");
106:                            lineBuffer.append(line);
107:                        } else {
108:                            // new header
109:                            if (prevline != null)
110:                                addHeaderLine(prevline);
111:                            else if (lineBuffer.length() > 0) {
112:                                // store previous header first
113:                                addHeaderLine(lineBuffer.toString());
114:                                lineBuffer.setLength(0);
115:                            }
116:                            prevline = line;
117:                        }
118:                    } while (line != null && line.length() > 0);
119:                } catch (IOException ioex) {
120:                    throw new MIMEParsingException("Error in input stream",
121:                            ioex);
122:                }
123:            }
124:
125:            /**
126:             * Return all the values for the specified header. The
127:             * values are String objects.  Returns <code>null</code>
128:             * if no headers with the specified name exist.
129:             *
130:             * @param	name header name
131:             * @return		array of header values, or null if none
132:             */
133:            List<String> getHeader(String name) {
134:                // XXX - should we just step through in index order?
135:                FinalArrayList<String> v = new FinalArrayList<String>(); // accumulate return values
136:
137:                int len = headers.size();
138:                for (int i = 0; i < len; i++) {
139:                    hdr h = (hdr) headers.get(i);
140:                    if (name.equalsIgnoreCase(h.name)) {
141:                        v.add(h.getValue());
142:                    }
143:                }
144:                return (v.size() == 0) ? null : v;
145:            }
146:
147:            /**
148:             * Return all the headers as an Enumeration of
149:             * {@link Header} objects.
150:             *
151:             * @return	Header objects
152:             */
153:            FinalArrayList<? extends Header> getAllHeaders() {
154:                return headers; // conceptually it should be read-only, but for performance reason I'm not wrapping it here
155:            }
156:
157:            /**
158:             * Add an RFC822 header line to the header store.
159:             * If the line starts with a space or tab (a continuation line),
160:             * add it to the last header line in the list. <p>
161:             * <p/>
162:             * Note that RFC822 headers can only contain US-ASCII characters
163:             *
164:             * @param	line	raw RFC822 header line
165:             */
166:            void addHeaderLine(String line) {
167:                try {
168:                    char c = line.charAt(0);
169:                    if (c == ' ' || c == '\t') {
170:                        hdr h = (hdr) headers.get(headers.size() - 1);
171:                        h.line += "\r\n" + line;
172:                    } else
173:                        headers.add(new hdr(line));
174:                } catch (StringIndexOutOfBoundsException e) {
175:                    // line is empty, ignore it
176:                    return;
177:                } catch (NoSuchElementException e) {
178:                    // XXX - vector is empty?
179:                }
180:            }
181:
182:        }
183:
184:        /*
185:         * A private utility class to represent an individual header.
186:         */
187:
188:        class hdr implements  Header {
189:
190:            String name; // the canonicalized (trimmed) name of this header
191:            // XXX - should name be stored in lower case?
192:            String line; // the entire RFC822 header "line"
193:
194:            /*
195:             * Constructor that takes a line and splits out
196:             * the header name.
197:             */
198:            hdr(String l) {
199:                int i = l.indexOf(':');
200:                if (i < 0) {
201:                    // should never happen
202:                    name = l.trim();
203:                } else {
204:                    name = l.substring(0, i).trim();
205:                }
206:                line = l;
207:            }
208:
209:            /*
210:             * Constructor that takes a header name and value.
211:             */
212:            hdr(String n, String v) {
213:                name = n;
214:                line = n + ": " + v;
215:            }
216:
217:            /*
218:             * Return the "name" part of the header line.
219:             */
220:            public String getName() {
221:                return name;
222:            }
223:
224:            /*
225:             * Return the "value" part of the header line.
226:             */
227:            public String getValue() {
228:                int i = line.indexOf(':');
229:                if (i < 0)
230:                    return line;
231:
232:                int j;
233:                if (name.equalsIgnoreCase("Content-Description")) {
234:                    // Content-Description should retain the folded whitespace after header unfolding -
235:                    // rf. RFC2822 section 2.2.3, rf. RFC2822 section 3.2.3
236:                    for (j = i + 1; j < line.length(); j++) {
237:                        char c = line.charAt(j);
238:                        if (!(/*c == ' ' ||*/c == '\t' || c == '\r' || c == '\n'))
239:                            break;
240:                    }
241:                } else {
242:                    // skip whitespace after ':'
243:                    for (j = i + 1; j < line.length(); j++) {
244:                        char c = line.charAt(j);
245:                        if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n'))
246:                            break;
247:                    }
248:                }
249:                return line.substring(j);
250:            }
251:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.