KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > web > webmail > WebmailServlet


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.web.webmail;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.IntMap;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.Vfs;
36 import com.caucho.vfs.WriteStream;
37
38 import javax.servlet.GenericServlet JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.ServletRequest JavaDoc;
41 import javax.servlet.ServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44
45 public class WebmailServlet extends GenericServlet JavaDoc {
46   private boolean parseMail(ReadStream is, Path dst)
47     throws IOException JavaDoc
48   {
49     CharBuffer line = CharBuffer.allocate();
50     String JavaDoc topId = null;
51     int count = 1;
52     WriteStream ws = null;
53     IntMap messages = new IntMap();
54
55     try {
56       while (true) {
57         do {
58           line.clear();
59           if (! is.readLine(line)) {
60             if (ws != null)
61               ws.println("</message>");
62             return false;
63           }
64           if (ws != null && ! line.startsWith("From ")) {
65             for (int i = 0; i < line.length(); i++) {
66               char ch = line.charAt(i);
67               if (ch == '<')
68                 ws.print("&lt;");
69               else
70                 ws.print(ch);
71             }
72             ws.println();
73           }
74         } while (! line.startsWith("From "));
75
76         if (ws != null) {
77           ws.println("</message>");
78           ws.close();
79           ws = null;
80         }
81
82
83         String JavaDoc date = null;
84         String JavaDoc subject = null;
85         String JavaDoc from = null;
86         String JavaDoc id = null;
87         String JavaDoc references = null;
88
89         do {
90           line.clear();
91           if (! is.readLine(line))
92             return false;
93           if (line.length() == 0)
94             break;
95
96           String JavaDoc lower = line.toString().toLowerCase();
97
98           if (lower.startsWith("subject: ")) {
99             subject = line.substring("subject: ".length()).trim();
100
101             if (subject.toLowerCase().startsWith("re:"))
102               subject = subject.substring(3).trim();
103           }
104           else if (lower.startsWith("from: ")) {
105             from = line.substring("from: ".length());
106           }
107           else if (lower.startsWith("date: ")) {
108             date = line.substring("from: ".length());
109           }
110         } while (line.length() > 0);
111
112         int index = messages.get(subject);
113
114         if (index >= 0) {
115           ws = dst.lookup("" + index + ".xtp").openAppend();
116         }
117         else {
118           if (subject != null && ! subject.equals(""))
119             messages.put(subject, count);
120
121           ws = dst.lookup("" + count++ + ".xtp").openWrite();
122           ws.println("<title>" + subject + "</title>");
123         }
124         ws.println("<em>" + from + "</em>");
125         ws.println("<date>" + date + "</date>");
126         ws.println("<message>");
127       }
128     } finally {
129       if (ws != null)
130         ws.close();
131     }
132   }
133
134   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
135     throws ServletException JavaDoc, IOException JavaDoc
136   {
137     PrintWriter JavaDoc pw = response.getWriter();
138
139     Path path = Vfs.lookup("/home/ferg/majordomo/archive/resin-interest.0006");
140     Path dst = Vfs.lookup("/tmp/dst");
141     dst.mkdirs();
142
143     ReadStream is = path.openRead();
144     try {
145       parseMail(is, dst);
146     } finally {
147       is.close();
148     }
149     pw.println("done");
150   }
151 }
152
Popular Tags