]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/DiscoveryResponse.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / DiscoveryResponse.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.protocol;
58
59
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;
89
90
91 /**
92  *  DiscoveryResponse.
93  *
94  *  <p/>This message is part of the standard JXTA Peer Discovery Protocol (PDP).
95  *
96  *  <pre>
97  * &lt;xs:element name="DiscoveryResponse" type="jxta:DiscoveryResponse"/>
98  *
99  * &lt;xs:complexType name="DiscoveryResponse">
100  *   &lt;xs:sequence>
101  *     &lt;xs:element name="Type" type="jxta:DiscoveryQueryType"/>
102  *     &lt;xs:element name="Count" type="xs:unsignedInt" minOccurs="0"/>
103  *     &lt;xs:element name="Attr" type="xs:string" minOccurs="0"/>
104  *     &lt;xs:element name="Value" type="xs:string" minOccurs="0"/>
105  *     &lt;!-- The following should refer to a peer adv, but is instead a whole doc for historical reasons -->
106  *     &lt;xs:element name="PeerAdv" minOccurs="0">
107  *     &lt;xs:complexType>
108  *       &lt;xs:simpleContent>
109  *         &lt;xs:extension base="xs:string">
110  *           &lt;xs:attribute name="Expiration" type="xs:unsignedLong"/>
111  *         &lt;/xs:extension>
112  *       &lt;/xs:simpleContent>
113  *     &lt;/xs:complexType>
114  *     &lt;/xs:element>
115  *     &lt;xs:element name="Response" maxOccurs="unbounded">
116  *     &lt;xs:complexType>
117  *       &lt;xs:simpleContent>
118  *         &lt;xs:extension base="xs:string">
119  *           &lt;xs:attribute name="Expiration" type="xs:unsignedLong"/>
120  *         &lt;/xs:extension>
121  *       &lt;/xs:simpleContent>
122  *     &lt;/xs:complexType>
123  *     &lt;/xs:element>
124  *   &lt;/xs:sequence>
125  * &lt;/xs:complexType>
126  * </pre>
127  *
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>
132  */
133 public class DiscoveryResponse extends DiscoveryResponseMsg {
134
135     private final static transient Logger LOG = Logger.getLogger(DiscoveryResponse.class.getName());
136
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";
144
145     /**
146      *  Constructor for new instances.
147      */
148     public DiscoveryResponse() {}
149
150     /**
151      *  Construct from a StructuredDocument
152      *
153      *@param  root  Description of the Parameter
154      */
155     public DiscoveryResponse(Element root) {
156
157         if (!XMLElement.class.isInstance(root)) {
158             throw new IllegalArgumentException(getClass().getName() + " only supports XMLElement");
159         }
160         XMLElement doc = (XMLElement) root;
161         String docName = doc.getName();
162
163         if (!getAdvertisementType().equals(docName)) {
164             throw new IllegalArgumentException(
165                     "Could not construct : " + getClass().getName() + " from doc containing a " + docName);
166         }
167         readIt(doc);
168     }
169
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public Document getDocument(MimeMediaType asMimeType) {
175
176         StructuredTextDocument adv = (StructuredTextDocument)
177                 StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType());
178
179         if (adv instanceof XMLDocument) {
180             ((XMLDocument) adv).addAttribute("xmlns:jxta", "http://jxta.org");
181         }
182
183         Element e;
184
185         e = adv.createElement(countTag, Integer.toString(responses.size()));
186         adv.appendChild(e);
187         e = adv.createElement(typeTag, Integer.toString(type));
188         adv.appendChild(e);
189
190         PeerAdvertisement myPeerAdv = getPeerAdvertisement();
191
192         if (null != myPeerAdv) {
193             e = adv.createElement(peerAdvTag, myPeerAdv.toString());
194             adv.appendChild(e);
195         }
196
197         if ((attr != null) && (attr.length() > 0)) {
198             e = adv.createElement(queryAttrTag, getQueryAttr());
199             adv.appendChild(e);
200             if ((value != null) && (value.length() > 0)) {
201                 e = adv.createElement(queryValueTag, value);
202                 adv.appendChild(e);
203             }
204         }
205
206         Enumeration<String> advs = getResponses();
207         Enumeration exps = getExpirations();
208
209         try {
210             while (advs.hasMoreElements()) {
211                 Long l = (Long) exps.nextElement();
212                 String response = advs.nextElement();
213
214                 e = adv.createElement(responsesTag, response);
215
216                 adv.appendChild(e);
217                 if (adv instanceof Attributable) {
218                     ((Attributable) e).addAttribute(expirationTag, l.toString());
219                 }
220             }
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);
224             }
225             IllegalStateException failure = new IllegalStateException("Got an Exception during doc creation");
226
227             failure.initCause(failed);
228             throw failure;
229         }
230         return adv;
231     }
232
233     /**
234      *  Parses a document into this object
235      *
236      *@param  doc  Document
237      */
238     private void readIt(XMLElement doc) {
239         Vector<String> res = new Vector<String>();
240         Vector<Long> exps = new Vector<Long>();
241
242         try {
243             Enumeration elements = doc.getChildren();
244
245             while (elements.hasMoreElements()) {
246                 XMLElement elem = (XMLElement) elements.nextElement();
247
248                 if (elem.getName().equals(typeTag)) {
249                     type = Integer.parseInt(elem.getTextValue());
250                     continue;
251                 }
252
253                 if (elem.getName().equals(peerAdvTag)) {
254                     String peerString = elem.getTextValue();
255
256                     if (null == peerString) {
257                         continue;
258                     }
259
260                     peerString = peerString.trim();
261                     if (peerString.length() > 0) {
262                         XMLDocument xmlPeerAdv = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(
263                                 MimeMediaType.XMLUTF8, new StringReader(peerString));
264
265                         setPeerAdvertisement((PeerAdvertisement) AdvertisementFactory.newAdvertisement(xmlPeerAdv));
266                     }
267                     continue;
268                 }
269
270                 if (elem.getName().equals(queryAttrTag)) {
271                     setQueryAttr(elem.getTextValue());
272                     continue;
273                 }
274
275                 if (elem.getName().equals(queryValueTag)) {
276                     setQueryValue(elem.getTextValue());
277                     continue;
278                 }
279
280                 if (elem.getName().equals(responsesTag)) {
281                     // get the response
282                     String aResponse = elem.getTextValue();
283
284                     if (null == aResponse) {
285                         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
286                             LOG.fine("Discarding an empty response tag");
287                         }
288                         continue;
289                     }
290                     res.add(aResponse);
291
292                     long exp;
293                     // get expiration associated with this response
294                     Attribute attr = (elem).getAttribute(expirationTag);
295
296                     if (null != attr) {
297                         exp = Long.parseLong(attr.getValue());
298                     } else {
299                         // if there are no attribute use DEFAULT_EXPIRATION
300                         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
301                             LOG.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 ");
303                         }
304                         exp = DiscoveryService.DEFAULT_EXPIRATION;
305                     }
306
307                     exps.add(exp);
308                 }
309             }
310         } catch (Exception failed) {
311             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
312                 LOG.log(Level.WARNING, "Got an Exception during Parse ", failed);
313             }
314             IllegalArgumentException failure = new IllegalArgumentException("Got an Exception during parse");
315
316             failure.initCause(failed);
317             throw failure;
318         }
319         
320         setResponses(res);
321         setExpirations(exps);
322     }
323
324     /**
325      * Return a string representation of this message. The string will
326      * contain the message formated as a UTF-8 encoded XML Document.
327      *
328      * @return String  a String containing the message.
329      */
330     @Override
331     public String toString() {
332         
333         try {
334             XMLDocument doc = (XMLDocument) getDocument(MimeMediaType.XMLUTF8);
335             
336             return doc.toString();
337         } catch (Throwable e) {
338             if (e instanceof Error) {
339                 throw (Error) e;
340             } else if (e instanceof RuntimeException) {
341                 throw (RuntimeException) e;
342             } else {
343                 throw new UndeclaredThrowableException(e);
344             }
345         }
346     }
347 }