1.1 --- a/src/org/sonews/storage/ArticleHead.java Sun Aug 29 17:28:58 2010 +0200
1.2 +++ b/src/org/sonews/storage/ArticleHead.java Sun Nov 06 00:08:05 2011 +0100
1.3 @@ -31,131 +31,122 @@
1.4 * @author Christian Lins
1.5 * @since sonews/0.5.0
1.6 */
1.7 -public class ArticleHead
1.8 +public class ArticleHead
1.9 {
1.10
1.11 - protected InternetHeaders headers = null;
1.12 - protected String headerSrc = null;
1.13 -
1.14 - protected ArticleHead()
1.15 - {
1.16 - }
1.17 -
1.18 - public ArticleHead(String headers)
1.19 - {
1.20 - try
1.21 - {
1.22 - // Parse the header
1.23 - this.headers = new InternetHeaders(
1.24 - new ByteArrayInputStream(headers.getBytes()));
1.25 - }
1.26 - catch(MessagingException ex)
1.27 - {
1.28 - ex.printStackTrace();
1.29 - }
1.30 - }
1.31 -
1.32 - /**
1.33 - * Returns the header field with given name.
1.34 - * @param name Name of the header field(s).
1.35 - * @param returnNull If set to true, this method will return null instead
1.36 - * of an empty array if there is no header field found.
1.37 - * @return Header values or empty string.
1.38 - */
1.39 - public String[] getHeader(String name, boolean returnNull)
1.40 - {
1.41 - String[] ret = this.headers.getHeader(name);
1.42 - if(ret == null && !returnNull)
1.43 - {
1.44 - ret = new String[]{""};
1.45 - }
1.46 - return ret;
1.47 - }
1.48 + protected InternetHeaders headers = null;
1.49 + protected String headerSrc = null;
1.50
1.51 - public String[] getHeader(String name)
1.52 - {
1.53 - return getHeader(name, false);
1.54 - }
1.55 -
1.56 - /**
1.57 - * Sets the header value identified through the header name.
1.58 - * @param name
1.59 - * @param value
1.60 - */
1.61 - public void setHeader(String name, String value)
1.62 - {
1.63 - this.headers.setHeader(name, value);
1.64 - this.headerSrc = null;
1.65 - }
1.66 + protected ArticleHead()
1.67 + {
1.68 + }
1.69
1.70 - public Enumeration getAllHeaders()
1.71 - {
1.72 - return this.headers.getAllHeaders();
1.73 - }
1.74 + public ArticleHead(String headers)
1.75 + {
1.76 + try {
1.77 + // Parse the header
1.78 + this.headers = new InternetHeaders(
1.79 + new ByteArrayInputStream(headers.getBytes()));
1.80 + } catch (MessagingException ex) {
1.81 + ex.printStackTrace();
1.82 + }
1.83 + }
1.84
1.85 - /**
1.86 - * @return Header source code of this Article.
1.87 - */
1.88 - public String getHeaderSource()
1.89 - {
1.90 - if(this.headerSrc != null)
1.91 - {
1.92 - return this.headerSrc;
1.93 - }
1.94 + /**
1.95 + * Returns the header field with given name.
1.96 + * @param name Name of the header field(s).
1.97 + * @param returnNull If set to true, this method will return null instead
1.98 + * of an empty array if there is no header field found.
1.99 + * @return Header values or empty string.
1.100 + */
1.101 + public String[] getHeader(String name, boolean returnNull)
1.102 + {
1.103 + String[] ret = this.headers.getHeader(name);
1.104 + if (ret == null && !returnNull) {
1.105 + ret = new String[] {""};
1.106 + }
1.107 + return ret;
1.108 + }
1.109
1.110 - StringBuffer buf = new StringBuffer();
1.111 + public String[] getHeader(String name)
1.112 + {
1.113 + return getHeader(name, false);
1.114 + }
1.115
1.116 - for(Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();)
1.117 - {
1.118 - Header entry = (Header)en.nextElement();
1.119 + /**
1.120 + * Sets the header value identified through the header name.
1.121 + * @param name
1.122 + * @param value
1.123 + */
1.124 + public void setHeader(String name, String value)
1.125 + {
1.126 + this.headers.setHeader(name, value);
1.127 + this.headerSrc = null;
1.128 + }
1.129
1.130 - String value = entry.getValue().replaceAll("[\r\n]", " ");
1.131 - buf.append(entry.getName());
1.132 - buf.append(": ");
1.133 - buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
1.134 + public Enumeration getAllHeaders()
1.135 + {
1.136 + return this.headers.getAllHeaders();
1.137 + }
1.138
1.139 - if(en.hasMoreElements())
1.140 - {
1.141 - buf.append("\r\n");
1.142 - }
1.143 - }
1.144 + /**
1.145 + * @return Header source code of this Article.
1.146 + */
1.147 + public String getHeaderSource()
1.148 + {
1.149 + if (this.headerSrc != null) {
1.150 + return this.headerSrc;
1.151 + }
1.152
1.153 - this.headerSrc = buf.toString();
1.154 - return this.headerSrc;
1.155 - }
1.156 + StringBuffer buf = new StringBuffer();
1.157
1.158 - /**
1.159 - * Sets the headers of this Article. If headers contain no
1.160 - * Message-Id a new one is created.
1.161 - * @param headers
1.162 - */
1.163 - public void setHeaders(InternetHeaders headers)
1.164 - {
1.165 - this.headers = headers;
1.166 - this.headerSrc = null;
1.167 - validateHeaders();
1.168 - }
1.169 + for (Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();) {
1.170 + Header entry = (Header) en.nextElement();
1.171
1.172 - /**
1.173 - * Checks some headers for their validity and generates an
1.174 - * appropriate Path-header for this host if not yet existing.
1.175 - * This method is called by some Article constructors and the
1.176 - * method setHeaders().
1.177 - * @return true if something on the headers was changed.
1.178 - */
1.179 - protected void validateHeaders()
1.180 - {
1.181 - // Check for valid Path-header
1.182 - final String path = getHeader(Headers.PATH)[0];
1.183 - final String host = Config.inst().get(Config.HOSTNAME, "localhost");
1.184 - if(!path.startsWith(host))
1.185 - {
1.186 - StringBuffer pathBuf = new StringBuffer();
1.187 - pathBuf.append(host);
1.188 - pathBuf.append('!');
1.189 - pathBuf.append(path);
1.190 - this.headers.setHeader(Headers.PATH, pathBuf.toString());
1.191 - }
1.192 - }
1.193 -
1.194 + String value = entry.getValue().replaceAll("[\r\n]", " ");
1.195 + buf.append(entry.getName());
1.196 + buf.append(": ");
1.197 + buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
1.198 +
1.199 + if (en.hasMoreElements()) {
1.200 + buf.append("\r\n");
1.201 + }
1.202 + }
1.203 +
1.204 + this.headerSrc = buf.toString();
1.205 + return this.headerSrc;
1.206 + }
1.207 +
1.208 + /**
1.209 + * Sets the headers of this Article. If headers contain no
1.210 + * Message-Id a new one is created.
1.211 + * @param headers
1.212 + */
1.213 + public void setHeaders(InternetHeaders headers)
1.214 + {
1.215 + this.headers = headers;
1.216 + this.headerSrc = null;
1.217 + validateHeaders();
1.218 + }
1.219 +
1.220 + /**
1.221 + * Checks some headers for their validity and generates an
1.222 + * appropriate Path-header for this host if not yet existing.
1.223 + * This method is called by some Article constructors and the
1.224 + * method setHeaders().
1.225 + * @return true if something on the headers was changed.
1.226 + */
1.227 + protected void validateHeaders()
1.228 + {
1.229 + // Check for valid Path-header
1.230 + final String path = getHeader(Headers.PATH)[0];
1.231 + final String host = Config.inst().get(Config.HOSTNAME, "localhost");
1.232 + if (!path.startsWith(host)) {
1.233 + StringBuffer pathBuf = new StringBuffer();
1.234 + pathBuf.append(host);
1.235 + pathBuf.append('!');
1.236 + pathBuf.append(path);
1.237 + this.headers.setHeader(Headers.PATH, pathBuf.toString());
1.238 + }
1.239 + }
1.240 }