<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>HTTP Callbacks</title><link>https://community.telligent.com/community/11/w/developer-training/65061/http-callbacks</link><description /><dc:language>en-US</dc:language><generator>14.0.0.586 14</generator><item><title>HTTP Callbacks</title><link>https://community.telligent.com/community/11/w/developer-training/65061/http-callbacks</link><pubDate>Wed, 05 Aug 2020 15:06:11 GMT</pubDate><guid isPermaLink="false">e3c13bad-a0e8-49e0-95b6-d839679727fb</guid><dc:creator>Former Member</dc:creator><comments>https://community.telligent.com/community/11/w/developer-training/65061/http-callbacks#comments</comments><description>Current Revision posted to Developer Training by Former Member on 08/05/2020 15:06:11&lt;br /&gt;
&lt;p&gt;HttpCallbacks allow plugins to respond directly to HTTP&amp;nbsp;requests.&lt;/p&gt;
&lt;p&gt;[toc]&lt;/p&gt;
&lt;h2&gt;&lt;a id="Why_use_an_HttpCallback" name="Why_use_an_HttpCallback"&gt;&lt;/a&gt;Why use an HttpCallback?&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;HttpCallbacks are a platform supported alternative to HttpHandlers. &amp;nbsp;Additionally the platform validates requests against&amp;nbsp;Cross-Site Request Forgery and user authentication is persisted to HttpCallbacks.&lt;/p&gt;
&lt;h2&gt;&lt;a id="Creating_an_HttpCallback" name="Creating_an_HttpCallback"&gt;&lt;/a&gt;Creating an HttpCallback&lt;/h2&gt;
&lt;p&gt;To create a&amp;nbsp;callback the&amp;nbsp; &lt;a href="/developers/w/developer90/50289.ihttpcallback-plugin-type"&gt;IHttpCallback&lt;/a&gt; interface must be implemented, this interface&amp;nbsp;requires a reference to the&amp;nbsp;Telligent.Evolution.Components.dll. &amp;nbsp;The IHttpCallback interface extends &lt;a class="ExistingPageLink" title="Click to view the page titled: Plugins" href="/training/w/developer90/52443.plugins"&gt;IPlugin&lt;/a&gt;&amp;nbsp;to add support for handling HTTP&amp;nbsp;requests. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;First, we will use the SetController method to get a reference to the IHttpCallbackController and set a private variable. &amp;nbsp;This will give us access to the controller and the ability to generate URLs for our callback plugin.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;IHttpCallbackController _callbackController;

void IHttpCallback.SetController(IHttpCallbackController controller)
{
    _callbackController = controller;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Second, the ProcessRequest method will handle requests to the callback URL. &amp;nbsp;In this instance we will look for a querystring variable named &amp;quot;value&amp;quot;, if found the code will return javascript that will write the value to the browser&amp;#39;s console. &amp;nbsp;If &amp;nbsp;the &amp;quot;value&amp;quot; variable is not present in the callback URL, a 404 will be returned.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;void IHttpCallback.ProcessRequest(System.Web.HttpContextBase httpContext)
{
    var value = String.Empty;

    if (httpContext.Request.QueryString[&amp;quot;value&amp;quot;] != null)
        value = httpContext.Request.QueryString[&amp;quot;value&amp;quot;].ToString(CultureInfo.InvariantCulture);

    if (!String.IsNullOrEmpty(value))
    {
        httpContext.Response.ContentType = &amp;quot;text/javascript&amp;quot;;
        httpContext.Response.Write(&amp;quot;alert(&amp;#39;&amp;quot; + PublicApi.Html.EnsureEncoded(value) + &amp;quot;&amp;#39;);&amp;quot;);
    }
    else
    {
        httpContext.Response.StatusCode = 404;
        return;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is the source code for our sample HttpCallback:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Text;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.UI.Version1;

namespace Telligent.Evolution.Examples
{
    public class SampleCallback : IHttpCallback
    {
        #region IHttpCallback Members

        void IHttpCallback.ProcessRequest(System.Web.HttpContextBase httpContext)
        {
            var value = String.Empty;

            if (httpContext.Request.QueryString[&amp;quot;value&amp;quot;] != null)
                value = httpContext.Request.QueryString[&amp;quot;value&amp;quot;].ToString(CultureInfo.InvariantCulture);

            if (!String.IsNullOrEmpty(value))
            {
                httpContext.Response.ContentType = &amp;quot;text/javascript&amp;quot;;
                httpContext.Response.Write(&amp;quot;alert(&amp;#39;&amp;quot; + PublicApi.Html.EnsureEncoded(value) + &amp;quot;&amp;#39;);&amp;quot;);
            }
            else
            {
                httpContext.Response.StatusCode = 404;
                return;
            }
        }

        IHttpCallbackController _callbackController;

        void IHttpCallback.SetController(IHttpCallbackController controller)
        {
            _callbackController = controller;
        }
        #endregion

        #region IPlugin Members
        string Extensibility.Version1.IPlugin.Description
        {
            get { return &amp;quot;Uses an IHttpCallback to display messages in the browser console.&amp;quot;; }
        }

        void Extensibility.Version1.IPlugin.Initialize()
        {

        }

        string Extensibility.Version1.IPlugin.Name
        {
            get { return &amp;quot;Sample Callback&amp;quot;; }
        }
        #endregion
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;On its own, the HttpCallback can generate URLs and handle requests to those URLs, but we will need to add some more functionality to make use of the plugin. &amp;nbsp;With the addition of &lt;a href="/developers/w/developer90/50286.ihtmlheaderextension-plugin-type"&gt;IHtmlHeaderExtension&lt;/a&gt;, we can output the callback URL as a script tag in the header of the page. &amp;nbsp;Our HttpCallback plugin can then respond to that request.&lt;/p&gt;
&lt;p&gt;The Header extension uses the IHttpCallbackController variable to generate the correct URLs for the HttpCallback plugin. The sample code will render one script tag with a value querystring and a second script tag without a querystring.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;string IHtmlHeaderExtension.GetHeader(RenderTarget target)
{
    var javascript1 = new NameValueCollection();
    javascript1[&amp;quot;value&amp;quot;] = &amp;quot;Sample Callback Response&amp;quot;;

    var sb = new StringBuilder();

    sb.AppendFormat(@&amp;quot;&amp;lt;script src=&amp;quot;&amp;quot;{0}&amp;quot;&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;quot;, _callbackController.GetUrl(javascript1));
    sb.AppendFormat(@&amp;quot;&amp;lt;script src=&amp;quot;&amp;quot;{0}&amp;quot;&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;quot;, _callbackController.GetUrl());

    return sb.ToString();
}

bool IHtmlHeaderExtension.IsCacheable
{
    get { return true; }
}

bool IHtmlHeaderExtension.VaryCacheByUser
{
    get { return false; }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is the completed source code containing both the HttpCallback and the HtmlHeaderExtension implementations:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://community.telligent.com/cfs-file/__key/communityserver-wikis-components-files/00-00-00-12-83/0172_2E00_SampleHttpCallback_2E00_cs"&gt;community.telligent.com/.../0172_2E00_SampleHttpCallback_2E00_cs&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once this code is deployed to our Verint Community site and the Sample Callback plugin is enabled, browsing to a page should result in a javascript alert being displayed on the site with the message Sample Callback Response. &amp;nbsp;The second callback request with no querystring should result in a 404 error and not a 2nd alert message.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file/__key/communityserver-wikis-components-files/00-00-00-12-83/2308.png"&gt;&lt;img alt=" " src="/resized-image/__size/600x240/__key/communityserver-wikis-components-files/00-00-00-12-83/2308.png" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: IHttpCallback&lt;/div&gt;
</description></item><item><title>HTTP Callbacks</title><link>https://community.telligent.com/community/11/w/developer-training/65061/http-callbacks/revision/1</link><pubDate>Thu, 13 Jun 2019 19:23:56 GMT</pubDate><guid isPermaLink="false">e3c13bad-a0e8-49e0-95b6-d839679727fb</guid><dc:creator>Ben Tiedt</dc:creator><comments>https://community.telligent.com/community/11/w/developer-training/65061/http-callbacks#comments</comments><description>Revision 1 posted to Developer Training by Ben Tiedt on 06/13/2019 19:23:56&lt;br /&gt;
&lt;p&gt;HttpCallbacks allow plugins to respond directly to HTTP&amp;nbsp;requests.&lt;/p&gt;
&lt;p&gt;[toc]&lt;/p&gt;
&lt;h2&gt;&lt;a id="Why_use_an_HttpCallback" name="Why_use_an_HttpCallback"&gt;&lt;/a&gt;Why use an HttpCallback?&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;HttpCallbacks are a platform supported alternative to HttpHandlers. &amp;nbsp;Additionally the platform validates requests against&amp;nbsp;Cross-Site Request Forgery and user authentication is persisted to HttpCallbacks.&lt;/p&gt;
&lt;h2&gt;&lt;a id="Creating_an_HttpCallback" name="Creating_an_HttpCallback"&gt;&lt;/a&gt;Creating an HttpCallback&lt;/h2&gt;
&lt;p&gt;To create a&amp;nbsp;callback the&amp;nbsp; &lt;a href="/developers/w/developer90/50289.ihttpcallback-plugin-type"&gt;IHttpCallback&lt;/a&gt; interface must be implemented, this interface&amp;nbsp;requires a reference to the&amp;nbsp;Telligent.Evolution.Components.dll. &amp;nbsp;The IHttpCallback interface extends &lt;a class="ExistingPageLink" title="Click to view the page titled: Plugins" href="/training/w/developer90/52443.plugins"&gt;IPlugin&lt;/a&gt;&amp;nbsp;to add support for handling HTTP&amp;nbsp;requests. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;First, we will use the SetController method to get a reference to the IHttpCallbackController and set a private variable. &amp;nbsp;This will give us access to the controller and the ability to generate URLs for our callback plugin.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;IHttpCallbackController _callbackController;

void IHttpCallback.SetController(IHttpCallbackController controller)
{
    _callbackController = controller;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Second, the ProcessRequest method will handle requests to the callback URL. &amp;nbsp;In this instance we will look for a querystring variable named &amp;quot;value&amp;quot;, if found the code will return javascript that will write the value to the browser&amp;#39;s console. &amp;nbsp;If &amp;nbsp;the &amp;quot;value&amp;quot; variable is not present in the callback URL, a 404 will be returned.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;void IHttpCallback.ProcessRequest(System.Web.HttpContextBase httpContext)
{
    var value = String.Empty;

    if (httpContext.Request.QueryString[&amp;quot;value&amp;quot;] != null)
        value = httpContext.Request.QueryString[&amp;quot;value&amp;quot;].ToString(CultureInfo.InvariantCulture);

    if (!String.IsNullOrEmpty(value))
    {
        httpContext.Response.ContentType = &amp;quot;text/javascript&amp;quot;;
        httpContext.Response.Write(&amp;quot;alert(&amp;#39;&amp;quot; + PublicApi.Html.EnsureEncoded(value) + &amp;quot;&amp;#39;);&amp;quot;);
    }
    else
    {
        httpContext.Response.StatusCode = 404;
        return;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is the source code for our sample HttpCallback:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Text;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.UI.Version1;

namespace Telligent.Evolution.Examples
{
    public class SampleCallback : IHttpCallback
    {
        #region IHttpCallback Members

        void IHttpCallback.ProcessRequest(System.Web.HttpContextBase httpContext)
        {
            var value = String.Empty;

            if (httpContext.Request.QueryString[&amp;quot;value&amp;quot;] != null)
                value = httpContext.Request.QueryString[&amp;quot;value&amp;quot;].ToString(CultureInfo.InvariantCulture);

            if (!String.IsNullOrEmpty(value))
            {
                httpContext.Response.ContentType = &amp;quot;text/javascript&amp;quot;;
                httpContext.Response.Write(&amp;quot;alert(&amp;#39;&amp;quot; + PublicApi.Html.EnsureEncoded(value) + &amp;quot;&amp;#39;);&amp;quot;);
            }
            else
            {
                httpContext.Response.StatusCode = 404;
                return;
            }
        }

        IHttpCallbackController _callbackController;

        void IHttpCallback.SetController(IHttpCallbackController controller)
        {
            _callbackController = controller;
        }
        #endregion

        #region IPlugin Members
        string Extensibility.Version1.IPlugin.Description
        {
            get { return &amp;quot;Uses an IHttpCallback to display messages in the browser console.&amp;quot;; }
        }

        void Extensibility.Version1.IPlugin.Initialize()
        {

        }

        string Extensibility.Version1.IPlugin.Name
        {
            get { return &amp;quot;Sample Callback&amp;quot;; }
        }
        #endregion
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;On its own, the HttpCallback can generate URLs and handle requests to those URLs, but we will need to add some more functionality to make use of the plugin. &amp;nbsp;With the addition of &lt;a href="/developers/w/developer90/50286.ihtmlheaderextension-plugin-type"&gt;IHtmlHeaderExtension&lt;/a&gt;, we can output the callback URL as a script tag in the header of the page. &amp;nbsp;Our HttpCallback plugin can then respond to that request.&lt;/p&gt;
&lt;p&gt;The Header extension uses the IHttpCallbackController variable to generate the correct URLs for the HttpCallback plugin. The sample code will render one script tag with a value querystring and a second script tag without a querystring.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;string IHtmlHeaderExtension.GetHeader(RenderTarget target)
{
    var javascript1 = new NameValueCollection();
    javascript1[&amp;quot;value&amp;quot;] = &amp;quot;Sample Callback Response&amp;quot;;

    var sb = new StringBuilder();

    sb.AppendFormat(@&amp;quot;&amp;lt;script src=&amp;quot;&amp;quot;{0}&amp;quot;&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;quot;, _callbackController.GetUrl(javascript1));
    sb.AppendFormat(@&amp;quot;&amp;lt;script src=&amp;quot;&amp;quot;{0}&amp;quot;&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&amp;quot;, _callbackController.GetUrl());

    return sb.ToString();
}

bool IHtmlHeaderExtension.IsCacheable
{
    get { return true; }
}

bool IHtmlHeaderExtension.VaryCacheByUser
{
    get { return false; }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is the completed source code containing both the HttpCallback and the HtmlHeaderExtension implementations:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://community.telligent.com/cfs-file/__key/communityserver-wikis-components-files/00-00-00-12-83/0172_2E00_SampleHttpCallback_2E00_cs"&gt;community.telligent.com/.../0172_2E00_SampleHttpCallback_2E00_cs&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once this code is deployed to our Telligent Community site and the Sample Callback plugin is enabled, browsing to a page should result in a javascript alert being displayed on the site with the message Sample Callback Response. &amp;nbsp;The second callback request with no querystring should result in a 404 error and not a 2nd alert message.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file/__key/communityserver-wikis-components-files/00-00-00-12-83/2308.png"&gt;&lt;img src="/resized-image/__size/600x240/__key/communityserver-wikis-components-files/00-00-00-12-83/2308.png" alt=" " /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;

&lt;div style="font-size: 90%;"&gt;Tags: IHttpCallback&lt;/div&gt;
</description></item></channel></rss>