KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > Header


1 package SnowMailClient.model;
2
3
4 import SnowMailClient.utils.MailMessageUtils;
5 import SnowMailClient.utils.NumberedLineReader;
6
7
8 import java.util.*;
9 import java.text.*;
10 import java.io.*;
11
12
13 /** A mail header
14 */

15 public class Header
16 {
17   public static boolean debug = false;
18
19   /** when a message is actually signed by XX but NOT VERIFIED !
20   */

21   public final static String JavaDoc GPG_IS_SIGNED_BY = "gpg-snowmail-is-signed-by";
22
23   /** when a message has been signed by XX but the signature block has been removed and successfully checked
24   */

25   public final static String JavaDoc GPG_HAS_BEEN_SIGNED_BY = "gpg-snowmail-has-been-signed-by";
26
27   private final List<HeaderEntry> entries = new ArrayList<HeaderEntry>();
28
29
30   public Header()
31   {
32   } // Constructor
33

34   public int getEntriesCount() { return entries.size(); }
35   public HeaderEntry getEntryAt(int pos) { return entries.get(pos); }
36
37   public void removeAllEntries()
38   {
39     entries.clear();
40   }
41
42   public void addEntry(HeaderEntry he)
43   {
44     entries.add(he);
45   }
46
47   public void removeEntry(HeaderEntry he)
48   {
49     entries.remove(he);
50   }
51
52   public void remove(String JavaDoc key)
53   {
54      HeaderEntry he = this.getEntry(key);
55      if(he!=null) removeEntry(he);
56   }
57
58
59
60   /** do not overwrite (Received entries has multiple occurences)
61   */

62   public void addEntry(String JavaDoc key, String JavaDoc value)
63   {
64      HeaderEntry he = new HeaderEntry(key, value);
65      this.addEntry(he);
66   }
67
68   /** overwrite if existing...
69   */

70   public void setEntryOverwrite(String JavaDoc key, String JavaDoc value)
71   {
72      HeaderEntry he = getEntry(key);
73      if(he!=null)
74      {
75        this.removeEntry(he);
76      }
77
78      he = new HeaderEntry(key, value);
79      this.addEntry(he);
80   }
81
82   /** null if not found
83   */

84   public HeaderEntry getEntry(String JavaDoc key)
85   {
86     for(HeaderEntry he: entries)
87     {
88        if(he.isEquals(key)) return he;
89     }
90     return null;
91   }
92
93   public boolean hasEntry(String JavaDoc key)
94   {
95     for(HeaderEntry he: entries)
96     {
97        if(he.isEquals(key)) return true;
98     }
99     return false;
100   }
101
102   /** zero length if none found
103   */

104   public Vector<HeaderEntry> getAllEntries(String JavaDoc key)
105   {
106     Vector<HeaderEntry> found = new Vector<HeaderEntry>();
107     for(HeaderEntry he: entries)
108     {
109        if(he.isEquals(key)) found.addElement(he);
110     }
111     return found;
112   }
113
114   public String JavaDoc getEntryValue(String JavaDoc key, String JavaDoc def)
115   {
116    return getRawEntryValue(key, def);
117   }
118
119
120   public String JavaDoc getRawEntryValue(String JavaDoc key, String JavaDoc def)
121   {
122     for(HeaderEntry he: entries)
123     {
124        if(he.isEquals(key)) return he.getValue();
125     }
126     return def;
127   }
128
129
130   /** the header string representation. Use to_ASCII_String() for a correct sendeable representation !
131   */

132   public String JavaDoc toString()
133   {
134     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
135     for(HeaderEntry he: entries)
136     {
137        sb.append(he.toString()+"\r\n");
138     }
139     return sb.toString();
140   }
141
142   public String JavaDoc to_ASCII_String()
143   {
144     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
145     for(HeaderEntry he: entries)
146     {
147        sb.append(he.to_ASCII_String()+"\r\n");
148     }
149     return sb.toString();
150   }
151
152
153     /** reads the header.
154      * stops when the first empty line occurs
155      */

156     public static void parseHeader(final NumberedLineReader in, final Header header) throws Exception JavaDoc
157     {
158       String JavaDoc actualTag = null; // lowercased and trimmed!
159
StringBuffer JavaDoc actualArgBuffer = new StringBuffer JavaDoc();
160       boolean nextLineSameTag = false;
161
162       String JavaDoc actualLine = in.readLine();
163       // until end or empty line
164
while(actualLine != null && !actualLine.trim().equals(""))
165       {
166           actualLine = actualLine.trim();
167           if(debug) System.out.println("Header line = " + actualLine);
168
169           // a tag cannot have spaces, we use this info
170
//
171
int pos = actualLine.indexOf(':');
172           int posSp = actualLine.indexOf(' ');
173
174           //System.out.println(actualLine);
175
if(pos==-1 || (posSp>0 && posSp<pos))
176           {
177               if(actualTag == null)
178               {
179                   // [Sep2005]: accept empty headers without space after it
180
if(actualLine.startsWith("--"))
181                   {
182                      in.undoRead(); // LET THE NEXT PART READ THIS AS ITS FIRST LINE !!!
183
return;
184                   }
185                   throw new Exception JavaDoc("First tag empty '"+actualLine+"'");
186               }
187               else
188               {
189                  // same tag, new argument
190
actualArgBuffer.append("\n "+actualLine.trim());
191               }
192           }
193           else
194           {
195               // new tag, store the old
196
if( actualTag !=null)
197               {
198                   actualTag = actualTag.trim();
199                   if(actualTag.equals("subject") || actualTag.equals("from") || actualTag.equals("to"))
200                   {
201                     String JavaDoc val = MailMessageUtils.decodeCharsetCodedArgumentFully(
202                           actualArgBuffer.toString());
203                     header.addEntry(actualTag, val);
204                   }
205                   else
206                   {
207                     header.addEntry(actualTag, actualArgBuffer.toString());
208                   }
209               }
210               // the new one
211
actualTag = actualLine.substring(0,pos).toLowerCase().trim();
212               actualArgBuffer.delete(0, actualArgBuffer.length());
213               actualArgBuffer.append(actualLine.substring(pos+1).trim());
214           }
215
216           // next line
217
actualLine = in.readLine();
218       }
219
220       // don't forget to store the last
221
if( actualTag !=null)
222       {
223          header.addEntry(actualTag, actualArgBuffer.toString());
224       }
225     }
226
227
228
229 } // Header
Popular Tags