1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/storage/ArticleHead.java Thu Aug 20 14:31:19 2009 +0200
1.3 @@ -0,0 +1,161 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.storage;
1.23 +
1.24 +import java.io.ByteArrayInputStream;
1.25 +import java.util.Enumeration;
1.26 +import javax.mail.Header;
1.27 +import javax.mail.MessagingException;
1.28 +import javax.mail.internet.InternetHeaders;
1.29 +import javax.mail.internet.MimeUtility;
1.30 +import org.sonews.config.Config;
1.31 +
1.32 +/**
1.33 + * An article with no body only headers.
1.34 + * @author Christian Lins
1.35 + * @since sonews/0.5.0
1.36 + */
1.37 +public class ArticleHead
1.38 +{
1.39 +
1.40 + protected InternetHeaders headers = null;
1.41 + protected String headerSrc = null;
1.42 +
1.43 + protected ArticleHead()
1.44 + {
1.45 + }
1.46 +
1.47 + public ArticleHead(String headers)
1.48 + {
1.49 + try
1.50 + {
1.51 + // Parse the header
1.52 + this.headers = new InternetHeaders(
1.53 + new ByteArrayInputStream(headers.getBytes()));
1.54 + }
1.55 + catch(MessagingException ex)
1.56 + {
1.57 + ex.printStackTrace();
1.58 + }
1.59 + }
1.60 +
1.61 + /**
1.62 + * Returns the header field with given name.
1.63 + * @param name Name of the header field(s).
1.64 + * @param returnNull If set to true, this method will return null instead
1.65 + * of an empty array if there is no header field found.
1.66 + * @return Header values or empty string.
1.67 + */
1.68 + public String[] getHeader(String name, boolean returnNull)
1.69 + {
1.70 + String[] ret = this.headers.getHeader(name);
1.71 + if(ret == null && !returnNull)
1.72 + {
1.73 + ret = new String[]{""};
1.74 + }
1.75 + return ret;
1.76 + }
1.77 +
1.78 + public String[] getHeader(String name)
1.79 + {
1.80 + return getHeader(name, false);
1.81 + }
1.82 +
1.83 + /**
1.84 + * Sets the header value identified through the header name.
1.85 + * @param name
1.86 + * @param value
1.87 + */
1.88 + public void setHeader(String name, String value)
1.89 + {
1.90 + this.headers.setHeader(name, value);
1.91 + this.headerSrc = null;
1.92 + }
1.93 +
1.94 + public Enumeration getAllHeaders()
1.95 + {
1.96 + return this.headers.getAllHeaders();
1.97 + }
1.98 +
1.99 + /**
1.100 + * @return Header source code of this Article.
1.101 + */
1.102 + public String getHeaderSource()
1.103 + {
1.104 + if(this.headerSrc != null)
1.105 + {
1.106 + return this.headerSrc;
1.107 + }
1.108 +
1.109 + StringBuffer buf = new StringBuffer();
1.110 +
1.111 + for(Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();)
1.112 + {
1.113 + Header entry = (Header)en.nextElement();
1.114 +
1.115 + String value = entry.getValue().replaceAll("[\r\n]", " ");
1.116 + buf.append(entry.getName());
1.117 + buf.append(": ");
1.118 + buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
1.119 +
1.120 + if(en.hasMoreElements())
1.121 + {
1.122 + buf.append("\r\n");
1.123 + }
1.124 + }
1.125 +
1.126 + this.headerSrc = buf.toString();
1.127 + return this.headerSrc;
1.128 + }
1.129 +
1.130 + /**
1.131 + * Sets the headers of this Article. If headers contain no
1.132 + * Message-Id a new one is created.
1.133 + * @param headers
1.134 + */
1.135 + public void setHeaders(InternetHeaders headers)
1.136 + {
1.137 + this.headers = headers;
1.138 + this.headerSrc = null;
1.139 + validateHeaders();
1.140 + }
1.141 +
1.142 + /**
1.143 + * Checks some headers for their validity and generates an
1.144 + * appropriate Path-header for this host if not yet existing.
1.145 + * This method is called by some Article constructors and the
1.146 + * method setHeaders().
1.147 + * @return true if something on the headers was changed.
1.148 + */
1.149 + protected void validateHeaders()
1.150 + {
1.151 + // Check for valid Path-header
1.152 + final String path = getHeader(Headers.PATH)[0];
1.153 + final String host = Config.inst().get(Config.HOSTNAME, "localhost");
1.154 + if(!path.startsWith(host))
1.155 + {
1.156 + StringBuffer pathBuf = new StringBuffer();
1.157 + pathBuf.append(host);
1.158 + pathBuf.append('!');
1.159 + pathBuf.append(path);
1.160 + this.headers.setHeader(Headers.PATH, pathBuf.toString());
1.161 + }
1.162 + }
1.163 +
1.164 +}