Expose lots of constructors and accessors
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / FCPPluginReply.java
1 /*
2  * jFCPlib - FCPPluginReply.java - Copyright © 2008 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 net.pterodactylus.fcp;
20
21 import java.io.InputStream;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 /**
27  * The “FCPPluginReply” is sent by a plugin as a response to a
28  * {@link FCPPluginMessage} message.
29  *
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  */
32 public class FCPPluginReply extends BaseMessage implements Identifiable {
33
34         /** The payload input stream. */
35         private final InputStream payloadInputStream;
36
37         /**
38          * Creates a new “FCPPluginReply” message that wraps the received message.
39          *
40          * @param receivedMessage
41          *            The received message
42          * @param payloadInputStream
43          *            The optional input stream for the payload
44          */
45         public FCPPluginReply(FcpMessage receivedMessage, InputStream payloadInputStream) {
46                 super(receivedMessage);
47                 this.payloadInputStream = payloadInputStream;
48         }
49
50         /**
51          * Returns the name of the plugin.
52          *
53          * @return The name of the plugin
54          */
55         public String getPluginName() {
56                 return getField("PluginName");
57         }
58
59         /**
60          * Returns the identifier of the request.
61          *
62          * @return The identifier of the request
63          */
64         @Override
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 }