コード例 #1
0
 /// <summary>
 /// Frees resources used by the <b>HtmlContentProvider</b>.
 /// </summary>
 public virtual void Dispose()
 {
     if (this.publisher != null) {
     this.publisher.Dispose();
     this.publisher = null;
     }
 }
コード例 #2
0
        /// <summary>
        /// Gets a <b>Publisher</b> for the <b>HtmlContentProvider</b>.
        /// </summary>
        /// <param name="context">The context of the request the Publisher is for.</param>
        /// <returns>A <b>Publisher</b> for publishing HTML content.</returns>
        private Publisher GetPublisher(
            TransitionContext context)
        {
            // TODO: name shouldn't be hard coded here
            string name = "html";

            if (this.publisher == null) {
                    //  get the settings from config file
            ControllerConfigSection config = ConfigurationManager.GetSection(
                    TritonConfigurationSection.SectionName + "/content") as ControllerConfigSection;

                    //  make sure we have the proper config info
            if (config == null) {
                throw new ConfigurationErrorsException("Load of triton/content config section failed.");
            }
            if (config.ContentProviders[name] == null) {
                throw new ConfigurationErrorsException(
                        string.Format("No contentProvider found in confg for '{0}'.", name));
            }

                    //  get the name of the publisher
            string publisherName = config.ContentProviders[name].Publisher;

            if (!string.IsNullOrEmpty(publisherName)) {
                this.publisher = Publisher.GetPublisher(publisherName);
            }
            }

            return this.publisher;
        }
コード例 #3
0
        /// <summary>
        /// Publishes the content contained in the publishParam.
        /// </summary>
        /// <param name="publishParam">Contains the content to be published.</param>
        /// <param name="context">The Context of the request the content is being published for.</param>
        /// <param name="publisher">The publisher </param>
        /// <returns></returns>
        public virtual string Publish(
            object publishParam,
            TransitionContext context,
            Publisher publisher)
        {
            string content = publishParam as string;
            string key = context.PublishKey;

            LogManager.GetCurrentClassLogger().Debug(
                traceMessage => traceMessage("PagePublisher.Publish page {0}: start page = {1}, event = {2}",
                        context.EndState.Id,
                        context.StartState.Id,
                        context.StartEvent));

            PublishRecord pubRec = publisher.GetPublishRecord(key, context, true);

            try {
                    //  make sure some other thread is not already publishing the page
            if (!pubRec.Publishing) {
                        //  flag the page as in the process of being published
                pubRec.Publishing = true;
                        //  get the key for the PublishRecord
                string filePrefix = pubRec.Key;
                        //  strip off the state ID
                        //  (should find a better way to do this so it's not so tightly coupled with
                        //  what MakeKey's implementation is
                filePrefix = filePrefix.Substring(filePrefix.IndexOf('_') + 1);

                        // replace special characters with their unicode representations.
                filePrefix = this.fileNameRegEx.Replace(filePrefix, this.fileNameEvaluator);

                        //  get the publish state -- the one we are publishing
                PublishableState publishState = (PublishableState)context.EndState;

            //				try {
            //					content = publishState.GetPublishContent(context);
            //				} catch (Exception e) {
            //					Logger.GetLogger(LOGGER).ReportInnerExceptions = true;
            //					Logger.GetLogger(LOGGER).Error("PagePublisher.Publish: ", e);
            //					throw e;
            //				}

                string fileName = publishState.BaseFileName;
                string section = publishState.Section;

                        //  build the path to the directory to publish to
                string targetPath = string.Format(@"{0}{1}/{2}", this.publishPath, publishState.Site, section);
                        //  make sure the directory exists
                IO.CreateDirectory(this.basePath + targetPath);

                        //	if the length of full path exceeds the maximum,
                        //	use hash code of filePrefix instead of filePrefix to construct tagetPath
                string tmpFullPath = string.Format(@"{0}{1}/{2}_{3}.html",
                        this.basePath, targetPath, filePrefix, fileName);
                if (tmpFullPath.Length > this.maxPathLen) {
                    filePrefix = filePrefix.GetHashCode().ToString();
                }
                        //  add the file name to the path
                targetPath = string.Format(@"{0}/{1}_{2}", targetPath, filePrefix, fileName);
                string path = this.basePath + targetPath;

                StreamWriter writer = null;
                try {
                    writer = new StreamWriter(path.ToLower(), false, Encoding.UTF8);

                            //  build comment content to append to published page
                    string append = PUBLISH_MARKER;
                            //  check config for setting to include published timestamp, and include timestamp if indicated
                    PublishConfigSection config = ConfigurationManager.GetSection(
                            TritonConfigurationSection.SectionName + "/publishing") as PublishConfigSection;
                    if (config.Settings[INCLUDE_TIMESTAMP_SETTING] != null) {
                        bool includeTimestamp;
                        if (bool.TryParse(config.Settings[INCLUDE_TIMESTAMP_SETTING].Value, out includeTimestamp) && includeTimestamp) {
                            append += " [" + DateTime.Now.ToString() + "]";
                        }
                    }

                            //  write the content received from Execute to the publish file
                    writer.Write(content + Environment.NewLine + "<!--" + append + "-->");

                } catch (Exception e) {
                    LogManager.GetCurrentClassLogger().Error(
                            errorMessage => errorMessage("PagePublisher.Publish write: "), e);
                    throw;
                } finally {
                    if (writer != null) {
                        writer.Close();
                    }
                }

                        //  make sure the relative URL starts with "/"
                if (!targetPath.StartsWith("/")) {
                    targetPath = "/" + targetPath;
                }

                pubRec.PublishedPath = targetPath;
                pubRec.LastPublished = DateTime.Now;

                LogManager.GetCurrentClassLogger().DebugFormat("Published {0} with key {1}", pubRec.PublishedPath, pubRec.Key);
            }
            } catch (Exception e) {
            LogManager.GetCurrentClassLogger().Error(
                    errorMessage => errorMessage("PagePublisher.Publish: "), e);
            content = null;
            } finally {
                    //  we never want to leave the page in a state of publishing
            pubRec.Publishing = false;
            }

            return content;
        }
コード例 #4
0
 public void AddPublisher(
     Publisher publisher)
 {
     // TODO: add the Publisher to some kind of pool
 }
コード例 #5
0
        /// <summary>
        /// Factory method for constructing Publishers.
        /// </summary>
        /// <param name="name">The name of the publisher to make.</param>
        /// <returns>A <b>Publisher</b> of the named type.</returns>
        private static Publisher MakePublisher(
            string name)
        {
            Publisher publisher = null;

            PublishConfigSection config = ConfigurationManager.GetSection(
                TritonConfigurationSection.SectionName + "/publishing") as PublishConfigSection;

            if (config.Publish) {
                    //  make sure we found the publishing section and publisher definition
                    //  for the request publisher
            if (config == null) {
                throw new ConfigurationErrorsException("No publishing section found in config.");
            }
            if (config.Publishers == null) {
                throw new ConfigurationErrorsException("No publishers identified in config.");
            }
            if (config.Publishers[name] == null) {
                throw new ConfigurationErrorsException("No publisher identified in config for: " + name);
            }

                    //  instantiate the publisher
            publisher = new Publisher(name);

                    //  get the handler (ContentPublisher) for the publisher
            string handlerClass = config.Publishers[name].Handler;

            if (handlerClass != null) {
                try {
                    publisher.contentPublisher = (IContentPublisher)Activator.CreateInstance(Type.GetType(handlerClass));

                            //  if there are IPublisherRules defined for the publisher, create the rules
                    if ((config.Publishers[name].Rules != null) && (config.Publishers[name].Rules.Count > 0)) {
                        foreach (PublisherRule ruleConfig in config.Publishers[name].Rules) {
                            try {
                                IPublisherRule rule = PublisherRuleFactory.Make(ruleConfig.Name, ruleConfig.Class);
                                if (publisher.contentPublisher.Rules == null) {
                                    publisher.contentPublisher.Rules = new List<IPublisherRule>();
                                }
                                publisher.contentPublisher.Rules.Add(rule);
                            } catch (Exception e) {
                                LogManager.GetCurrentClassLogger().Error(msg => msg(string.Format(
                                        "Publisher : Error occurred creating the publisher rule {0} with class {1}.",
                                        ruleConfig.Name, ruleConfig.Class)), e);
                            }
                        }
                    }
                } catch (Exception) {}
            }
            }

            return publisher;
        }