org/sonews/storage/Article.java
changeset 3 2fdc9cc89502
child 12 bb6990c0dd1a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/storage/Article.java	Wed Jul 22 14:04:05 2009 +0200
     1.3 @@ -0,0 +1,336 @@
     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.io.ByteArrayOutputStream;
    1.26 +import java.io.IOException;
    1.27 +import java.io.InputStream;
    1.28 +import java.nio.charset.Charset;
    1.29 +import java.security.MessageDigest;
    1.30 +import java.security.NoSuchAlgorithmException;
    1.31 +import java.util.UUID;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Enumeration;
    1.34 +import java.util.List;
    1.35 +import javax.mail.Header;
    1.36 +import javax.mail.Message;
    1.37 +import javax.mail.MessagingException;
    1.38 +import javax.mail.Multipart;
    1.39 +import javax.mail.internet.InternetHeaders;
    1.40 +import org.sonews.config.Config;
    1.41 +import org.sonews.util.Log;
    1.42 +
    1.43 +/**
    1.44 + * Represents a newsgroup article.
    1.45 + * @author Christian Lins
    1.46 + * @author Denis Schwerdel
    1.47 + * @since n3tpd/0.1
    1.48 + */
    1.49 +public class Article extends ArticleHead
    1.50 +{
    1.51 +  
    1.52 +  /**
    1.53 +   * Loads the Article identified by the given ID from the JDBCDatabase.
    1.54 +   * @param messageID
    1.55 +   * @return null if Article is not found or if an error occurred.
    1.56 +   */
    1.57 +  public static Article getByMessageID(final String messageID)
    1.58 +  {
    1.59 +    try
    1.60 +    {
    1.61 +      return StorageManager.current().getArticle(messageID);
    1.62 +    }
    1.63 +    catch(StorageBackendException ex)
    1.64 +    {
    1.65 +      ex.printStackTrace();
    1.66 +      return null;
    1.67 +    }
    1.68 +  }
    1.69 +  
    1.70 +  private byte[] body       = new byte[0];
    1.71 +  
    1.72 +  /**
    1.73 +   * Default constructor.
    1.74 +   */
    1.75 +  public Article()
    1.76 +  {
    1.77 +  }
    1.78 +  
    1.79 +  /**
    1.80 +   * Creates a new Article object using the date from the given
    1.81 +   * raw data.
    1.82 +   */
    1.83 +  public Article(String headers, byte[] body)
    1.84 +  {
    1.85 +    try
    1.86 +    {
    1.87 +      this.body  = body;
    1.88 +
    1.89 +      // Parse the header
    1.90 +      this.headers = new InternetHeaders(
    1.91 +        new ByteArrayInputStream(headers.getBytes()));
    1.92 +      
    1.93 +      this.headerSrc = headers;
    1.94 +    }
    1.95 +    catch(MessagingException ex)
    1.96 +    {
    1.97 +      ex.printStackTrace();
    1.98 +    }
    1.99 +  }
   1.100 +
   1.101 +  /**
   1.102 +   * Creates an Article instance using the data from the javax.mail.Message
   1.103 +   * object.
   1.104 +   * @see javax.mail.Message
   1.105 +   * @param msg
   1.106 +   * @throws IOException
   1.107 +   * @throws MessagingException
   1.108 +   */
   1.109 +  public Article(final Message msg)
   1.110 +    throws IOException, MessagingException
   1.111 +  {
   1.112 +    this.headers = new InternetHeaders();
   1.113 +
   1.114 +    for(Enumeration e = msg.getAllHeaders() ; e.hasMoreElements();) 
   1.115 +    {
   1.116 +      final Header header = (Header)e.nextElement();
   1.117 +      this.headers.addHeader(header.getName(), header.getValue());
   1.118 +    }
   1.119 +    
   1.120 +    // The "content" of the message can be a String if it's a simple text/plain
   1.121 +    // message, a Multipart object or an InputStream if the content is unknown.
   1.122 +    final Object content = msg.getContent();
   1.123 +    if(content instanceof String)
   1.124 +    {
   1.125 +      this.body = ((String)content).getBytes();
   1.126 +    }
   1.127 +    else if(content instanceof Multipart) // probably subclass MimeMultipart
   1.128 +    {
   1.129 +      // We're are not interested in the different parts of the MultipartMessage,
   1.130 +      // so we simply read in all data which *can* be huge.
   1.131 +      InputStream in = msg.getInputStream();
   1.132 +      this.body = readContent(in);
   1.133 +    }
   1.134 +    else if(content instanceof InputStream)
   1.135 +    {
   1.136 +      // The message format is unknown to the Message class, but we can
   1.137 +      // simply read in the whole message data.
   1.138 +      this.body = readContent((InputStream)content);
   1.139 +    }
   1.140 +    else
   1.141 +    {
   1.142 +      // Unknown content is probably a malformed mail we should skip.
   1.143 +      // On the other hand we produce an inconsistent mail mirror, but no
   1.144 +      // mail system must transport invalid content.
   1.145 +      Log.msg("Skipping message due to unknown content. Throwing exception...", true);
   1.146 +      throw new MessagingException("Unknown content: " + content);
   1.147 +    }
   1.148 +    
   1.149 +    // Validate headers
   1.150 +    validateHeaders();
   1.151 +  }
   1.152 +
   1.153 +  /**
   1.154 +   * Reads from the given InputString into a byte array.
   1.155 +   * TODO: Move this generalized method to org.sonews.util.io.Resource.
   1.156 +   * @param in
   1.157 +   * @return
   1.158 +   * @throws IOException
   1.159 +   */
   1.160 +  private byte[] readContent(InputStream in)
   1.161 +    throws IOException
   1.162 +  {
   1.163 +    ByteArrayOutputStream out = new ByteArrayOutputStream();
   1.164 +
   1.165 +    int b = in.read();
   1.166 +    while(b >= 0)
   1.167 +    {
   1.168 +      out.write(b);
   1.169 +      b = in.read();
   1.170 +    }
   1.171 +
   1.172 +    return out.toByteArray();
   1.173 +  }
   1.174 +
   1.175 +  /**
   1.176 +   * Removes the header identified by the given key.
   1.177 +   * @param headerKey
   1.178 +   */
   1.179 +  public void removeHeader(final String headerKey)
   1.180 +  {
   1.181 +    this.headers.removeHeader(headerKey);
   1.182 +    this.headerSrc = null;
   1.183 +  }
   1.184 +
   1.185 +  /**
   1.186 +   * Generates a message id for this article and sets it into
   1.187 +   * the header object. You have to update the JDBCDatabase manually to make this
   1.188 +   * change persistent.
   1.189 +   * Note: a Message-ID should never be changed and only generated once.
   1.190 +   */
   1.191 +  private String generateMessageID()
   1.192 +  {
   1.193 +    String randomString;
   1.194 +    MessageDigest md5;
   1.195 +    try
   1.196 +    {
   1.197 +      md5 = MessageDigest.getInstance("MD5");
   1.198 +      md5.reset();
   1.199 +      md5.update(getBody());
   1.200 +      md5.update(getHeader(Headers.SUBJECT)[0].getBytes());
   1.201 +      md5.update(getHeader(Headers.FROM)[0].getBytes());
   1.202 +      byte[] result = md5.digest();
   1.203 +      StringBuffer hexString = new StringBuffer();
   1.204 +      for (int i = 0; i < result.length; i++)
   1.205 +      {
   1.206 +        hexString.append(Integer.toHexString(0xFF & result[i]));
   1.207 +      }
   1.208 +      randomString = hexString.toString();
   1.209 +    }
   1.210 +    catch (NoSuchAlgorithmException e)
   1.211 +    {
   1.212 +      e.printStackTrace();
   1.213 +      randomString = UUID.randomUUID().toString();
   1.214 +    }
   1.215 +    String msgID = "<" + randomString + "@"
   1.216 +        + Config.inst().get(Config.HOSTNAME, "localhost") + ">";
   1.217 +    
   1.218 +    this.headers.setHeader(Headers.MESSAGE_ID, msgID);
   1.219 +    
   1.220 +    return msgID;
   1.221 +  }
   1.222 +
   1.223 +  /**
   1.224 +   * Returns the body string.
   1.225 +   */
   1.226 +  public byte[] getBody()
   1.227 +  {
   1.228 +    return body;
   1.229 +  }
   1.230 +
   1.231 +  /**
   1.232 +   * @return Charset of the body text
   1.233 +   */
   1.234 +  private Charset getBodyCharset()
   1.235 +  {
   1.236 +    // We espect something like 
   1.237 +    // Content-Type: text/plain; charset=ISO-8859-15
   1.238 +    String contentType = getHeader(Headers.CONTENT_TYPE)[0];
   1.239 +    int idxCharsetStart = contentType.indexOf("charset=") + "charset=".length();
   1.240 +    int idxCharsetEnd   = contentType.indexOf(";", idxCharsetStart);
   1.241 +    
   1.242 +    String charsetName = "UTF-8";
   1.243 +    if(idxCharsetStart >= 0 && idxCharsetStart < contentType.length())
   1.244 +    {
   1.245 +      if(idxCharsetEnd < 0)
   1.246 +      {
   1.247 +        charsetName = contentType.substring(idxCharsetStart);
   1.248 +      }
   1.249 +      else
   1.250 +      {
   1.251 +        charsetName = contentType.substring(idxCharsetStart, idxCharsetEnd);
   1.252 +      }
   1.253 +    }
   1.254 +    
   1.255 +    // Sometimes there are '"' around the name
   1.256 +    if(charsetName.length() > 2 &&
   1.257 +      charsetName.charAt(0) == '"' && charsetName.endsWith("\""))
   1.258 +    {
   1.259 +      charsetName = charsetName.substring(1, charsetName.length() - 2);
   1.260 +    }
   1.261 +    
   1.262 +    // Create charset
   1.263 +    Charset charset = Charset.forName("UTF-8"); // This MUST be supported by JVM
   1.264 +    try
   1.265 +    {
   1.266 +      charset = Charset.forName(charsetName);
   1.267 +    }
   1.268 +    catch(Exception ex)
   1.269 +    {
   1.270 +      Log.msg(ex.getMessage(), false);
   1.271 +      Log.msg("Article.getBodyCharset(): Unknown charset: " + charsetName, false);
   1.272 +    }
   1.273 +    return charset;
   1.274 +  }
   1.275 +  
   1.276 +  /**
   1.277 +   * @return Numerical IDs of the newsgroups this Article belongs to.
   1.278 +   */
   1.279 +  public List<Group> getGroups()
   1.280 +  {
   1.281 +    String[]         groupnames = getHeader(Headers.NEWSGROUPS)[0].split(",");
   1.282 +    ArrayList<Group> groups     = new ArrayList<Group>();
   1.283 +
   1.284 +    try
   1.285 +    {
   1.286 +      for(String newsgroup : groupnames)
   1.287 +      {
   1.288 +        newsgroup = newsgroup.trim();
   1.289 +        Group group = StorageManager.current().getGroup(newsgroup);
   1.290 +        if(group != null &&         // If the server does not provide the group, ignore it
   1.291 +          !groups.contains(group))  // Yes, there may be duplicates
   1.292 +        {
   1.293 +          groups.add(group);
   1.294 +        }
   1.295 +      }
   1.296 +    }
   1.297 +    catch(StorageBackendException ex)
   1.298 +    {
   1.299 +      ex.printStackTrace();
   1.300 +      return null;
   1.301 +    }
   1.302 +    return groups;
   1.303 +  }
   1.304 +
   1.305 +  public void setBody(byte[] body)
   1.306 +  {
   1.307 +    this.body = body;
   1.308 +  }
   1.309 +  
   1.310 +  /**
   1.311 +   * 
   1.312 +   * @param groupname Name(s) of newsgroups
   1.313 +   */
   1.314 +  public void setGroup(String groupname)
   1.315 +  {
   1.316 +    this.headers.setHeader(Headers.NEWSGROUPS, groupname);
   1.317 +  }
   1.318 +
   1.319 +  /**
   1.320 +   * Returns the Message-ID of this Article. If the appropriate header
   1.321 +   * is empty, a new Message-ID is created.
   1.322 +   * @return Message-ID of this Article.
   1.323 +   */
   1.324 +  public String getMessageID()
   1.325 +  {
   1.326 +    String[] msgID = getHeader(Headers.MESSAGE_ID);
   1.327 +    return msgID[0].equals("") ? generateMessageID() : msgID[0];
   1.328 +  }
   1.329 +  
   1.330 +  /**
   1.331 +   * @return String containing the Message-ID.
   1.332 +   */
   1.333 +  @Override
   1.334 +  public String toString()
   1.335 +  {
   1.336 +    return getMessageID();
   1.337 +  }
   1.338 +
   1.339 +}