org/sonews/daemon/command/ArticleCommand.java
changeset 2 1090e2141798
child 3 2fdc9cc89502
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/command/ArticleCommand.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,164 @@
     1.4 +/*
     1.5 + *   SONEWS 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 org.sonews.daemon.command;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.sql.SQLException;
    1.26 +import org.sonews.daemon.storage.Article;
    1.27 +import org.sonews.daemon.NNTPConnection;
    1.28 +import org.sonews.daemon.storage.Group;
    1.29 +
    1.30 +/**
    1.31 + * Class handling the ARTICLE, BODY and HEAD commands.
    1.32 + * @author Christian Lins
    1.33 + * @author Dennis Schwerdel
    1.34 + * @since n3tpd/0.1
    1.35 + */
    1.36 +public class ArticleCommand extends AbstractCommand
    1.37 +{
    1.38 +  
    1.39 +  public ArticleCommand(final NNTPConnection connection)
    1.40 +  {
    1.41 +    super(connection);
    1.42 +  }
    1.43 +
    1.44 +  @Override
    1.45 +  public boolean hasFinished()
    1.46 +  {
    1.47 +    return true;
    1.48 +  }
    1.49 +
    1.50 +  // TODO: Refactor this method to reduce its complexity!
    1.51 +  @Override
    1.52 +  public void processLine(final String line)
    1.53 +    throws IOException
    1.54 +  {
    1.55 +    final String[] command = line.split(" ");
    1.56 +    
    1.57 +    Article article  = null;
    1.58 +    long    artIndex = -1;
    1.59 +    if (command.length == 1)
    1.60 +    {
    1.61 +      article = getCurrentArticle();
    1.62 +      if (article == null)
    1.63 +      {
    1.64 +        printStatus(420, "no current article has been selected");
    1.65 +        return;
    1.66 +      }
    1.67 +    }
    1.68 +    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
    1.69 +    {
    1.70 +      // Message-ID
    1.71 +      article = Article.getByMessageID(command[1]);
    1.72 +      if (article == null)
    1.73 +      {
    1.74 +        printStatus(430, "no such article found");
    1.75 +        return;
    1.76 +      }
    1.77 +    }
    1.78 +    else
    1.79 +    {
    1.80 +      // Message Number
    1.81 +      try
    1.82 +      {
    1.83 +        Group currentGroup = connection.getCurrentGroup();
    1.84 +        if(currentGroup == null)
    1.85 +        {
    1.86 +          printStatus(400, "no group selected");
    1.87 +          return;
    1.88 +        }
    1.89 +        
    1.90 +        artIndex = Long.parseLong(command[1]);
    1.91 +        article  = Article.getByArticleNumber(artIndex, currentGroup);
    1.92 +      }
    1.93 +      catch(NumberFormatException ex)
    1.94 +      {
    1.95 +        ex.printStackTrace();
    1.96 +      }
    1.97 +      catch(SQLException ex)
    1.98 +      {
    1.99 +        ex.printStackTrace();
   1.100 +      }
   1.101 +
   1.102 +      if (article == null)
   1.103 +      {
   1.104 +        printStatus(423, "no such article number in this group");
   1.105 +        return;
   1.106 +      }
   1.107 +      setCurrentArticle(article);
   1.108 +    }
   1.109 +
   1.110 +    if(command[0].equalsIgnoreCase("ARTICLE"))
   1.111 +    {
   1.112 +      printStatus(220, artIndex + " " + article.getMessageID()
   1.113 +          + " article retrieved - head and body follow");
   1.114 +      
   1.115 +      println(article.getHeaderSource());
   1.116 +      
   1.117 +      println("");
   1.118 +      println(article.getBody(), article.getBodyCharset());
   1.119 +      println(".");
   1.120 +    }
   1.121 +    else if(command[0].equalsIgnoreCase("BODY"))
   1.122 +    {
   1.123 +      printStatus(222, artIndex + " " + article.getMessageID() + " body");
   1.124 +      println(article.getBody(), article.getBodyCharset());
   1.125 +      println(".");
   1.126 +    }
   1.127 +    
   1.128 +    /*
   1.129 +     * HEAD: This command is mandatory.
   1.130 +     *
   1.131 +     * Syntax
   1.132 +     *    HEAD message-id
   1.133 +     *    HEAD number
   1.134 +     *    HEAD
   1.135 +     *
   1.136 +     * Responses
   1.137 +     *
   1.138 +     * First form (message-id specified)
   1.139 +     *  221 0|n message-id    Headers follow (multi-line)
   1.140 +     *  430                   No article with that message-id
   1.141 +     *
   1.142 +     * Second form (article number specified)
   1.143 +     *  221 n message-id      Headers follow (multi-line)
   1.144 +     *  412                   No newsgroup selected
   1.145 +     *  423                   No article with that number
   1.146 +     *
   1.147 +     * Third form (current article number used)
   1.148 +     *  221 n message-id      Headers follow (multi-line)
   1.149 +     *  412                   No newsgroup selected
   1.150 +     *  420                   Current article number is invalid
   1.151 +     *
   1.152 +     * Parameters
   1.153 +     *  number        Requested article number
   1.154 +     *  n             Returned article number
   1.155 +     *  message-id    Article message-id
   1.156 +     */
   1.157 +    else if(command[0].equalsIgnoreCase("HEAD"))
   1.158 +    {
   1.159 +      printStatus(221, artIndex + " " + article.getMessageID()
   1.160 +          + " Headers follow (multi-line)");
   1.161 +      
   1.162 +      println(article.getHeaderSource());
   1.163 +      println(".");
   1.164 +    }
   1.165 +  }  
   1.166 +  
   1.167 +}