/** * Get the value(s) associated with the given header name. * * @param name header name * @param trimed if <code>true</code> the header value is trimed. * @return value(s) associated with the header name */ private String[] getHeaderValues(String name, boolean trimed) { SmtpHeader h = headers.get(name); if (h == null || h.getValues() == null) return new String[0]; else { if (trimed) { String[] vals = new String[h.getValues().size()]; int i = 0; for (String s : h.getValues()) { vals[i] = s.trim(); i += 1; } return vals; } else return h.getValues().toArray(new String[h.getValues().size()]); } }
/** Adds a header to the Map. */ public void addHeaderLine(String line) { if (line != null && !"".equals(line)) { int pos = line.indexOf(": "); int termPos = line.indexOf(' '); if (pos > 0 && (termPos == -1 || pos < termPos)) { line = line.trim(); String name = line.substring(0, pos); List<String> vals = new ArrayList<String>(); if (line.length() >= pos + 2) { line = line.substring(pos + 2); if (name.equals(lastHeaderName)) { SmtpHeader h = (SmtpHeader) headers.get(name); if (h != null) { h.getValues().add(line); return; } else vals.add(line); } else { lastHeaderName = name; if (lastHeaderName.startsWith("X-")) // eXtended header so leave it intact vals.add(line); else parseMultipleValues(line, vals); } } headers.put(name, new SmtpHeader(name, vals)); } else { // Additional header value SmtpHeader h = headers.get(lastHeaderName); if (lastHeaderName.startsWith("X-")) h.getValues().add(line); else if (SmtpHeadersInterface.SUBJECT.equals(lastHeaderName)) h.getValues().set(0, h.getValues().get(0) + '\n' + line); else parseMultipleValues(line, h.getValues()); } } }