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.
     1 /*
     2  *   StarOffice 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 com.so.news.command;
    20 
    21 import java.io.IOException;
    22 import java.text.SimpleDateFormat;
    23 import java.util.Locale;
    24 import java.util.Map;
    25 import com.so.news.Debug;
    26 import com.so.news.NNTPConnection;
    27 import com.so.news.storage.Article;
    28 
    29 /**
    30  * Class handling the ARTICLE command.
    31  * @author Christian Lins
    32  * @author Dennis Schwerdel
    33  */
    34 public class ArticleCommand extends Command
    35 {
    36   public ArticleCommand(NNTPConnection connection)
    37   {
    38     super(connection);
    39   }
    40 
    41   public boolean process(String[] command) throws IOException
    42   {
    43     String commandName = command[0];
    44 
    45     // untested, RFC977 compliant
    46     Article article = null;
    47     if (command.length <= 1)
    48     {
    49       article = getCurrentArticle();
    50       if (article == null)
    51       {
    52         printStatus(420, "no current article has been selected");
    53         return true;
    54       }
    55     }
    56     else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
    57     {
    58       // Message-ID
    59       article = Article.getByMessageID(command[1]);
    60       if (article == null)
    61       {
    62         printStatus(430, "no such article found");
    63         return true;
    64       }
    65     }
    66     else
    67     {
    68       // Message Number
    69       try
    70       {
    71         int num = Integer.parseInt(command[1]);
    72         article = Article.getByNumberInGroup(connection.getCurrentGroup(), num);
    73       }
    74       catch (Exception ex)
    75       {
    76         ex.printStackTrace(Debug.getInstance().getStream());
    77         System.err.println(ex.getLocalizedMessage());
    78       }
    79       if (article == null)
    80       {
    81         printStatus(423, "no such article number in this group");
    82         return true;
    83       }
    84       setCurrentArticle(article);
    85     }
    86 
    87     if (commandName.equalsIgnoreCase("ARTICLE"))
    88     {
    89       printStatus(220, article.getNumberInGroup() + " " + article.getMessageID()
    90           + " article retrieved - head and body follow");
    91       Map<String, String> header = article.getHeader();
    92       for(Map.Entry<String, String> entry : header.entrySet())
    93       {
    94         if(entry.getKey().equals("Date"))
    95         {
    96           SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
    97           printTextPart("Date: " + sdf.format(article.getDate()));
    98         }
    99         else
   100           printTextPart(entry.getKey() + ": " + entry.getValue());
   101       }
   102       println("");
   103       printText(article.getBody());
   104     }
   105     else if (commandName.equalsIgnoreCase("HEAD"))
   106     {
   107       printStatus(500, "No longer supported! Use XOVER instead.");
   108       return false;
   109     }
   110     else if (commandName.equalsIgnoreCase("BODY"))
   111     {
   112       printStatus(222, article.getNumberInGroup() + " " + article.getMessageID()
   113           + " body");
   114       printText(article.getBody());
   115     }
   116     else if (commandName.equalsIgnoreCase("STAT"))
   117     {
   118       printStatus(223, article.getNumberInGroup() + " " + article.getMessageID()
   119           + " article retrieved - request text separately");
   120     }
   121     return true;
   122   }
   123 
   124 }