WIP
parent
3fe417cead
commit
23e1787298
|
@ -0,0 +1,118 @@
|
|||
// /**
|
||||
// * File: JsonCallResource.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.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using ln.http.exceptions;
|
||||
namespace ln.http.resources
|
||||
{
|
||||
public class CallableAttribute : Attribute
|
||||
{
|
||||
public String Alias { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public abstract class JsonCallResource : BaseResource
|
||||
{
|
||||
Dictionary<string, MethodInfo[]> callableMethods = new Dictionary<string, MethodInfo[]>();
|
||||
|
||||
public JsonCallResource(Resource container, string name)
|
||||
: base(container, name)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize()
|
||||
{
|
||||
Dictionary<string, List<MethodInfo>> callables = new Dictionary<string, List<MethodInfo>>();
|
||||
|
||||
foreach (MethodInfo methodInfo in this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
|
||||
{
|
||||
CallableAttribute callableAttribute = methodInfo.GetCustomAttribute<CallableAttribute>();
|
||||
if (callableAttribute != null)
|
||||
{
|
||||
string alias = callableAttribute.Alias == null ? methodInfo.Name : callableAttribute.Alias;
|
||||
if (!callables.ContainsKey(alias))
|
||||
{
|
||||
callables.Add(alias, new List<MethodInfo>());
|
||||
}
|
||||
callables[alias].Add(methodInfo);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (String alias in callables.Keys)
|
||||
{
|
||||
callableMethods.Add(alias, callables[alias].ToArray());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private MethodInfo FindMethodSignature(String methodName,int nParameters)
|
||||
{
|
||||
foreach (MethodInfo methodInfo in callableMethods[methodName])
|
||||
{
|
||||
if (nParameters == methodInfo.GetParameters().Length)
|
||||
{
|
||||
return methodInfo;
|
||||
}
|
||||
}
|
||||
throw new ArgumentException("No method signature matching the parameters was found");
|
||||
}
|
||||
|
||||
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
||||
{
|
||||
if (!httpRequest.GetRequestHeader("Content-Type").Equals("application/json"))
|
||||
{
|
||||
throw new HttpException("JSON Method call failed, call object not received");
|
||||
}
|
||||
|
||||
MethodCall methodCall = JsonConvert.DeserializeObject<MethodCall>(httpRequest.ContentReader.ReadToEnd());
|
||||
MethodResult methodResult;
|
||||
|
||||
try
|
||||
{
|
||||
methodResult = new MethodResult();
|
||||
methodResult.MethodName = methodCall.MethodName;
|
||||
|
||||
MethodInfo methodInfo = FindMethodSignature(methodCall.MethodName, methodCall.Parameters.Length);
|
||||
methodResult.Result = methodInfo.Invoke(this, methodCall.Parameters);
|
||||
} catch (Exception e)
|
||||
{
|
||||
methodResult = new MethodResult();
|
||||
methodResult.MethodName = methodCall.MethodName;
|
||||
methodResult.Exception = e;
|
||||
}
|
||||
|
||||
String result = JsonConvert.SerializeObject(methodResult);
|
||||
|
||||
HttpResponse httpResponse = new HttpResponse(httpRequest);
|
||||
httpResponse.SetHeader("content-type", "application/json");
|
||||
httpResponse.ContentWriter.Write(result);
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
|
||||
class MethodCall
|
||||
{
|
||||
public String MethodName;
|
||||
public object[] Parameters;
|
||||
}
|
||||
|
||||
class MethodResult
|
||||
{
|
||||
public String MethodName;
|
||||
public object Result;
|
||||
public Exception Exception;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -28,6 +28,9 @@
|
|||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@ -39,6 +42,7 @@
|
|||
<Compile Include="DirectoryResource.cs" />
|
||||
<Compile Include="BaseResource.cs" />
|
||||
<Compile Include="TemplateResource.cs" />
|
||||
<Compile Include="JsonCallResource.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ln.http\ln.http.csproj">
|
||||
|
@ -50,5 +54,8 @@
|
|||
<Name>ln.templates</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net47" />
|
||||
</packages>
|
Loading…
Reference in New Issue