📄 Update year in copyright line
[jSite.git] / src / main / java / de / todesbaum / util / freenet / fcp2 / Message.java
1 /*
2  * jSite - Message.java - Copyright Â© 2006–2019 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.InputStream;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26
27 /**
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>.
32  *
33  * @author David Roden &lt;droden@gmail.com&gt;
34  * @version $Id$
35  * @see de.todesbaum.util.freenet.fcp2.Client
36  */
37 public class Message {
38
39         /** The name of this message. */
40         private final String name;
41
42         /** The identifier of this message. */
43         private String identifier = "";
44
45         /** The parameters of this message. */
46         private Map<String, String> parameters = new HashMap<String, String>();
47
48         /** The payload. */
49         private InputStream payloadInputStream;
50
51         /**
52          * Creates a new message with the specified name.
53          *
54          * @param name
55          *            The name of this message
56          */
57         public Message(String name) {
58                 this.name = name;
59         }
60
61         /**
62          * Returns the identifier of this message.
63          *
64          * @return The identifier
65          */
66         public String getIdentifier() {
67                 return identifier;
68         }
69
70         /**
71          * Sets the identifier of this message.
72          *
73          * @param identifier
74          *            The identifier of this message
75          */
76         public void setIdentifier(String identifier) {
77                 this.identifier = identifier;
78         }
79
80         /**
81          * Returns the name of this message.
82          *
83          * @return The name of this message
84          */
85         public String getName() {
86                 return name;
87         }
88
89         /**
90          * Tests whether this message contains the parameter with the specified key.
91          * Key names are compared ignoring case.
92          *
93          * @param key
94          *            The name of the parameter
95          * @return <code>true</code> if this parameter exists in this message,
96          *         <code>false</code> otherwise
97          */
98         public boolean containsKey(String key) {
99                 return parameters.containsKey(key.toLowerCase());
100         }
101
102         /**
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
105          * out.
106          *
107          * @return All parameters of this message
108          */
109         public Set<Entry<String, String>> entrySet() {
110                 return parameters.entrySet();
111         }
112
113         /**
114          * Returns the value of the parameter with the name specified by
115          * <code>key</code>.
116          *
117          * @param key
118          *            The name of the parameter
119          * @return The value of the parameter
120          */
121         public String get(String key) {
122                 return parameters.get(key.toLowerCase());
123         }
124
125         /**
126          * Stores the specified value as parameter with the name specified by
127          * <code>key</code>.
128          *
129          * @param key
130          *            The name of the parameter
131          * @param value
132          *            The value of the parameter
133          * @return The previous value, or <code>null</code> if there was no
134          *         previous value
135          */
136         public String put(String key, String value) {
137                 return parameters.put(key.toLowerCase(), value);
138         }
139
140         /**
141          * Returns the number of parameters in this message.
142          *
143          * @return The number of parameters
144          */
145         public int size() {
146                 return parameters.size();
147         }
148
149         /**
150          * @return Returns the payloadInputStream.
151          */
152         public InputStream getPayloadInputStream() {
153                 return payloadInputStream;
154         }
155
156         /**
157          * @param payloadInputStream
158          *            The payloadInputStream to set.
159          */
160         public void setPayloadInputStream(InputStream payloadInputStream) {
161                 this.payloadInputStream = payloadInputStream;
162         }
163
164         /**
165          * Returns a textual representation of this message, containing its name,
166          * the identifier, and the parameters.
167          *
168          * @return A textual representation of this message
169          */
170         @Override
171         public String toString() {
172                 return name + "[identifier=" + identifier + ",parameters=" + parameters.toString() + "]";
173         }
174
175 }