1 36 package org.columba.ristretto.message; 37 38 import java.io.InputStream ; 39 import java.util.Enumeration ; 40 import java.util.Hashtable ; 41 42 import org.columba.ristretto.io.CharSequenceSource; 43 import org.columba.ristretto.io.SourceInputStream; 44 import org.columba.ristretto.io.Streamable; 45 46 53 public class Header implements Streamable { 54 private static final int MAXLINELENGTH = 78; 55 private Hashtable header; 56 57 private static final String [] unlimitedFields = { 58 "received", "x-" 59 }; 60 61 62 65 public Header() { 66 header = new Hashtable (); 67 } 68 69 75 public void set(String key, Object value) { 76 if( value instanceof String ) 77 header.put( key, value); 78 else 79 header.put( key, value.toString()); 80 } 81 82 88 public void append(String key, String value) { 89 String oldvalue = (String ) header.get(key); 90 if( oldvalue == null || !isUnlimited(key)) { 91 header.put(key, value); 92 } else { 93 header.put(key, oldvalue + value); 94 } 95 } 96 97 103 public String get( String key ) { 104 return (String ) header.get(key); 105 } 106 107 112 public int length() { 113 return header.size(); 114 } 115 116 119 public Object clone() { 120 Header cloneHeader = new Header(); 121 cloneHeader.header = (Hashtable ) this.header.clone(); 122 return cloneHeader; 123 } 124 125 128 public int count() { 129 return header.size(); 130 } 131 132 135 public String toString() { 136 StringBuffer result = new StringBuffer (); 137 Enumeration keys = header.keys(); 138 while( keys.hasMoreElements() ) { 139 String key = (String ) keys.nextElement(); 140 result.append(key); 141 result.append(": "); 142 result.append( foldLine(get(key), MAXLINELENGTH - key.length() - 2) ); 143 result.append( "\r\n" ); 144 } 145 result.append("\r\n"); 146 return result.toString(); 147 } 148 149 154 public void merge( Header arg ) { 155 Object key; 156 Enumeration keys = arg.header.keys(); 157 while( keys.hasMoreElements() ) { 158 key = keys.nextElement(); 159 this.header.put( key, arg.header.get(key)); 160 } 161 } 162 163 private CharSequence foldLine(String line, int firstMaxLength) { 164 if( line.length() <= firstMaxLength || firstMaxLength <= 0) { 166 return line; 167 } 168 169 int foldPos = line.indexOf(' ', firstMaxLength); 171 if( foldPos == -1 ) { 172 return line; 173 } 174 175 StringBuffer result = new StringBuffer ( line.length() + 3); 176 result.append( line.subSequence(0, foldPos)); 177 result.append("\r\n "); 178 int lastFoldPos = foldPos; 179 180 foldPos += MAXLINELENGTH; 181 if( foldPos < line.length() ) { 182 foldPos = line.indexOf(' ', foldPos); 183 } 184 185 while( foldPos != -1 && foldPos < line.length() ) { 186 result.append( line.subSequence(0, foldPos)); 187 result.append("\r\n "); 188 lastFoldPos = foldPos; 189 190 foldPos += MAXLINELENGTH; 191 if( foldPos < line.length() ) { 192 foldPos = line.indexOf(' ', foldPos); 193 } 194 } 195 196 result.append(line.substring(lastFoldPos)); 197 198 return result; 199 } 200 201 204 public InputStream getInputStream() { 205 return new SourceInputStream( new CharSequenceSource( toString() )); 206 } 207 208 213 public Enumeration getKeys() { 214 return header.keys(); 215 } 216 217 private static boolean isUnlimited( String key) { 218 String ignoredCase = key.toLowerCase(); 219 for( int i=0; i<unlimitedFields.length; i++) { 220 if( ignoredCase.startsWith(unlimitedFields[i])) { 221 return true; 222 } 223 } 224 return false; 225 } 226 227 228 } 229 | Popular Tags |