public static string GetHighestPriorityUri(XrdDocument document, Uri relationType) { foreach (XrdLink link in document.Links) { if (link.Relation.Equals(relationType)) { return(link.Href); } } return(null); }
private void Parse() { XmlNamespaceManager nsMgr = new XmlNamespaceManager(cursor.NameTable); nsMgr.AddNamespace("xrd", "http://docs.oasis-open.org/ns/xri/xrd-1.0"); var expires = cursor.SelectSingleNode("/xrd:XRD/xrd:Expires", nsMgr); var subject = cursor.SelectSingleNode("/xrd:XRD/xrd:Subject", nsMgr); var aliases = GetAll(cursor.Select("/xrd:XRD/xrd:Alias", nsMgr)); if (subject == null) throw new XrdParseException("Missing Subject"); Uri subjectUri; if (Uri.TryCreate(subject.Value, UriKind.Absolute, out subjectUri)) { DateTime? expirationDate = null; if (expires != null) expirationDate = expires.ValueAsDateTime; List<XrdLink> links = new List<XrdLink>(); XPathNodeIterator linkIter = cursor.Select("/xrd:XRD/xrd:Link", nsMgr); while (linkIter.MoveNext()) { var rel = linkIter.Current.SelectSingleNode("@rel", nsMgr); var type = linkIter.Current.SelectSingleNode("@type", nsMgr); var href = linkIter.Current.SelectSingleNode("@href", nsMgr); var template = linkIter.Current.SelectSingleNode("@template", nsMgr); Uri relUrl; if (rel != null && Uri.TryCreate(rel.Value, UriKind.Absolute, out relUrl)) { string typeStr = null; if (type != null) typeStr = type.Value; string hrefUrl = null; if (href != null) hrefUrl = href.Value; string templateStr = null; if (template != null) templateStr = template.Value; XrdLink link = new XrdLink(relUrl, typeStr, hrefUrl, templateStr); links.Add(link); } } // Only keep the aliases that can be parsed as valid absolute URIs List<Uri> validAliases = new List<Uri>(aliases.Count); foreach (string alias in aliases) { Uri aliasUri; if (Uri.TryCreate(alias, UriKind.Absolute, out aliasUri)) validAliases.Add(aliasUri); } result = new XrdDocument(subjectUri, expirationDate, validAliases, links); } }
public XrdParser(Stream xrd) { doc = new XPathDocument(xrd); result = new XrdDocument(null); cursor = doc.CreateNavigator(); }
public static XrdDocument FetchXRD(Uri location) { HttpWebResponse response; Uri xrdUrl = null; MemoryStream xrdStream = null; try { using (MemoryStream stream = FetchWebDocument(location, XRD_AND_HTML_TYPES, out response)) { if (stream != null) { if (IsXrdDocument(response.ContentType.ToLowerInvariant(), stream)) { // We fetched an XRD document directly, skip ahead xrdUrl = location; xrdStream = stream; response.Close(); } else { #region LRDD // 1. Check the HTTP headers for Link: <...>; rel="describedby"; ... xrdUrl = FindXrdDocumentLocationInHeaders(response.Headers); // 2. Check the document body for <link rel="describedby" ...> if (xrdUrl == null) { xrdUrl = FindXrdDocumentLocationInHtmlMetaTags(stream.GetStreamString()); } // 3. TODO: Try and grab the /host-meta document if (xrdUrl == null) { xrdUrl = FindXrdDocumentLocationFromHostMeta(new Uri(location, "/host-meta")); } response.Close(); // 4. Fetch the XRD document if (xrdUrl != null) { xrdStream = FetchWebDocument(xrdUrl, XRD_TYPES, out response); if (!IsXrdDocument(response.ContentType.ToLowerInvariant(), xrdStream)) { m_log.Error("XRD fetch from " + xrdUrl + " failed"); xrdStream = null; } response.Close(); } #endregion LRDD } if (xrdStream != null) { XrdParser parser = new XrdParser(xrdStream); XrdDocument doc = parser.Document; xrdStream.Dispose(); return(doc); } } else { m_log.Warn("XRD discovery on endpoint " + location + " failed"); } } } catch (XrdParseException ex) { m_log.Warn("Failed to parse XRD document at " + location + ": " + ex.Message); } return(null); }
private void Parse() { XmlNamespaceManager nsMgr = new XmlNamespaceManager(cursor.NameTable); nsMgr.AddNamespace("xrd", "http://docs.oasis-open.org/ns/xri/xrd-1.0"); var expires = cursor.SelectSingleNode("/xrd:XRD/xrd:Expires", nsMgr); var subject = cursor.SelectSingleNode("/xrd:XRD/xrd:Subject", nsMgr); var aliases = GetAll(cursor.Select("/xrd:XRD/xrd:Alias", nsMgr)); if (subject == null) { throw new XrdParseException("Missing Subject"); } Uri subjectUri; if (Uri.TryCreate(subject.Value, UriKind.Absolute, out subjectUri)) { DateTime?expirationDate = null; if (expires != null) { expirationDate = expires.ValueAsDateTime; } List <XrdLink> links = new List <XrdLink>(); XPathNodeIterator linkIter = cursor.Select("/xrd:XRD/xrd:Link", nsMgr); while (linkIter.MoveNext()) { var rel = linkIter.Current.SelectSingleNode("@rel", nsMgr); var type = linkIter.Current.SelectSingleNode("@type", nsMgr); var href = linkIter.Current.SelectSingleNode("@href", nsMgr); var template = linkIter.Current.SelectSingleNode("@template", nsMgr); Uri relUrl; if (rel != null && Uri.TryCreate(rel.Value, UriKind.Absolute, out relUrl)) { string typeStr = null; if (type != null) { typeStr = type.Value; } string hrefUrl = null; if (href != null) { hrefUrl = href.Value; } string templateStr = null; if (template != null) { templateStr = template.Value; } XrdLink link = new XrdLink(relUrl, typeStr, hrefUrl, templateStr); links.Add(link); } } // Only keep the aliases that can be parsed as valid absolute URIs List <Uri> validAliases = new List <Uri>(aliases.Count); foreach (string alias in aliases) { Uri aliasUri; if (Uri.TryCreate(alias, UriKind.Absolute, out aliasUri)) { validAliases.Add(aliasUri); } } result = new XrdDocument(subjectUri, expirationDate, validAliases, links); } }
public static string GetHighestPriorityUri(XrdDocument document, Uri relationType) { foreach (XrdLink link in document.Links) { if (link.Relation.Equals(relationType)) return link.Href; } return null; }