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
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
package org.sonews.daemon.command;
chris@1
    19
chris@1
    20
import java.io.IOException;
chris@3
    21
import org.sonews.storage.Article;
chris@1
    22
import org.sonews.daemon.NNTPConnection;
chris@3
    23
import org.sonews.storage.StorageBackendException;
chris@1
    24
chris@1
    25
/**
chris@1
    26
 * Implementation of the STAT command.
chris@1
    27
 * @author Christian Lins
chris@1
    28
 * @since sonews/0.5.0
chris@1
    29
 */
cli@48
    30
public class StatCommand implements Command {
chris@1
    31
cli@37
    32
	@Override
cli@48
    33
	public String[] getSupportedCommandStrings() {
cli@48
    34
		return new String[]{"STAT"};
cli@37
    35
	}
chris@1
    36
cli@37
    37
	@Override
cli@48
    38
	public boolean hasFinished() {
cli@37
    39
		return true;
cli@37
    40
	}
chris@1
    41
cli@37
    42
	@Override
cli@48
    43
	public String impliedCapability() {
cli@37
    44
		return null;
cli@37
    45
	}
cli@20
    46
cli@37
    47
	@Override
cli@48
    48
	public boolean isStateful() {
cli@37
    49
		return false;
cli@37
    50
	}
chris@3
    51
cli@37
    52
	// TODO: Method has various exit points => Refactor!
cli@37
    53
	@Override
cli@37
    54
	public void processLine(NNTPConnection conn, final String line, byte[] raw)
cli@48
    55
			throws IOException, StorageBackendException {
cli@37
    56
		final String[] command = line.split(" ");
chris@1
    57
cli@37
    58
		Article article = null;
cli@37
    59
		if (command.length == 1) {
cli@37
    60
			article = conn.getCurrentArticle();
cli@37
    61
			if (article == null) {
cli@37
    62
				conn.println("420 no current article has been selected");
cli@37
    63
				return;
cli@37
    64
			}
cli@37
    65
		} else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN)) {
cli@37
    66
			// Message-ID
cli@37
    67
			article = Article.getByMessageID(command[1]);
cli@37
    68
			if (article == null) {
cli@37
    69
				conn.println("430 no such article found");
cli@37
    70
				return;
cli@37
    71
			}
cli@37
    72
		} else {
cli@37
    73
			// Message Number
cli@37
    74
			try {
cli@37
    75
				long aid = Long.parseLong(command[1]);
cli@37
    76
				article = conn.getCurrentChannel().getArticle(aid);
cli@37
    77
			} catch (NumberFormatException ex) {
cli@37
    78
				ex.printStackTrace();
cli@37
    79
			} catch (StorageBackendException ex) {
cli@37
    80
				ex.printStackTrace();
cli@37
    81
			}
cli@37
    82
			if (article == null) {
cli@37
    83
				conn.println("423 no such article number in this group");
cli@37
    84
				return;
cli@37
    85
			}
cli@37
    86
			conn.setCurrentArticle(article);
cli@37
    87
		}
cli@37
    88
cli@37
    89
		conn.println("223 " + conn.getCurrentChannel().getIndexOf(article) + " "
cli@48
    90
				+ article.getMessageID()
cli@48
    91
				+ " article retrieved - request text separately");
cli@37
    92
	}
chris@1
    93
}