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