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: */ chris@3: public class ArticleHead chris@3: { chris@3: chris@3: protected InternetHeaders headers = null; chris@3: protected String headerSrc = null; chris@3: chris@3: protected ArticleHead() chris@3: { chris@3: } chris@3: chris@3: public ArticleHead(String headers) chris@3: { chris@3: try chris@3: { chris@3: // Parse the header chris@3: this.headers = new InternetHeaders( chris@3: new ByteArrayInputStream(headers.getBytes())); chris@3: } chris@3: catch(MessagingException ex) chris@3: { chris@3: ex.printStackTrace(); chris@3: } chris@3: } chris@3: chris@3: /** chris@3: * Returns the header field with given name. chris@3: * @param name Name of the header field(s). chris@3: * @param returnNull If set to true, this method will return null instead chris@3: * of an empty array if there is no header field found. chris@3: * @return Header values or empty string. chris@3: */ chris@3: public String[] getHeader(String name, boolean returnNull) chris@3: { chris@3: String[] ret = this.headers.getHeader(name); chris@3: if(ret == null && !returnNull) chris@3: { chris@3: ret = new String[]{""}; chris@3: } chris@3: return ret; chris@3: } chris@3: chris@3: public String[] getHeader(String name) chris@3: { chris@3: return getHeader(name, false); chris@3: } chris@3: chris@3: /** chris@3: * Sets the header value identified through the header name. chris@3: * @param name chris@3: * @param value chris@3: */ chris@3: public void setHeader(String name, String value) chris@3: { chris@3: this.headers.setHeader(name, value); chris@3: this.headerSrc = null; chris@3: } chris@3: chris@3: public Enumeration getAllHeaders() chris@3: { chris@3: return this.headers.getAllHeaders(); chris@3: } chris@3: chris@3: /** chris@3: * @return Header source code of this Article. chris@3: */ chris@3: public String getHeaderSource() chris@3: { chris@3: if(this.headerSrc != null) chris@3: { chris@3: return this.headerSrc; chris@3: } chris@3: chris@3: StringBuffer buf = new StringBuffer(); chris@3: chris@3: for(Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) chris@3: { chris@3: Header entry = (Header)en.nextElement(); chris@3: chris@3: String value = entry.getValue().replaceAll("[\r\n]", " "); chris@3: buf.append(entry.getName()); chris@3: buf.append(": "); chris@3: buf.append(MimeUtility.fold(entry.getName().length() + 2, value)); chris@3: chris@3: if(en.hasMoreElements()) chris@3: { chris@3: buf.append("\r\n"); chris@3: } chris@3: } chris@3: chris@3: this.headerSrc = buf.toString(); chris@3: return this.headerSrc; chris@3: } chris@3: chris@3: /** chris@3: * Sets the headers of this Article. If headers contain no chris@3: * Message-Id a new one is created. chris@3: * @param headers chris@3: */ chris@3: public void setHeaders(InternetHeaders headers) chris@3: { chris@3: this.headers = headers; chris@3: this.headerSrc = null; chris@3: validateHeaders(); chris@3: } chris@3: chris@3: /** chris@3: * Checks some headers for their validity and generates an chris@3: * appropriate Path-header for this host if not yet existing. chris@3: * This method is called by some Article constructors and the chris@3: * method setHeaders(). chris@3: * @return true if something on the headers was changed. chris@3: */ chris@3: protected void validateHeaders() chris@3: { chris@3: // Check for valid Path-header chris@3: final String path = getHeader(Headers.PATH)[0]; chris@3: final String host = Config.inst().get(Config.HOSTNAME, "localhost"); chris@3: if(!path.startsWith(host)) chris@3: { chris@3: StringBuffer pathBuf = new StringBuffer(); chris@3: pathBuf.append(host); chris@3: pathBuf.append('!'); chris@3: pathBuf.append(path); chris@3: this.headers.setHeader(Headers.PATH, pathBuf.toString()); chris@3: } chris@3: } chris@3: chris@3: }