src/org/sonews/daemon/command/StatCommand.java
author Christian Lins <christian@lins.me>
Wed Sep 14 23:25:00 2011 +0200 (2011-09-14)
changeset 62 be4e87479855
parent 37 74139325d305
permissions -rwxr-xr-x
Reformatting XDaemonCommand
     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 package org.sonews.daemon.command;
    19 
    20 import java.io.IOException;
    21 import org.sonews.storage.Article;
    22 import org.sonews.daemon.NNTPConnection;
    23 import org.sonews.storage.StorageBackendException;
    24 
    25 /**
    26  * Implementation of the STAT command.
    27  * @author Christian Lins
    28  * @since sonews/0.5.0
    29  */
    30 public class StatCommand implements Command {
    31 
    32 	@Override
    33 	public String[] getSupportedCommandStrings() {
    34 		return new String[]{"STAT"};
    35 	}
    36 
    37 	@Override
    38 	public boolean hasFinished() {
    39 		return true;
    40 	}
    41 
    42 	@Override
    43 	public String impliedCapability() {
    44 		return null;
    45 	}
    46 
    47 	@Override
    48 	public boolean isStateful() {
    49 		return false;
    50 	}
    51 
    52 	// TODO: Method has various exit points => Refactor!
    53 	@Override
    54 	public void processLine(NNTPConnection conn, final String line, byte[] raw)
    55 			throws IOException, StorageBackendException {
    56 		final String[] command = line.split(" ");
    57 
    58 		Article article = null;
    59 		if (command.length == 1) {
    60 			article = conn.getCurrentArticle();
    61 			if (article == null) {
    62 				conn.println("420 no current article has been selected");
    63 				return;
    64 			}
    65 		} else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN)) {
    66 			// Message-ID
    67 			article = Article.getByMessageID(command[1]);
    68 			if (article == null) {
    69 				conn.println("430 no such article found");
    70 				return;
    71 			}
    72 		} else {
    73 			// Message Number
    74 			try {
    75 				long aid = Long.parseLong(command[1]);
    76 				article = conn.getCurrentChannel().getArticle(aid);
    77 			} catch (NumberFormatException ex) {
    78 				ex.printStackTrace();
    79 			} catch (StorageBackendException ex) {
    80 				ex.printStackTrace();
    81 			}
    82 			if (article == null) {
    83 				conn.println("423 no such article number in this group");
    84 				return;
    85 			}
    86 			conn.setCurrentArticle(article);
    87 		}
    88 
    89 		conn.println("223 " + conn.getCurrentChannel().getIndexOf(article) + " "
    90 				+ article.getMessageID()
    91 				+ " article retrieved - request text separately");
    92 	}
    93 }