3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.util.freenet.fcp2;
22 import java.io.InputStream;
23 import java.util.HashMap;
26 import java.util.Map.Entry;
29 * Contains replies sent by the Freenet node. A message always has a name, and
30 * most of the messages also have an identifier which binds it to a specific
31 * command. Exceptions are among others <code>NodeHello</code>,
32 * <code>SSKKeypair</code>, and <code>EndListPersistentRequests</code>.
34 * @author David Roden <droden@gmail.com>
36 * @see de.todesbaum.util.freenet.fcp2.Client
38 public class Message {
40 /** The name of this message. */
41 private final String name;
43 /** The identifier of this message. */
44 private String identifier = "";
46 /** The parameters of this message. */
47 private Map<String, String> parameters = new HashMap<String, String>();
50 private InputStream payloadInputStream;
53 * Creates a new message with the specified name.
56 * The name of this message
58 public Message(String name) {
63 * Returns the identifier of this message.
65 * @return The identifier
67 public String getIdentifier() {
72 * Sets the identifier of this message.
75 * The identifier of this message
77 public void setIdentifier(String identifier) {
78 this.identifier = identifier;
82 * Returns the name of this message.
84 * @return The name of this message
86 public String getName() {
91 * Tests whether this message contains the parameter with the specified key.
92 * Key names are compared ignoring case.
95 * The name of the parameter
96 * @return <code>true</code> if this parameter exists in this message,
97 * <code>false</code> otherwise
99 public boolean containsKey(String key) {
100 return parameters.containsKey(key.toLowerCase());
104 * Returns all parameters of this message. The keys of the entries are all
105 * lower case so if you want to match the parameter names you have to watch
108 * @return All parameters of this message
110 public Set<Entry<String, String>> entrySet() {
111 return parameters.entrySet();
115 * Returns the value of the parameter with the name specified by
119 * The name of the parameter
120 * @return The value of the parameter
122 public String get(String key) {
123 return parameters.get(key.toLowerCase());
127 * Stores the specified value as parameter with the name specified by
131 * The name of the parameter
133 * The value of the parameter
134 * @return The previous value, or <code>null</code> if there was no
137 public String put(String key, String value) {
138 return parameters.put(key.toLowerCase(), value);
142 * Returns the number of parameters in this message.
144 * @return The number of parameters
147 return parameters.size();
151 * @return Returns the payloadInputStream.
153 public InputStream getPayloadInputStream() {
154 return payloadInputStream;
158 * @param payloadInputStream
159 * The payloadInputStream to set.
161 public void setPayloadInputStream(InputStream payloadInputStream) {
162 this.payloadInputStream = payloadInputStream;
166 * Returns a textual representation of this message, containing its name,
167 * the identifier, and the parameters.
169 * @return A textual representation of this message
172 public String toString() {
173 return name + "[identifier=" + identifier + ",parameters=" + parameters.toString() + "]";