2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.impl.protocol;
60 import net.jxta.document.*;
61 import net.jxta.id.IDFactory;
62 import net.jxta.peer.PeerID;
63 import net.jxta.protocol.SrdiMessage;
64 import java.util.logging.Level;
65 import net.jxta.logging.Logging;
66 import java.util.logging.Logger;
68 import java.io.IOException;
69 import java.io.InputStream;
71 import java.net.URISyntaxException;
72 import java.util.Enumeration;
73 import java.util.Iterator;
74 import java.util.List;
78 * SrdiMessageImpl provides the SRDI message binding
80 public class SrdiMessageImpl extends SrdiMessage {
83 * The Log4J debugging category.
85 private final static Logger LOG = Logger.getLogger(SrdiMessageImpl.class.getName());
90 public final static String pidTag = "PID";
95 public final static String scopeTag = "ttl";
100 public final static String entryTag = "Entry";
103 * Primary Key element name
105 public final static String pKeyTag = "PKey";
108 * Secondary Key element name
110 public final static String sKeyTag = "SKey";
115 public final static String valTag = "Value";
118 * Expiration element name
120 public final static String expirationTag = "Expiration";
123 * Construct an empty doc
125 public SrdiMessageImpl() {
126 setScope(PERSISTONLY);
130 * Construct a doc from InputStream
132 * @param stream the underlying input stream.
133 * @throws IOException if an I/O error occurs.
134 * @deprecated It's better to generate the document yourself. This method
135 * cannot deduce the mime type of the content.
138 public SrdiMessageImpl(InputStream stream) throws IOException {
140 // We are asked to assume that the message from which this response
141 // is constructed is an XML document.
142 XMLDocument doc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, stream);
148 * Construct from a StructuredDocument
150 * @param root the underlying document
152 public SrdiMessageImpl(Element root) {
153 if (!XMLElement.class.isInstance(root)) {
154 throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
157 XMLElement doc = (XMLElement) root;
158 String doctype = doc.getName();
160 String typedoctype = "";
161 Attribute itsType = doc.getAttribute("type");
163 if (null != itsType) {
164 typedoctype = itsType.getValue();
167 if (!doctype.equals(getMessageType()) && !getMessageType().equals(typedoctype)) {
168 throw new IllegalArgumentException(
169 "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
175 * Construct a msg from entries
177 * @param peerid PeerID associated with this message
178 * @param scope scope @see #PERSISTONLY, #REPLICATE
179 * @param pKey primary key
180 * @param entries the entries for this message
182 public SrdiMessageImpl(PeerID peerid, int scope, String pKey, List<Entry> entries) {
191 * Construct a msg consisting of a single entry
193 * @param peerid PeerID associated with this message
194 * @param scope scope @see #PERSISTONLY, #REPLICATE
195 * @param pKey primary key
196 * @param key the secondary key
197 * @param value value for the key
198 * @param expiration expirations for this entry
200 public SrdiMessageImpl(PeerID peerid, int scope, String pKey, String key, String value, long expiration) {
205 addEntry(key, value, expiration);
209 * Construct a doc from vectors of strings
211 * @param peerid PeerID associated with this message
212 * @param scope scope @see #PERSISTONLY, #REPLICATE
213 * @param pKey primary key
214 * @param entries the entries for this message
216 public SrdiMessageImpl(String peerid, int scope, String pKey, List<Entry> entries) {
221 pid = (PeerID) IDFactory.fromURI(new URI(peerid));
222 } catch (URISyntaxException badID) {
223 throw new IllegalArgumentException("Invalid PeerID ID in message");
233 * @param doc the element
235 public void readIt(XMLElement doc) {
241 Enumeration elements = doc.getChildren();
243 while (elements.hasMoreElements()) {
244 XMLElement elem = (XMLElement) elements.nextElement();
246 if (elem.getName().equals(pidTag)) {
248 URI pID = new URI(elem.getTextValue());
250 setPeerID((PeerID) IDFactory.fromURI(pID));
251 } catch (URISyntaxException badID) {
252 throw new IllegalArgumentException("Invalid PeerID ID in message");
256 if (elem.getName().equals(pKeyTag)) {
257 setPrimaryKey(elem.getTextValue());
259 if (elem.getName().equals(scopeTag)) {
260 setScope(Integer.parseInt(elem.getTextValue()));
263 if (elem.getName().equals(entryTag)) {
264 Attribute keyEl = elem.getAttribute(sKeyTag);
269 key = keyEl.getValue();
272 value = elem.getTextValue();
274 Attribute expAttr = elem.getAttribute(expirationTag);
276 if (expAttr != null) {
277 String expstr = expAttr.getValue();
279 expiration = Long.parseLong(expstr);
283 SrdiMessage.Entry entry = new SrdiMessage.Entry(key, value, expiration);
287 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
288 LOG.fine("SrdiMessage Entry with a Null value");
296 * return a Document representation of this object
298 * @param encodeAs the mime type encoding
299 * @return document represtation of this object
302 public Document getDocument(MimeMediaType encodeAs) {
304 StructuredTextDocument adv = (StructuredTextDocument)
305 StructuredDocumentFactory.newStructuredDocument(encodeAs, getMessageType());
307 if (adv instanceof Attributable) {
308 ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
312 Iterator eachEntry = getEntries().iterator();
313 PeerID peerid = getPeerID();
315 if (peerid != null) {
316 e = adv.createElement(pidTag, peerid.toString());
319 if (getPrimaryKey() != null) {
320 e = adv.createElement(pKeyTag, getPrimaryKey());
323 if (getScope() > 0) {
324 e = adv.createElement(scopeTag, Integer.toString(getScope()));
328 while (eachEntry.hasNext()) {
329 SrdiMessage.Entry entry = (SrdiMessage.Entry) eachEntry.next();
331 if (entry.key == null && entry.value == null) {
335 e = adv.createElement(entryTag, entry.value);
337 ((Attributable) e).addAttribute(expirationTag, Long.toString(entry.expiration));
338 ((Attributable) e).addAttribute(sKeyTag, entry.key);
344 * returns the document string representation of this object
346 * @return String representation of the of this message type
349 public String toString() {
350 StructuredTextDocument doc = (StructuredTextDocument) getDocument(MimeMediaType.XMLUTF8);
352 return doc.toString();