diff -r ed84c8bdd87b -r aae4b4688700 src/org/sonews/storage/ArticleHead.java --- a/src/org/sonews/storage/ArticleHead.java Sun Aug 29 17:28:58 2010 +0200 +++ b/src/org/sonews/storage/ArticleHead.java Wed Oct 12 00:11:25 2011 +0200 @@ -31,131 +31,122 @@ * @author Christian Lins * @since sonews/0.5.0 */ -public class ArticleHead +public class ArticleHead { - protected InternetHeaders headers = null; - protected String headerSrc = null; - - protected ArticleHead() - { - } - - public ArticleHead(String headers) - { - try - { - // Parse the header - this.headers = new InternetHeaders( - new ByteArrayInputStream(headers.getBytes())); - } - catch(MessagingException ex) - { - ex.printStackTrace(); - } - } - - /** - * Returns the header field with given name. - * @param name Name of the header field(s). - * @param returnNull If set to true, this method will return null instead - * of an empty array if there is no header field found. - * @return Header values or empty string. - */ - public String[] getHeader(String name, boolean returnNull) - { - String[] ret = this.headers.getHeader(name); - if(ret == null && !returnNull) - { - ret = new String[]{""}; - } - return ret; - } + protected InternetHeaders headers = null; + protected String headerSrc = null; - public String[] getHeader(String name) - { - return getHeader(name, false); - } - - /** - * Sets the header value identified through the header name. - * @param name - * @param value - */ - public void setHeader(String name, String value) - { - this.headers.setHeader(name, value); - this.headerSrc = null; - } + protected ArticleHead() + { + } - public Enumeration getAllHeaders() - { - return this.headers.getAllHeaders(); - } + public ArticleHead(String headers) + { + try { + // Parse the header + this.headers = new InternetHeaders( + new ByteArrayInputStream(headers.getBytes())); + } catch (MessagingException ex) { + ex.printStackTrace(); + } + } - /** - * @return Header source code of this Article. - */ - public String getHeaderSource() - { - if(this.headerSrc != null) - { - return this.headerSrc; - } + /** + * Returns the header field with given name. + * @param name Name of the header field(s). + * @param returnNull If set to true, this method will return null instead + * of an empty array if there is no header field found. + * @return Header values or empty string. + */ + public String[] getHeader(String name, boolean returnNull) + { + String[] ret = this.headers.getHeader(name); + if (ret == null && !returnNull) { + ret = new String[] {""}; + } + return ret; + } - StringBuffer buf = new StringBuffer(); + public String[] getHeader(String name) + { + return getHeader(name, false); + } - for(Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) - { - Header entry = (Header)en.nextElement(); + /** + * Sets the header value identified through the header name. + * @param name + * @param value + */ + public void setHeader(String name, String value) + { + this.headers.setHeader(name, value); + this.headerSrc = null; + } - String value = entry.getValue().replaceAll("[\r\n]", " "); - buf.append(entry.getName()); - buf.append(": "); - buf.append(MimeUtility.fold(entry.getName().length() + 2, value)); + public Enumeration getAllHeaders() + { + return this.headers.getAllHeaders(); + } - if(en.hasMoreElements()) - { - buf.append("\r\n"); - } - } + /** + * @return Header source code of this Article. + */ + public String getHeaderSource() + { + if (this.headerSrc != null) { + return this.headerSrc; + } - this.headerSrc = buf.toString(); - return this.headerSrc; - } + StringBuffer buf = new StringBuffer(); - /** - * Sets the headers of this Article. If headers contain no - * Message-Id a new one is created. - * @param headers - */ - public void setHeaders(InternetHeaders headers) - { - this.headers = headers; - this.headerSrc = null; - validateHeaders(); - } + for (Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) { + Header entry = (Header) en.nextElement(); - /** - * Checks some headers for their validity and generates an - * appropriate Path-header for this host if not yet existing. - * This method is called by some Article constructors and the - * method setHeaders(). - * @return true if something on the headers was changed. - */ - protected void validateHeaders() - { - // Check for valid Path-header - final String path = getHeader(Headers.PATH)[0]; - final String host = Config.inst().get(Config.HOSTNAME, "localhost"); - if(!path.startsWith(host)) - { - StringBuffer pathBuf = new StringBuffer(); - pathBuf.append(host); - pathBuf.append('!'); - pathBuf.append(path); - this.headers.setHeader(Headers.PATH, pathBuf.toString()); - } - } - + String value = entry.getValue().replaceAll("[\r\n]", " "); + buf.append(entry.getName()); + buf.append(": "); + buf.append(MimeUtility.fold(entry.getName().length() + 2, value)); + + if (en.hasMoreElements()) { + buf.append("\r\n"); + } + } + + this.headerSrc = buf.toString(); + return this.headerSrc; + } + + /** + * Sets the headers of this Article. If headers contain no + * Message-Id a new one is created. + * @param headers + */ + public void setHeaders(InternetHeaders headers) + { + this.headers = headers; + this.headerSrc = null; + validateHeaders(); + } + + /** + * Checks some headers for their validity and generates an + * appropriate Path-header for this host if not yet existing. + * This method is called by some Article constructors and the + * method setHeaders(). + * @return true if something on the headers was changed. + */ + protected void validateHeaders() + { + // Check for valid Path-header + final String path = getHeader(Headers.PATH)[0]; + final String host = Config.inst().get(Config.HOSTNAME, "localhost"); + if (!path.startsWith(host)) { + StringBuffer pathBuf = new StringBuffer(); + pathBuf.append(host); + pathBuf.append('!'); + pathBuf.append(path); + this.headers.setHeader(Headers.PATH, pathBuf.toString()); + } + } }