Initial Commit

master
Harald Wolff 2020-03-03 17:07:03 +01:00
commit f4c5df07c1
13 changed files with 497 additions and 0 deletions

4
.gitignore vendored 100644
View File

@ -0,0 +1,4 @@
obj
bin
ln.types
ln.logging

View File

@ -0,0 +1,21 @@
using System;
using ln.http;
using ln.json.mapping;
namespace ln.json.http
{
public static class HTTPRequestExtensions
{
public static HttpResponse SendJSON(this HttpRequest request, object value)
{
HttpResponse response = new HttpResponse(request);
response.SetHeader("Content-Type", "application/json");
response.ContentWriter.Write(
(value is JSONValue jvalue) ? jvalue.ToString() : JSONMapper.DefaultMapper.ToJson(value).ToString()
);
response.ContentWriter.Flush();
return response;
}
public static JSONValue GetJSON(this HttpRequest httpRequest) => JSONParser.Parse(httpRequest.ContentReader.ReadToEnd());
}
}

View File

@ -0,0 +1,43 @@
using System;
using ln.http.router;
using ln.http.exceptions;
using ln.http;
using ln.json.mapping;
using ln.types.odb.ng;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace ln.json.http
{
public abstract class HttpObjectContainer : SimpleRouter
{
public HttpObjectContainer()
{
}
public abstract object GetNativeIdentity(string identityText);
public abstract object GetObjectIdentity(object o);
public abstract object CreateObject();
public abstract void DeleteObject(object o);
public abstract IEnumerable GetObjects();
public virtual object GetObject(object identity)
{
foreach (object o in GetObjects())
{
if (Object.Equals(identity, GetObjectIdentity(o)))
return o;
}
throw new KeyNotFoundException();
}
HttpResponse Request(HttpRequest request)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using ln.http;
using System.Collections;
using System.Collections.Generic;
namespace ln.json.http
{
public class HttpObjectMapper
{
public Type NativeType { get; }
public HttpObjectMapper(Type nativeType)
{
NativeType = nativeType;
}
public HttpResponse Request(ObjectRequest objectRequest)
{
throw new NotImplementedException();
}
}
}

11
IObjectHandler.cs 100644
View File

@ -0,0 +1,11 @@
using System;
using ln.http;
using System.Collections;
using System.Collections.Generic;
namespace ln.json.http
{
public interface IObjectHandler
{
HttpResponse Dispatch(HttpRequest httpRequest, object owner, Queue<string> pathQueue);
}
}

103
MappingContainer.cs 100644
View File

@ -0,0 +1,103 @@
// /**
// * File: MappingContainer.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 System.Collections.Generic;
using ln.http.router;
using ln.http.exceptions;
using ln.json.mapping;
using ln.types.odb.ng;
namespace ln.json.http
{
public class MappingContainer: SimpleRouter
{
Mapper Mapper { get; }
Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
public MappingContainer(Mapper mapper)
{
Mapper = mapper;
AddSimpleRoute("/:type/:identity", Request);
AddSimpleRoute("/:type", Request);
}
public void AddSupportedType<T>() => AddSupportedType(typeof(T));
public void AddSupportedType(Type type)
{
typeCache.Add(type.Name, type);
}
Type FindType(HttpRequest request)
{
if (request.ContainsParameter("type"))
{
return typeCache[request.GetParameter("type")];
}
throw new KeyNotFoundException();
}
public HttpResponse RedirectTo(HttpRequest request, object item)
{
if (!Mapper.IdentityCache.TryGetIdentity(item, out Guid identity))
throw new HttpException(500, "Internal Failure");
return request.Redirect("{0}", /*request.GetParameter("type"),*/ identity );
}
public HttpResponse Request(HttpRequest request)
{
Type requestedType = FindType(request);
if (request.ContainsParameter("identity"))
{
if (request.Method.Equals("GET"))
{
object o = Mapper.Load(requestedType, Guid.Parse(request.GetParameter("identity")));
if (o == null)
throw new HttpException(404, "Not Found");
return request.SendJSON(o);
}
else if (request.Method.Equals("PUT"))
{
object o = Mapper.Load(requestedType, Guid.Parse(request.GetParameter("identity")));
if (o == null)
throw new HttpException(404, "Not Found");
JSONMapper.DefaultMapper.Apply(request.ContentReader.ReadToEnd(), o);
Mapper.Save(requestedType, o);
return RedirectTo(request, o);
}
else
throw new MethodNotAllowedException();
}
else if (request.Method.Equals("GET"))
{
JSONObject jo = new JSONObject();
foreach (Guid identity in Mapper.GetDocumentIDs(requestedType))
{
jo[identity.ToString()] = JSONMapper.DefaultMapper.ToJson(Mapper.Load(requestedType, identity));
}
return request.SendJSON(jo);
}
else if (request.Method.Equals("POST"))
{
if (!request.GetRequestHeader("content-type").Equals("application/json"))
throw new UnsupportedMediaTypeException();
object o = JSONMapper.DefaultMapper.FromJson(request.ContentReader.ReadToEnd(), requestedType);
Mapper.Save(requestedType, o);
return RedirectTo(request, o);
}
throw new BadRequestException();
}
}
}

56
ObjectHandler.cs 100644
View File

@ -0,0 +1,56 @@
// /**
// * File: ObjectHandler.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;
using ln.http;
using ln.types.reflection;
using ln.json.mapping;
namespace ln.json.http
{
public class ObjectHandler
{
public ObjectHandler()
{
}
public HttpResponse DoObject(HttpRequest httpRequest, object o, Queue<string> pathQueue)
{
if (pathQueue.Count > 1)
{
string next = pathQueue.Dequeue();
TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(o.GetType());
if (typeDescriptor.ContainsAttribute(next))
{
}
}
else if (pathQueue.Count == 0)
{
switch (httpRequest.Method)
{
case "GET":
return httpRequest.SendJSON(JSONMapper.DefaultMapper.ToJson(o));
case "PUT":
break;
default:
throw new NotImplementedException();
}
}
else
{
}
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,97 @@
using System;
using ln.http.router;
using ln.http;
using System.Collections.Generic;
using System.Reflection;
using ln.types;
using ln.json.mapping;
using ln.http.exceptions;
using System.Linq;
using ln.logging;
using ln.types.reflection;
using ln.json.reflection;
namespace ln.json.http
{
public class ObjectPoolContainerRestAPI : SimpleRouter
{
public ObjectPoolContainer ObjectPoolContainer { get; }
public ObjectPoolContainerSerializer ObjectPoolContainerSerializer { get; }
public ObjectPoolContainerRestAPI():this(new ObjectPoolContainer()) { }
public ObjectPoolContainerRestAPI(ObjectPoolContainer objectPoolContainer)
{
AddSimpleRoute("/:type", RequestType);
AddSimpleRoute("/:type/:identity", RequestInstance);
AddSimpleRoute("/:type/:identity/*", RequestInstancePath);
ObjectPoolContainer = objectPoolContainer;
ObjectPoolContainerSerializer = new ObjectPoolContainerSerializer(objectPoolContainer);
}
HttpResponse RequestType(HttpRequest httpRequest)
{
ObjectPool objectPool = ObjectPoolContainer[httpRequest.GetParameter("type")];
switch (httpRequest.Method)
{
case "GET":
JSONArray instanceList = new JSONArray();
foreach (object instance in objectPool.Instances)
{
instanceList.Add(ObjectPoolContainerSerializer.SerializeObject(objectPool, instance));
}
return httpRequest.SendJSON(instanceList);
case "POST":
object o = null;
try
{
o = ObjectPoolContainerSerializer.UnserializeObject(objectPool, httpRequest.GetJSON() as JSONObject);
objectPool.Add(o);
return httpRequest.Redirect(objectPool.GetIdentity(o).ToString());
}
catch (Exception e)
{
Logging.Log(e);
(o as IDisposable)?.Dispose();
throw new HttpException(500, String.Format("Internal Error {0}", e.ToString()));
}
}
throw new NotImplementedException();
}
HttpResponse RequestInstance(HttpRoutingContext routingContext, HttpRequest httpRequest)
{
ObjectPool objectPool = ObjectPoolContainer[httpRequest.GetParameter("type")];
object o = objectPool[Cast.To(httpRequest.GetParameter("identity"), objectPool.IdentityType)];
switch (httpRequest.Method)
{
case "DELETE":
objectPool.Remove(objectPool.GetIdentity(o));
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.StatusCode = 204;
return httpResponse;
case "PUT":
object identity = objectPool.GetIdentity(o);
ObjectPoolContainerSerializer.UpdateObject(objectPool, o, httpRequest.GetJSON() as JSONObject);
if (!objectPool.GetIdentity(o).Equals(identity))
{
objectPool.Remove(identity);
objectPool.Add(o);
}
return httpRequest.Redirect(objectPool.GetIdentity(o).ToString());
case "GET":
return httpRequest.SendJSON(ObjectPoolContainerSerializer.SerializeObject(objectPool, o));
}
throw new BadRequestException();
}
HttpResponse RequestInstancePath(HttpRoutingContext routingContext, HttpRequest httpRequest)
{
// ToDo: Implement Sub-Instance Level access...
throw new NotImplementedException();
}
}
}

30
ObjectRequest.cs 100644
View File

@ -0,0 +1,30 @@
using System;
using ln.http;
using System.Collections;
namespace ln.json.http
{
public class ObjectRequest
{
public ObjectRequest ParentRequest { get; }
public HttpRequest HttpRequest { get; }
public string[] RequestPath { get; }
public object Value { get; }
public ObjectRequest(HttpRequest httpRequest,string requestPath)
{
HttpRequest = httpRequest;
RequestPath = requestPath.Split(new char[] { '/' });
}
ObjectRequest(ObjectRequest parentRequest,HttpRequest httpRequest,string[] requestPath,object lastValue)
{
ParentRequest = parentRequest;
HttpRequest = httpRequest;
RequestPath = requestPath;
Value = lastValue;
}
public string[] RequestedPath => throw new NotImplementedException();
}
}

View File

@ -0,0 +1,26 @@
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.json.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("")]

17
TypeReflection.cs 100644
View File

@ -0,0 +1,17 @@
using System;
using ln.json.mapping;
namespace ln.json.http
{
public class TypeReflection
{
public Type NativeType { get; }
public TypeReflection()
{
}
public JSONValue ToJSON(object value) => JSONMapper.DefaultMapper.ToJson(value);
}
}

View File

@ -0,0 +1,60 @@
<?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>{9D06CBC0-9AED-48A8-8080-1514109E5AB3}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>ln.json.http</RootNamespace>
<AssemblyName>ln.json.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="HTTPRequestExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="HttpObjectContainer.cs" />
<Compile Include="MappingContainer.cs" />
<Compile Include="IObjectHandler.cs" />
<Compile Include="ObjectHandler.cs" />
<Compile Include="ObjectPoolContainerRestAPI.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.json\ln.json.csproj">
<Project>{D9342117-3249-4D8B-87C9-51A50676B158}</Project>
<Name>ln.json</Name>
</ProjectReference>
<ProjectReference Include="..\ln.types\ln.types.csproj">
<Project>{8D9AB9A5-E513-4BA7-A450-534F6456BF28}</Project>
<Name>ln.types</Name>
</ProjectReference>
<ProjectReference Include="..\ln.logging\ln.logging.csproj">
<Project>{D471A566-9FB6-41B2-A777-3C32874ECD0E}</Project>
<Name>ln.logging</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Default' ">
<StartAction>Project</StartAction>
<ConsolePause>true</ConsolePause>
</PropertyGroup>
</Project>