2 * jFCPlib - FCPPluginReply.java - Copyright © 2008–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.fcp;
20 import java.io.InputStream;
21 import java.util.HashMap;
23 import java.util.Map.Entry;
26 * The “FCPPluginReply” is sent by a plugin as a response to a
27 * {@link FCPPluginMessage} message.
29 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31 public class FCPPluginReply extends BaseMessage implements Identifiable {
33 /** The payload input stream. */
34 private final InputStream payloadInputStream;
37 * Creates a new “FCPPluginReply” message that wraps the received message.
39 * @param receivedMessage
40 * The received message
41 * @param payloadInputStream
42 * The optional input stream for the payload
44 public FCPPluginReply(FcpMessage receivedMessage, InputStream payloadInputStream) {
45 super(receivedMessage);
46 this.payloadInputStream = payloadInputStream;
50 * Returns the name of the plugin.
52 * @return The name of the plugin
54 public String getPluginName() {
55 return getField("PluginName");
59 * Returns the identifier of the request.
61 * @return The identifier of the request
64 public String getIdentifier() {
65 return getField("Identifier");
69 * Returns the length of the optional payload.
71 * @return The length of the payload, or <code>-1</code> if there is no
72 * payload or the length could not be parsed
74 public long getDataLength() {
75 return FcpUtils.safeParseLong(getField("DataLength"));
79 * Returns a reply from the plugin.
82 * The name of the reply
83 * @return The value of the reply
85 public String getReply(String key) {
86 return getField("Replies." + key);
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!
94 * @return All replies from the plugin
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());
108 * Returns the optional payload.
110 * @return The payload of the reply, or <code>null</code> if there is no
113 public InputStream getPayloadInputStream() {
114 return payloadInputStream;