df46925b5a5bdf0ffcff071587d37fbe0b5028e4
[jFCPlib.git] / src / net / pterodactylus / fcp / FCPPluginReply.java
1 /*
2  * jSite2 - FCPPluginReply.java -
3  * Copyright © 2008 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 net.pterodactylus.fcp;
21
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 /**
28  * The “FCPPluginReply” is sent by a plugin as a response to a
29  * {@link FCPPluginMessage} message.
30  *
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  */
33 public class FCPPluginReply extends BaseMessage {
34
35         /** The payload input stream. */
36         private final InputStream payloadInputStream;
37
38         /**
39          * Creates a new “FCPPluginReply” message that wraps the received message.
40          *
41          * @param receivedMessage
42          *            The received message
43          * @param payloadInputStream
44          *            The optional input stream for the payload
45          */
46         FCPPluginReply(FcpMessage receivedMessage, InputStream payloadInputStream) {
47                 super(receivedMessage);
48                 this.payloadInputStream = payloadInputStream;
49         }
50
51         /**
52          * Returns the name of the plugin.
53          *
54          * @return The name of the plugin
55          */
56         public String getPluginName() {
57                 return getField("PluginName");
58         }
59
60         /**
61          * Returns the identifier of the request.
62          *
63          * @return The identifier of the request
64          */
65         public String getIdentifier() {
66                 return getField("Identifier");
67         }
68
69         /**
70          * Returns the length of the optional payload.
71          *
72          * @return The length of the payload, or <code>-1</code> if there is no
73          *         payload or the length could not be parsed
74          */
75         public long getDataLength() {
76                 return FcpUtils.safeParseLong(getField("DataLength"));
77         }
78
79         /**
80          * Returns a reply from the plugin.
81          *
82          * @param key
83          *            The name of the reply
84          * @return The value of the reply
85          */
86         public String getReply(String key) {
87                 return getField("Replies." + key);
88         }
89
90         /**
91          * Returns all replies from the plugin. The plugin sends replies as normal
92          * message fields prefixed by “Replies.”. The keys of the returned map do
93          * not contain this prefix!
94          *
95          * @return All replies from the plugin
96          */
97         public Map<String, String> getReplies() {
98                 Map<String, String> fields = getFields();
99                 Map<String, String> replies = new HashMap<String, String>();
100                 for (Entry<String, String> field : fields.entrySet()) {
101                         if (field.getKey().startsWith("Replies.")) {
102                                 replies.put(field.getKey().substring(8), field.getValue());
103                         }
104                 }
105                 return replies;
106         }
107
108         /**
109          * Returns the optional payload.
110          *
111          * @return The payload of the reply, or <code>null</code> if there is no
112          *         payload
113          */
114         public InputStream getPayloadInputStream() {
115                 return payloadInputStream;
116         }
117
118 }