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)
49 this.headers = new InternetHeaders(
50 new ByteArrayInputStream(headers.getBytes()));
52 catch(MessagingException ex)
59 * Returns the header field with given name.
60 * @param name Name of the header field(s).
61 * @param returnNull If set to true, this method will return null instead
62 * of an empty array if there is no header field found.
63 * @return Header values or empty string.
65 public String[] getHeader(String name, boolean returnNull)
67 String[] ret = this.headers.getHeader(name);
68 if(ret == null && !returnNull)
70 ret = new String[]{""};
75 public String[] getHeader(String name)
77 return getHeader(name, false);
81 * Sets the header value identified through the header name.
85 public void setHeader(String name, String value)
87 this.headers.setHeader(name, value);
88 this.headerSrc = null;
91 public Enumeration getAllHeaders()
93 return this.headers.getAllHeaders();
97 * @return Header source code of this Article.
99 public String getHeaderSource()
101 if(this.headerSrc != null)
103 return this.headerSrc;
106 StringBuffer buf = new StringBuffer();
108 for(Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();)
110 Header entry = (Header)en.nextElement();
112 String value = entry.getValue().replaceAll("[\r\n]", " ");
113 buf.append(entry.getName());
115 buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
117 if(en.hasMoreElements())
123 this.headerSrc = buf.toString();
124 return this.headerSrc;
128 * Sets the headers of this Article. If headers contain no
129 * Message-Id a new one is created.
132 public void setHeaders(InternetHeaders headers)
134 this.headers = headers;
135 this.headerSrc = null;
140 * Checks some headers for their validity and generates an
141 * appropriate Path-header for this host if not yet existing.
142 * This method is called by some Article constructors and the
143 * method setHeaders().
144 * @return true if something on the headers was changed.
146 protected void validateHeaders()
148 // Check for valid Path-header
149 final String path = getHeader(Headers.PATH)[0];
150 final String host = Config.inst().get(Config.HOSTNAME, "localhost");
151 if(!path.startsWith(host))
153 StringBuffer pathBuf = new StringBuffer();
154 pathBuf.append(host);
156 pathBuf.append(path);
157 this.headers.setHeader(Headers.PATH, pathBuf.toString());