KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2001 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.vfs.Path;
32 import com.caucho.vfs.ReadStream;
33 import com.caucho.vfs.WriteStream;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39
40 public class MboxArchive {
41   private Path path;
42
43   private ArrayList JavaDoc threads;
44   private ArrayList JavaDoc messages;
45
46   public MboxArchive(Path path)
47     throws IOException JavaDoc
48   {
49     this.path = path;
50   }
51
52   public ArrayList JavaDoc getThreads()
53   {
54     return threads;
55   }
56
57   public ArrayList JavaDoc getMessages()
58   {
59     return messages;
60   }
61
62   public void analyzeMessages()
63     throws IOException JavaDoc
64   {
65     threads = new ArrayList JavaDoc();
66     messages = new ArrayList JavaDoc();
67
68     int count = 0;
69     HashMap JavaDoc map = new HashMap JavaDoc();
70     HashMap JavaDoc subjectMap = new HashMap JavaDoc();
71
72     ReadStream rawIs = path.openRead();
73     MboxStream mboxStream = new MboxStream(rawIs);
74     ReadStream message;
75
76     while ((message = mboxStream.openRead()) != null) {
77       MboxMessage msg = new MboxMessage(count++);
78
79       String JavaDoc id = (String JavaDoc) message.getAttribute("Message-Id");
80
81       id = normalizeMessageId(id);
82       
83       msg.setMessageId(id);
84       
85       msg.setFrom((String JavaDoc) message.getAttribute("From"));
86       String JavaDoc subject = (String JavaDoc) message.getAttribute("Subject");
87       msg.setSubject(subject);
88
89       messages.add(msg);
90
91       String JavaDoc inReplyTo = (String JavaDoc) message.getAttribute("In-Reply-To");
92
93       inReplyTo = normalizeMessageId(inReplyTo);
94
95       MboxMessage parent = null;
96       if (inReplyTo != null)
97         parent = (MboxMessage) map.get(inReplyTo);
98
99       if (parent == null)
100         parent = getParentBySubject(subjectMap, subject);
101
102       if (parent != null)
103         msg.setParent(parent);
104       else
105         threads.add(msg);
106           
107       map.put(id, msg);
108       setParentBySubject(subjectMap, subject, msg);
109     }
110
111     rawIs.close();
112   }
113
114   private void generateThread(WriteStream os, MboxMessage threadHeader)
115   {
116     
117   }
118
119   private String JavaDoc normalizeMessageId(String JavaDoc id)
120   {
121     if (id == null)
122       return id;
123
124     int start = id.indexOf(id, '<');
125     if (start < 0)
126       return id;
127     
128     int end = id.indexOf(id, '>');
129     if (end < start)
130       return id;
131
132     return id.substring(start, end);
133   }
134
135   private MboxMessage getParentBySubject(Map JavaDoc subjectMap, String JavaDoc subject)
136   {
137     if (subject == null || subject.equals(""))
138       return null;
139
140     MboxMessage message = (MboxMessage) subjectMap.get(subject);
141     if (message != null)
142       return message;
143
144     while (subject.length() > 4) {
145       String JavaDoc prefix = subject.substring(0, 4).toLowerCase();
146       
147       if (! prefix.equals("re: ") && ! prefix.equals("aw: "))
148         return null;
149
150       subject = subject.substring(4);
151
152       message = (MboxMessage) subjectMap.get(subject);
153       if (message != null)
154         return message;
155     }
156
157     return null;
158   }
159
160   private void setParentBySubject(Map JavaDoc subjectMap, String JavaDoc subject,
161                                   MboxMessage message)
162   {
163     if (subject == null || subject.equals(""))
164       return;
165
166     while (subject.length() > 4) {
167       String JavaDoc prefix = subject.substring(0, 4).toLowerCase();
168       
169       if (! prefix.equals("re: ") && ! prefix.equals("aw: ")) {
170         subjectMap.put(subject, message);
171         return;
172       }
173
174       subject = subject.substring(4);
175     }
176
177     if (subject == null || subject.equals(""))
178       return;
179
180     subjectMap.put(subject, message);
181   }
182 }
183
Popular Tags