using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using CMS.PortalControls; using CMS.GlobalHelper; using CMS.CMSHelper; using CMS.PortalEngine; using CMS.EventLog; public partial class CMSWebParts_General_CustomResponse : CMSAbstractWebPart { #region "Properties" /// /// Gets or sets the response content disposition /// public string ContentDisposition { get { return ValidationHelper.GetString(this.GetValue("ContentDisposition"), String.Empty); } set { this.SetValue("ContentDisposition", value); } } /// /// Gets or sets the response content type /// public string ContentType { get { return ValidationHelper.GetString(this.GetValue("ContentType"), "text/plain"); } set { this.SetValue("ContentType", value); } } /// /// Gets or sets the response encoding /// public string Encoding { get { return ValidationHelper.GetString(this.GetValue("Encoding"), "utf-8"); } set { this.SetValue("Encoding", value); } } /// /// Gets or sets the response status code /// public string StatusCode { get { return ValidationHelper.GetString(this.GetValue("StatusCode"), "200"); } set { this.SetValue("StatusCode", value); } } /// /// Gets or sets the response content /// public string Content { get { return ValidationHelper.GetString(this.GetValue("Content"), String.Empty); } set { this.SetValue("Content", value); } } #endregion #region "Methods" /// /// Content loaded event handler /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties /// protected void SetupControl() { if (this.StopProcessing) { // Do not process } else { // Switch by view mode switch (CMSContext.ViewMode) { case ViewModeEnum.LiveSite: case ViewModeEnum.Preview: // Keep current response HttpResponse response = HttpContext.Current.Response; // Clear response response.Clear(); // Content type response.ContentType = ContentType; // Encoding SetEncoding(response); // Status code SetStatus(response); // Content disposition if (!String.IsNullOrEmpty(ContentDisposition)) { response.AddHeader("Content-Disposition", ContentDisposition); } // Write to the output response.Write(Content); // Close response RequestHelper.EndResponse(); break; default: lblInfo.Visible = true; break; } } } /// /// Reloads the control data /// public override void ReloadData() { base.ReloadData(); SetupControl(); } #endregion #region "Helper methods" /// /// Sets response encoding /// /// Current response private void SetEncoding(HttpResponse response) { // Use default UTF-8 encoding Encoding enc = System.Text.Encoding.UTF8; // Try set custom encoding try { enc = System.Text.Encoding.GetEncoding(Encoding); response.ContentEncoding = enc; } catch (Exception ex) { EventLogProvider.LogException("CustomResponse", "SetEncoding", ex); } } /// /// Sets the response status code /// /// Current response private void SetStatus(HttpResponse response) { // Set 200 - OK status by default response.StatusCode = 200; string status = StatusCode; if (!String.IsNullOrEmpty(status)) { // Try split by dot string[] statusArr = status.Split('.'); // Set status code response.StatusCode = ValidationHelper.GetInteger(statusArr[0], 200); // Set sub status code if is defined if (statusArr.Length > 1) { response.SubStatusCode = ValidationHelper.GetInteger(statusArr[1], 0); } } } #endregion }