b238db3f7b297ab93a29ab18cab962fe5a469782
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / FcpMessage.java
1 /*
2  * jFCPlib - FcpMessage.java - Copyright © 2008 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 net.pterodactylus.fcp;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 /**
31  * An FCP message. FCP messages consist of a name, an arbitrary amount of
32  * “fields” (i.e. key-value pairs), a message end marker, and optional payload
33  * data that follows the marker.
34  *
35  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36  */
37 public class FcpMessage implements Iterable<String> {
38
39         /** Constant for the linefeed. */
40         private static final String LINEFEED = "\r\n";
41
42         /** The name of the message. */
43         private final String name;
44
45         /** The fields of the message. */
46         private final Map<String, String> fields = new HashMap<String, String>();
47
48         /** The optional payload input stream. */
49         private InputStream payloadInputStream;
50
51         /**
52          * Creates a new FCP message with the given name.
53          *
54          * @param name
55          *            The name of the FCP message
56          */
57         public FcpMessage(String name) {
58                 this(name, null);
59         }
60
61         /**
62          * Creates a new FCP message with the given name and the given payload input
63          * stream. The payload input stream is not read until the message is sent to
64          * the node using {@link FcpConnection#sendMessage(FcpMessage)}.
65          *
66          * @param name
67          *            The name of the message
68          * @param payloadInputStream
69          *            The payload of the message
70          */
71         public FcpMessage(String name, InputStream payloadInputStream) {
72                 this.name = name;
73                 this.payloadInputStream = payloadInputStream;
74         }
75
76         /**
77          * Returns the name of the message.
78          *
79          * @return The name of the message
80          */
81         public String getName() {
82                 return name;
83         }
84
85         /**
86          * Checks whether this message has a field with the given name.
87          *
88          * @param field
89          *            The name of the field to check for
90          * @return <code>true</code> if the message has a field with the given name,
91          *         <code>false</code> otherwise
92          */
93         public boolean hasField(String field) {
94                 return fields.containsKey(field);
95         }
96
97         /**
98          * Sets the field with the given name to the given value. If the field
99          * already exists in this message it is overwritten.
100          *
101          * @param field
102          *            The name of the field
103          * @param value
104          *            The value of the field
105          */
106         public void setField(String field, String value) {
107                 if ((field == null) || (value == null)) {
108                         throw new NullPointerException(((field == null) ? "field " : "value ") + "must not be null");
109                 }
110                 fields.put(field, value);
111         }
112
113         /**
114          * Returns the value of the given field.
115          *
116          * @param field
117          *            The name of the field
118          * @return The value of the field, or <code>null</code> if there is no such
119          *         field
120          */
121         public String getField(String field) {
122                 return fields.get(field);
123         }
124
125         /**
126          * Returns all fields of this message.
127          *
128          * @return All fields of this message
129          */
130         public Map<String, String> getFields() {
131                 return Collections.unmodifiableMap(fields);
132         }
133
134         /**
135          * {@inheritDoc}
136          */
137         public Iterator<String> iterator() {
138                 return fields.keySet().iterator();
139         }
140
141         /**
142          * Sets the payload input stream of the message.
143          *
144          * @param payloadInputStream
145          *            The payload input stream
146          */
147         public void setPayloadInputStream(InputStream payloadInputStream) {
148                 this.payloadInputStream = payloadInputStream;
149         }
150
151         /**
152          * Writes this message to the given output stream. If the message has a
153          * payload (i.e. {@link #payloadInputStream} is not <code>null</code>) the
154          * payload is written to the given output stream after the message as well.
155          * That means that this method can only be called once because on the second
156          * invocation the payload input stream could not be read (again).
157          *
158          * @param outputStream
159          *            The output stream to write the message to
160          * @throws IOException
161          *             if an I/O error occurs
162          */
163         public void write(OutputStream outputStream) throws IOException {
164                 writeLine(outputStream, name);
165                 for (Entry<String, String> fieldEntry : fields.entrySet()) {
166                         writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue());
167                 }
168                 writeLine(outputStream, "EndMessage");
169                 outputStream.flush();
170                 if (payloadInputStream != null) {
171                         FcpUtils.copy(payloadInputStream, outputStream);
172                         outputStream.flush();
173                 }
174         }
175
176         //
177         // PRIVATE METHODS
178         //
179
180         /**
181          * Writes the given line (followed by {@link #LINEFEED} to the given output
182          * stream, using UTF-8 as encoding.
183          *
184          * @param outputStream
185          *            The output stream to write to
186          * @param line
187          *            The line to write
188          * @throws IOException
189          *             if an I/O error occurs
190          */
191         private void writeLine(OutputStream outputStream, String line) throws IOException {
192                 outputStream.write((line + LINEFEED).getBytes("UTF-8"));
193         }
194
195 }