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.id.CBID;
60 import net.jxta.impl.id.UUID.IDBytes;
62 import java.io.IOException;
63 import java.io.InputStream;
64 import java.net.MalformedURLException;
66 import java.net.URISyntaxException;
68 import java.net.UnknownServiceException;
72 * The instantiator for the CBID ID Format.
74 * <p/>For "seed" variant constructors, the "seed" must be a certificate.
76 public class Instantiator implements net.jxta.id.IDFactory.URIInstantiator {
81 final static String CBIDEncoded = "cbid";
86 public String getSupportedIDFormat() {
93 public net.jxta.id.ID fromURL(URL source) throws MalformedURLException, UnknownServiceException {
96 if (!net.jxta.id.ID.URIEncodingName.equalsIgnoreCase(source.getProtocol())) {
97 throw new UnknownServiceException("URI protocol type was not as expected.");
100 String encoded = source.getFile();
102 int colonAt = encoded.indexOf(':');
104 // There's a colon right?
106 throw new UnknownServiceException("URN namespace was missing.");
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.");
114 // skip the namespace portion and the colon
115 encoded = encoded.substring(colonAt + 1);
117 int dashAt = encoded.indexOf('-');
119 // there's a dash, right?
121 throw new UnknownServiceException("URN Encodingtype was missing.");
124 if (!encoded.substring(0, dashAt).equals(getSupportedIDFormat())) {
125 throw new UnknownServiceException("JXTA ID Format was not as expected.");
129 encoded = encoded.substring(dashAt + 1);
131 // check that the length is even
132 if (0 != (encoded.length() % 2)) {
133 throw new MalformedURLException("URN contains an odd number of chars");
136 // check that the length is long enough
137 if (encoded.length() < 2) {
138 throw new MalformedURLException("URN does not contain enough chars");
141 // check that id is short enough
142 if (IDFormat.IdByteArraySize < (encoded.length() % 2)) {
143 throw new MalformedURLException("URN contains too many chars");
146 net.jxta.id.ID result = null;
147 IDBytes id = new IDBytes();
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);
155 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
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);
163 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
165 } catch (NumberFormatException caught) {
166 throw new MalformedURLException("Invalid Character in JXTA URI");
169 switch (id.bytes[IDFormat.flagsOffset + IDFormat.flagsIdTypeOffset]) {
170 case IDFormat.flagCodatID:
171 result = new CodatID(id);
174 case IDFormat.flagPeerGroupID:
175 result = new PeerGroupID(id);
176 result = (PeerGroupID) IDFormat.translateToWellKnown(result);
179 case IDFormat.flagPeerID:
180 result = new PeerID(id);
183 case IDFormat.flagPipeID:
184 result = new PipeID(id);
187 case IDFormat.flagModuleClassID:
188 result = new ModuleClassID(id);
191 case IDFormat.flagModuleSpecID:
192 result = new ModuleSpecID(id);
196 throw new MalformedURLException("JXTA ID Type not recognized");
205 public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID) {
206 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
208 return new CodatID(peerGroupID);
214 public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
215 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
217 return new CodatID(peerGroupID, seed);
223 public net.jxta.codat.CodatID newCodatID(net.jxta.peergroup.PeerGroupID groupID, InputStream in) throws IOException {
224 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
226 return new CodatID(peerGroupID, in);
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);
235 return new CodatID(peerGroupID, seed, in);
241 public net.jxta.peergroup.PeerGroupID newPeerGroupID() {
242 return new PeerGroupID();
248 public net.jxta.peergroup.PeerGroupID newPeerGroupID(byte[] seed) {
249 return new PeerGroupID(seed);
255 public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent) {
256 return new PeerGroupID();
262 public net.jxta.peergroup.PeerGroupID newPeerGroupID(net.jxta.peergroup.PeerGroupID parent, byte[] seed) {
263 PeerGroupID parentGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(parent);
265 return new PeerGroupID(parentGroupID, seed);
271 public net.jxta.peer.PeerID newPeerID(net.jxta.peergroup.PeerGroupID groupID) {
272 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
274 throw new UnsupportedOperationException("Must provide a cert as seed to generate a peer id.");
280 public net.jxta.peer.PeerID newPeerID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
281 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
283 return new PeerID(peerGroupID, seed);
289 public net.jxta.pipe.PipeID newPipeID(net.jxta.peergroup.PeerGroupID groupID) {
290 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
292 return new PipeID(peerGroupID);
298 public net.jxta.pipe.PipeID newPipeID(net.jxta.peergroup.PeerGroupID groupID, byte[] seed) {
299 PeerGroupID peerGroupID = (PeerGroupID) IDFormat.translateFromWellKnown(groupID);
301 return new PipeID(peerGroupID, seed);
307 public net.jxta.platform.ModuleClassID newModuleClassID() {
308 return new ModuleClassID();
314 public net.jxta.platform.ModuleClassID newModuleClassID(net.jxta.platform.ModuleClassID classID) {
315 return new ModuleClassID((ModuleClassID) classID);
321 public net.jxta.platform.ModuleSpecID newModuleSpecID(net.jxta.platform.ModuleClassID classID) {
322 return new ModuleSpecID((ModuleClassID) classID);
328 public net.jxta.id.ID fromURI(URI source) throws URISyntaxException {
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.");
335 String decoded = source.getSchemeSpecificPart();
337 int colonAt = decoded.indexOf(':');
339 // There's a colon right?
341 throw new URISyntaxException(source.toString(), "URN namespace was missing.");
344 // check the namespace
345 if (!net.jxta.id.ID.URNNamespace.equalsIgnoreCase(decoded.substring(0, colonAt))) {
346 throw new URISyntaxException(source.toString()
348 "URN namespace was not as expected. (" + net.jxta.id.ID.URNNamespace + "!=" + decoded.substring(0, colonAt)
352 // skip the namespace portion and the colon
353 decoded = decoded.substring(colonAt + 1);
355 return fromURNNamespaceSpecificPart(decoded);
361 public net.jxta.id.ID fromURNNamespaceSpecificPart(String source) throws URISyntaxException {
362 int dashAt = source.indexOf('-');
364 // there's a dash, right?
366 throw new URISyntaxException(source, "URN Encodingtype was missing.");
369 if (!source.substring(0, dashAt).equals(getSupportedIDFormat())) {
370 throw new URISyntaxException(source, "JXTA ID Format was not as expected.");
374 source = source.substring(dashAt + 1);
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");
381 // check that the length is long enough
382 if (source.length() < 2) {
383 throw new URISyntaxException(source, "URN does not contain enough chars");
386 // check that id is short enough
387 if (IDFormat.IdByteArraySize < (source.length() % 2)) {
388 throw new URISyntaxException(source, "URN contains too many chars");
391 net.jxta.id.ID result = null;
392 IDBytes id = new IDBytes();
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);
400 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
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);
408 id.bytes[eachByte] = (byte) Integer.parseInt(twoChars, 16);
410 } catch (NumberFormatException caught) {
411 throw new URISyntaxException(source, "Invalid Character in JXTA URI");
414 switch (id.bytes[IDFormat.flagsOffset + IDFormat.flagsIdTypeOffset]) {
415 case IDFormat.flagCodatID:
416 result = new CodatID(id);
419 case IDFormat.flagPeerGroupID:
420 result = new PeerGroupID(id);
421 result = (PeerGroupID) IDFormat.translateToWellKnown(result);
424 case IDFormat.flagPeerID:
425 result = new PeerID(id);
428 case IDFormat.flagPipeID:
429 result = new PipeID(id);
432 case IDFormat.flagModuleClassID:
433 result = new ModuleClassID(id);
436 case IDFormat.flagModuleSpecID:
437 result = new ModuleSpecID(id);
441 throw new URISyntaxException(source, "JXTA ID Type not recognized");