Exemple #1
0
        /// <summary>
        /// Loads the appropriate xml file and loads the IFileInstruction
        /// </summary>
        /// <param name="context">httpcontext</param>
        /// <param name="url">Requested URL</param>
        void RewriteUrl(HttpContext context, string url)
        {
            if (url.EndsWith("/")) //The key will never be just / because of the statement above
            {
                //strip the trailing /
                url = url.Substring(0, url.LastIndexOf('/'));
            }

            //Store the url so it can be rewritten for logging.
            context.Items[REQUEST_URL_KEY] = url;

            IFileInstruction fileInstruction = null;

            try
            {
                //Load the XML file to create a file instruction object of type IFileInstruction.If the XML file fails to create IFileInstruction
                //Object log error
                fileInstruction = FileInstructionFactory.GetFileInstruction(url);
            }
            catch (Exception ex)
            {
                log.Error("RewriteUrl(): Requested URL: " + context.Items[REQUEST_URL_KEY] + "\nFailed to Create IFileInstruction with the XML file Provided.", ex);
                RaiseErrorPage();
                return;
            }
            //Exiting because there is no page assembly instruction for the requested URL.
            if (fileInstruction == null)
            {
                return;
            }

            // Do not reset the client path because it'll break form action url's.
            try
            {
                //If we should not index this item, then add the X-Robots-Tag header.  This works
                //just like the <meta name="robots"> tag.
                if (fileInstruction.DoNotIndex)
                {
                    context.Response.AppendHeader("X-Robots-Tag", "noindex, nofollow");
                }
                context.RewritePath(fileInstruction.FilePath, false);
            }

            catch (HttpException ex)
            {
                log.Error("RewriteUrl(): Requested URL: " + context.Items[REQUEST_URL_KEY] + "\nFailed to rewrite URL.", ex);
                RaiseErrorPage();
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates an IFileInstruction derived instance from the XML at the specified path.        ///
        ///
        /// </summary>
        public static IFileInstruction GetFileInstruction(string requestedPath)
        {
            bool isFileInstructionXmlValid = true;

            string xmlFilePath = HttpContext.Current.Server.MapPath(String.Format(ContentDeliveryEngineConfig.PathInformation.FilePathFormat.Path, requestedPath));

            // Input validation.
            if (xmlFilePath == null)
            {
                throw new ArgumentNullException("pathToXml", "The pathToXml parameter cannot be null.");
            }

            if (!File.Exists(xmlFilePath))
            {
                return(null);
            }

            if (ContentDeliveryEngineConfig.FileInstruction.FileInstructionTypes.EnableValidation == true)
            {
                isFileInstructionXmlValid = FileInstructionFactory.ValidateXml(xmlFilePath,
                                                                               HttpContext.Current.Server.MapPath(ContentDeliveryEngineConfig.FileInstruction.FileInstructionTypes.XsdPath));
            }

            if (isFileInstructionXmlValid == false)
            {
                return(null);
            }

            // Load the XML file into an XmlReader and create a IFileInstruction derived instance from the XML.
            IFileInstruction fileInstruction = null;

            try
            {
                using (FileStream xmlFile = File.Open(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
                {
                    using (XmlReader xmlReader = XmlReader.Create(xmlFile))
                    {
                        // Read just enough to get the file instruction type we want to create
                        // e.g. a GenericFileInstruction, etc.
                        xmlReader.MoveToContent();

                        // Get the name of the serializer type we need (the document element name is the
                        // name of the type).
                        string fileInstructionTypeName = xmlReader.LocalName;

                        // Make sure the serializer we need is in the cache (if it isn't, we've got invalid XML).
                        if (Instance._serializers.ContainsKey(fileInstructionTypeName) == false)
                        {
                            string message = String.Format("Unable to find XmlSerializer for type \"{0}.\"  Either \"{0}\" is an unsupported type and the XML file at \"{1}\" is invalid or the file instruction configuration needs a new entry to support \"{0}\"", fileInstructionTypeName, xmlFilePath);
                            throw new FileInstructionException(message);
                        }

                        // Get the serializer from the cache.
                        XmlSerializer serializer = Instance._serializers[fileInstructionTypeName];


                        // Deserialize the XML into an object.
                        fileInstruction = (IFileInstruction)serializer.Deserialize(xmlReader);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FileInstructionException(String.Format("Unable to create IFileInstruction for file \"{0}.\"", xmlFilePath), ex);
            }

            return(fileInstruction);
        }