protected virtual Localization ResolveDocsLocalization(Uri url) { // Try if the URL looks like a Tridion Docs / DDWebApp URL string urlPath = url.GetComponents(UriComponents.Path, UriFormat.Unescaped); if (string.IsNullOrEmpty(urlPath)) { return(null); // No, it doesn't } Match match = DocsPattern.Match(urlPath); if (!match.Success) { return(null); // No, it doesn't } string localizationId = match.Groups["pubId"].Value; Localization result; if (!KnownLocalizations.TryGetValue(localizationId, out result)) { result = new DocsLocalization { Id = localizationId }; KnownLocalizations[localizationId] = result; } result.EnsureInitialized(); return(result); }
public override Localization GetLocalization(string localizationId) { using (new Tracer(localizationId)) { Localization result; if (!KnownLocalizations.TryGetValue(localizationId, out result)) { // Check for namespace prefix (ish: or tcm:) if (localizationId.StartsWith("ish:")) { CmUri uri = CmUri.FromString(localizationId); result = new DocsLocalization(uri.PublicationId); } else if (localizationId.StartsWith("tcm:")) { CmUri uri = CmUri.FromString(localizationId); result = new Localization { Id = uri.ItemId.ToString() }; } else { // Attempt to resolve it from Docs var client = ApiClientFactory.Instance.CreateClient(); Publication publication = client.GetPublication(ContentNamespace.Docs, int.Parse(localizationId), null, null); result = publication != null ? new DocsLocalization(publication.PublicationId) : base.GetLocalization(localizationId); } KnownLocalizations[localizationId] = result; } return(result); } }
public virtual ActionResult Binary(int publicationId, int binaryId, params string[] rest) { try { var docsLocalization = new DocsLocalization(publicationId); StaticContentItem content = ContentProviderExt.GetStaticContentItem(binaryId, docsLocalization); return(new FileStreamResult(content.GetContentStream(), content.ContentType)); } catch (Exception ex) { return(ServerError(ex)); } }
/// <summary> /// Extracts and returns a collection of topics from the query's results /// </summary> private List <Topic> GetDocsTopics(List <ItemEdge> results) { var topics = new List <Topic>(); if (results != null) { foreach (ItemEdge edge in results) { Page page = edge.Node as Page; if (page == null) { Log.Debug("Node not is not a Page, skipping."); continue; } int docsPublicationId = (int)edge.Node.PublicationId; Localization docsLocalization = new DocsLocalization(docsPublicationId); docsLocalization.EnsureInitialized(); // Deserialize Page Content as R2 Data Model string pageModelJson = page.RawContent.Content; PageModelData pageModelData = JsonConvert.DeserializeObject <PageModelData>(pageModelJson, DataModelBinder.SerializerSettings); // Extract the R2 Data Model of the Topic and convert it to a Strongly Typed View Model EntityModelData topicModelData = pageModelData.Regions[0].Entities[0]; EntityModel topicModel = ModelBuilderPipeline.CreateEntityModel(topicModelData, null, docsLocalization); Topic topic = topicModel as Topic; if (topic == null) { Log.Warn($"Unexpected View Model type for {topicModel}: '{topicModel.GetType().FullName}'"); continue; } // Post-process the Strongly Typed Topic topic.Id = topicModelData.XpmMetadata["ComponentID"] as string; topic.Link = GetFullyQualifiedUrlForTopic(topicModelData.LinkUrl); topics.Add(topic); } } return(topics); }
public void BuildEntityModel_ThroughModelBuilderPipeline_Success() { Localization testLocalization = new DocsLocalization(); testLocalization.EnsureInitialized(); string testTopicId = "1612-1970"; string testTitle = "DITA title"; string testBody = "<div class=\"section \">First section</div><div class=\"section \">Second section</div>"; GenericTopic genericTopic = new GenericTopic { TopicTitle = "<Test topic title>", TopicBody = $"<h1 class=\"title \">{testTitle}</h1><div class=\"body \">{testBody}</div>" }; PageModelData testPageModelData = new PageModelData { Id = "666", Regions = new List <RegionModelData> { new RegionModelData { Name = "Main", Entities = new List <EntityModelData> { new EntityModelData { Id = testTopicId, SchemaId = "1", // Tridion Docs uses a hard-coded/fake Schema ID. Content = new ContentModelData { { "topicTitle", genericTopic.TopicTitle }, { "topicBody", genericTopic.TopicBody } }, MvcData = new DataModel.MvcData { AreaName = "Ish", ViewName = "Topic" } } }, MvcData = new DataModel.MvcData { AreaName = "Core", // TODO: test with ISH data ViewName = "Main" } } }, MvcData = new DataModel.MvcData { AreaName = "Test", ViewName = "SimpleTestPage" } }; PageModel pageModel = ModelBuilderPipeline.CreatePageModel(testPageModelData, false, testLocalization); Assert.IsNotNull(pageModel, "pageModel"); OutputJson(pageModel); TestStronglyTypedTopic result = pageModel.Regions["Main"].Entities[0] as TestStronglyTypedTopic; Assert.IsNotNull(result); Assert.AreEqual(testTopicId, result.Id, "result.Id"); Assert.AreEqual(genericTopic.TopicTitle, result.TopicTitle, "result.TopicTitle"); Assert.AreEqual(testTitle, result.Title, "result.Title"); Assert.AreEqual(testBody, result.BodyRichText.ToString(), "result.BodyRichText.ToString()"); Assert.AreEqual("First section", result.FirstSection, "result.FirstSection"); Assert.AreEqual(result.GetDefaultView(null), result.MvcData, "result.MvcData"); }