It is possible to configure dynamic linking so that resolved links give a full url including the domain and protocol, and we are using this on a project to do cross-publication linking. Now we want to also do cross publication searching, however my search results are coming back without the domain prefixed. I do have a publication id in my search results, so I was wondering if I could use this to read the domain from the cd_link_conf file using the CD API. is this possible (in .NET for Tridion 2011 SP1)? I guess the best alternative is to manually parse this file and store the publication id => domain mapping in the web app cache.
3 Answers
I am not sure though I was able to achieve following
- Using Deployer Extension, I was able to read the cd_deployer_config.xml file but no other configuration file like cd_storage_config.xml I was able to read
- Using Storage Extension, I was able to read the cd_storage_config.xml file but no other file like cd_deployer_config etc. I was able to read.
In a nutshell (best of my knowledge, not sure though), directly it is not possible to interact, however you can use below generic java code to read any xml file:
/**
* Get value of a node specified by the nodeID of an XML file specified by the xmlFilePath
* @param nodeID - Node Name in the xml, value of which need to be extracted
* @param xmlFilePath - File Path
* @return Value of the XMLNode specified by the nodeID
*/
public String GetNodeValueFromXML(String nodeID, String xmlFilePath)
{
log.info("Entering GetNodeValueFromXML with XML File Path: " + xmlFilePath + " And Node ID: "+ nodeID);
String returnValue = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
Document doc = null;
try
{
docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(new File(xmlFilePath));
NodeList nodeList = doc.getElementsByTagName(nodeID);
if(nodeList != null && nodeList.getLength() > 0)
{
returnValue = nodeList.item(0).getTextContent();
}
}
catch (<Exception Type> e)
{
//Catch Exception Here
}
finally
{
doc= null;
docBuilder = null;
docBuilderFactory = null;
}
log.info("Exiting GetNodeValueFromXML Method");
return returnValue;
}
-
Thanks Pankaj, I am using .NET so its not much help! I added a .NET example in my answer... Commented May 14, 2013 at 12:38
The .Net CD API does not list anything you could use for this and the cd_link_conf.xml is a java configuration file which does not comply with the .Net config file schema (so you have no hope of using .Net builtin configuration APIs).
As such I can only agree with your conclusion:
I guess the best alternative is to manually parse this file and store the publication id => domain mapping in the web app cache.
I am almost 100% sure its not possible now, so here is a .NET class to get what you need (as a static property which is loaded only once...
public class Config
{
private static Dictionary<int, string> _domainMap = null;
public static Dictionary<int, string> DomainMap
{
get
{
if (_domainMap == null)
{
_domainMap = GetPublicationDomainMapping(HttpContext.Current.Server.MapPath("~/bin"));
}
return _domainMap;
}
}
private static Dictionary<int, string> GetPublicationDomainMapping(string rootApplicationFolder)
{
Dictionary<int, string> data = new Dictionary<int, string>();
XDocument config = XDocument.Load(rootApplicationFolder + "/config/cd_link_conf.xml");
if (config != null)
{
foreach (var pub in config.Descendants("Publication"))
{
data.Add(Int32.Parse(pub.Attribute("Id").Value), GetHost(pub.Element("Host")));
}
}
return data;
}
private static string GetHost(XElement xElement)
{
string protocol = xElement.Attribute("Protocol") == null ? "http" : xElement.Attribute("Protocol").Value;
string domain = xElement.Attribute("Domain") == null ? "no-domain-in-cd_link_conf" : xElement.Attribute("Domain").Value;
string port = xElement.Attribute("Port") == null ? "" : ":" + xElement.Attribute("Port").Value;
string path = (xElement.Attribute("Path") == null || xElement.Attribute("Path").Value == "/") ? "" : xElement.Attribute("Path").Value;
return String.Format("{0}://{1}{2}{3}", protocol, domain, port, path);
}
}