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