147 lines
4.8 KiB
C#
147 lines
4.8 KiB
C#
|
// /**
|
|||
|
// * File: MyClass.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 ln.http;
|
|||
|
using ln.http.router;
|
|||
|
using ln.manage;
|
|||
|
using System.Linq;
|
|||
|
using ln.json;
|
|||
|
using ln.json.mapping;
|
|||
|
|
|||
|
namespace ln.manage.http
|
|||
|
{
|
|||
|
public class ContainerRouter : SimpleRouter
|
|||
|
{
|
|||
|
IManagedContainer ManagedContainer { get; }
|
|||
|
|
|||
|
public ContainerRouter(IManagedContainer managedContainer)
|
|||
|
{
|
|||
|
ManagedContainer = managedContainer;
|
|||
|
Sync();
|
|||
|
}
|
|||
|
|
|||
|
public void Sync()
|
|||
|
{
|
|||
|
foreach (SimpleRoute simpleRoute in Routes)
|
|||
|
Remove(simpleRoute);
|
|||
|
|
|||
|
Sync(ManagedContainer);
|
|||
|
}
|
|||
|
void Sync(IManagedContainer container)
|
|||
|
{
|
|||
|
string cpath = String.Join("/", container.GetContainerPath());
|
|||
|
Console.WriteLine(cpath);
|
|||
|
|
|||
|
AddSimpleRoute(cpath, new ContainerTarget(container));
|
|||
|
AddSimpleRoute(String.Concat(cpath, "/_/:name"), new EntityTarget(container));
|
|||
|
|
|||
|
foreach (IManagedContainer child in container.Children)
|
|||
|
Sync(child);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
class ContainerTarget : RouterTarget
|
|||
|
{
|
|||
|
IManagedContainer Container { get; }
|
|||
|
|
|||
|
public ContainerTarget(IManagedContainer container)
|
|||
|
{
|
|||
|
Container = container;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public override HttpResponse GET(HttpRequest request)
|
|||
|
{
|
|||
|
HttpResponse response = new HttpResponse(request);
|
|||
|
response.SetHeader("content-type", "application/json");
|
|||
|
|
|||
|
JSONArray jlist = new JSONArray();
|
|||
|
|
|||
|
foreach (IManagedObject managedObject in Container.GetManagedObjects())
|
|||
|
{
|
|||
|
jlist.Add(EntityTarget.MO2Json(managedObject));
|
|||
|
}
|
|||
|
|
|||
|
response.ContentWriter.Write(jlist.ToString());
|
|||
|
response.ContentWriter.Flush();
|
|||
|
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
public override HttpResponse PROPFIND(HttpRequest request)
|
|||
|
{
|
|||
|
HttpResponse response = new HttpResponse(request);
|
|||
|
response.SetHeader("content-type", "application/json");
|
|||
|
response.ContentWriter.Write(Describe(Container).ToString());
|
|||
|
response.ContentWriter.Flush();
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
public static JSONObject Describe(IManagedContainer container)
|
|||
|
{
|
|||
|
JSONObject jcontainer = new JSONObject();
|
|||
|
jcontainer["path"] = String.Join("/", container.GetContainerPath());
|
|||
|
jcontainer["name"] = container.Name;
|
|||
|
|
|||
|
JSONObject jproperties = new JSONObject();
|
|||
|
foreach (PropertyDescriptor propertyDescriptor in container.PropertyDescriptors)
|
|||
|
{
|
|||
|
jproperties[propertyDescriptor.PropertyName] = propertyDescriptor.CreateJsonDescriptor();
|
|||
|
}
|
|||
|
|
|||
|
jcontainer["properties"] = jproperties;
|
|||
|
|
|||
|
JSONArray jchildren = new JSONArray();
|
|||
|
foreach (IManagedContainer child in container.Children)
|
|||
|
jchildren.Add(Describe(child));
|
|||
|
jcontainer["children"] = jchildren;
|
|||
|
|
|||
|
return jcontainer;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
class EntityTarget : RouterTarget
|
|||
|
{
|
|||
|
IManagedContainer Container { get; }
|
|||
|
|
|||
|
public EntityTarget(IManagedContainer container)
|
|||
|
{
|
|||
|
Container = container;
|
|||
|
}
|
|||
|
|
|||
|
public override HttpResponse GET(HttpRequest request)
|
|||
|
{
|
|||
|
HttpResponse response = new HttpResponse(request);
|
|||
|
IManagedObject managedObject = Container.GetManagedObject(request.GetParameter("name"));
|
|||
|
response.SetHeader("content-type", "application/json");
|
|||
|
response.ContentWriter.Write(MO2Json(managedObject).ToString());
|
|||
|
response.ContentWriter.Flush();
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
public static JSONObject MO2Json(IManagedObject managedObject)
|
|||
|
{
|
|||
|
JSONObject jmo = new JSONObject();
|
|||
|
jmo["source"] = String.Join("/", managedObject.Container.GetContainerPath());
|
|||
|
JSONObject jprops = new JSONObject();
|
|||
|
|
|||
|
foreach (PropertyDescriptor propertyDescriptor in managedObject.Container.PropertyDescriptors)
|
|||
|
{
|
|||
|
jprops[propertyDescriptor.PropertyName] = JSONMapper.DefaultMapper.ToJson(managedObject.GetValue(propertyDescriptor.PropertyName));
|
|||
|
}
|
|||
|
|
|||
|
jmo["properties"] = jprops;
|
|||
|
return jmo;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|