X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Futil%2Ffreenet%2Ffcp2%2FMessage.java;fp=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Futil%2Ffreenet%2Ffcp2%2FMessage.java;h=6c934ec578adb54fc98792e9d5596844d0be970c;hb=38bdc433e50669e8244a63b5af59e597f88f1d29;hp=0000000000000000000000000000000000000000;hpb=f14b9fbe6d88e23920b10a75ebeba4d38390301b;p=jSite.git diff --git a/src/main/java/de/todesbaum/util/freenet/fcp2/Message.java b/src/main/java/de/todesbaum/util/freenet/fcp2/Message.java new file mode 100644 index 0000000..6c934ec --- /dev/null +++ b/src/main/java/de/todesbaum/util/freenet/fcp2/Message.java @@ -0,0 +1,175 @@ +/* + * jSite - Message.java - Copyright © 2006–2012 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package de.todesbaum.util.freenet.fcp2; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +/** + * Contains replies sent by the Freenet node. A message always has a name, and + * most of the messages also have an identifier which binds it to a specific + * command. Exceptions are among others NodeHello, + * SSKKeypair, and EndListPersistentRequests. + * + * @author David Roden <droden@gmail.com> + * @version $Id$ + * @see de.todesbaum.util.freenet.fcp2.Client + */ +public class Message { + + /** The name of this message. */ + private final String name; + + /** The identifier of this message. */ + private String identifier = ""; + + /** The parameters of this message. */ + private Map parameters = new HashMap(); + + /** The payload. */ + private InputStream payloadInputStream; + + /** + * Creates a new message with the specified name. + * + * @param name + * The name of this message + */ + public Message(String name) { + this.name = name; + } + + /** + * Returns the identifier of this message. + * + * @return The identifier + */ + public String getIdentifier() { + return identifier; + } + + /** + * Sets the identifier of this message. + * + * @param identifier + * The identifier of this message + */ + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + /** + * Returns the name of this message. + * + * @return The name of this message + */ + public String getName() { + return name; + } + + /** + * Tests whether this message contains the parameter with the specified key. + * Key names are compared ignoring case. + * + * @param key + * The name of the parameter + * @return true if this parameter exists in this message, + * false otherwise + */ + public boolean containsKey(String key) { + return parameters.containsKey(key.toLowerCase()); + } + + /** + * Returns all parameters of this message. The keys of the entries are all + * lower case so if you want to match the parameter names you have to watch + * out. + * + * @return All parameters of this message + */ + public Set> entrySet() { + return parameters.entrySet(); + } + + /** + * Returns the value of the parameter with the name specified by + * key. + * + * @param key + * The name of the parameter + * @return The value of the parameter + */ + public String get(String key) { + return parameters.get(key.toLowerCase()); + } + + /** + * Stores the specified value as parameter with the name specified by + * key. + * + * @param key + * The name of the parameter + * @param value + * The value of the parameter + * @return The previous value, or null if there was no + * previous value + */ + public String put(String key, String value) { + return parameters.put(key.toLowerCase(), value); + } + + /** + * Returns the number of parameters in this message. + * + * @return The number of parameters + */ + public int size() { + return parameters.size(); + } + + /** + * @return Returns the payloadInputStream. + */ + public InputStream getPayloadInputStream() { + return payloadInputStream; + } + + /** + * @param payloadInputStream + * The payloadInputStream to set. + */ + public void setPayloadInputStream(InputStream payloadInputStream) { + this.payloadInputStream = payloadInputStream; + } + + /** + * Returns a textual representation of this message, containing its name, + * the identifier, and the parameters. + * + * @return A textual representation of this message + */ + @Override + public String toString() { + return name + "[identifier=" + identifier + ",parameters=" + parameters.toString() + "]"; + } + +}