src/org/sonews/storage/ArticleHead.java
author cli
Sun, 29 Aug 2010 17:43:58 +0200
changeset 36 c404a87db5b7
parent 35 ed84c8bdd87b
child 37 74139325d305
permissions -rw-r--r--
Add some checks to prevent #13 happen.
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
 */
chris@3
    34
public class ArticleHead 
chris@3
    35
{
chris@3
    36
chris@3
    37
  protected InternetHeaders headers   = null;
chris@3
    38
  protected String          headerSrc = null;
chris@3
    39
  
chris@3
    40
  protected ArticleHead()
chris@3
    41
  {
chris@3
    42
  }
chris@3
    43
  
chris@3
    44
  public ArticleHead(String headers)
chris@3
    45
  {
chris@3
    46
    try
chris@3
    47
    {
chris@3
    48
      // Parse the header
chris@3
    49
      this.headers = new InternetHeaders(
chris@3
    50
          new ByteArrayInputStream(headers.getBytes()));
chris@3
    51
    }
chris@3
    52
    catch(MessagingException ex)
chris@3
    53
    {
chris@3
    54
      ex.printStackTrace();
chris@3
    55
    }
chris@3
    56
  }
chris@3
    57
  
chris@3
    58
  /**
chris@3
    59
   * Returns the header field with given name.
chris@3
    60
   * @param name Name of the header field(s).
chris@3
    61
   * @param returnNull If set to true, this method will return null instead
chris@3
    62
   *                   of an empty array if there is no header field found.
chris@3
    63
   * @return Header values or empty string.
chris@3
    64
   */
chris@3
    65
  public String[] getHeader(String name, boolean returnNull)
chris@3
    66
  {
chris@3
    67
    String[] ret = this.headers.getHeader(name);
chris@3
    68
    if(ret == null && !returnNull)
chris@3
    69
    {
chris@3
    70
      ret = new String[]{""};
chris@3
    71
    }
chris@3
    72
    return ret;
chris@3
    73
  }
chris@3
    74
chris@3
    75
  public String[] getHeader(String name)
chris@3
    76
  {
chris@3
    77
    return getHeader(name, false);
chris@3
    78
  }
chris@3
    79
  
chris@3
    80
  /**
chris@3
    81
   * Sets the header value identified through the header name.
chris@3
    82
   * @param name
chris@3
    83
   * @param value
chris@3
    84
   */
chris@3
    85
  public void setHeader(String name, String value)
chris@3
    86
  {
chris@3
    87
    this.headers.setHeader(name, value);
chris@3
    88
    this.headerSrc = null;
chris@3
    89
  }
chris@3
    90
chris@3
    91
    public Enumeration getAllHeaders()
chris@3
    92
  {
chris@3
    93
    return this.headers.getAllHeaders();
chris@3
    94
  }
chris@3
    95
chris@3
    96
  /**
chris@3
    97
   * @return Header source code of this Article.
chris@3
    98
   */
chris@3
    99
  public String getHeaderSource()
chris@3
   100
  {
chris@3
   101
    if(this.headerSrc != null)
chris@3
   102
    {
chris@3
   103
      return this.headerSrc;
chris@3
   104
    }
chris@3
   105
chris@3
   106
    StringBuffer buf = new StringBuffer();
chris@3
   107
chris@3
   108
    for(Enumeration en = this.headers.getAllHeaders(); en.hasMoreElements();)
chris@3
   109
    {
chris@3
   110
      Header entry = (Header)en.nextElement();
chris@3
   111
chris@3
   112
      String value = entry.getValue().replaceAll("[\r\n]", " ");
chris@3
   113
      buf.append(entry.getName());
chris@3
   114
      buf.append(": ");
chris@3
   115
      buf.append(MimeUtility.fold(entry.getName().length() + 2, value));
chris@3
   116
chris@3
   117
      if(en.hasMoreElements())
chris@3
   118
      {
chris@3
   119
        buf.append("\r\n");
chris@3
   120
      }
chris@3
   121
    }
chris@3
   122
chris@3
   123
    this.headerSrc = buf.toString();
chris@3
   124
    return this.headerSrc;
chris@3
   125
  }
chris@3
   126
chris@3
   127
  /**
chris@3
   128
   * Sets the headers of this Article. If headers contain no
chris@3
   129
   * Message-Id a new one is created.
chris@3
   130
   * @param headers
chris@3
   131
   */
chris@3
   132
  public void setHeaders(InternetHeaders headers)
chris@3
   133
  {
chris@3
   134
    this.headers   = headers;
chris@3
   135
    this.headerSrc = null;
chris@3
   136
    validateHeaders();
chris@3
   137
  }
chris@3
   138
cli@18
   139
  /**
chris@3
   140
   * Checks some headers for their validity and generates an
chris@3
   141
   * appropriate Path-header for this host if not yet existing.
chris@3
   142
   * This method is called by some Article constructors and the
chris@3
   143
   * method setHeaders().
chris@3
   144
   * @return true if something on the headers was changed.
chris@3
   145
   */
chris@3
   146
  protected void validateHeaders()
chris@3
   147
  {
chris@3
   148
    // Check for valid Path-header
chris@3
   149
    final String path = getHeader(Headers.PATH)[0];
chris@3
   150
    final String host = Config.inst().get(Config.HOSTNAME, "localhost");
chris@3
   151
    if(!path.startsWith(host))
chris@3
   152
    {
chris@3
   153
      StringBuffer pathBuf = new StringBuffer();
chris@3
   154
      pathBuf.append(host);
chris@3
   155
      pathBuf.append('!');
chris@3
   156
      pathBuf.append(path);
chris@3
   157
      this.headers.setHeader(Headers.PATH, pathBuf.toString());
chris@3
   158
    }
chris@3
   159
  }
chris@3
   160
  
chris@3
   161
}