1 package com.protomatter.util; 2 3 52 53 import java.io.*; 54 import java.util.*; 55 56 64 public class MIMEAttachment 65 implements Serializable 66 { 67 private Hashtable headers = new Hashtable(); 68 private boolean isBinary = false; 69 private byte[] content = null; 70 private static String CRLF = "\r\n"; 71 72 76 public MIMEAttachment(String type, String description, String content) 77 { 78 this(); 79 setHeader("Content-Type", type); 80 setHeader("Content-Description", description); 81 this.content = content.getBytes(); 82 setBinary(false); 83 } 84 85 90 public MIMEAttachment(String type, String description, byte[] data, boolean isBinary) 91 { 92 this(type, description, data); 93 setBinary(isBinary); 94 } 95 96 100 public MIMEAttachment(String type, String description, byte[] data) 101 { 102 this(); 103 setHeader("Content-Type", type); 104 setHeader("Content-Description", description); 105 this.content = data; 106 setBinary(true); 107 } 108 109 112 public MIMEAttachment() 113 { 114 super(); 115 } 116 117 121 public void setHeaders(Hashtable headers) 122 { 123 this.headers = headers; 124 } 125 126 130 public void setBinary(boolean b) 131 { 132 isBinary = b; 133 } 134 135 138 public void setContent(String content) 139 { 140 this.content = content.getBytes(); 141 } 142 143 146 public void setContent(byte[] content) 147 { 148 this.content = content; 149 } 150 151 154 public void setHeader(String headerName, String headerVal) 155 { 156 headers.put(headerName, headerVal); 157 } 158 159 162 public void removeHeader(String headerName) 163 { 164 headers.remove(headerName); 165 } 166 167 170 public String getHeader(String headerName) 171 { 172 String val = (String )headers.get(headerName); 173 return val; 174 } 175 176 185 public String getSubHeader(String name, String sub) 186 { 187 String h = getHeader(name); 188 if (h == null) return h; 190 StringTokenizer st = new StringTokenizer(h, ";"); 191 if (sub.equals("")) 192 return stripSurroundingWhiteSpace(st.nextToken()); 193 else 194 st.nextToken(); while (st.hasMoreTokens()) 196 { 197 StringTokenizer nst = new StringTokenizer(stripSurroundingWhiteSpace(st.nextToken()), "="); 198 String key = nst.nextToken(); 199 String val = nst.nextToken(); 200 if (key.equals(sub)) 201 { 202 if (val.startsWith("\"") && val.endsWith("\"")) 203 return val.substring(1, val.length() -1); 204 else 205 return val; 206 } 207 } 208 return ""; 210 } 211 212 private static String stripSurroundingWhiteSpace(String s) 214 { 215 int i=0; int j=0; for (i=0; i<s.length() && Character.isWhitespace(s.charAt(i)); i++); 218 for (j=s.length()-1; j>=0 && Character.isWhitespace(s.charAt(j)); j--); 219 return s.substring(i, j+1); 220 } 221 222 225 public String toString() 226 { 227 StringWriter sw = new StringWriter(); 228 PrintWriter pw = new PrintWriter(sw); 229 write(pw); 230 pw.flush(); 231 return sw.toString(); 232 } 233 234 238 public void write(PrintWriter w) 239 { 240 Enumeration e = headers.keys(); 241 while (e.hasMoreElements()) 242 { 243 String hName = (String )e.nextElement(); 244 String hVal = getHeader(hName); 245 w.print(hName); 246 w.print(": "); 247 w.print(hVal); 248 w.print(CRLF); 249 } 250 if (isBinary()) 251 { 252 if (getHeader("Content-Transfer-Encoding") == null) 253 { 254 w.print("Content-Transfer-Encoding: BASE64"); 255 w.print(CRLF); 256 } 257 w.print(CRLF); 258 w.print(CRLF); 259 String encoded = Base64.encode(content); 260 261 int start = 0; 263 int end = 65; 264 while (start < encoded.length()) 265 { 266 if (end >= encoded.length()) 267 end = encoded.length(); 268 w.print(encoded.substring(start, end)); 269 w.print(CRLF); 270 start = end; 271 end += 65; 272 } 273 } 274 else { 276 w.print(CRLF); 277 w.print(new String (content)); 278 } 279 } 280 281 286 public byte[] getContent() 287 { 288 return content; 289 } 290 291 294 public Enumeration getHeaderNames() 295 { 296 return headers.keys(); 297 } 298 299 302 public boolean isBinary() 303 { 304 return isBinary; 305 } 306 } 307
| Popular Tags
|