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