trunk/com/so/news/command/ArticleCommand.java
author chris <chris@marvin>
Tue, 20 Jan 2009 10:21:03 +0100
changeset 0 f907866f0e4b
permissions -rw-r--r--
Initial import.
chris@0
     1
/*
chris@0
     2
 *   StarOffice News Server
chris@0
     3
 *   see AUTHORS for the list of contributors
chris@0
     4
 *
chris@0
     5
 *   This program is free software: you can redistribute it and/or modify
chris@0
     6
 *   it under the terms of the GNU General Public License as published by
chris@0
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@0
     8
 *   (at your option) any later version.
chris@0
     9
 *
chris@0
    10
 *   This program is distributed in the hope that it will be useful,
chris@0
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@0
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@0
    13
 *   GNU General Public License for more details.
chris@0
    14
 *
chris@0
    15
 *   You should have received a copy of the GNU General Public License
chris@0
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@0
    17
 */
chris@0
    18
chris@0
    19
package com.so.news.command;
chris@0
    20
chris@0
    21
import java.io.IOException;
chris@0
    22
import java.text.SimpleDateFormat;
chris@0
    23
import java.util.Locale;
chris@0
    24
import java.util.Map;
chris@0
    25
import com.so.news.Debug;
chris@0
    26
import com.so.news.NNTPConnection;
chris@0
    27
import com.so.news.storage.Article;
chris@0
    28
chris@0
    29
/**
chris@0
    30
 * Class handling the ARTICLE command.
chris@0
    31
 * @author Christian Lins
chris@0
    32
 * @author Dennis Schwerdel
chris@0
    33
 */
chris@0
    34
public class ArticleCommand extends Command
chris@0
    35
{
chris@0
    36
  public ArticleCommand(NNTPConnection connection)
chris@0
    37
  {
chris@0
    38
    super(connection);
chris@0
    39
  }
chris@0
    40
chris@0
    41
  public boolean process(String[] command) throws IOException
chris@0
    42
  {
chris@0
    43
    String commandName = command[0];
chris@0
    44
chris@0
    45
    // untested, RFC977 compliant
chris@0
    46
    Article article = null;
chris@0
    47
    if (command.length <= 1)
chris@0
    48
    {
chris@0
    49
      article = getCurrentArticle();
chris@0
    50
      if (article == null)
chris@0
    51
      {
chris@0
    52
        printStatus(420, "no current article has been selected");
chris@0
    53
        return true;
chris@0
    54
      }
chris@0
    55
    }
chris@0
    56
    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
chris@0
    57
    {
chris@0
    58
      // Message-ID
chris@0
    59
      article = Article.getByMessageID(command[1]);
chris@0
    60
      if (article == null)
chris@0
    61
      {
chris@0
    62
        printStatus(430, "no such article found");
chris@0
    63
        return true;
chris@0
    64
      }
chris@0
    65
    }
chris@0
    66
    else
chris@0
    67
    {
chris@0
    68
      // Message Number
chris@0
    69
      try
chris@0
    70
      {
chris@0
    71
        int num = Integer.parseInt(command[1]);
chris@0
    72
        article = Article.getByNumberInGroup(connection.getCurrentGroup(), num);
chris@0
    73
      }
chris@0
    74
      catch (Exception ex)
chris@0
    75
      {
chris@0
    76
        ex.printStackTrace(Debug.getInstance().getStream());
chris@0
    77
        System.err.println(ex.getLocalizedMessage());
chris@0
    78
      }
chris@0
    79
      if (article == null)
chris@0
    80
      {
chris@0
    81
        printStatus(423, "no such article number in this group");
chris@0
    82
        return true;
chris@0
    83
      }
chris@0
    84
      setCurrentArticle(article);
chris@0
    85
    }
chris@0
    86
chris@0
    87
    if (commandName.equalsIgnoreCase("ARTICLE"))
chris@0
    88
    {
chris@0
    89
      printStatus(220, article.getNumberInGroup() + " " + article.getMessageID()
chris@0
    90
          + " article retrieved - head and body follow");
chris@0
    91
      Map<String, String> header = article.getHeader();
chris@0
    92
      for(Map.Entry<String, String> entry : header.entrySet())
chris@0
    93
      {
chris@0
    94
        if(entry.getKey().equals("Date"))
chris@0
    95
        {
chris@0
    96
          SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
chris@0
    97
          printTextPart("Date: " + sdf.format(article.getDate()));
chris@0
    98
        }
chris@0
    99
        else
chris@0
   100
          printTextPart(entry.getKey() + ": " + entry.getValue());
chris@0
   101
      }
chris@0
   102
      println("");
chris@0
   103
      printText(article.getBody());
chris@0
   104
    }
chris@0
   105
    else if (commandName.equalsIgnoreCase("HEAD"))
chris@0
   106
    {
chris@0
   107
      printStatus(500, "No longer supported! Use XOVER instead.");
chris@0
   108
      return false;
chris@0
   109
    }
chris@0
   110
    else if (commandName.equalsIgnoreCase("BODY"))
chris@0
   111
    {
chris@0
   112
      printStatus(222, article.getNumberInGroup() + " " + article.getMessageID()
chris@0
   113
          + " body");
chris@0
   114
      printText(article.getBody());
chris@0
   115
    }
chris@0
   116
    else if (commandName.equalsIgnoreCase("STAT"))
chris@0
   117
    {
chris@0
   118
      printStatus(223, article.getNumberInGroup() + " " + article.getMessageID()
chris@0
   119
          + " article retrieved - request text separately");
chris@0
   120
    }
chris@0
   121
    return true;
chris@0
   122
  }
chris@0
   123
chris@0
   124
}