// TODO catch if 404 etc happens public static XmlDocument FetchFeedAndNormaliseToAtom(string url) { string cacheKey = string.Format("SyndicationUtil.FetchFeedAndNormaliseToAtom::{0}", url); if (CacheUtil.Exist(cacheKey)) { return(CacheUtil.Get <XmlDocument>(cacheKey)); } XmlDocument doc = null; Assert.EnsureStringValue(url, "url"); SyndicationFeed feed = null; using (XmlTextReader r = new XmlTextReader(url)) { feed = SyndicationFeed.Load(r); } if (feed == null) { return(null); } StringWriter stringWriter = new StringWriter(); XmlTextWriter xmltextWriter = new XmlTextWriter(stringWriter); xmltextWriter.Formatting = Formatting.Indented; Atom10FeedFormatter formatter = feed.GetAtom10Formatter(); formatter.WriteTo(xmltextWriter); doc = new XmlDocument(); string xml = stringWriter.ToString(); // We do not wan't namespaces on the first node // TODO Do this smarter xml = xml.Remove(0, xml.IndexOf(">")); xml = "<feed>" + xml; doc.LoadXml(xml); CacheUtil.Insert(cacheKey, doc, 5, false); return(doc); }
private static string GetFormatViaKey(string key, out bool failed) { string cacheKey = string.Format("{0}#key:{1}", CacheUtil.GetCacheKey("FormatDate"), key); //"CacheUtil.GetCacheKey("FormatDate"); if (CacheUtil.Exist(cacheKey)) { failed = false; return(CacheUtil.Get <string>(cacheKey)); } Node homeNode = UpacContext.Current.HomeNode; if (homeNode == null) { failed = true; return("FormatDate failed: Not in upac context"); } Node node = homeNode.GetDescendantViaDocumentTypePath("ConfigurationContainer/ConfigurationDateTime"); if (node == null) { failed = true; return("FormatDate failed: Could not find config node ConfigurationContainer/ConfigurationDateTime"); } string format = node.GetPropertyValue(key); if (string.IsNullOrEmpty(format)) { failed = true; return(String.Concat("Date format key not found: ", key)); } CacheUtil.Insert(cacheKey, format, 10, true); failed = false; return(format); }
/// <summary> /// Gets the O embed HTML. /// </summary> /// <param name="url">The OEmbed URL/Source.</param> /// <param name="maxWidth">Width of the max.</param> /// <param name="maxHeight">Height of the max.</param> /// <returns>The html or an error message</returns> public static string GetOEmbedHtml(string url, string maxWidth, string maxHeight) { StringBuilder output = new StringBuilder(); if (!string.IsNullOrEmpty(url)) { string jsonResponse = string.Empty; StringBuilder oohembedUrl = new StringBuilder(); oohembedUrl.Append("http://oohembed.com/oohembed/?"); if (!string.IsNullOrEmpty(maxWidth)) { int width = CommonUtil.ConvertToIntSafe(maxWidth, -1); if (width > -1) { oohembedUrl.Append("maxwidth="); oohembedUrl.Append(width); oohembedUrl.Append("&"); } } if (!string.IsNullOrEmpty(maxHeight)) { int height = CommonUtil.ConvertToIntSafe(maxHeight, -1); if (height > -1) { oohembedUrl.Append("maxheight="); oohembedUrl.Append(height); oohembedUrl.Append("&"); } } oohembedUrl.Append("url="); oohembedUrl.Append(HttpUtility.UrlEncode(url)); string cacheKey = string.Format("Upac.Core.Utilities.OEmbed::{0}", oohembedUrl); Log.DebugFormat("Try to look in cache via cachekey: {0}", cacheKey); if (CacheUtil.Exist(cacheKey)) { Log.Debug("Cache gave a hit. Return html from cache"); return(CacheUtil.Get <string>(cacheKey)); } Log.Debug("No content in cache"); System.Net.WebClient webClient = new System.Net.WebClient(); try { jsonResponse = webClient.DownloadString(oohembedUrl.ToString()); } catch (System.Net.WebException exception) { if (exception.Status != System.Net.WebExceptionStatus.ProtocolError) { Log.Error("Exception smidt", exception); throw; } // If it's a ProtocolError. // oembed throws a 404 status, if the resource could not be looked up. output.Append("<a href=\"" + url + "\">" + url + "</a>"); output.Append("<br /><em>Error with embedding the source<br />oohembed source: " + oohembedUrl + "</em>"); } if (!string.IsNullOrEmpty(jsonResponse)) { JavaScriptSerializer scriptSerializer = new JavaScriptSerializer(); OEmbedResponse businessObject = scriptSerializer.Deserialize <OEmbedResponse>(jsonResponse); string html = businessObject.GetHtml(); CacheUtil.Insert(cacheKey, html, 10, false); output.Append(html); } } return(output.ToString()); }