2 * jSite - Command.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.util.freenet.fcp2;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.Writer;
26 * Abstract base class for all commands.
28 * In addition to the replies listed at the type comment of each specific
29 * command the node can <strong>always</strong> send the following messages:
30 * <code>ProtocolError</code> (if this library screws up),
31 * <code>CloseConnectionDuplicateClientName</code> (if a client with the same
32 * name of the {@link de.todesbaum.util.freenet.fcp2.Connection} connects). So
33 * when receiving messages from the node you should always be prepared for
34 * something you did not expect.
36 * @author David Roden <droden@gmail.com>
39 public abstract class Command {
41 /** The line feed sequence used by the library. */
42 protected static final String LINEFEED = "\r\n";
45 * The name of the command. The name is sent to the node so it can not be
48 private final String commandName;
51 * The identifier of the command. This identifier is used to identify
52 * replies that are caused by a command.
54 private final String identifier;
57 * Creates a new command with the specified name and identifier.
60 * The name of the command
62 * The identifier of the command
64 public Command(String name, String identifier) {
65 this.commandName = name;
66 this.identifier = identifier;
70 * Returns the name of this command.
72 * @return The name of this command
74 public String getCommandName() {
79 * Return the identifier of this command.
81 * @return The identifier of this command
83 public String getIdentifier() {
88 * Writes all parameters to the specified writer.
90 * <strong>NOTE:</strong> Subclasses of Command <strong>must</strong> call
91 * <code>super.write(writer)</code> before or after writing their own
95 * The stream to write the parameters to
97 * if an I/O error occurs
99 protected void write(Writer writer) throws IOException {
100 if (identifier != null)
101 writer.write("Identifier=" + identifier + LINEFEED);
105 * Returns whether this command has payload to send after the message.
106 * Subclasses need to return <code>true</code> here if they need to send
107 * payload after the message.
109 * @return <code>true</code> if this command has payload to send,
110 * <code>false</code> otherwise
112 protected boolean hasPayload() {
117 * Returns the payload of this command as an {@link InputStream}. This
118 * method is never called if {@link #hasPayload()} returns
119 * <code>false</code>.
121 * @return The payload of this command
123 protected InputStream getPayload() {
128 * Returns the length of the payload. This method is never called if
129 * {@link #hasPayload()} returns <code>false</code>.
131 * @return The length of the payload
133 protected long getPayloadLength() {