ln.http.resources/ReflectiveResource.cs

61 lines
2.2 KiB
C#
Raw Normal View History

2019-04-04 19:34:13 +02:00
// /**
// * File: JsonCallResource.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
2019-04-05 00:59:02 +02:00
using ln.http.resources.reflection;
2019-04-15 09:18:37 +02:00
using ln.types.threads;
2019-08-19 14:12:26 +02:00
using ln.json.mapping;
2019-04-04 19:34:13 +02:00
namespace ln.http.resources
{
2019-04-05 00:59:02 +02:00
public class ReflectiveResource : Resource
2019-04-04 19:34:13 +02:00
{
public override bool HandlesDispatching => true;
2019-04-05 00:59:02 +02:00
Reflector reflector;
Object o;
2019-04-04 19:34:13 +02:00
2019-08-19 14:12:26 +02:00
public JSONMapper JSONMapper { get; private set; }
2019-04-15 09:18:37 +02:00
2019-04-04 19:34:13 +02:00
public ReflectiveResource(Resource container, string name,object o)
: base(container, name)
{
2019-04-05 00:59:02 +02:00
this.o = o;
this.reflector = Reflector.GetReflector(o.GetType());
2019-08-19 14:12:26 +02:00
this.JSONMapper = JSONMapper.DefaultMapper;
2019-04-04 19:34:13 +02:00
}
2019-04-05 00:59:02 +02:00
public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack)
2019-04-04 19:34:13 +02:00
{
2019-04-15 09:18:37 +02:00
object currentValue = Timing.Meassure("reflector walk", () => reflector.InvokeRequest(httpRequest, pathStack, this.o));
//String jsonValue = Timing.Meassure("json converter", () => JsonConvert.SerializeObject(currentValue));
2019-04-04 19:34:13 +02:00
2019-04-05 00:59:02 +02:00
HttpResponse response = new HttpResponse(httpRequest);
2019-04-04 19:34:13 +02:00
2019-04-05 00:59:02 +02:00
response.SetHeader("content-type", "application/json");
2019-04-15 09:18:37 +02:00
//response.ContentWriter.Write(jsonValue);
Timing.Meassure("json converter to stream", () => {
2019-08-19 14:12:26 +02:00
response.ContentWriter.Write(
JSONMapper.ToJson(currentValue).ToString()
);
2019-04-15 09:18:37 +02:00
});
2019-04-04 19:34:13 +02:00
2019-04-05 00:59:02 +02:00
return response;
2019-04-04 19:34:13 +02:00
}
2019-04-05 00:59:02 +02:00
public override bool Contains(string name) => throw new NotImplementedException();
public override void AddResource(Resource resource) => throw new NotImplementedException();
public override void RemoveResource(Resource resource) => throw new NotImplementedException();
public override IEnumerable<Resource> GetResources() => throw new NotImplementedException();
public override HttpResponse GetResponse(HttpRequest httpRequest) => throw new NotImplementedException();
2019-04-04 19:34:13 +02:00
}
}