using System;
using System.IO;
using EmbedIO;
namespace Artemis.Core.Services
{
///
/// Represents a plugin web endpoint receiving an a and returning a or
/// .
///
public class StringPluginEndPoint : PluginEndPoint
{
private readonly Action? _requestHandler;
private readonly Func? _responseRequestHandler;
internal StringPluginEndPoint(PluginFeature pluginFeature, string name, PluginsModule pluginsModule, Action requestHandler) : base(pluginFeature, name, pluginsModule)
{
_requestHandler = requestHandler;
}
internal StringPluginEndPoint(PluginFeature pluginFeature, string name, PluginsModule pluginsModule, Func requestHandler) : base(pluginFeature, name, pluginsModule)
{
_responseRequestHandler = requestHandler;
}
#region Overrides of PluginEndPoint
///
internal override void ProcessRequest(IHttpContext context)
{
if (context.Request.HttpVerb != HttpVerbs.Post)
throw HttpException.MethodNotAllowed("This end point only accepts POST calls");
context.Response.ContentType = MimeType.PlainText;
using TextReader reader = context.OpenRequestText();
string? response;
if (_requestHandler != null)
{
_requestHandler(reader.ReadToEnd());
return;
}
else if (_responseRequestHandler != null)
response = _responseRequestHandler(reader.ReadToEnd());
else
throw new ArtemisCoreException("String plugin end point has no request handler");
using TextWriter writer = context.OpenResponseText();
writer.Write(response);
}
#endregion
}
}