public override Localization ResolveLocalization(Uri url)
{
using (new Tracer(url))
{
string urlLeftPart = url.GetLeftPart(UriPartial.Path);
// TODO PERF: to optimize caching, we could only take the first part of the URL path (e.g. one or two levels)
int espaceIndex = urlLeftPart.IndexOf("%");
if (espaceIndex > 0)
{
// TODO: This is a work-around for a bug in SDL Web 8 Publication Mapping: URLs with escaped characters don't resolve properly (CRQ-1585).
// Therefore we truncate the URL at the first escaped character for now (assuming that the URL is still specific enough to resolve the right Publication).
urlLeftPart = urlLeftPart.Substring(0, espaceIndex);
}
IPublicationMapping mapping = null;
try
{
// NOTE: we're not using UrlToLocalizationMapping here, because we may match too eagerly on a base URL when there is a matching mapping with a more specific URL.
mapping = _mappingsRetriever.GetPublicationMapping(urlLeftPart);
}
catch (Exception ex)
{
// CIL throws Sdl.Web.Delivery.Service.InvalidResourceException if the mapping cannot be resolved.
// We don't have a direct reference to Sdl.Web.Delivery.Service, so we just check the type name
if (ex.GetType().FullName != "Sdl.Web.Delivery.Service.InvalidResourceException")
{
throw;
}
Log.Debug("Exception occurred in DynamicMappingsRetriever.GetPublicationMapping('{0}'):\n{1}", urlLeftPart, ex.ToString());
// Let mapping be null, we'll handle it below.
}
if (mapping == null || mapping.Port != url.Port.ToString()) // See CRQ-1195
{
throw new DxaUnknownLocalizationException(string.Format("No matching Localization found for URL '{0}'", urlLeftPart));
}
Localization result;
lock (KnownLocalizations)
{
string localizationId = mapping.PublicationId.ToString();
if (!KnownLocalizations.TryGetValue(localizationId, out result))
{
result = new Localization
{
LocalizationId = localizationId,
Path = mapping.Path
};
KnownLocalizations.Add(localizationId, result);
}
else
{
// we fill in the path regardless as it may of been
// a partially created localization.
result.Path = mapping.Path;
}
}
result.EnsureInitialized();
return result;
}
}