2 * jSite - Message.java - Copyright © 2006–2019 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.InputStream;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
28 * Contains replies sent by the Freenet node. A message always has a name, and
29 * most of the messages also have an identifier which binds it to a specific
30 * command. Exceptions are among others <code>NodeHello</code>,
31 * <code>SSKKeypair</code>, and <code>EndListPersistentRequests</code>.
33 * @author David Roden <droden@gmail.com>
35 * @see de.todesbaum.util.freenet.fcp2.Client
37 public class Message {
39 /** The name of this message. */
40 private final String name;
42 /** The identifier of this message. */
43 private String identifier = "";
45 /** The parameters of this message. */
46 private Map<String, String> parameters = new HashMap<String, String>();
49 private InputStream payloadInputStream;
52 * Creates a new message with the specified name.
55 * The name of this message
57 public Message(String name) {
62 * Returns the identifier of this message.
64 * @return The identifier
66 public String getIdentifier() {
71 * Sets the identifier of this message.
74 * The identifier of this message
76 public void setIdentifier(String identifier) {
77 this.identifier = identifier;
81 * Returns the name of this message.
83 * @return The name of this message
85 public String getName() {
90 * Tests whether this message contains the parameter with the specified key.
91 * Key names are compared ignoring case.
94 * The name of the parameter
95 * @return <code>true</code> if this parameter exists in this message,
96 * <code>false</code> otherwise
98 public boolean containsKey(String key) {
99 return parameters.containsKey(key.toLowerCase());
103 * Returns all parameters of this message. The keys of the entries are all
104 * lower case so if you want to match the parameter names you have to watch
107 * @return All parameters of this message
109 public Set<Entry<String, String>> entrySet() {
110 return parameters.entrySet();
114 * Returns the value of the parameter with the name specified by
118 * The name of the parameter
119 * @return The value of the parameter
121 public String get(String key) {
122 return parameters.get(key.toLowerCase());
126 * Stores the specified value as parameter with the name specified by
130 * The name of the parameter
132 * The value of the parameter
133 * @return The previous value, or <code>null</code> if there was no
136 public String put(String key, String value) {
137 return parameters.put(key.toLowerCase(), value);
141 * Returns the number of parameters in this message.
143 * @return The number of parameters
146 return parameters.size();
150 * @return Returns the payloadInputStream.
152 public InputStream getPayloadInputStream() {
153 return payloadInputStream;
157 * @param payloadInputStream
158 * The payloadInputStream to set.
160 public void setPayloadInputStream(InputStream payloadInputStream) {
161 this.payloadInputStream = payloadInputStream;
165 * Returns a textual representation of this message, containing its name,
166 * the identifier, and the parameters.
168 * @return A textual representation of this message
171 public String toString() {
172 return name + "[identifier=" + identifier + ",parameters=" + parameters.toString() + "]";