]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/id/CBID/Instantiator.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / id / CBID / Instantiator.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.id.CBID;
58
59
60 import net.jxta.impl.id.UUID.IDBytes;
61
62 import java.io.IOException;
63 import java.io.InputStream;
64 import java.net.MalformedURLException;
65 import java.net.URI;
66 import java.net.URISyntaxException;
67 import java.net.URL;
68 import java.net.UnknownServiceException;
69
70
71 /**
72  * The instantiator for the CBID ID Format.
73  * <p/>
74  * <p/>For "seed" variant constructors, the "seed" must be a certificate.
75  */
76 public class Instantiator implements net.jxta.id.IDFactory.URIInstantiator {
77
78     /**
79      * Our ID Format
80      */
81     final static String CBIDEncoded = "cbid";
82
83     /**
84      * {@inheritDoc}
85      */
86     public String getSupportedIDFormat() {
87         return CBIDEncoded;
88     }
89
90     /**
91      * {@inheritDoc}
92      */
93     public net.jxta.id.ID fromURL(URL source) throws MalformedURLException, UnknownServiceException {
94
95         // check the protocol
96         if (!net.jxta.id.ID.URIEncodingName.equalsIgnoreCase(source.getProtocol())) {
97             throw new UnknownServiceException("URI protocol type was not as expected.");
98         }
99
100         String encoded = source.getFile();
101
102         int colonAt = encoded.indexOf(':');
103
104         // There's a colon right?
105         if (-1 == colonAt) {
106             throw new UnknownServiceException("URN namespace was missing.");
107         }
108
109         // check the namespace
110         if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(encoded.substring(0, colonAt))) {
111             throw new UnknownServiceException("URN namespace was not as expected.");
112         }
113
114         // skip the namespace portion and the colon
115         encoded = encoded.substring(colonAt + 1);
116
117         int dashAt = encoded.indexOf('-');
118
119         // there's a dash, right?
120         if (-1 == dashAt) {
121             throw new UnknownServiceException("URN Encodingtype was missing.");
122         }
123
124         if (!encoded.substring(0, dashAt).equals(getSupportedIDFormat())) {
125             throw new UnknownServiceException("JXTA ID Format was not as expected.");
126         }
127
128         // skip the dash
129         encoded = encoded.substring(dashAt + 1);
130
131         // check that the length is even
132         if (0 != (encoded.length() % 2)) {
133             throw new MalformedURLException("URN contains an odd number of chars");
134         }
135
136         // check that the length is long enough
137         if (encoded.length() < 2) {
138             throw new MalformedURLException("URN does not contain enough chars");
139         }
140
141         // check that id is short enough
142         if (IDFormat.IdByteArraySize < (encoded.length() % 2)) {
143             throw new MalformedURLException("URN contains too many chars");
144         }
145
146         net.jxta.id.ID result = null;
147         IDBytes id = new IDBytes();
148
149         try {
150             // do the primary portion.
151             for (int eachByte = 0; eachByte < ((encoded.length() / 2) - IDFormat.flagsSize); eachByte++) {
152                 int index = eachByte * 2;
153                 String twoChars = encoded.substring(index, index + 2);
154
155                 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
156             }
157
158             // do the flags
159             for (int eachByte = IDFormat.flagsOffset; eachByte < IDFormat.IdByteArraySize; eachByte++) {
160                 int index = encoded.length() - (IDFormat.IdByteArraySize - eachByte) * 2;
161                 String twoChars = encoded.substring(index, index + 2);
162
163                 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
164             }
165         } catch (NumberFormatException caught) {
166             throw new MalformedURLException("Invalid Character in JXTA URI");
167         }
168
169         switch (id.bytes[IDFormat.flagsOffset + IDFormat.flagsIdTypeOffset]) {
170         case IDFormat.flagCodatID:
171             result = new CodatID(id);
172             break;
173
174         case IDFormat.flagPeerGroupID:
175             result = new PeerGroupID(id);
176             result = (PeerGroupID) IDFormat.translateToWellKnown(result);
177             break;
178
179         case IDFormat.flagPeerID:
180             result = new PeerID(id);
181             break;
182
183         case IDFormat.flagPipeID:
184             result = new PipeID(id);
185             break;
186
187         case IDFormat.flagModuleClassID:
188             result = new ModuleClassID(id);
189             break;
190
191         case IDFormat.flagModuleSpecID:
192             result = new ModuleSpecID(id);
193             break;
194
195         default:
196             throw new MalformedURLException("JXTA ID Type not recognized");
197         }
198
199         return result;
200     }
201
202     /**
203      * {@inheritDoc}
204      */
205     public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID) {
206         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
207
208         return new CodatID(peerGroupID);
209     }
210
211     /**
212      * {@inheritDoc}
213      */
214     public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
215         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
216
217         return new CodatID(peerGroupID, seed);
218     }
219
220     /**
221      * {@inheritDoc}
222      */
223     public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID, InputStream in) throws IOException {
224         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
225
226         return new CodatID(peerGroupID, in);
227     }
228
229     /**
230      * {@inheritDoc}
231      */
232     public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed, InputStream in) throws IOException {
233         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
234
235         return new CodatID(peerGroupID, seed, in);
236     }
237
238     /**
239      * {@inheritDoc}
240      */
241     public net.jxta.peergroup.PeerGroupID newPeerGroupID() {
242         return new PeerGroupID();
243     }
244
245     /**
246      * {@inheritDoc}
247      */
248     public net.jxta.peergroup.PeerGroupID newPeerGroupID(byte[] seed) {
249         return new PeerGroupID(seed);
250     }
251
252     /**
253      * {@inheritDoc}
254      */
255     public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent) {
256         return new PeerGroupID();
257     }
258
259     /**
260      * {@inheritDoc}
261      */
262     public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent, byte[] seed) {
263         PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(parent);
264
265         return new PeerGroupID(parentGroupID, seed);
266     }
267
268     /**
269      * {@inheritDoc}
270      */
271     public net.jxta.peer.PeerID newPeerID(net.jxta.peergroup.PeerGroupID groupID) {
272         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
273
274         throw new UnsupportedOperationException("Must provide a cert as seed to generate a peer id.");
275     }
276
277     /**
278      * {@inheritDoc}
279      */
280     public net.jxta.peer.PeerID newPeerID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
281         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
282
283         return new PeerID(peerGroupID, seed);
284     }
285
286     /**
287      * {@inheritDoc}
288      */
289     public net.jxta.pipe.PipeID newPipeID(net.jxta.peergroup.PeerGroupID groupID) {
290         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
291
292         return new PipeID(peerGroupID);
293     }
294
295     /**
296      * {@inheritDoc}
297      */
298     public net.jxta.pipe.PipeID newPipeID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
299         PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
300
301         return new PipeID(peerGroupID, seed);
302     }
303
304     /**
305      * {@inheritDoc}
306      */
307     public net.jxta.platform.ModuleClassID newModuleClassID() {
308         return new ModuleClassID();
309     }
310
311     /**
312      * {@inheritDoc}
313      */
314     public net.jxta.platform.ModuleClassID newModuleClassID(net.jxta.platform.ModuleClassID classID) {
315         return new ModuleClassID((ModuleClassID) classID);
316     }
317
318     /**
319      * {@inheritDoc}
320      */
321     public net.jxta.platform.ModuleSpecID newModuleSpecID(net.jxta.platform.ModuleClassID classID) {
322         return new ModuleSpecID((ModuleClassID) classID);
323     }
324
325     /**
326      * {@inheritDoc}
327      */
328     public net.jxta.id.ID fromURI(URI source) throws URISyntaxException {
329
330         // check the protocol
331         if (!net.jxta.id.ID.URIEncodingName.equalsIgnoreCase(source.getScheme())) {
332             throw new URISyntaxException(source.toString(), "URI scheme was not as expected.");
333         }
334
335         String decoded = source.getSchemeSpecificPart();
336
337         int colonAt = decoded.indexOf(':');
338
339         // There's a colon right?
340         if (-1 == colonAt) {
341             throw new URISyntaxException(source.toString(), "URN namespace was missing.");
342         }
343
344         // check the namespace
345         if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(decoded.substring(0, colonAt))) {
346             throw new URISyntaxException(source.toString()
347                     ,
348                     "URN namespace was not as expected. (" + net.jxta.id.ID.URNNamespace + "!=" + decoded.substring(0, colonAt)
349                     + ")");
350         }
351
352         // skip the namespace portion and the colon
353         decoded = decoded.substring(colonAt + 1);
354
355         return fromURNNamespaceSpecificPart(decoded);
356     }
357
358     /**
359      * {@inheritDoc}
360      */
361     public net.jxta.id.ID fromURNNamespaceSpecificPart(String source) throws URISyntaxException {
362         int dashAt = source.indexOf('-');
363
364         // there's a dash, right?
365         if (-1 == dashAt) {
366             throw new URISyntaxException(source, "URN Encodingtype was missing.");
367         }
368
369         if (!source.substring(0, dashAt).equals(getSupportedIDFormat())) {
370             throw new URISyntaxException(source, "JXTA ID Format was not as expected.");
371         }
372
373         // skip the dash
374         source = source.substring(dashAt + 1);
375
376         // check that the length is even
377         if (0 != (source.length() % 2)) {
378             throw new URISyntaxException(source, "URN contains an odd number of chars");
379         }
380
381         // check that the length is long enough
382         if (source.length() < 2) {
383             throw new URISyntaxException(source, "URN does not contain enough chars");
384         }
385
386         // check that id is short enough
387         if (IDFormat.IdByteArraySize < (source.length() % 2)) {
388             throw new URISyntaxException(source, "URN contains too many chars");
389         }
390
391         net.jxta.id.ID result = null;
392         IDBytes id = new IDBytes();
393
394         try {
395             // do the primary portion.
396             for (int eachByte = 0; eachByte < ((source.length() / 2) - IDFormat.flagsSize); eachByte++) {
397                 int index = eachByte * 2;
398                 String twoChars = source.substring(index, index + 2);
399
400                 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
401             }
402
403             // do the flags
404             for (int eachByte = IDFormat.flagsOffset; eachByte < IDFormat.IdByteArraySize; eachByte++) {
405                 int index = source.length() - (IDFormat.IdByteArraySize - eachByte) * 2;
406                 String twoChars = source.substring(index, index + 2);
407
408                 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
409             }
410         } catch (NumberFormatException caught) {
411             throw new URISyntaxException(source, "Invalid Character in JXTA URI");
412         }
413
414         switch (id.bytes[IDFormat.flagsOffset + IDFormat.flagsIdTypeOffset]) {
415         case IDFormat.flagCodatID:
416             result = new CodatID(id);
417             break;
418
419         case IDFormat.flagPeerGroupID:
420             result = new PeerGroupID(id);
421             result = (PeerGroupID) IDFormat.translateToWellKnown(result);
422             break;
423
424         case IDFormat.flagPeerID:
425             result = new PeerID(id);
426             break;
427
428         case IDFormat.flagPipeID:
429             result = new PipeID(id);
430             break;
431
432         case IDFormat.flagModuleClassID:
433             result = new ModuleClassID(id);
434             break;
435
436         case IDFormat.flagModuleSpecID:
437             result = new ModuleSpecID(id);
438             break;
439
440         default:
441             throw new URISyntaxException(source, "JXTA ID Type not recognized");
442         }
443         return result;
444     }
445 }