// creates an XRD obj that contains an authority resolution service endpoint with the given URI public static XRD createAuthRoot(string uri) { XRD xrd = new XRD(); // construct an authority resolution service Service srv = new Service(); TrustType tt = new TrustType(); // default trust type string authMediaType = Tags.CONTENT_TYPE_XRDS + ";" + tt.getParameterPair(); srv.addMediaType(authMediaType, SEPElement.MATCH_ATTR_CONTENT, false); srv.addType(Tags.SERVICE_AUTH_RES); srv.addURI(uri); // add it to the XRD xrd.addService(srv); return xrd; }
/* **************************************************************************** * stuff() **************************************************************************** */ /** * */ void stuff(XRIAuthority oAuth, XRD oDescriptor) { lock (_lock) { stuff(oAuth, oDescriptor, oAuth.NumSubSegments); } }
//throws XRIResolutionException /* ************************************************************************ * getDataFromURI() ************************************************************************ */ /** * */ protected InputStream getDataFromURI(URI uri, String query, ResolverFlags flags, ResolverState state) { // if we got the nonexistent "command" return a not found if (uri.getPath().indexOf("!nonexistent") >= 0) { XRD xrd = new XRD(); xrd.setQuery("!nonexistent"); xrd.setStatus(new Status(Status.AUTH_RES_NOT_FOUND)); XRDS xrds = new XRDS(); xrds.add(xrd); return new ByteArrayInputStream(xrds.toString().getBytes()); } XRDS oDescs = new XRDS(); // just the same response always XRD oFoo = new XRD(); oFoo.setQuery("!foo"); oFoo.setStatus(new Status(Status.SUCCESS)); oDescs.add(oFoo); XRD oBar = new XRD(); oBar.setQuery("!bar"); oBar.setStatus(new Status(Status.SUCCESS)); oDescs.add(oBar); if (uri.getPath().indexOf("!baz") > 0) { XRD baz = new XRD(); baz.setQuery("!baz"); baz.setStatus(new Status(Status.AUTH_RES_NOT_FOUND, "Not found")); oDescs.add(baz); } return new ByteArrayInputStream(oDescs.toString().getBytes()); }
/** * Sets the XRD for the "=" Authority. * @param oDesc The descriptor for the "=" Authority */ public void setEqualsAuthority(XRD oDesc) { GCSAuthority oAuth = new GCSAuthority("="); set(oAuth, oDesc); }
/** * Sets the XRD for the "@" Authority. * @param oDesc The descriptor for the "@" Authority * @deprecated */ public void setAtAuthority(XRD oDesc) { GCSAuthority oAuth = new GCSAuthority("@"); set(oAuth, oDesc); }
/** * Sets the XRD for the final subsegment of the XRIAuthority. * The authority may contain multiple subgments. Each internal subsegment * will not be set. * @param oAuth The XRIAuthority element to set * @param oDesc The descriptor for the final subsegment in the oAuth * @deprecated */ public void set(XRIAuthority oAuth, XRD oDesc) { moBasicCache.stuff(oAuth, oDesc); moTrustedCache.stuff(oAuth, oDesc); }
// throws XRIResolutionException protected InputStream getDataFromURI(URI oURI, string query, ResolverFlags flags, ResolverState state) { // the path better be empty if ((oURI.getPath() != null) && (oURI.getPath().Length > 0)) return null; XRD oDesc = new XRD(); XRI oExternal = new XRI("xri://@foo"); oDesc.addRef(new Ref(oExternal.toString())); XRDS oDescs = new XRDS(); oDescs.add(oDesc); state.pushResolved(oURI.toString(), flags.toString(), oDesc.toString(), oURI); return new ByteArrayInputStream(oDescs.toString().getBytes()); }
} // setUnresolved() /* **************************************************************************** * addXRIDescriptor() **************************************************************************** */ /** * Adds the specified XRI Descriptor to the chain */ public void addXRIDescriptor(XRD oXRID) { moXRIDescriptors.add(oXRID); } // addXRIDescriptor()
/* ************************************************************************ * run() ************************************************************************ */ /** * */ public void run() { XRD oDummy = new XRD(); Service dummyService = new Service(); dummyService.addMediaType(Tags.CONTENT_TYPE_XRDS + ";trust=none"); dummyService.addType(Tags.SERVICE_AUTH_RES); dummyService.addURI("http://www.example.com/xri/resolve?id=1"); oDummy.addService(dummyService); String[] oCases = { "@!a1!b2!c3!d4", "@!x1!y2!z3", "@!a1!b2!c3", "@!a1!b2", "@!a1!b2!m3", "@!a1!o2!p3", "@!a1!o2!q3", "@!a1!b2!c3!d4!e5", "@!x1!y2" }; Cache oCache = new Cache(1000); for (int i = 0; i < 1000; i++) { int x = moRand.nextInt(oCases.length); bool bStuff = moRand.nextBoolean(); XRIAuthority oAuth = (XRIAuthority) AuthorityPath.buildAuthorityPath(oCases[x]); if (bStuff) { oCache.stuff(oAuth, oDummy); } else { oCache.prune(oAuth); } oCache.find(oAuth, true); } }
/* **************************************************************************** * testConcurrent() **************************************************************************** */ /** * */ public void testConcurrent() { Cache oCache = new Cache(1000); oCache.prune((XRIAuthority) AuthorityPath.buildAuthorityPath("@")); assertTrue("Initial cache not empty", oCache.getNumNodes() == 0); XRD oDesc = new XRD(); Service atAuthService = new Service(); atAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + ";trust=none"); atAuthService.addType(Tags.SERVICE_AUTH_RES); atAuthService.addURI("http://gcs.epok.net/xri/resolve?ns=at"); oDesc.addService(atAuthService); GCSAuthority oAuth = new GCSAuthority("@"); oCache.stuff(oAuth, oDesc); assertTrue("Initial cache incorrect", oCache.getNumNodes() == 1); oCache.setMaxSize(5); Random oRand = new Random(); try { Thread[] oThreads = new StuffPruneThread[100]; for (int i = 0; i < oThreads.length; i++) { oThreads[i] = new StuffPruneThread(oRand); } for (int i = 0; i < oThreads.length; i++) { oThreads[i].start(); } for (int i = 0; i < oThreads.length; i++) { oThreads[i].join(); } } catch (Exception e) { assertTrue("Unexpected exception" + e, false); } oCache.dump(); assertTrue( "Max cache size not honored", oCache.getNumNodes() <= oCache.getMaxSize()); Cache.CachedValue oVal = oCache.find( (XRIAuthority) AuthorityPath.buildAuthorityPath("@"), false); assertTrue("Cached value for @ not found", oVal != null); }
/* ************************************************************************ * Constructor() ************************************************************************ */ /** * Constructs a value with the corresponding parameters */ CachedValue(XRD oDesc, int nDepth) { moDescriptor = oDesc; mnDepth = nDepth; } // Constructor()
} // findNode() /* **************************************************************************** * stuff() **************************************************************************** */ /** * */ synchronized void stuff(XRIAuthority oAuth, XRD oDescriptor) { stuff(oAuth, oDescriptor, oAuth.getNumSubSegments()); } // stuff()
} // main() /* **************************************************************************** * process() **************************************************************************** */ /** * Executes the xrilookup command as indicated by the input args. See * outputUsage for the forms of invocation and details about the program * arguments. * * @param sArgs - command line arguments (e.g., from main) * @param sOutput - program output (e.g., for stdout) * * @return SUCCESS or FAILURE */ public int process(StringBuilder sOutput, String[] sArgs) { try { // re-initialize variables so this may be called more than once sOutput.setLength(0); msTargetXRI = null; mbIsVerbose = false; msRootEqualsURI = ROOT_DEF_EQ_URI; msRootAtURI = ROOT_DEF_AT_URI; msRootBangURI = ROOT_DEF_BANG_URI; msProxyURI = null; mbCheckRoots = true; msRootFile = null; try { setTrustType(TRUST_TYPE); } catch (Exception e) { e.printStackTrace(); } // exit with message if no arguments passed on the command line if (sArgs.length == 0) { outputPleaseTypeHelp(sOutput); return FAILURE; } // this is the "help" form of invocation (usage 2) if (sArgs[0].equalsIgnoreCase(CMD_HELP)) { outputUsage(sOutput); return SUCCESS; } // from here on, we're dealing with the "normal" form of invocation // (usage 1). // scan the args, setting member variables for options, rootURI, // and resolve XRI int iResult = scanArgs(sOutput, sArgs); if (iResult == FAILURE) { return FAILURE; } // validate that the root uris are ok if (mbCheckRoots) { iResult = validateRootURIs(sOutput); if (iResult == FAILURE) { return FAILURE; } } // create and configure a resolver Resolver resolver = new Resolver(); // populate the root with whatever trustType the user requested String trustParam = ";" + trustType.getParameterPair(); XRD eqRoot = new XRD(); Service eqAuthService = new Service(); eqAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + trustParam, SEPElement.MATCH_ATTR_CONTENT, Boolean.FALSE); eqAuthService.addType(Tags.SERVICE_AUTH_RES); eqAuthService.addURI(msRootEqualsURI); eqRoot.addService(eqAuthService); XRD atRoot = new XRD(); Service atAuthService = new Service(); atAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + trustParam, SEPElement.MATCH_ATTR_CONTENT, Boolean.FALSE); atAuthService.addType(Tags.SERVICE_AUTH_RES); atAuthService.addURI(msRootAtURI); atRoot.addService(atAuthService); XRD bangRoot = new XRD(); Service bangAuthService = new Service(); bangAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + trustParam, SEPElement.MATCH_ATTR_CONTENT, Boolean.FALSE); bangAuthService.addType(Tags.SERVICE_AUTH_RES); bangAuthService.addURI(msRootBangURI); bangRoot.addService(bangAuthService); resolver.setAuthority("=", eqRoot); resolver.setAuthority("@", atRoot); resolver.setAuthority("!", bangRoot); if (msRootFile != null) { setRootsFromFile(resolver, msRootFile); } // invoke the tracert lookup(sOutput, resolver); } catch (Throwable oThrowable) { outputException(sOutput, oThrowable); return FAILURE; } return SUCCESS; }
/* **************************************************************************** * stuff() **************************************************************************** */ /** * Adds a descriptors associated with a subsegment to the cache. * @param oAuth - The Authority containing a subsegment whose descriptor is * provided * @param oDescriptor - The descriptor of the subsegment * @param nDepth - The index of the subsegment in oAuth */ void stuff( XRIAuthority oAuth, XRD oDescriptor, int nDepth) { lock (_lock) { // get the community node CacheNode oCommunityNode = moRootNode.mkdir(oAuth.RootAuthority); // if necessary, create a node for this authority path CacheNode oNode = oCommunityNode.mkdir(oAuth, 0, nDepth); // set the correct descriptor for the node oNode.moCacheValue = new CachedValue(oDescriptor, nDepth); trim(); } }
/* **************************************************************************** * testCache() **************************************************************************** */ /** * */ public void testCache() { Cache oCache = new Cache(1000); assertTrue("Initial cache not empty", oCache.getNumNodes() == 0); XRD oDesc = new XRD(); Service atAuthService = new Service(); atAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + ";trust=none"); atAuthService.addType(Tags.SERVICE_AUTH_RES); atAuthService.addURI("http://gcs.epok.net/xri/resolve?ns=at"); oDesc.addService(atAuthService); XRD oDummy = new XRD(); Service dummyService = new Service(); dummyService.addMediaType(Tags.CONTENT_TYPE_XRDS + ";trust=none"); dummyService.addType(Tags.SERVICE_AUTH_RES); dummyService.addURI("http://www.example.com/xri/resolve?id=1"); oDummy.addService(dummyService); GCSAuthority oAuth = new GCSAuthority("@"); oCache.stuff(oAuth, oDesc); assertTrue("Initial cache incorrect", oCache.getNumNodes() == 1); oCache.stuff( (XRIAuthority) AuthorityPath.buildAuthorityPath("@!a!b!foo"), oDummy); assertTrue("Cache size incorrect", oCache.getNumNodes() == 4); oCache.stuff( (XRIAuthority) AuthorityPath.buildAuthorityPath("@!a!c!moo"), oDummy); assertTrue("Cache size incorrect", oCache.getNumNodes() == 6); oCache.stuff( (XRIAuthority) AuthorityPath.buildAuthorityPath("@!a!c!woo"), oDummy); assertTrue("Cache size incorrect", oCache.getNumNodes() == 7); Cache.CachedValue oVal = oCache.find( (XRIAuthority) AuthorityPath.buildAuthorityPath("@!a!c!woo"), false); assertTrue("Cached value not found", oVal != null); oVal = oCache.find( (XRIAuthority) AuthorityPath.buildAuthorityPath("@!a!b!woo"), false); assertTrue("Cached value should not have been found", oVal == null); oCache.dump(); }
/* ************************************************************************ * Constructor() ************************************************************************ */ /** * Constructs a value with the corresponding parameters */ internal CachedValue(XRD oDesc, int nDepth) { moDescriptor = oDesc; mnDepth = nDepth; }
} // main() /* **************************************************************************** * process() **************************************************************************** */ /** * Executes the xritracert command as indicated by the input args. See * outputUsage for the forms of invocation and details about the program * arguments. * * @param sArgs - command line arguments (e.g., from main) * @param sOutput - program output (e.g., for stdout) * * @return SUCCESS or FAILURE */ public int process(StringBuilder sOutput, String[] sArgs) { try { // re-initialize variables so this may be called more than once sOutput.setLength(0); msTargetXRI = null; mbIsVerbose = false; mbDontOutputHeader = false; msRootEqualsURI = ROOT_DEF_URI; msRootAtURI = ROOT_DEF_URI; // exit with message if no arguments passed on the command line if (sArgs.length == 0) { outputPleaseTypeHelp(sOutput); return FAILURE; } // this is the "help" form of invocation (usage 2) if (sArgs[0].equalsIgnoreCase(CMD_HELP)) { outputUsage(sOutput); return SUCCESS; } // from here on, we're dealing with the "normal" form of invocation // (usage 1). // scan the args, setting member variables for options, rootURI, // and resolve XRI int iResult = scanArgs(sOutput, sArgs); if (iResult == FAILURE) { return FAILURE; } // validate that the root uris are ok iResult = validateRootURIs(sOutput); if (iResult == FAILURE) { return FAILURE; } // create and configure a resolver Resolver resolver = new Resolver(); XRD eqRoot = new XRD(); Service eqAuthService = new Service(); eqAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + ";trust=none"); eqAuthService.addType(Tags.SERVICE_AUTH_RES); eqAuthService.addURI(msRootEqualsURI); eqRoot.addService(eqAuthService); XRD atRoot = new XRD(); Service atAuthService = new Service(); atAuthService.addMediaType(Tags.CONTENT_TYPE_XRDS + ";trust=none"); atAuthService.addType(Tags.SERVICE_AUTH_RES); atAuthService.addURI(msRootAtURI); atRoot.addService(atAuthService); resolver.setAuthority("=", eqRoot); resolver.setAuthority("@", atRoot); // invoke the tracert tracert(sOutput, resolver); } catch (Throwable oThrowable) { outputException(sOutput, oThrowable); return FAILURE; } return SUCCESS; }