src/org/sonews/storage/ArticleHead.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 12 Oct 2011 00:25:14 +0200
changeset 73 1feed5fbf147
parent 37 74139325d305
permissions -rwxr-xr-x
Drupal: vyhození zbytečností.
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.storage;
chris@3
    20
chris@3
    21
import java.io.ByteArrayInputStream;
chris@3
    22
import java.util.Enumeration;
chris@3
    23
import javax.mail.Header;
chris@3
    24
import javax.mail.MessagingException;
chris@3
    25
import javax.mail.internet.InternetHeaders;
chris@3
    26
import javax.mail.internet.MimeUtility;
chris@3
    27
import org.sonews.config.Config;
chris@3
    28
chris@3
    29
/**
chris@3
    30
 * An article with no body only headers.
chris@3
    31
 * @author Christian Lins
chris@3
    32
 * @since sonews/0.5.0
chris@3
    33
 */
cli@37
    34
public class ArticleHead
chris@3
    35
{
chris@3
    36
cli@37
    37
	protected InternetHeaders headers = null;
cli@37
    38
	protected String headerSrc = null;
chris@3
    39
cli@37
    40
	protected ArticleHead()
cli@37
    41
	{
cli@37
    42
	}
chris@3
    43
cli@37
    44
	public ArticleHead(String headers)
cli@37
    45
	{
cli@37
    46
		try {
cli@37
    47
			// Parse the header
cli@37
    48
			this.headers = new InternetHeaders(
cli@37
    49
				new ByteArrayInputStream(headers.getBytes()));
cli@37
    50
		} catch (MessagingException ex) {
cli@37
    51
			ex.printStackTrace();
cli@37
    52
		}
cli@37
    53
	}
chris@3
    54
cli@37
    55
	/**
cli@37
    56
	 * Returns the header field with given name.
cli@37
    57
	 * @param name Name of the header field(s).
cli@37
    58
	 * @param returnNull If set to true, this method will return null instead
cli@37
    59
	 *                   of an empty array if there is no header field found.
cli@37
    60
	 * @return Header values or empty string.
cli@37
    61
	 */
cli@37
    62
	public String[] getHeader(String name, boolean returnNull)
cli@37
    63
	{
cli@37
    64
		String[] ret = this.headers.getHeader(name);
cli@37
    65
		if (ret == null && !returnNull) {
cli@37
    66
			ret = new String[] {""};
cli@37
    67
		}
cli@37
    68
		return ret;
cli@37
    69
	}
chris@3
    70
cli@37
    71
	public String[] getHeader(String name)
cli@37
    72
	{
cli@37
    73
		return getHeader(name, false);
cli@37
    74
	}
chris@3
    75
cli@37
    76
	/**
cli@37
    77
	 * Sets the header value identified through the header name.
cli@37
    78
	 * @param name
cli@37
    79
	 * @param value
cli@37
    80
	 */
cli@37
    81
	public void setHeader(String name, String value)
cli@37
    82
	{
cli@37
    83
		this.headers.setHeader(name, value);
cli@37
    84
		this.headerSrc = null;
cli@37
    85
	}
chris@3
    86
cli@37
    87
	public Enumeration getAllHeaders()
cli@37
    88
	{
cli@37
    89
		return this.headers.getAllHeaders();
cli@37
    90
	}
chris@3
    91
cli@37
    92
	/**
cli@37
    93
	 * @return Header source code of this Article.
cli@37
    94
	 */
cli@37
    95
	public String getHeaderSource()
cli@37
    96
	{
cli@37
    97
		if (this.headerSrc != null) {
cli@37
    98
			return this.headerSrc;
cli@37
    99
		}
chris@3
   100
cli@37
   101
		StringBuffer buf = new StringBuffer();
chris@3
   102
cli@37
   103
		for (Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) {
cli@37
   104
			Header entry = (Header) en.nextElement();
chris@3
   105
cli@37
   106
			String value = entry.getValue().replaceAll("[\r\n]", " ");
cli@37
   107
			buf.append(entry.getName());
cli@37
   108
			buf.append(": ");
cli@37
   109
			buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
cli@37
   110
cli@37
   111
			if (en.hasMoreElements()) {
cli@37
   112
				buf.append("\r\n");
cli@37
   113
			}
cli@37
   114
		}
cli@37
   115
cli@37
   116
		this.headerSrc = buf.toString();
cli@37
   117
		return this.headerSrc;
cli@37
   118
	}
cli@37
   119
cli@37
   120
	/**
cli@37
   121
	 * Sets the headers of this Article. If headers contain no
cli@37
   122
	 * Message-Id a new one is created.
cli@37
   123
	 * @param headers
cli@37
   124
	 */
cli@37
   125
	public void setHeaders(InternetHeaders headers)
cli@37
   126
	{
cli@37
   127
		this.headers = headers;
cli@37
   128
		this.headerSrc = null;
cli@37
   129
		validateHeaders();
cli@37
   130
	}
cli@37
   131
cli@37
   132
	/**
cli@37
   133
	 * Checks some headers for their validity and generates an
cli@37
   134
	 * appropriate Path-header for this host if not yet existing.
cli@37
   135
	 * This method is called by some Article constructors and the
cli@37
   136
	 * method setHeaders().
cli@37
   137
	 * @return true if something on the headers was changed.
cli@37
   138
	 */
cli@37
   139
	protected void validateHeaders()
cli@37
   140
	{
cli@37
   141
		// Check for valid Path-header
cli@37
   142
		final String path = getHeader(Headers.PATH)[0];
cli@37
   143
		final String host = Config.inst().get(Config.HOSTNAME, "localhost");
cli@37
   144
		if (!path.startsWith(host)) {
cli@37
   145
			StringBuffer pathBuf = new StringBuffer();
cli@37
   146
			pathBuf.append(host);
cli@37
   147
			pathBuf.append('!');
cli@37
   148
			pathBuf.append(path);
cli@37
   149
			this.headers.setHeader(Headers.PATH, pathBuf.toString());
cli@37
   150
		}
cli@37
   151
	}
chris@3
   152
}