3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.storage;
21 import java.io.ByteArrayInputStream;
22 import java.util.Enumeration;
23 import javax.mail.Header;
24 import javax.mail.MessagingException;
25 import javax.mail.internet.InternetHeaders;
26 import javax.mail.internet.MimeUtility;
27 import org.sonews.config.Config;
30 * An article with no body only headers.
31 * @author Christian Lins
34 public class ArticleHead
37 protected InternetHeaders headers = null;
38 protected String headerSrc = null;
40 protected ArticleHead()
44 public ArticleHead(String headers)
48 this.headers = new InternetHeaders(
49 new ByteArrayInputStream(headers.getBytes()));
50 } catch (MessagingException ex) {
56 * Returns the header field with given name.
57 * @param name Name of the header field(s).
58 * @param returnNull If set to true, this method will return null instead
59 * of an empty array if there is no header field found.
60 * @return Header values or empty string.
62 public String[] getHeader(String name, boolean returnNull)
64 String[] ret = this.headers.getHeader(name);
65 if (ret == null && !returnNull) {
66 ret = new String[] {""};
71 public String[] getHeader(String name)
73 return getHeader(name, false);
77 * Sets the header value identified through the header name.
81 public void setHeader(String name, String value)
83 this.headers.setHeader(name, value);
84 this.headerSrc = null;
87 public Enumeration getAllHeaders()
89 return this.headers.getAllHeaders();
93 * @return Header source code of this Article.
95 public String getHeaderSource()
97 if (this.headerSrc != null) {
98 return this.headerSrc;
101 StringBuffer buf = new StringBuffer();
103 for (Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) {
104 Header entry = (Header) en.nextElement();
106 String value = entry.getValue().replaceAll("[\r\n]", " ");
107 buf.append(entry.getName());
109 buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
111 if (en.hasMoreElements()) {
116 this.headerSrc = buf.toString();
117 return this.headerSrc;
121 * Sets the headers of this Article. If headers contain no
122 * Message-Id a new one is created.
125 public void setHeaders(InternetHeaders headers)
127 this.headers = headers;
128 this.headerSrc = null;
133 * Checks some headers for their validity and generates an
134 * appropriate Path-header for this host if not yet existing.
135 * This method is called by some Article constructors and the
136 * method setHeaders().
137 * @return true if something on the headers was changed.
139 protected void validateHeaders()
141 // Check for valid Path-header
142 final String path = getHeader(Headers.PATH)[0];
143 final String host = Config.inst().get(Config.HOSTNAME, "localhost");
144 if (!path.startsWith(host)) {
145 StringBuffer pathBuf = new StringBuffer();
146 pathBuf.append(host);
148 pathBuf.append(path);
149 this.headers.setHeader(Headers.PATH, pathBuf.toString());