]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/protocol/SrdiMessageImpl.java
7e890e7aecc41d1cfd73f815c5b8ff5dc1ee4861
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / protocol / SrdiMessageImpl.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 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;
67
68 import java.io.IOException;
69 import java.io.InputStream;
70 import java.net.URI;
71 import java.net.URISyntaxException;
72 import java.util.Enumeration;
73 import java.util.Iterator;
74 import java.util.List;
75
76
77 /**
78  * SrdiMessageImpl provides the SRDI message binding
79  */
80 public class SrdiMessageImpl extends SrdiMessage {
81
82     /**
83      * The Log4J debugging category.
84      */
85     private final static Logger LOG = Logger.getLogger(SrdiMessageImpl.class.getName());
86
87     /**
88      * PeerID element name
89      */
90     public final static String pidTag = "PID";
91
92     /**
93      * scope element name
94      */
95     public final static String scopeTag = "ttl";
96
97     /**
98      * Entry element name
99      */
100     public final static String entryTag = "Entry";
101
102     /**
103      * Primary Key element name
104      */
105     public final static String pKeyTag = "PKey";
106
107     /**
108      * Secondary Key element name
109      */
110     public final static String sKeyTag = "SKey";
111
112     /**
113      * Value element name
114      */
115     public final static String valTag = "Value";
116
117     /**
118      * Expiration element name
119      */
120     public final static String expirationTag = "Expiration";
121
122     /**
123      * Construct an empty doc
124      */
125     public SrdiMessageImpl() {
126         setScope(PERSISTONLY);
127     }
128
129     /**
130      * Construct a doc from InputStream
131      *
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.
136      */
137     @Deprecated
138     public SrdiMessageImpl(InputStream stream) throws IOException {
139
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);
143
144         readIt(doc);
145     }
146
147     /**
148      * Construct from a StructuredDocument
149      *
150      * @param root the underlying document
151      */
152     public SrdiMessageImpl(Element root) {
153         if (!XMLElement.class.isInstance(root)) {
154             throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
155         }
156
157         XMLElement doc = (XMLElement) root;
158         String doctype = doc.getName();
159
160         String typedoctype = "";
161         Attribute itsType = doc.getAttribute("type");
162
163         if (null != itsType) {
164             typedoctype = itsType.getValue();
165         }
166
167         if (!doctype.equals(getMessageType()) && !getMessageType().equals(typedoctype)) {
168             throw new IllegalArgumentException(
169                     "Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
170         }
171         readIt(doc);
172     }
173
174     /**
175      * Construct a msg from entries
176      *
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
181      */
182     public SrdiMessageImpl(PeerID peerid, int scope, String pKey, List<Entry> entries) {
183
184         setPeerID(peerid);
185         setScope(scope);
186         setPrimaryKey(pKey);
187         setEntries(entries);
188     }
189
190     /**
191      * Construct a msg consisting of a single entry
192      *
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
199      */
200     public SrdiMessageImpl(PeerID peerid, int scope, String pKey, String key, String value, long expiration) {
201
202         setPeerID(peerid);
203         setScope(scope);
204         setPrimaryKey(pKey);
205         addEntry(key, value, expiration);
206     }
207
208     /**
209      * Construct a doc from vectors of strings
210      *
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
215      */
216     public SrdiMessageImpl(String peerid, int scope, String pKey, List<Entry> entries) {
217
218         PeerID pid;
219
220         try {
221             pid = (PeerID) IDFactory.fromURI(new URI(peerid));
222         } catch (URISyntaxException badID) {
223             throw new IllegalArgumentException("Invalid PeerID ID in message");
224         }
225
226         setPeerID(pid);
227         setScope(scope);
228         setPrimaryKey(pKey);
229         setEntries(entries);
230     }
231
232     /**
233      * @param doc the element
234      */
235     public void readIt(XMLElement doc) {
236
237         String key;
238         String value;
239         long expiration;
240
241         Enumeration elements = doc.getChildren();
242
243         while (elements.hasMoreElements()) {
244             XMLElement elem = (XMLElement) elements.nextElement();
245
246             if (elem.getName().equals(pidTag)) {
247                 try {
248                     URI pID = new URI(elem.getTextValue());
249
250                     setPeerID((PeerID) IDFactory.fromURI(pID));
251                 } catch (URISyntaxException badID) {
252                     throw new IllegalArgumentException("Invalid PeerID ID in message");
253                 }
254                 continue;
255             }
256             if (elem.getName().equals(pKeyTag)) {
257                 setPrimaryKey(elem.getTextValue());
258             }
259             if (elem.getName().equals(scopeTag)) {
260                 setScope(Integer.parseInt(elem.getTextValue()));
261             }
262
263             if (elem.getName().equals(entryTag)) {
264                 Attribute keyEl = elem.getAttribute(sKeyTag);
265
266                 if (keyEl == null) {
267                     key = "NA";
268                 } else {
269                     key = keyEl.getValue();
270                 }
271
272                 value = elem.getTextValue();
273                 if (null != value) {
274                     Attribute expAttr = elem.getAttribute(expirationTag);
275
276                     if (expAttr != null) {
277                         String expstr = expAttr.getValue();
278
279                         expiration = Long.parseLong(expstr);
280                     } else {
281                         expiration = -1;
282                     }
283                     SrdiMessage.Entry entry = new SrdiMessage.Entry(key, value, expiration);
284
285                     addEntry(entry);
286                 } else {
287                     if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
288                         LOG.fine("SrdiMessage Entry with a Null value");
289                     }
290                 }
291             }
292         }
293     }
294
295     /**
296      * return a Document representation of this object
297      *
298      * @param encodeAs the mime type encoding
299      * @return document represtation of this object
300      */
301     @Override
302     public Document getDocument(MimeMediaType encodeAs) {
303
304         StructuredTextDocument adv = (StructuredTextDocument)
305                 StructuredDocumentFactory.newStructuredDocument(encodeAs, getMessageType());
306
307         if (adv instanceof Attributable) {
308             ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
309         }
310
311         Element e;
312         Iterator eachEntry = getEntries().iterator();
313         PeerID peerid = getPeerID();
314
315         if (peerid != null) {
316             e = adv.createElement(pidTag, peerid.toString());
317             adv.appendChild(e);
318         }
319         if (getPrimaryKey() != null) {
320             e = adv.createElement(pKeyTag, getPrimaryKey());
321             adv.appendChild(e);
322         }
323         if (getScope() > 0) {
324             e = adv.createElement(scopeTag, Integer.toString(getScope()));
325             adv.appendChild(e);
326         }
327
328         while (eachEntry.hasNext()) {
329             SrdiMessage.Entry entry = (SrdiMessage.Entry) eachEntry.next();
330
331             if (entry.key == null && entry.value == null) {
332                 // skip bad entries
333                 continue;
334             }
335             e = adv.createElement(entryTag, entry.value);
336             adv.appendChild(e);
337             ((Attributable) e).addAttribute(expirationTag, Long.toString(entry.expiration));
338             ((Attributable) e).addAttribute(sKeyTag, entry.key);
339         }
340         return adv;
341     }
342
343     /**
344      * returns the document string representation of this object
345      *
346      * @return String representation of the of this message type
347      */
348     @Override
349     public String toString() {
350         StructuredTextDocument doc = (StructuredTextDocument) getDocument(MimeMediaType.XMLUTF8);
351
352         return doc.toString();
353     }
354 }
355