chris@3: /* chris@3: * SONEWS News Server chris@3: * see AUTHORS for the list of contributors chris@3: * chris@3: * This program is free software: you can redistribute it and/or modify chris@3: * it under the terms of the GNU General Public License as published by chris@3: * the Free Software Foundation, either version 3 of the License, or chris@3: * (at your option) any later version. chris@3: * chris@3: * This program is distributed in the hope that it will be useful, chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@3: * GNU General Public License for more details. chris@3: * chris@3: * You should have received a copy of the GNU General Public License chris@3: * along with this program. If not, see . chris@3: */ chris@3: chris@3: package org.sonews.storage; chris@3: chris@3: import java.io.ByteArrayInputStream; chris@3: import java.util.Enumeration; chris@3: import javax.mail.Header; chris@3: import javax.mail.MessagingException; chris@3: import javax.mail.internet.InternetHeaders; chris@3: import javax.mail.internet.MimeUtility; chris@3: import org.sonews.config.Config; chris@3: chris@3: /** chris@3: * An article with no body only headers. chris@3: * @author Christian Lins chris@3: * @since sonews/0.5.0 chris@3: */ cli@37: public class ArticleHead chris@3: { chris@3: cli@37: protected InternetHeaders headers = null; cli@37: protected String headerSrc = null; chris@3: cli@37: protected ArticleHead() cli@37: { cli@37: } chris@3: cli@37: public ArticleHead(String headers) cli@37: { cli@37: try { cli@37: // Parse the header cli@37: this.headers = new InternetHeaders( cli@37: new ByteArrayInputStream(headers.getBytes())); cli@37: } catch (MessagingException ex) { cli@37: ex.printStackTrace(); cli@37: } cli@37: } chris@3: cli@37: /** cli@37: * Returns the header field with given name. cli@37: * @param name Name of the header field(s). cli@37: * @param returnNull If set to true, this method will return null instead cli@37: * of an empty array if there is no header field found. cli@37: * @return Header values or empty string. cli@37: */ cli@37: public String[] getHeader(String name, boolean returnNull) cli@37: { cli@37: String[] ret = this.headers.getHeader(name); cli@37: if (ret == null && !returnNull) { cli@37: ret = new String[] {""}; cli@37: } cli@37: return ret; cli@37: } chris@3: cli@37: public String[] getHeader(String name) cli@37: { cli@37: return getHeader(name, false); cli@37: } chris@3: cli@37: /** cli@37: * Sets the header value identified through the header name. cli@37: * @param name cli@37: * @param value cli@37: */ cli@37: public void setHeader(String name, String value) cli@37: { cli@37: this.headers.setHeader(name, value); cli@37: this.headerSrc = null; cli@37: } chris@3: cli@37: public Enumeration getAllHeaders() cli@37: { cli@37: return this.headers.getAllHeaders(); cli@37: } chris@3: cli@37: /** cli@37: * @return Header source code of this Article. cli@37: */ cli@37: public String getHeaderSource() cli@37: { cli@37: if (this.headerSrc != null) { cli@37: return this.headerSrc; cli@37: } chris@3: cli@37: StringBuffer buf = new StringBuffer(); chris@3: cli@37: for (Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) { cli@37: Header entry = (Header) en.nextElement(); chris@3: cli@37: String value = entry.getValue().replaceAll("[\r\n]", " "); cli@37: buf.append(entry.getName()); cli@37: buf.append(": "); cli@37: buf.append(MimeUtility.fold(entry.getName().length() + 2, value)); cli@37: cli@37: if (en.hasMoreElements()) { cli@37: buf.append("\r\n"); cli@37: } cli@37: } cli@37: cli@37: this.headerSrc = buf.toString(); cli@37: return this.headerSrc; cli@37: } cli@37: cli@37: /** cli@37: * Sets the headers of this Article. If headers contain no cli@37: * Message-Id a new one is created. cli@37: * @param headers cli@37: */ cli@37: public void setHeaders(InternetHeaders headers) cli@37: { cli@37: this.headers = headers; cli@37: this.headerSrc = null; cli@37: validateHeaders(); cli@37: } cli@37: cli@37: /** cli@37: * Checks some headers for their validity and generates an cli@37: * appropriate Path-header for this host if not yet existing. cli@37: * This method is called by some Article constructors and the cli@37: * method setHeaders(). cli@37: * @return true if something on the headers was changed. cli@37: */ cli@37: protected void validateHeaders() cli@37: { cli@37: // Check for valid Path-header cli@37: final String path = getHeader(Headers.PATH)[0]; cli@37: final String host = Config.inst().get(Config.HOSTNAME, "localhost"); cli@37: if (!path.startsWith(host)) { cli@37: StringBuffer pathBuf = new StringBuffer(); cli@37: pathBuf.append(host); cli@37: pathBuf.append('!'); cli@37: pathBuf.append(path); cli@37: this.headers.setHeader(Headers.PATH, pathBuf.toString()); cli@37: } cli@37: } chris@3: }