Initial Commit

master
Harald Wolff 2020-01-13 08:59:17 +01:00
commit 0e8bfdd259
4 changed files with 275 additions and 0 deletions

43
.gitignore vendored 100644
View File

@ -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

146
ContainerRouter.cs 100644
View File

@ -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;
}
}
}
}

View File

@ -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("")]

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{90B8D805-E60A-4986-ADAA-1753BE248EB1}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>ln.manage.http</RootNamespace>
<AssemblyName>ln.manage.http</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="ContainerRouter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.http\ln.http.csproj">
<Project>{CEEEEB41-3059-46A2-A871-2ADE22C013D9}</Project>
<Name>ln.http</Name>
</ProjectReference>
<ProjectReference Include="..\ln.manage\ln.manage.csproj">
<Project>{D4E4FD39-6C21-4FCC-8DE0-6494FBE82CEA}</Project>
<Name>ln.manage</Name>
</ProjectReference>
<ProjectReference Include="..\ln.json\ln.json.csproj">
<Project>{D9342117-3249-4D8B-87C9-51A50676B158}</Project>
<Name>ln.json</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>