src/org/sonews/daemon/command/ArticleCommand.java
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 48 b78e77619152
     1.1 --- a/src/org/sonews/daemon/command/ArticleCommand.java	Sun Aug 29 17:43:58 2010 +0200
     1.2 +++ b/src/org/sonews/daemon/command/ArticleCommand.java	Sun Aug 29 18:17:37 2010 +0200
     1.3 @@ -33,142 +33,120 @@
     1.4  public class ArticleCommand implements Command
     1.5  {
     1.6  
     1.7 -  @Override
     1.8 -  public String[] getSupportedCommandStrings()
     1.9 -  {
    1.10 -    return new String[] {"ARTICLE", "BODY", "HEAD"};
    1.11 -  }
    1.12 +	@Override
    1.13 +	public String[] getSupportedCommandStrings()
    1.14 +	{
    1.15 +		return new String[] {"ARTICLE", "BODY", "HEAD"};
    1.16 +	}
    1.17  
    1.18 -  @Override
    1.19 -  public boolean hasFinished()
    1.20 -  {
    1.21 -    return true;
    1.22 -  }
    1.23 +	@Override
    1.24 +	public boolean hasFinished()
    1.25 +	{
    1.26 +		return true;
    1.27 +	}
    1.28  
    1.29 -  @Override
    1.30 -  public String impliedCapability()
    1.31 -  {
    1.32 -    return null;
    1.33 -  }
    1.34 +	@Override
    1.35 +	public String impliedCapability()
    1.36 +	{
    1.37 +		return null;
    1.38 +	}
    1.39  
    1.40 -  @Override
    1.41 -  public boolean isStateful()
    1.42 -  {
    1.43 -    return false;
    1.44 -  }
    1.45 +	@Override
    1.46 +	public boolean isStateful()
    1.47 +	{
    1.48 +		return false;
    1.49 +	}
    1.50  
    1.51 -  // TODO: Refactor this method to reduce its complexity!
    1.52 -  @Override
    1.53 -  public void processLine(NNTPConnection conn, final String line, byte[] raw)
    1.54 -    throws IOException
    1.55 -  {
    1.56 -    final String[] command = line.split(" ");
    1.57 -    
    1.58 -    Article article  = null;
    1.59 -    long    artIndex = -1;
    1.60 -    if (command.length == 1)
    1.61 -    {
    1.62 -      article = conn.getCurrentArticle();
    1.63 -      if (article == null)
    1.64 -      {
    1.65 -        conn.println("420 no current article has been selected");
    1.66 -        return;
    1.67 -      }
    1.68 -    }
    1.69 -    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
    1.70 -    {
    1.71 -      // Message-ID
    1.72 -      article = Article.getByMessageID(command[1]);
    1.73 -      if (article == null)
    1.74 -      {
    1.75 -        conn.println("430 no such article found");
    1.76 -        return;
    1.77 -      }
    1.78 -    }
    1.79 -    else
    1.80 -    {
    1.81 -      // Message Number
    1.82 -      try
    1.83 -      {
    1.84 -        Channel currentGroup = conn.getCurrentChannel();
    1.85 -        if(currentGroup == null)
    1.86 -        {
    1.87 -          conn.println("400 no group selected");
    1.88 -          return;
    1.89 -        }
    1.90 -        
    1.91 -        artIndex = Long.parseLong(command[1]);
    1.92 -        article  = currentGroup.getArticle(artIndex);
    1.93 -      }
    1.94 -      catch(NumberFormatException ex)
    1.95 -      {
    1.96 -        ex.printStackTrace();
    1.97 -      }
    1.98 -      catch(StorageBackendException ex)
    1.99 -      {
   1.100 -        ex.printStackTrace();
   1.101 -      }
   1.102 +	// TODO: Refactor this method to reduce its complexity!
   1.103 +	@Override
   1.104 +	public void processLine(NNTPConnection conn, final String line, byte[] raw)
   1.105 +		throws IOException
   1.106 +	{
   1.107 +		final String[] command = line.split(" ");
   1.108  
   1.109 -      if (article == null)
   1.110 -      {
   1.111 -        conn.println("423 no such article number in this group");
   1.112 -        return;
   1.113 -      }
   1.114 -      conn.setCurrentArticle(article);
   1.115 -    }
   1.116 +		Article article = null;
   1.117 +		long artIndex = -1;
   1.118 +		if (command.length == 1) {
   1.119 +			article = conn.getCurrentArticle();
   1.120 +			if (article == null) {
   1.121 +				conn.println("420 no current article has been selected");
   1.122 +				return;
   1.123 +			}
   1.124 +		} else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN)) {
   1.125 +			// Message-ID
   1.126 +			article = Article.getByMessageID(command[1]);
   1.127 +			if (article == null) {
   1.128 +				conn.println("430 no such article found");
   1.129 +				return;
   1.130 +			}
   1.131 +		} else {
   1.132 +			// Message Number
   1.133 +			try {
   1.134 +				Channel currentGroup = conn.getCurrentChannel();
   1.135 +				if (currentGroup == null) {
   1.136 +					conn.println("400 no group selected");
   1.137 +					return;
   1.138 +				}
   1.139  
   1.140 -    if(command[0].equalsIgnoreCase("ARTICLE"))
   1.141 -    {
   1.142 -      conn.println("220 " + artIndex + " " + article.getMessageID()
   1.143 -          + " article retrieved - head and body follow");
   1.144 -      conn.println(article.getHeaderSource());
   1.145 -      conn.println("");
   1.146 -      conn.println(article.getBody());
   1.147 -      conn.println(".");
   1.148 -    }
   1.149 -    else if(command[0].equalsIgnoreCase("BODY"))
   1.150 -    {
   1.151 -      conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
   1.152 -      conn.println(article.getBody());
   1.153 -      conn.println(".");
   1.154 -    }
   1.155 -    
   1.156 -    /*
   1.157 -     * HEAD: This command is mandatory.
   1.158 -     *
   1.159 -     * Syntax
   1.160 -     *    HEAD message-id
   1.161 -     *    HEAD number
   1.162 -     *    HEAD
   1.163 -     *
   1.164 -     * Responses
   1.165 -     *
   1.166 -     * First form (message-id specified)
   1.167 -     *  221 0|n message-id    Headers follow (multi-line)
   1.168 -     *  430                   No article with that message-id
   1.169 -     *
   1.170 -     * Second form (article number specified)
   1.171 -     *  221 n message-id      Headers follow (multi-line)
   1.172 -     *  412                   No newsgroup selected
   1.173 -     *  423                   No article with that number
   1.174 -     *
   1.175 -     * Third form (current article number used)
   1.176 -     *  221 n message-id      Headers follow (multi-line)
   1.177 -     *  412                   No newsgroup selected
   1.178 -     *  420                   Current article number is invalid
   1.179 -     *
   1.180 -     * Parameters
   1.181 -     *  number        Requested article number
   1.182 -     *  n             Returned article number
   1.183 -     *  message-id    Article message-id
   1.184 -     */
   1.185 -    else if(command[0].equalsIgnoreCase("HEAD"))
   1.186 -    {
   1.187 -      conn.println("221 " + artIndex + " " + article.getMessageID()
   1.188 -          + " Headers follow (multi-line)");
   1.189 -      conn.println(article.getHeaderSource());
   1.190 -      conn.println(".");
   1.191 -    }
   1.192 -  }  
   1.193 -  
   1.194 +				artIndex = Long.parseLong(command[1]);
   1.195 +				article = currentGroup.getArticle(artIndex);
   1.196 +			} catch (NumberFormatException ex) {
   1.197 +				ex.printStackTrace();
   1.198 +			} catch (StorageBackendException ex) {
   1.199 +				ex.printStackTrace();
   1.200 +			}
   1.201 +
   1.202 +			if (article == null) {
   1.203 +				conn.println("423 no such article number in this group");
   1.204 +				return;
   1.205 +			}
   1.206 +			conn.setCurrentArticle(article);
   1.207 +		}
   1.208 +
   1.209 +		if (command[0].equalsIgnoreCase("ARTICLE")) {
   1.210 +			conn.println("220 " + artIndex + " " + article.getMessageID()
   1.211 +				+ " article retrieved - head and body follow");
   1.212 +			conn.println(article.getHeaderSource());
   1.213 +			conn.println("");
   1.214 +			conn.println(article.getBody());
   1.215 +			conn.println(".");
   1.216 +		} else if (command[0].equalsIgnoreCase("BODY")) {
   1.217 +			conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
   1.218 +			conn.println(article.getBody());
   1.219 +			conn.println(".");
   1.220 +		} /*
   1.221 +		 * HEAD: This command is mandatory.
   1.222 +		 *
   1.223 +		 * Syntax
   1.224 +		 *    HEAD message-id
   1.225 +		 *    HEAD number
   1.226 +		 *    HEAD
   1.227 +		 *
   1.228 +		 * Responses
   1.229 +		 *
   1.230 +		 * First form (message-id specified)
   1.231 +		 *  221 0|n message-id    Headers follow (multi-line)
   1.232 +		 *  430                   No article with that message-id
   1.233 +		 *
   1.234 +		 * Second form (article number specified)
   1.235 +		 *  221 n message-id      Headers follow (multi-line)
   1.236 +		 *  412                   No newsgroup selected
   1.237 +		 *  423                   No article with that number
   1.238 +		 *
   1.239 +		 * Third form (current article number used)
   1.240 +		 *  221 n message-id      Headers follow (multi-line)
   1.241 +		 *  412                   No newsgroup selected
   1.242 +		 *  420                   Current article number is invalid
   1.243 +		 *
   1.244 +		 * Parameters
   1.245 +		 *  number        Requested article number
   1.246 +		 *  n             Returned article number
   1.247 +		 *  message-id    Article message-id
   1.248 +		 */ else if (command[0].equalsIgnoreCase("HEAD")) {
   1.249 +			conn.println("221 " + artIndex + " " + article.getMessageID()
   1.250 +				+ " Headers follow (multi-line)");
   1.251 +			conn.println(article.getHeaderSource());
   1.252 +			conn.println(".");
   1.253 +		}
   1.254 +	}
   1.255  }