Files
TouchSocket/RRQMSocket.RPC.WebApi/Converter/XmlDataConverter.cs
若汝棋茗 44e4400eaf 修复文件
2021-08-13 21:06:02 +08:00

129 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权除特别声明或在RRQMCore.XREF命名空间的代码归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.Helper;
using RRQMCore.Serialization;
using RRQMSocket.Http;
namespace RRQMSocket.RPC.WebApi
{
/// <summary>
/// Xml结果转换器
/// </summary>
public class XmlDataConverter : ApiDataConverter
{
/// <summary>
/// OnPost
/// </summary>
/// <param name="httpRequest"></param>
/// <param name="methodInvoker"></param>
/// <param name="methodInstance"></param>
public override void OnPost(HttpRequest httpRequest, ref MethodInvoker methodInvoker, MethodInstance methodInstance)
{
switch (httpRequest.Content_Type)
{
case "application/x-www-form-urlencoded":
{
if (httpRequest.Params != null)
{
for (int i = 0; i < methodInstance.Parameters.Length; i++)
{
if (httpRequest.Params.TryGetValue(methodInstance.ParameterNames[i], out string value))
{
methodInvoker.Parameters[i] = value.ParseToType(methodInstance.ParameterTypes[i]);
}
else
{
methodInvoker.Parameters[i] = methodInstance.ParameterTypes[i].GetDefault();
}
}
}
break;
}
case "application/xml":
{
if (methodInstance.Parameters.Length > 0)
{
for (int i = 0; i < methodInstance.Parameters.Length; i++)
{
if (i == 0)
{
methodInvoker.Parameters[i] = SerializeConvert.XmlDeserializeFromBytes(
httpRequest.Encoding.GetBytes(httpRequest.Body), methodInstance.ParameterTypes[0]);
}
else
{
methodInvoker.Parameters[i] = methodInstance.ParameterTypes[i].GetDefault();
}
}
}
break;
}
}
}
/// <summary>
/// 在调用完成时转换结果
/// </summary>
/// <param name="methodInvoker"></param>
/// <param name="methodInstance"></param>
/// <returns></returns>
public override HttpResponse OnResult(MethodInvoker methodInvoker, MethodInstance methodInstance)
{
HttpRequest httpRequest = (HttpRequest)methodInvoker.Flag;
HttpResponse httpResponse = new HttpResponse();
switch (methodInvoker.Status)
{
case InvokeStatus.Success:
{
if (methodInvoker.ReturnParameter != null)
{
httpResponse.FromXML(SerializeConvert.XmlSerializeToString(methodInvoker.ReturnParameter));
break;
}
else
{
httpResponse.FromText(string.Empty);
}
break;
}
case InvokeStatus.UnFound:
{
string xmlString = SerializeConvert.XmlSerializeToString(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromXML(xmlString, "404");
break;
}
case InvokeStatus.UnEnable:
{
string xmlString = SerializeConvert.XmlSerializeToString(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromXML(xmlString, "405");
break;
}
case InvokeStatus.Abort:
{
string xmlString = SerializeConvert.XmlSerializeToString(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromXML(xmlString, "403");
break;
}
case InvokeStatus.InvocationException:
case InvokeStatus.Exception:
{
string xmlString = SerializeConvert.XmlSerializeToString(new ActionResult() { Status = methodInvoker.Status, Message = methodInvoker.StatusMessage });
httpResponse.FromXML(xmlString, "422");
break;
}
}
return httpResponse;
}
}
}