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