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 java.io.IOException;
61 import java.io.InputStream;
62 import java.io.InputStreamReader;
63 import java.io.Reader;
64 import java.io.StringReader;
65 import java.io.UnsupportedEncodingException;
66 import java.lang.reflect.UndeclaredThrowableException;
67 import java.util.ArrayList;
68 import java.util.Enumeration;
69 import java.util.List;
70 import java.util.Vector;
71 import net.jxta.discovery.DiscoveryService;
72 import net.jxta.document.Advertisement;
73 import net.jxta.document.AdvertisementFactory;
74 import net.jxta.document.Attributable;
75 import net.jxta.document.Attribute;
76 import net.jxta.document.Document;
77 import net.jxta.document.Element;
78 import net.jxta.document.MimeMediaType;
79 import net.jxta.document.StructuredDocumentFactory;
80 import net.jxta.document.StructuredTextDocument;
81 import net.jxta.document.TextElement;
82 import net.jxta.document.XMLDocument;
83 import net.jxta.document.XMLElement;
84 import net.jxta.protocol.DiscoveryResponseMsg;
85 import net.jxta.protocol.PeerAdvertisement;
86 import java.util.logging.Level;
87 import net.jxta.logging.Logging;
88 import java.util.logging.Logger;
94 * <p/>This message is part of the standard JXTA Peer Discovery Protocol (PDP).
97 * <xs:element name="DiscoveryResponse" type="jxta:DiscoveryResponse"/>
99 * <xs:complexType name="DiscoveryResponse">
101 * <xs:element name="Type" type="jxta:DiscoveryQueryType"/>
102 * <xs:element name="Count" type="xs:unsignedInt" minOccurs="0"/>
103 * <xs:element name="Attr" type="xs:string" minOccurs="0"/>
104 * <xs:element name="Value" type="xs:string" minOccurs="0"/>
105 * <!-- The following should refer to a peer adv, but is instead a whole doc for historical reasons -->
106 * <xs:element name="PeerAdv" minOccurs="0">
107 * <xs:complexType>
108 * <xs:simpleContent>
109 * <xs:extension base="xs:string">
110 * <xs:attribute name="Expiration" type="xs:unsignedLong"/>
112 * </xs:simpleContent>
113 * </xs:complexType>
115 * <xs:element name="Response" maxOccurs="unbounded">
116 * <xs:complexType>
117 * <xs:simpleContent>
118 * <xs:extension base="xs:string">
119 * <xs:attribute name="Expiration" type="xs:unsignedLong"/>
121 * </xs:simpleContent>
122 * </xs:complexType>
125 * </xs:complexType>
128 *@see net.jxta.discovery.DiscoveryService
129 *@see net.jxta.impl.discovery.DiscoveryServiceImpl
130 *@see <a href="https://jxta-spec.dev.java.net/nonav/JXTAProtocols.html#proto-pdp"
131 * target="_blank">JXTA Protocols Specification : Peer Discovery Protocol</a>
133 public class DiscoveryResponse extends DiscoveryResponseMsg {
135 private final static transient Logger LOG = Logger.getLogger(DiscoveryResponse.class.getName());
137 private final static String countTag = "Count";
138 private final static String expirationTag = "Expiration";
139 private final static String peerAdvTag = "PeerAdv";
140 private final static String queryAttrTag = "Attr";
141 private final static String queryValueTag = "Value";
142 private final static String responsesTag = "Response";
143 private final static String typeTag = "Type";
146 * Constructor for new instances.
148 public DiscoveryResponse() {}
151 * Construct from a StructuredDocument
153 *@param root Description of the Parameter
155 public DiscoveryResponse(Element root) {
157 if (!XMLElement.class.isInstance(root)) {
158 throw new IllegalArgumentException(getClass().getName() + " only supports XMLElement");
160 XMLElement doc = (XMLElement) root;
161 String docName = doc.getName();
163 if (!getAdvertisementType().equals(docName)) {
164 throw new IllegalArgumentException(
165 "Could not construct : " + getClass().getName() + " from doc containing a " + docName);
174 public Document getDocument(MimeMediaType asMimeType) {
176 StructuredTextDocument adv = (StructuredTextDocument)
177 StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType());
179 if (adv instanceof XMLDocument) {
180 ((XMLDocument) adv).addAttribute("xmlns:jxta", "http://jxta.org");
185 e = adv.createElement(countTag, Integer.toString(responses.size()));
187 e = adv.createElement(typeTag, Integer.toString(type));
190 PeerAdvertisement myPeerAdv = getPeerAdvertisement();
192 if (null != myPeerAdv) {
193 e = adv.createElement(peerAdvTag, myPeerAdv.toString());
197 if ((attr != null) && (attr.length() > 0)) {
198 e = adv.createElement(queryAttrTag, getQueryAttr());
200 if ((value != null) && (value.length() > 0)) {
201 e = adv.createElement(queryValueTag, value);
206 Enumeration<String> advs = getResponses();
207 Enumeration exps = getExpirations();
210 while (advs.hasMoreElements()) {
211 Long l = (Long) exps.nextElement();
212 String response = advs.nextElement();
214 e = adv.createElement(responsesTag, response);
217 if (adv instanceof Attributable) {
218 ((Attributable) e).addAttribute(expirationTag, l.toString());
221 } catch (Exception failed) {
222 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
223 LOG.log(Level.WARNING, "Got an Exception during doc creation", failed);
225 IllegalStateException failure = new IllegalStateException("Got an Exception during doc creation");
227 failure.initCause(failed);
234 * Parses a document into this object
238 private void readIt(XMLElement doc) {
239 Vector<String> res = new Vector<String>();
240 Vector<Long> exps = new Vector<Long>();
243 Enumeration elements = doc.getChildren();
245 while (elements.hasMoreElements()) {
246 XMLElement elem = (XMLElement) elements.nextElement();
248 if (elem.getName().equals(typeTag)) {
249 type = Integer.parseInt(elem.getTextValue());
253 if (elem.getName().equals(peerAdvTag)) {
254 String peerString = elem.getTextValue();
256 if (null == peerString) {
260 peerString = peerString.trim();
261 if (peerString.length() > 0) {
262 XMLDocument xmlPeerAdv = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(
263 MimeMediaType.XMLUTF8, new StringReader(peerString));
265 setPeerAdvertisement((PeerAdvertisement) AdvertisementFactory.newAdvertisement(xmlPeerAdv));
270 if (elem.getName().equals(queryAttrTag)) {
271 setQueryAttr(elem.getTextValue());
275 if (elem.getName().equals(queryValueTag)) {
276 setQueryValue(elem.getTextValue());
280 if (elem.getName().equals(responsesTag)) {
282 String aResponse = elem.getTextValue();
284 if (null == aResponse) {
285 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
286 LOG.fine("Discarding an empty response tag");
293 // get expiration associated with this response
294 Attribute attr = (elem).getAttribute(expirationTag);
297 exp = Long.parseLong(attr.getValue());
299 // if there are no attribute use DEFAULT_EXPIRATION
300 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
302 "Received an old-style DiscoveryResponse.\n You received a response from a peer that does \nnot support advertisement aging. \nSetting expiration to DiscoveryService.DEFAULT_EXPIRATION ");
304 exp = DiscoveryService.DEFAULT_EXPIRATION;
310 } catch (Exception failed) {
311 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
312 LOG.log(Level.WARNING, "Got an Exception during Parse ", failed);
314 IllegalArgumentException failure = new IllegalArgumentException("Got an Exception during parse");
316 failure.initCause(failed);
321 setExpirations(exps);
325 * Return a string representation of this message. The string will
326 * contain the message formated as a UTF-8 encoded XML Document.
328 * @return String a String containing the message.
331 public String toString() {
334 XMLDocument doc = (XMLDocument) getDocument(MimeMediaType.XMLUTF8);
336 return doc.toString();
337 } catch (Throwable e) {
338 if (e instanceof Error) {
340 } else if (e instanceof RuntimeException) {
341 throw (RuntimeException) e;
343 throw new UndeclaredThrowableException(e);