1 21 22 27 28 package javax.mail.internet; 29 30 import java.io.*; 31 import java.util.*; 32 import javax.mail.*; 33 import com.sun.mail.util.LineInputStream; 34 35 64 65 public class InternetHeaders { 66 78 protected static final class InternetHeader extends Header { 79 87 String line; 90 94 public InternetHeader(String l) { 95 super("", ""); int i = l.indexOf(':'); 97 if (i < 0) { 98 name = l.trim(); 100 } else { 101 name = l.substring(0, i).trim(); 102 } 103 line = l; 104 } 105 106 109 public InternetHeader(String n, String v) { 110 super(n, ""); 111 if (v != null) 112 line = n + ": " + v; 113 else 114 line = null; 115 } 116 117 120 public String getValue() { 121 int i = line.indexOf(':'); 122 if (i < 0) 123 return line; 124 int j; 126 for (j = i + 1; j < line.length(); j++) { 127 char c = line.charAt(j); 128 if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n')) 129 break; 130 } 131 return line.substring(j); 132 } 133 } 134 135 140 static class matchEnum implements Enumeration { 141 private Iterator e; private String names[]; private boolean match; private boolean want_line; private InternetHeader next_header; 149 155 matchEnum(List v, String n[], boolean m, boolean l) { 156 e = v.iterator(); 157 names = n; 158 match = m; 159 want_line = l; 160 next_header = null; 161 } 162 163 166 public boolean hasMoreElements() { 167 if (next_header == null) 170 next_header = nextMatch(); 171 return next_header != null; 172 } 173 174 177 public Object nextElement() { 178 if (next_header == null) 179 next_header = nextMatch(); 180 181 if (next_header == null) 182 throw new NoSuchElementException("No more headers"); 183 184 InternetHeader h = next_header; 185 next_header = null; 186 if (want_line) 187 return h.line; 188 else 189 return new Header(h.getName(), h.getValue()); 190 } 191 192 196 private InternetHeader nextMatch() { 197 next: 198 while (e.hasNext()) { 199 InternetHeader h = (InternetHeader)e.next(); 200 201 if (h.line == null) 203 continue; 204 205 if (names == null) 207 return match ? null : h; 208 209 for (int i = 0; i < names.length; i++) { 211 if (names[i].equalsIgnoreCase(h.getName())) { 212 if (match) 213 return h; 214 else 215 continue next; 219 } 220 } 221 if (!match) 223 return h; 224 } 225 return null; 226 } 227 } 228 229 230 245 protected List headers; 246 247 251 public InternetHeaders() { 252 headers = new ArrayList(40); 253 headers.add(new InternetHeader("Return-Path", null)); 254 headers.add(new InternetHeader("Received", null)); 255 headers.add(new InternetHeader("Resent-Date", null)); 256 headers.add(new InternetHeader("Resent-From", null)); 257 headers.add(new InternetHeader("Resent-Sender", null)); 258 headers.add(new InternetHeader("Resent-To", null)); 259 headers.add(new InternetHeader("Resent-Cc", null)); 260 headers.add(new InternetHeader("Resent-Bcc", null)); 261 headers.add(new InternetHeader("Resent-Message-Id", null)); 262 headers.add(new InternetHeader("Date", null)); 263 headers.add(new InternetHeader("From", null)); 264 headers.add(new InternetHeader("Sender", null)); 265 headers.add(new InternetHeader("Reply-To", null)); 266 headers.add(new InternetHeader("To", null)); 267 headers.add(new InternetHeader("Cc", null)); 268 headers.add(new InternetHeader("Bcc", null)); 269 headers.add(new InternetHeader("Message-Id", null)); 270 headers.add(new InternetHeader("In-Reply-To", null)); 271 headers.add(new InternetHeader("References", null)); 272 headers.add(new InternetHeader("Subject", null)); 273 headers.add(new InternetHeader("Comments", null)); 274 headers.add(new InternetHeader("Keywords", null)); 275 headers.add(new InternetHeader("Errors-To", null)); 276 headers.add(new InternetHeader("MIME-Version", null)); 277 headers.add(new InternetHeader("Content-Type", null)); 278 headers.add(new InternetHeader("Content-Transfer-Encoding", null)); 279 headers.add(new InternetHeader("Content-MD5", null)); 280 headers.add(new InternetHeader(":", null)); 281 headers.add(new InternetHeader("Content-Length", null)); 282 headers.add(new InternetHeader("Status", null)); 283 } 284 285 299 public InternetHeaders(InputStream is) throws MessagingException { 300 headers = new ArrayList(40); 301 load(is); 302 } 303 304 317 public void load(InputStream is) throws MessagingException { 318 String line; 321 LineInputStream lis = new LineInputStream(is); 322 String prevline = null; StringBuffer lineBuffer = new StringBuffer (); 325 326 try { 327 do { 329 line = lis.readLine(); 330 if (line != null && 331 (line.startsWith(" ") || line.startsWith("\t"))) { 332 if (prevline != null) { 334 lineBuffer.append(prevline); 335 prevline = null; 336 } 337 lineBuffer.append("\r\n"); 338 lineBuffer.append(line); 339 } else { 340 if (prevline != null) 342 addHeaderLine(prevline); 343 else if (lineBuffer.length() > 0) { 344 addHeaderLine(lineBuffer.toString()); 346 lineBuffer.setLength(0); 347 } 348 prevline = line; 349 } 350 } while (line != null && line.length() > 0); 351 } catch (IOException ioex) { 352 throw new MessagingException("Error in input stream", ioex); 353 } 354 } 355 356 364 public String [] getHeader(String name) { 365 Iterator e = headers.iterator(); 366 List v = new ArrayList(); 369 while (e.hasNext()) { 370 InternetHeader h = (InternetHeader)e.next(); 371 if (name.equalsIgnoreCase(h.getName()) && h.line != null) { 372 v.add(h.getValue()); 373 } 374 } 375 if (v.size() == 0) 376 return (null); 377 String r[] = new String [v.size()]; 379 r = (String [])v.toArray(r); 380 return (r); 381 } 382 383 395 public String getHeader(String name, String delimiter) { 396 String s[] = getHeader(name); 397 398 if (s == null) 399 return null; 400 401 if ((s.length == 1) || delimiter == null) 402 return s[0]; 403 404 StringBuffer r = new StringBuffer (s[0]); 405 for (int i = 1; i < s.length; i++) { 406 r.append(delimiter); 407 r.append(s[i]); 408 } 409 return r.toString(); 410 } 411 412 422 public void setHeader(String name, String value) { 423 boolean found = false; 424 425 for (int i = 0; i < headers.size(); i++) { 426 InternetHeader h = (InternetHeader)headers.get(i); 427 if (name.equalsIgnoreCase(h.getName())) { 428 if (!found) { 429 int j; 430 if (h.line != null && (j = h.line.indexOf(':')) >= 0) { 431 h.line = h.line.substring(0, j + 1) + " " + value; 432 } else { 434 h.line = name + ": " + value; 435 } 436 found = true; 437 } else { 438 headers.remove(i); 439 i--; } 441 } 442 } 443 444 if (!found) { 445 addHeader(name, value); 446 } 447 } 448 449 464 public void addHeader(String name, String value) { 465 int pos = headers.size(); 466 boolean addReverse = 467 name.equalsIgnoreCase("Received") || 468 name.equalsIgnoreCase("Return-Path"); 469 if (addReverse) 470 pos = 0; 471 for (int i = headers.size() - 1; i >= 0; i--) { 472 InternetHeader h = (InternetHeader)headers.get(i); 473 if (name.equalsIgnoreCase(h.getName())) { 474 if (addReverse) { 475 pos = i; 476 } else { 477 headers.add(i + 1, new InternetHeader(name, value)); 478 return; 479 } 480 } 481 if (h.getName().equals(":")) 483 pos = i; 484 } 485 headers.add(pos, new InternetHeader(name, value)); 486 } 487 488 492 public void removeHeader(String name) { 493 for (int i = 0; i < headers.size(); i++) { 494 InternetHeader h = (InternetHeader)headers.get(i); 495 if (name.equalsIgnoreCase(h.getName())) { 496 h.line = null; 497 } 500 } 501 } 502 503 509 public Enumeration getAllHeaders() { 510 return (new matchEnum(headers, null, false, false)); 511 } 512 513 518 public Enumeration getMatchingHeaders(String [] names) { 519 return (new matchEnum(headers, names, true, false)); 520 } 521 522 527 public Enumeration getNonMatchingHeaders(String [] names) { 528 return (new matchEnum(headers, names, false, false)); 529 } 530 531 541 public void addHeaderLine(String line) { 542 try { 543 char c = line.charAt(0); 544 if (c == ' ' || c == '\t') { 545 InternetHeader h = 546 (InternetHeader)headers.get(headers.size() - 1); 547 h.line += "\r\n" + line; 548 } else 549 headers.add(new InternetHeader(line)); 550 } catch (StringIndexOutOfBoundsException e) { 551 return; 553 } catch (NoSuchElementException e) { 554 } 556 } 557 558 561 public Enumeration getAllHeaderLines() { 562 return (getNonMatchingHeaderLines(null)); 563 } 564 565 568 public Enumeration getMatchingHeaderLines(String [] names) { 569 return (new matchEnum(headers, names, true, true)); 570 } 571 572 575 public Enumeration getNonMatchingHeaderLines(String [] names) { 576 return (new matchEnum(headers, names, false, true)); 577 } 578 } 579 | Popular Tags |