org/sonews/daemon/command/ArticleCommand.java
author cli
Thu, 06 Aug 2009 18:41:34 +0200
changeset 6 dcc7e491fbc9
parent 3 2fdc9cc89502
child 20 6ae5e4f8329b
permissions -rw-r--r--
Removing doc subdir which resides now in sonews-doc repository.
     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 org.sonews.storage.Article;
    23 import org.sonews.daemon.NNTPConnection;
    24 import org.sonews.storage.Channel;
    25 import org.sonews.storage.StorageBackendException;
    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 implements Command
    34 {
    35 
    36   @Override
    37   public String[] getSupportedCommandStrings()
    38   {
    39     return new String[] {"ARTICLE", "BODY", "HEAD"};
    40   }
    41 
    42   @Override
    43   public boolean hasFinished()
    44   {
    45     return true;
    46   }
    47 
    48   @Override
    49   public boolean isStateful()
    50   {
    51     return false;
    52   }
    53 
    54   // TODO: Refactor this method to reduce its complexity!
    55   @Override
    56   public void processLine(NNTPConnection conn, final String line, byte[] raw)
    57     throws IOException
    58   {
    59     final String[] command = line.split(" ");
    60     
    61     Article article  = null;
    62     long    artIndex = -1;
    63     if (command.length == 1)
    64     {
    65       article = conn.getCurrentArticle();
    66       if (article == null)
    67       {
    68         conn.println("420 no current article has been selected");
    69         return;
    70       }
    71     }
    72     else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
    73     {
    74       // Message-ID
    75       article = Article.getByMessageID(command[1]);
    76       if (article == null)
    77       {
    78         conn.println("430 no such article found");
    79         return;
    80       }
    81     }
    82     else
    83     {
    84       // Message Number
    85       try
    86       {
    87         Channel currentGroup = conn.getCurrentChannel();
    88         if(currentGroup == null)
    89         {
    90           conn.println("400 no group selected");
    91           return;
    92         }
    93         
    94         artIndex = Long.parseLong(command[1]);
    95         article  = currentGroup.getArticle(artIndex);
    96       }
    97       catch(NumberFormatException ex)
    98       {
    99         ex.printStackTrace();
   100       }
   101       catch(StorageBackendException ex)
   102       {
   103         ex.printStackTrace();
   104       }
   105 
   106       if (article == null)
   107       {
   108         conn.println("423 no such article number in this group");
   109         return;
   110       }
   111       conn.setCurrentArticle(article);
   112     }
   113 
   114     if(command[0].equalsIgnoreCase("ARTICLE"))
   115     {
   116       conn.println("220 " + artIndex + " " + article.getMessageID()
   117           + " article retrieved - head and body follow");
   118       conn.println(article.getHeaderSource());
   119       conn.println("");
   120       conn.println(article.getBody());
   121       conn.println(".");
   122     }
   123     else if(command[0].equalsIgnoreCase("BODY"))
   124     {
   125       conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
   126       conn.println(article.getBody());
   127       conn.println(".");
   128     }
   129     
   130     /*
   131      * HEAD: This command is mandatory.
   132      *
   133      * Syntax
   134      *    HEAD message-id
   135      *    HEAD number
   136      *    HEAD
   137      *
   138      * Responses
   139      *
   140      * First form (message-id specified)
   141      *  221 0|n message-id    Headers follow (multi-line)
   142      *  430                   No article with that message-id
   143      *
   144      * Second form (article number specified)
   145      *  221 n message-id      Headers follow (multi-line)
   146      *  412                   No newsgroup selected
   147      *  423                   No article with that number
   148      *
   149      * Third form (current article number used)
   150      *  221 n message-id      Headers follow (multi-line)
   151      *  412                   No newsgroup selected
   152      *  420                   Current article number is invalid
   153      *
   154      * Parameters
   155      *  number        Requested article number
   156      *  n             Returned article number
   157      *  message-id    Article message-id
   158      */
   159     else if(command[0].equalsIgnoreCase("HEAD"))
   160     {
   161       conn.println("221 " + artIndex + " " + article.getMessageID()
   162           + " Headers follow (multi-line)");
   163       conn.println(article.getHeaderSource());
   164       conn.println(".");
   165     }
   166   }  
   167   
   168 }