/// <inheritdoc />
        protected override async Task ProcessRequest(IHttpContext context)
        {
            if (context.Request.HttpVerb != HttpVerbs.Post && context.Request.HttpVerb != HttpVerbs.Put)
            {
                throw HttpException.MethodNotAllowed("This end point only accepts POST and PUT calls");
            }

            context.Response.ContentType = MimeType.Json;

            using TextReader reader = context.OpenRequestText();
            try
            {
                if (_profileModule != null)
                {
                    JsonConvert.PopulateObject(await reader.ReadToEndAsync(), _profileModule.DataModel);
                }
                else if (_module != null)
                {
                    JsonConvert.PopulateObject(await reader.ReadToEndAsync(), _module.DataModel);
                }
                else
                {
                    JsonConvert.PopulateObject(await reader.ReadToEndAsync(), _dataModelExpansion !.DataModel);
                }
            }
            catch (JsonException)
            {
                if (ThrowOnFail)
                {
                    throw;
                }
            }
        }
Example #2
0
        /// <inheritdoc />
        protected override async Task ProcessRequest(IHttpContext context)
        {
            if (context.Request.HttpVerb != HttpVerbs.Post && context.Request.HttpVerb != HttpVerbs.Put)
            {
                throw HttpException.MethodNotAllowed("This end point only accepts POST and PUT calls");
            }

            context.Response.ContentType = MimeType.PlainText;

            using TextReader reader = context.OpenRequestText();
            string?response;

            if (_requestHandler != null)
            {
                _requestHandler(await reader.ReadToEndAsync());
                return;
            }

            if (_responseRequestHandler != null)
            {
                response = _responseRequestHandler(await reader.ReadToEndAsync());
            }
            else
            {
                throw new ArtemisCoreException("String plugin end point has no request handler");
            }

            await using TextWriter writer = context.OpenResponseText();
            await writer.WriteAsync(response);
        }
Example #3
0
        /// <inheritdoc />
        protected override async Task ProcessRequest(IHttpContext context)
        {
            if (context.Request.HttpVerb != HttpVerbs.Post && context.Request.HttpVerb != HttpVerbs.Put)
            {
                throw HttpException.MethodNotAllowed("This end point only accepts POST and PUT calls");
            }

            context.Response.ContentType = MimeType.Json;

            using TextReader reader = context.OpenRequestText();
            object?response = null;

            try
            {
                T deserialized = JsonConvert.DeserializeObject <T>(await reader.ReadToEndAsync());

                if (_requestHandler != null)
                {
                    _requestHandler(deserialized);
                    return;
                }

                if (_responseRequestHandler != null)
                {
                    response = _responseRequestHandler(deserialized);
                }
                else
                {
                    throw new ArtemisCoreException("JSON plugin end point has no request handler");
                }
            }
            catch (JsonException)
            {
                if (ThrowOnFail)
                {
                    throw;
                }
            }

            await using TextWriter writer = context.OpenResponseText();
            await writer.WriteAsync(JsonConvert.SerializeObject(response));
        }
Example #4
0
 /// <summary>
 /// <para>Called when at least one route is matched for the requested URL path,
 /// but none of them is associated with the HTTP method of the request.</para>
 /// <para>The default behavior is to send an empty <c>405 Method Not Allowed</c> response.</para>
 /// </summary>
 /// <param name="context">The context of the request being handled.</param>
 /// <returns>A <see cref="Task"/> representing the ongoing operation.</returns>
 protected virtual Task OnMethodNotAllowedAsync(IHttpContext context)
 => throw HttpException.MethodNotAllowed();
Example #5
0
 /// <summary>
 /// <para>Unconditionally sends a <c>405 Method Not Allowed</c> response.</para>
 /// </summary>
 /// <param name="context">A <see cref="IHttpContext"/> interface representing the context of the request.</param>
 /// <param name="info">If the requested path has been successfully mapped to a resource (file or directory), the result of the mapping;
 /// otherwise, <see langword="null"/>.</param>
 /// <returns>This method never returns; it throws a <see cref="HttpException"/> instead.</returns>
 public static Task ThrowMethodNotAllowed(IHttpContext context, MappedResourceInfo info)
 => throw HttpException.MethodNotAllowed();