From 0e8bfdd2594db6c8b7a4f23f60aa46e60c108156 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Mon, 13 Jan 2020 08:59:17 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 43 +++++++++++ ContainerRouter.cs | 146 +++++++++++++++++++++++++++++++++++++ Properties/AssemblyInfo.cs | 35 +++++++++ ln.manage.http.csproj | 51 +++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 .gitignore create mode 100644 ContainerRouter.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 ln.manage.http.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2452a0c --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +# Autosave files +*~ + +# build +[Oo]bj/ +[Bb]in/ +packages/ +TestResults/ + +# globs +Makefile.in +*.DS_Store +*.sln.cache +*.suo +*.cache +*.pidb +*.userprefs +*.usertasks +config.log +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.user +*.tar.gz +tarballs/ +test-results/ +Thumbs.db +.vs/ + +# Mac bundle stuff +*.dmg +*.app + +# resharper +*_Resharper.* +*.Resharper + +# dotCover +*.dotCover + +ln.logging \ No newline at end of file diff --git a/ContainerRouter.cs b/ContainerRouter.cs new file mode 100644 index 0000000..48705dd --- /dev/null +++ b/ContainerRouter.cs @@ -0,0 +1,146 @@ +// /** +// * 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; + } + } + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2a8cdb2 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +// /** +// * File: AssemblyInfo.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.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("ln.manage.http")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/ln.manage.http.csproj b/ln.manage.http.csproj new file mode 100644 index 0000000..86bc53b --- /dev/null +++ b/ln.manage.http.csproj @@ -0,0 +1,51 @@ + + + + Debug + AnyCPU + {90B8D805-E60A-4986-ADAA-1753BE248EB1} + Library + ln.manage.http + ln.manage.http + v4.7 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + + + + + + + {CEEEEB41-3059-46A2-A871-2ADE22C013D9} + ln.http + + + {D4E4FD39-6C21-4FCC-8DE0-6494FBE82CEA} + ln.manage + + + {D9342117-3249-4D8B-87C9-51A50676B158} + ln.json + + + + \ No newline at end of file