Skip to content

Instantly share code, notes, and snippets.

@ryansroberts
Created February 12, 2010 16:40
Show Gist options
  • Save ryansroberts/302730 to your computer and use it in GitHub Desktop.
Save ryansroberts/302730 to your computer and use it in GitHub Desktop.
public class ContentResourceResolverContributor : IPipelineContributor
{
readonly IPageResourceRepository _pageResourceRepository;
readonly IPageResourceUriResolver _pageResourceUriResolver;
IEngine Engine { get; set; }
public ContentResourceResolverContributor(IEngine engine,IPageResourceRepository pageResourceRepository,IPageResourceUriResolver pageResourceUriResolver)
{
_pageResourceRepository = pageResourceRepository;
_pageResourceUriResolver = pageResourceUriResolver;
Engine = engine;
}
public void Initialize(IPipeline pipelineRunner)
{
pipelineRunner.Notify(FindContentType).Before<KnownStages.IUriMatching>();
}
public PipelineContinuation FindContentType(ICommunicationContext communicationContext)
{
ContentItem contentItem = null;
contentItem = GetContentItem(communicationContext);
if(contentItem != null)
{
var resourceType = _pageResourceRepository.ResourceTypeFor(contentItem.GetType());
communicationContext.PipelineData.SelectedResource = _pageResourceUriResolver.Match(resourceType,communicationContext.Request.Uri);
communicationContext.PipelineData.ResourceKey = resourceType;
communicationContext.PipelineData["n2page"] = contentItem;
}
return PipelineContinuation.Continue;
}
IEnumerable<string> PartialPathsFor(Uri uri)
{
for(var i = 0;i != uri.Segments.Count();i++)
{
var path = string.Join("", uri.Segments.Take(uri.Segments.Count() - i).ToArray());
if (path.EndsWith("/"))
path = path.TrimEnd(new[] {'/'});
yield return path;
}
}
ContentItem GetContentItem(ICommunicationContext communicationContext)
{
if (communicationContext.Request.Uri.PathAndQuery == "/")
return Engine.UrlParser.Parse("/");
foreach(var partialPath in PartialPathsFor(communicationContext.Request.Uri))
{
var contentItem = Engine.UrlParser.Parse(partialPath);
if (contentItem != null)
return contentItem;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment