104 lines
3.5 KiB
C#
104 lines
3.5 KiB
C#
// /**
|
|
// * 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();
|
|
}
|
|
}
|
|
}
|