]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/PipeResolverMsg.java
10e82bfc5ec0a6ee8af6c20056dfc8c68b3b7439
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / PipeResolverMsg.java
1 /*
2  * Copyright (c) 2002-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 net.jxta.document.*;
61 import net.jxta.id.ID;
62 import net.jxta.id.IDFactory;
63 import net.jxta.pipe.PipeID;
64 import net.jxta.protocol.PeerAdvertisement;
65 import net.jxta.protocol.PipeResolverMessage;
66 import java.util.logging.Level;
67 import net.jxta.logging.Logging;
68 import java.util.logging.Logger;
69
70 import java.io.IOException;
71 import java.io.StringReader;
72 import java.net.URI;
73 import java.net.URISyntaxException;
74 import java.util.Enumeration;
75 import java.util.Set;
76
77
78 /**
79  * This class implements {@link net.jxta.protocol.PipeResolverMessage} by
80  * providing {@link #initialize(Element)} and {@link #getDocument(MimeMediaType)}
81  * implementations.
82  * <p/>
83  * <p/>It implements the PipeResolver message for the standard Pipe
84  * Binding Protocol (PBP) with the following schema:
85  * <p/>
86  * <pre><code>
87  * &lt;xs:element name="jxta:PipeResolver" type="jxta:PipeResolver"/>
88  * <p/>
89  * &lt;xs:simpleType name="PipeResolverMsgType">
90  *   &lt;xs:restriction base="xs:string">
91  *     &lt;!-- QUERY -->
92  *     &lt;xs:enumeration value="Query"/>
93  *     &lt;!-- ANSWER -->
94  *     &lt;xs:enumeration value="Answer"/>
95  *   &lt;/xs:restriction>
96  * &lt;/xs:simpleType>
97  * <p/>
98  * &lt;xs:complexType name="PipeResolver">
99  *   &lt;xs:sequence>
100  *     &lt;xs:element name="MsgType" type="jxta:PipeResolverMsgType"/>
101  *     &lt;xs:element name="PipeId" type="jxta:JXTAID"/>
102  * &lt;xs:element name="Type" type="xs:string"/>
103  * <p/>
104  *     &lt;!-- used in the query -->
105  * &lt;xs:element name="Cached" type="xs:boolean" default="true" minOccurs="0"/>
106  *     &lt;xs:element name="Peer" type="jxta:JXTAID" minOccurs="0" maxOccurs="unbounded"/>
107  * <p/>
108  *     &lt;!-- used in the answer -->
109  *     &lt;xs:element name="Found" minOccurs="0" type="xs:boolean"/>
110  * &lt;!-- This should refer to a peer adv, but is instead a whole doc -->
111  * &lt;xs:element name="PeerAdv" minOccurs="0" type="xs:string" />
112  *   &lt;/xs:sequence>
113  * &lt;/xs:complexType>
114  * </code></pre>
115  *
116  * @see net.jxta.pipe.PipeService
117  * @see net.jxta.impl.pipe.PipeServiceImpl
118  * @see <a href="https://jxta-spec.dev.java.net/nonav/JXTAProtocols.html#proto-pbp" target="_blank">JXTA Protocols Specification : Pipe Binding Protocol</a>
119  */
120 public class PipeResolverMsg extends PipeResolverMessage {
121
122     /**
123      * Log4J Logger
124      */
125     private final static transient Logger LOG = Logger.getLogger(PipeResolverMsg.class.getName());
126
127     private final static String MsgTypeTag = "MsgType";
128     private final static String PipeIdTag = "PipeId";
129     private final static String PipeTypeTag = "Type";
130     private final static String PeerIdTag = "Peer";
131     private final static String PeerAdvTag = "PeerAdv";
132     private final static String FoundTag = "Found";
133
134     private final static String QueryMsgType = "Query";
135     private final static String AnswerMsgType = "Answer";
136
137     public PipeResolverMsg() {}
138
139     public PipeResolverMsg(Element root) {
140         initialize(root);
141     }
142
143     private void initialize(Element root) {
144         if (!TextElement.class.isInstance(root)) {
145             throw new IllegalArgumentException(getClass().getName() + " only supports TextElement");
146         }
147
148         TextElement doc = (TextElement) root;
149
150         String docName = doc.getName();
151
152         if (!docName.equals(getMessageType())) {
153             throw new IllegalArgumentException(
154                     "Could not construct : " + getClass().getName() + "from doc containing a " + docName);
155         }
156
157         Enumeration each = doc.getChildren();
158
159         while (each.hasMoreElements()) {
160             TextElement elem = (TextElement) each.nextElement();
161
162             if (elem.getName().equals(MsgTypeTag)) {
163                 String msgtype = elem.getTextValue();
164
165                 if (msgtype.equals(QueryMsgType)) {
166                     setMsgType(PipeResolverMessage.MessageType.QUERY);
167                 } else if (msgtype.equals(AnswerMsgType)) {
168                     setMsgType(PipeResolverMessage.MessageType.ANSWER);
169                 } else {
170                     throw new IllegalArgumentException("Unexpected Message Type in parsing.");
171                 }
172                 continue;
173             }
174
175             if (elem.getName().equals(PipeIdTag)) {
176                 try {
177                     URI pipeID = new URI(elem.getTextValue());
178
179                     setPipeID(IDFactory.fromURI(pipeID));
180                 } catch (URISyntaxException badID) {
181                     throw new IllegalArgumentException("Bad pipe ID in message");
182                 }
183                 continue;
184             }
185
186             if (elem.getName().equals(PipeTypeTag)) {
187                 setPipeType(elem.getTextValue());
188                 continue;
189             }
190
191             if (elem.getName().equals(PeerIdTag)) {
192                 try {
193                     URI peerID = new URI(elem.getTextValue());
194
195                     addPeerID(IDFactory.fromURI(peerID));
196                 } catch (URISyntaxException badID) {
197                     throw new IllegalArgumentException("Bad peer ID in message");
198                 }
199                 continue;
200             }
201
202             if (elem.getName().equals(FoundTag)) {
203                 setFound(Boolean.valueOf(elem.getTextValue()));
204                 continue;
205             }
206
207             // let's check whether the responder sent us a adv
208             if (elem.getName().equals(PeerAdvTag)) {
209                 String peerAdv = elem.getTextValue();
210
211                 try {
212                     setInputPeerAdv(
213                             (PeerAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8
214                             ,
215                             new StringReader(peerAdv)));
216                 } catch (IOException caught) {
217                     if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
218                         LOG.log(Level.FINE, "Malformed peer adv in message", caught);
219                     }
220                     throw new IllegalArgumentException("Malformed peer adv in message : " + caught.getMessage());
221                 }
222             }
223
224         }
225
226         // Begin validation
227         PipeResolverMessage.MessageType msgType = getMsgType();
228
229         if (null == msgType) {
230             throw new IllegalArgumentException("Message type was never set!");
231         }
232
233         ID pipeID = getPipeID();
234
235         if ((null == pipeID) || ID.nullID.equals(pipeID) || !(pipeID instanceof PipeID)) {
236             throw new IllegalArgumentException("Input Pipe ID not set or invalid");
237         }
238
239         if (null == getPipeType()) {
240             throw new IllegalArgumentException("Pipe type was never set!");
241         }
242
243         // Response extra checks
244         if (PipeResolverMessage.MessageType.ANSWER.equals(msgType)) {
245             if (getPeerIDs().isEmpty()) {
246                 throw new IllegalArgumentException("An answer without responses is invalid");
247             }
248         }
249     }
250
251     /**
252      * Creates a document out of the message.
253      *
254      * @param encodeAs The document representation format requested.
255      * @return the message as a document.
256      */
257     @Override
258     public Document getDocument(MimeMediaType encodeAs) {
259         StructuredTextDocument doc = (StructuredTextDocument)
260                 StructuredDocumentFactory.newStructuredDocument(encodeAs, getMessageType());
261
262         if (doc instanceof Attributable) {
263             ((Attributable) doc).addAttribute("xmlns:jxta", "http://jxta.org");
264         }
265
266         PipeResolverMessage.MessageType msgType = getMsgType();
267
268         if (null == msgType) {
269             throw new IllegalStateException("Message type was never set!");
270         }
271
272         ID pipeID = getPipeID();
273
274         if ((null == pipeID) || ID.nullID.equals(pipeID) || !(pipeID instanceof PipeID)) {
275             throw new IllegalStateException("Pipe ID not set or invalid.");
276         }
277
278         String pipeType = getPipeType();
279
280         if ((null == pipeType) || (0 == pipeType.trim().length())) {
281             throw new IllegalStateException("Pipe type was never set or is invalid.");
282         }
283
284         Element element;
285
286         if (PipeResolverMessage.MessageType.QUERY.equals(msgType)) {
287             element = doc.createElement(MsgTypeTag, QueryMsgType);
288         } else if (PipeResolverMessage.MessageType.ANSWER.equals(msgType)) {
289             element = doc.createElement(MsgTypeTag, AnswerMsgType);
290         } else {
291             throw new IllegalStateException("Unknown message type :" + msgType.toString());
292         }
293         doc.appendChild(element);
294
295         element = doc.createElement(PipeIdTag, pipeID.toString());
296         doc.appendChild(element);
297
298         if ((pipeType != null) && (0 != pipeType.length())) {
299             element = doc.createElement(PipeTypeTag, pipeType);
300             doc.appendChild(element);
301         }
302
303         // Write the peer ids.
304         Set peers = getPeerIDs();
305
306         if (PipeResolverMessage.MessageType.ANSWER.equals(msgType) && peers.isEmpty()) {
307             throw new IllegalStateException("An ANSWER message must contain at least one peer as part of the response.");
308         }
309
310         for (Object peer : peers) {
311             ID aPeer = (ID) peer;
312
313             element = doc.createElement(PeerIdTag, aPeer.toString());
314             doc.appendChild(element);
315         }
316
317         if (PipeResolverMessage.MessageType.QUERY.equals(msgType)) {// nothing for now...
318         } else if (PipeResolverMessage.MessageType.ANSWER.equals(msgType)) {
319             element = doc.createElement(FoundTag, (isFound() ? Boolean.TRUE : Boolean.FALSE).toString());
320             doc.appendChild(element);
321
322             PeerAdvertisement peerAdv = getInputPeerAdv();
323
324             if (peerAdv != null) {
325                 if (!peers.contains(peerAdv.getPeerID())) {
326                     throw new IllegalStateException(
327                             "Provided Peer Advertisement does not refer to one of the peers in the response list.");
328                 }
329
330                 StructuredTextDocument asDoc = (StructuredTextDocument) peerAdv.getDocument(MimeMediaType.XMLUTF8);
331
332                 element = doc.createElement(PeerAdvTag, asDoc.toString());
333                 doc.appendChild(element);
334             }
335         } else {
336             throw new IllegalStateException("Unknown message type :" + msgType.toString());
337         }
338         return doc;
339     }
340 }
341