org/sonews/daemon/command/ArticleCommand.java
author chris <chris@marvin>
Wed Jul 01 10:48:22 2009 +0200 (2009-07-01)
changeset 2 1090e2141798
child 3 2fdc9cc89502
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     1 /*
     2  *   SONEWS 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 org.sonews.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.sql.SQLException;
    23 import org.sonews.daemon.storage.Article;
    24 import org.sonews.daemon.NNTPConnection;
    25 import org.sonews.daemon.storage.Group;
    26 
    27 /**
    28  * Class handling the ARTICLE, BODY and HEAD commands.
    29  * @author Christian Lins
    30  * @author Dennis Schwerdel
    31  * @since n3tpd/0.1
    32  */
    33 public class ArticleCommand extends AbstractCommand
    34 {
    35   
    36   public ArticleCommand(final NNTPConnection connection)
    37   {
    38     super(connection);
    39   }
    40 
    41   @Override
    42   public boolean hasFinished()
    43   {
    44     return true;
    45   }
    46 
    47   // TODO: Refactor this method to reduce its complexity!
    48   @Override
    49   public void processLine(final String line)
    50     throws IOException
    51   {
    52     final String[] command = line.split(" ");
    53     
    54     Article article  = null;
    55     long    artIndex = -1;
    56     if (command.length == 1)
    57     {
    58       article = getCurrentArticle();
    59       if (article == null)
    60       {
    61         printStatus(420, "no current article has been selected");
    62         return;
    63       }
    64     }
    65     else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
    66     {
    67       // Message-ID
    68       article = Article.getByMessageID(command[1]);
    69       if (article == null)
    70       {
    71         printStatus(430, "no such article found");
    72         return;
    73       }
    74     }
    75     else
    76     {
    77       // Message Number
    78       try
    79       {
    80         Group currentGroup = connection.getCurrentGroup();
    81         if(currentGroup == null)
    82         {
    83           printStatus(400, "no group selected");
    84           return;
    85         }
    86         
    87         artIndex = Long.parseLong(command[1]);
    88         article  = Article.getByArticleNumber(artIndex, currentGroup);
    89       }
    90       catch(NumberFormatException ex)
    91       {
    92         ex.printStackTrace();
    93       }
    94       catch(SQLException ex)
    95       {
    96         ex.printStackTrace();
    97       }
    98 
    99       if (article == null)
   100       {
   101         printStatus(423, "no such article number in this group");
   102         return;
   103       }
   104       setCurrentArticle(article);
   105     }
   106 
   107     if(command[0].equalsIgnoreCase("ARTICLE"))
   108     {
   109       printStatus(220, artIndex + " " + article.getMessageID()
   110           + " article retrieved - head and body follow");
   111       
   112       println(article.getHeaderSource());
   113       
   114       println("");
   115       println(article.getBody(), article.getBodyCharset());
   116       println(".");
   117     }
   118     else if(command[0].equalsIgnoreCase("BODY"))
   119     {
   120       printStatus(222, artIndex + " " + article.getMessageID() + " body");
   121       println(article.getBody(), article.getBodyCharset());
   122       println(".");
   123     }
   124     
   125     /*
   126      * HEAD: This command is mandatory.
   127      *
   128      * Syntax
   129      *    HEAD message-id
   130      *    HEAD number
   131      *    HEAD
   132      *
   133      * Responses
   134      *
   135      * First form (message-id specified)
   136      *  221 0|n message-id    Headers follow (multi-line)
   137      *  430                   No article with that message-id
   138      *
   139      * Second form (article number specified)
   140      *  221 n message-id      Headers follow (multi-line)
   141      *  412                   No newsgroup selected
   142      *  423                   No article with that number
   143      *
   144      * Third form (current article number used)
   145      *  221 n message-id      Headers follow (multi-line)
   146      *  412                   No newsgroup selected
   147      *  420                   Current article number is invalid
   148      *
   149      * Parameters
   150      *  number        Requested article number
   151      *  n             Returned article number
   152      *  message-id    Article message-id
   153      */
   154     else if(command[0].equalsIgnoreCase("HEAD"))
   155     {
   156       printStatus(221, artIndex + " " + article.getMessageID()
   157           + " Headers follow (multi-line)");
   158       
   159       println(article.getHeaderSource());
   160       println(".");
   161     }
   162   }  
   163   
   164 }