trunk/com/so/news/command/ArticleCommand.java
changeset 0 f907866f0e4b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/trunk/com/so/news/command/ArticleCommand.java	Tue Jan 20 10:21:03 2009 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + *   StarOffice 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 com.so.news.command;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.text.SimpleDateFormat;
    1.26 +import java.util.Locale;
    1.27 +import java.util.Map;
    1.28 +import com.so.news.Debug;
    1.29 +import com.so.news.NNTPConnection;
    1.30 +import com.so.news.storage.Article;
    1.31 +
    1.32 +/**
    1.33 + * Class handling the ARTICLE command.
    1.34 + * @author Christian Lins
    1.35 + * @author Dennis Schwerdel
    1.36 + */
    1.37 +public class ArticleCommand extends Command
    1.38 +{
    1.39 +  public ArticleCommand(NNTPConnection connection)
    1.40 +  {
    1.41 +    super(connection);
    1.42 +  }
    1.43 +
    1.44 +  public boolean process(String[] command) throws IOException
    1.45 +  {
    1.46 +    String commandName = command[0];
    1.47 +
    1.48 +    // untested, RFC977 compliant
    1.49 +    Article article = null;
    1.50 +    if (command.length <= 1)
    1.51 +    {
    1.52 +      article = getCurrentArticle();
    1.53 +      if (article == null)
    1.54 +      {
    1.55 +        printStatus(420, "no current article has been selected");
    1.56 +        return true;
    1.57 +      }
    1.58 +    }
    1.59 +    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
    1.60 +    {
    1.61 +      // Message-ID
    1.62 +      article = Article.getByMessageID(command[1]);
    1.63 +      if (article == null)
    1.64 +      {
    1.65 +        printStatus(430, "no such article found");
    1.66 +        return true;
    1.67 +      }
    1.68 +    }
    1.69 +    else
    1.70 +    {
    1.71 +      // Message Number
    1.72 +      try
    1.73 +      {
    1.74 +        int num = Integer.parseInt(command[1]);
    1.75 +        article = Article.getByNumberInGroup(connection.getCurrentGroup(), num);
    1.76 +      }
    1.77 +      catch (Exception ex)
    1.78 +      {
    1.79 +        ex.printStackTrace(Debug.getInstance().getStream());
    1.80 +        System.err.println(ex.getLocalizedMessage());
    1.81 +      }
    1.82 +      if (article == null)
    1.83 +      {
    1.84 +        printStatus(423, "no such article number in this group");
    1.85 +        return true;
    1.86 +      }
    1.87 +      setCurrentArticle(article);
    1.88 +    }
    1.89 +
    1.90 +    if (commandName.equalsIgnoreCase("ARTICLE"))
    1.91 +    {
    1.92 +      printStatus(220, article.getNumberInGroup() + " " + article.getMessageID()
    1.93 +          + " article retrieved - head and body follow");
    1.94 +      Map<String, String> header = article.getHeader();
    1.95 +      for(Map.Entry<String, String> entry : header.entrySet())
    1.96 +      {
    1.97 +        if(entry.getKey().equals("Date"))
    1.98 +        {
    1.99 +          SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
   1.100 +          printTextPart("Date: " + sdf.format(article.getDate()));
   1.101 +        }
   1.102 +        else
   1.103 +          printTextPart(entry.getKey() + ": " + entry.getValue());
   1.104 +      }
   1.105 +      println("");
   1.106 +      printText(article.getBody());
   1.107 +    }
   1.108 +    else if (commandName.equalsIgnoreCase("HEAD"))
   1.109 +    {
   1.110 +      printStatus(500, "No longer supported! Use XOVER instead.");
   1.111 +      return false;
   1.112 +    }
   1.113 +    else if (commandName.equalsIgnoreCase("BODY"))
   1.114 +    {
   1.115 +      printStatus(222, article.getNumberInGroup() + " " + article.getMessageID()
   1.116 +          + " body");
   1.117 +      printText(article.getBody());
   1.118 +    }
   1.119 +    else if (commandName.equalsIgnoreCase("STAT"))
   1.120 +    {
   1.121 +      printStatus(223, article.getNumberInGroup() + " " + article.getMessageID()
   1.122 +          + " article retrieved - request text separately");
   1.123 +    }
   1.124 +    return true;
   1.125 +  }
   1.126 +
   1.127 +}