org/sonews/storage/Article.java
author cli
Sun, 29 Aug 2010 17:03:21 +0200
changeset 33 f9bf183447d1
parent 16 5a4a41cfc0a3
permissions -rw-r--r--
Article(javax.mail.Message) now has a safe method to read the byte body from the given message object (fixes #16).
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.io.ByteArrayOutputStream;
chris@3
    23
import java.io.IOException;
chris@3
    24
import java.security.MessageDigest;
chris@3
    25
import java.security.NoSuchAlgorithmException;
chris@3
    26
import java.util.UUID;
chris@3
    27
import java.util.ArrayList;
chris@3
    28
import java.util.Enumeration;
chris@3
    29
import java.util.List;
chris@3
    30
import javax.mail.Header;
chris@3
    31
import javax.mail.Message;
chris@3
    32
import javax.mail.MessagingException;
chris@3
    33
import javax.mail.internet.InternetHeaders;
chris@3
    34
import org.sonews.config.Config;
chris@3
    35
chris@3
    36
/**
chris@3
    37
 * Represents a newsgroup article.
chris@3
    38
 * @author Christian Lins
chris@3
    39
 * @author Denis Schwerdel
chris@3
    40
 * @since n3tpd/0.1
chris@3
    41
 */
chris@3
    42
public class Article extends ArticleHead
chris@3
    43
{
chris@3
    44
  
chris@3
    45
  /**
chris@3
    46
   * Loads the Article identified by the given ID from the JDBCDatabase.
chris@3
    47
   * @param messageID
chris@3
    48
   * @return null if Article is not found or if an error occurred.
chris@3
    49
   */
chris@3
    50
  public static Article getByMessageID(final String messageID)
chris@3
    51
  {
chris@3
    52
    try
chris@3
    53
    {
chris@3
    54
      return StorageManager.current().getArticle(messageID);
chris@3
    55
    }
chris@3
    56
    catch(StorageBackendException ex)
chris@3
    57
    {
chris@3
    58
      ex.printStackTrace();
chris@3
    59
      return null;
chris@3
    60
    }
chris@3
    61
  }
chris@3
    62
  
chris@3
    63
  private byte[] body       = new byte[0];
chris@3
    64
  
chris@3
    65
  /**
chris@3
    66
   * Default constructor.
chris@3
    67
   */
chris@3
    68
  public Article()
chris@3
    69
  {
chris@3
    70
  }
chris@3
    71
  
chris@3
    72
  /**
chris@3
    73
   * Creates a new Article object using the date from the given
chris@3
    74
   * raw data.
chris@3
    75
   */
chris@3
    76
  public Article(String headers, byte[] body)
chris@3
    77
  {
chris@3
    78
    try
chris@3
    79
    {
chris@3
    80
      this.body  = body;
chris@3
    81
chris@3
    82
      // Parse the header
chris@3
    83
      this.headers = new InternetHeaders(
chris@3
    84
        new ByteArrayInputStream(headers.getBytes()));
chris@3
    85
      
chris@3
    86
      this.headerSrc = headers;
chris@3
    87
    }
chris@3
    88
    catch(MessagingException ex)
chris@3
    89
    {
chris@3
    90
      ex.printStackTrace();
chris@3
    91
    }
chris@3
    92
  }
chris@3
    93
chris@3
    94
  /**
chris@3
    95
   * Creates an Article instance using the data from the javax.mail.Message
cli@33
    96
   * object. This constructor is called by the Mailinglist gateway.
chris@3
    97
   * @see javax.mail.Message
chris@3
    98
   * @param msg
chris@3
    99
   * @throws IOException
chris@3
   100
   * @throws MessagingException
chris@3
   101
   */
chris@3
   102
  public Article(final Message msg)
chris@3
   103
    throws IOException, MessagingException
chris@3
   104
  {
chris@3
   105
    this.headers = new InternetHeaders();
chris@3
   106
chris@3
   107
    for(Enumeration e = msg.getAllHeaders() ; e.hasMoreElements();) 
chris@3
   108
    {
chris@3
   109
      final Header header = (Header)e.nextElement();
chris@3
   110
      this.headers.addHeader(header.getName(), header.getValue());
chris@3
   111
    }
cli@33
   112
cli@33
   113
	// Reads the raw byte body using Message.writeTo(OutputStream out)
cli@33
   114
	this.body = readContent(msg);
chris@3
   115
    
chris@3
   116
    // Validate headers
chris@3
   117
    validateHeaders();
chris@3
   118
  }
chris@3
   119
chris@3
   120
  /**
cli@33
   121
   * Reads from the given Message into a byte array.
chris@3
   122
   * @param in
chris@3
   123
   * @return
chris@3
   124
   * @throws IOException
chris@3
   125
   */
cli@33
   126
  private byte[] readContent(Message in)
cli@33
   127
    throws IOException, MessagingException
chris@3
   128
  {
chris@3
   129
    ByteArrayOutputStream out = new ByteArrayOutputStream();
cli@33
   130
    in.writeTo(out);
chris@3
   131
    return out.toByteArray();
chris@3
   132
  }
chris@3
   133
chris@3
   134
  /**
chris@3
   135
   * Removes the header identified by the given key.
chris@3
   136
   * @param headerKey
chris@3
   137
   */
chris@3
   138
  public void removeHeader(final String headerKey)
chris@3
   139
  {
chris@3
   140
    this.headers.removeHeader(headerKey);
chris@3
   141
    this.headerSrc = null;
chris@3
   142
  }
chris@3
   143
chris@3
   144
  /**
chris@3
   145
   * Generates a message id for this article and sets it into
chris@3
   146
   * the header object. You have to update the JDBCDatabase manually to make this
chris@3
   147
   * change persistent.
chris@3
   148
   * Note: a Message-ID should never be changed and only generated once.
chris@3
   149
   */
chris@3
   150
  private String generateMessageID()
chris@3
   151
  {
chris@3
   152
    String randomString;
chris@3
   153
    MessageDigest md5;
chris@3
   154
    try
chris@3
   155
    {
chris@3
   156
      md5 = MessageDigest.getInstance("MD5");
chris@3
   157
      md5.reset();
chris@3
   158
      md5.update(getBody());
chris@3
   159
      md5.update(getHeader(Headers.SUBJECT)[0].getBytes());
chris@3
   160
      md5.update(getHeader(Headers.FROM)[0].getBytes());
chris@3
   161
      byte[] result = md5.digest();
chris@3
   162
      StringBuffer hexString = new StringBuffer();
chris@3
   163
      for (int i = 0; i < result.length; i++)
chris@3
   164
      {
chris@3
   165
        hexString.append(Integer.toHexString(0xFF & result[i]));
chris@3
   166
      }
chris@3
   167
      randomString = hexString.toString();
chris@3
   168
    }
chris@3
   169
    catch (NoSuchAlgorithmException e)
chris@3
   170
    {
chris@3
   171
      e.printStackTrace();
chris@3
   172
      randomString = UUID.randomUUID().toString();
chris@3
   173
    }
chris@3
   174
    String msgID = "<" + randomString + "@"
chris@3
   175
        + Config.inst().get(Config.HOSTNAME, "localhost") + ">";
chris@3
   176
    
chris@3
   177
    this.headers.setHeader(Headers.MESSAGE_ID, msgID);
chris@3
   178
    
chris@3
   179
    return msgID;
chris@3
   180
  }
chris@3
   181
chris@3
   182
  /**
chris@3
   183
   * Returns the body string.
chris@3
   184
   */
chris@3
   185
  public byte[] getBody()
chris@3
   186
  {
chris@3
   187
    return body;
chris@3
   188
  }
chris@3
   189
  
chris@3
   190
  /**
chris@3
   191
   * @return Numerical IDs of the newsgroups this Article belongs to.
chris@3
   192
   */
chris@3
   193
  public List<Group> getGroups()
chris@3
   194
  {
chris@3
   195
    String[]         groupnames = getHeader(Headers.NEWSGROUPS)[0].split(",");
chris@3
   196
    ArrayList<Group> groups     = new ArrayList<Group>();
chris@3
   197
chris@3
   198
    try
chris@3
   199
    {
chris@3
   200
      for(String newsgroup : groupnames)
chris@3
   201
      {
chris@3
   202
        newsgroup = newsgroup.trim();
chris@3
   203
        Group group = StorageManager.current().getGroup(newsgroup);
chris@3
   204
        if(group != null &&         // If the server does not provide the group, ignore it
chris@3
   205
          !groups.contains(group))  // Yes, there may be duplicates
chris@3
   206
        {
chris@3
   207
          groups.add(group);
chris@3
   208
        }
chris@3
   209
      }
chris@3
   210
    }
chris@3
   211
    catch(StorageBackendException ex)
chris@3
   212
    {
chris@3
   213
      ex.printStackTrace();
chris@3
   214
      return null;
chris@3
   215
    }
chris@3
   216
    return groups;
chris@3
   217
  }
chris@3
   218
chris@3
   219
  public void setBody(byte[] body)
chris@3
   220
  {
chris@3
   221
    this.body = body;
chris@3
   222
  }
chris@3
   223
  
chris@3
   224
  /**
chris@3
   225
   * 
chris@3
   226
   * @param groupname Name(s) of newsgroups
chris@3
   227
   */
chris@3
   228
  public void setGroup(String groupname)
chris@3
   229
  {
chris@3
   230
    this.headers.setHeader(Headers.NEWSGROUPS, groupname);
chris@3
   231
  }
chris@3
   232
chris@3
   233
  /**
chris@3
   234
   * Returns the Message-ID of this Article. If the appropriate header
chris@3
   235
   * is empty, a new Message-ID is created.
chris@3
   236
   * @return Message-ID of this Article.
chris@3
   237
   */
chris@3
   238
  public String getMessageID()
chris@3
   239
  {
chris@3
   240
    String[] msgID = getHeader(Headers.MESSAGE_ID);
chris@3
   241
    return msgID[0].equals("") ? generateMessageID() : msgID[0];
chris@3
   242
  }
chris@3
   243
  
chris@3
   244
  /**
chris@3
   245
   * @return String containing the Message-ID.
chris@3
   246
   */
chris@3
   247
  @Override
chris@3
   248
  public String toString()
chris@3
   249
  {
chris@3
   250
    return getMessageID();
chris@3
   251
  }
chris@3
   252
chris@3
   253
}