src/org/sonews/storage/ArticleHead.java
author cli
Mon, 30 Aug 2010 00:20:06 +0200
changeset 39 73b21e9f3958
parent 37 74139325d305
permissions -rw-r--r--
Some work on XDAEMON command.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 package org.sonews.storage;
    20 
    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;
    28 
    29 /**
    30  * An article with no body only headers.
    31  * @author Christian Lins
    32  * @since sonews/0.5.0
    33  */
    34 public class ArticleHead
    35 {
    36 
    37 	protected InternetHeaders headers = null;
    38 	protected String headerSrc = null;
    39 
    40 	protected ArticleHead()
    41 	{
    42 	}
    43 
    44 	public ArticleHead(String headers)
    45 	{
    46 		try {
    47 			// Parse the header
    48 			this.headers = new InternetHeaders(
    49 				new ByteArrayInputStream(headers.getBytes()));
    50 		} catch (MessagingException ex) {
    51 			ex.printStackTrace();
    52 		}
    53 	}
    54 
    55 	/**
    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.
    61 	 */
    62 	public String[] getHeader(String name, boolean returnNull)
    63 	{
    64 		String[] ret = this.headers.getHeader(name);
    65 		if (ret == null && !returnNull) {
    66 			ret = new String[] {""};
    67 		}
    68 		return ret;
    69 	}
    70 
    71 	public String[] getHeader(String name)
    72 	{
    73 		return getHeader(name, false);
    74 	}
    75 
    76 	/**
    77 	 * Sets the header value identified through the header name.
    78 	 * @param name
    79 	 * @param value
    80 	 */
    81 	public void setHeader(String name, String value)
    82 	{
    83 		this.headers.setHeader(name, value);
    84 		this.headerSrc = null;
    85 	}
    86 
    87 	public Enumeration getAllHeaders()
    88 	{
    89 		return this.headers.getAllHeaders();
    90 	}
    91 
    92 	/**
    93 	 * @return Header source code of this Article.
    94 	 */
    95 	public String getHeaderSource()
    96 	{
    97 		if (this.headerSrc != null) {
    98 			return this.headerSrc;
    99 		}
   100 
   101 		StringBuffer buf = new StringBuffer();
   102 
   103 		for (Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) {
   104 			Header entry = (Header) en.nextElement();
   105 
   106 			String value = entry.getValue().replaceAll("[\r\n]", " ");
   107 			buf.append(entry.getName());
   108 			buf.append(": ");
   109 			buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
   110 
   111 			if (en.hasMoreElements()) {
   112 				buf.append("\r\n");
   113 			}
   114 		}
   115 
   116 		this.headerSrc = buf.toString();
   117 		return this.headerSrc;
   118 	}
   119 
   120 	/**
   121 	 * Sets the headers of this Article. If headers contain no
   122 	 * Message-Id a new one is created.
   123 	 * @param headers
   124 	 */
   125 	public void setHeaders(InternetHeaders headers)
   126 	{
   127 		this.headers = headers;
   128 		this.headerSrc = null;
   129 		validateHeaders();
   130 	}
   131 
   132 	/**
   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.
   138 	 */
   139 	protected void validateHeaders()
   140 	{
   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);
   147 			pathBuf.append('!');
   148 			pathBuf.append(path);
   149 			this.headers.setHeader(Headers.PATH, pathBuf.toString());
   150 		}
   151 	}
   152 }