Article HTTP Callbacks

HttpCallbacks allow plugins to respond directly to HTTP requests.

Why use an HttpCallback?

HttpCallbacks are useful for direct HTTP handling of URLs that don't need to have a unique route (for unique/SEO-friendly routes see raw page definitions in Page Definition and URL Routing ).

Creating an HttpCallback

To create a callback,  that implements the IHttpAsyncCallback plugin interface. The IHttpCallback interface extends IPlugin to add support for handling HTTP requests:

IHttpCallbackController _callbackController;

#region IHttpCallback

public async Task ProcessRequest(HttpContext httpContext)
{
	var value = httpContext.Request.Query.GetFirst("value");
	if (!String.IsNullOrEmpty(value))
	{
		httpContext.Response.ContentType = "text/javascript";
		await httpContext.Response.WriteAsync("alert('" + Apis.Get<IHtml>().EnsureEncoded(value) + "');");
	}
	else
	{
		httpContext.Response.StatusCode = 404;
	}
}

public void SetController(IHttpCallbackController controller)
{
	_callbackController = controller;
}

#endregion

First, we will use the SetController() method to get a reference to the IHttpCallbackController  controller for this plugin and save it to a private variable.  This will give us access to the controller and the ability to generate URLs for our callback plugin.

Second, the ProcessRequest() method will handle requests to the callback URL.  In this instance we will look for a querystring variable named "value", if found the code will return javascript that will write the value to the browser's console.  If the "value" variable is not present in the callback URL, a 404 will be returned.

On its own, the IHttpAsyncCallback plugin can generate URLs and handle requests to those URLs, but we will need to add some more functionality to make use of the plugin.  With the addition of a IHtmlHeaderExtension plugin type implementation, we can output the callback URL as a script tag in the header of the page.  Our IHttpAsyncCallback plugin can then respond to that request.

The header extension uses the IHttpCallbackController variable to generate the correct URLs for the callback plugin. The sample code will render one script tag with a value querystring and a second script tag without a querystring.

Here is the completed source code containing both the HttpCallback and the HtmlHeaderExtension implementations:

using System;
using System.Collections.Specialized;
using System.Threading.Tasks;
using Telligent.Evolution.Extensibility;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.UI.Version1;
using Telligent.Evolution.Extensibility.Version1;

namespace Telligent.Evolution.Examples
{
	public class SampleCallback : IHttpAsyncCallback, IHtmlHeaderExtension
	{
		IHttpCallbackController _callbackController;

		#region IPlugin Members

		public string Name
		{
			get { return "Sample Callback"; }
		}
		public string Description
		{
			get { return "Uses an IHttpAsyncCallback to display messages in the browser console."; }
		}

		public void Initialize()
		{

		}

		#endregion

		#region IHttpCallback

		public async Task ProcessRequest(HttpContext httpContext)
		{
			var value = httpContext.Request.Query.GetFirst("value");
			if (!String.IsNullOrEmpty(value))
			{
				httpContext.Response.ContentType = "text/javascript";
				await httpContext.Response.WriteAsync("alert('" + Apis.Get<IHtml>().EnsureEncoded(value) + "');");
			}
			else
			{
				httpContext.Response.StatusCode = 404;
			}
		}

		public void SetController(IHttpCallbackController controller)
		{
			_callbackController = controller;
		}

		#endregion

		#region IHtmlHeaderExtension

		public string GetHeader(RenderTarget target)
		{
			var queryString = new NameValueCollection();
			queryString["value"] = "Sample Callback Response";

			return $""""
				<script src="{Apis.Get<IHtml>().EncodeAttribute(_callbackController.GetUrl(queryString))}"></script>
				"""";
		}

		public bool IsCacheable
		{
			get { return true; }
		}

		public bool VaryCacheByUser
		{
			get { return false; }
		}

		#endregion
	}
}

Once this   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.