63aaa9c7c14efb233bff195c1cbd5dc73cc78a74
[jSite.git] / src / main / java / de / todesbaum / util / freenet / fcp2 / Command.java
1 /*
2  * jSite - Command.java - Copyright © 2006–2012 David Roden
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package de.todesbaum.util.freenet.fcp2;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.Writer;
24
25 /**
26  * Abstract base class for all commands.
27  * <p>
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.
35  *
36  * @author David Roden &lt;droden@gmail.com&gt;
37  * @version $Id$
38  */
39 public abstract class Command {
40
41         /** The line feed sequence used by the library. */
42         protected static final String LINEFEED = "\r\n";
43
44         /**
45          * The name of the command. The name is sent to the node so it can not be
46          * chosen arbitrarily!
47          */
48         private final String commandName;
49
50         /**
51          * The identifier of the command. This identifier is used to identify
52          * replies that are caused by a command.
53          */
54         private final String identifier;
55
56         /**
57          * Creates a new command with the specified name and identifier.
58          *
59          * @param name
60          *            The name of the command
61          * @param identifier
62          *            The identifier of the command
63          */
64         public Command(String name, String identifier) {
65                 this.commandName = name;
66                 this.identifier = identifier;
67         }
68
69         /**
70          * Returns the name of this command.
71          *
72          * @return The name of this command
73          */
74         public String getCommandName() {
75                 return commandName;
76         }
77
78         /**
79          * Return the identifier of this command.
80          *
81          * @return The identifier of this command
82          */
83         public String getIdentifier() {
84                 return identifier;
85         }
86
87         /**
88          * Writes all parameters to the specified writer.
89          * <p>
90          * <strong>NOTE:</strong> Subclasses of Command <strong>must</strong> call
91          * <code>super.write(writer)</code> before or after writing their own
92          * parameters!
93          *
94          * @param writer
95          *            The stream to write the parameters to
96          * @throws IOException
97          *             if an I/O error occurs
98          */
99         protected void write(Writer writer) throws IOException {
100                 if (identifier != null)
101                         writer.write("Identifier=" + identifier + LINEFEED);
102         }
103
104         /**
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.
108          *
109          * @return <code>true</code> if this command has payload to send,
110          *         <code>false</code> otherwise
111          */
112         protected boolean hasPayload() {
113                 return false;
114         }
115
116         /**
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>.
120          *
121          * @return The payload of this command
122          */
123         protected InputStream getPayload() {
124                 return null;
125         }
126
127         /**
128          * Returns the length of the payload. This method is never called if
129          * {@link #hasPayload()} returns <code>false</code>.
130          *
131          * @return The length of the payload
132          */
133         protected long getPayloadLength() {
134                 return -1;
135         }
136
137 }