You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
12 KiB
390 lines
12 KiB
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using OT.COM.SignalerMessage;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
|
|
namespace DefenseWeb.Models
|
|
{
|
|
|
|
public abstract class AApiServiceBase
|
|
{
|
|
#region Define
|
|
private const string _TAG_CALLBACK = "callback";
|
|
|
|
private class CQS2Json
|
|
{
|
|
class CQSJsonNode
|
|
{
|
|
public object Data { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
if (null == Data)
|
|
{
|
|
sb.Append("null");
|
|
}
|
|
else if (Data is string)
|
|
{
|
|
string sData = Data.ToString();
|
|
if (0 < sData.Length && true == IsNumeric(sData))
|
|
{
|
|
sb.Append(Data.ToString());
|
|
}
|
|
else if (0 == sData.Length)
|
|
{
|
|
sb.Append("null");
|
|
}
|
|
else
|
|
{
|
|
sb.Append("\"" + Data.ToString() + "\"");
|
|
}
|
|
}
|
|
else if (Data is Dictionary<string, CQSJsonNode>)
|
|
{
|
|
Dictionary<string, CQSJsonNode> dic = Data as Dictionary<string, CQSJsonNode>;
|
|
sb.Append("{");
|
|
int nCount = 0;
|
|
foreach (string sKey in dic.Keys)
|
|
{
|
|
if (0 != nCount)
|
|
{
|
|
sb.Append(",");
|
|
}
|
|
sb.Append("\"" + sKey + "\" : ");
|
|
sb.Append(dic[sKey].ToString());
|
|
nCount = nCount + 1;
|
|
}
|
|
|
|
sb.Append("}");
|
|
}
|
|
else if (Data is List<CQSJsonNode>)
|
|
{
|
|
List<CQSJsonNode> l = Data as List<CQSJsonNode>;
|
|
sb.Append("[");
|
|
int nCount = 0;
|
|
foreach (CQSJsonNode n in l)
|
|
{
|
|
if (0 != nCount)
|
|
{
|
|
sb.Append(",");
|
|
}
|
|
sb.Append(n.ToString());
|
|
nCount = nCount + 1;
|
|
}
|
|
|
|
sb.Append("]");
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("No handle");
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public void AddKeyValue(string i_sKey, CQSJsonNode i_Value)
|
|
{
|
|
if (null == Data)
|
|
{
|
|
Data = new Dictionary<string, CQSJsonNode>();
|
|
}
|
|
Dictionary<string, CQSJsonNode> dic = Data as Dictionary<string, CQSJsonNode>;
|
|
dic.Add(i_sKey, i_Value);
|
|
}
|
|
|
|
public void AppendList(CQSJsonNode i_Value)
|
|
{
|
|
if (null == Data)
|
|
{
|
|
Data = new List<CQSJsonNode>();
|
|
}
|
|
List<CQSJsonNode> l = Data as List<CQSJsonNode>;
|
|
l.Add(i_Value);
|
|
}
|
|
|
|
public void SetValue(string i_Value)
|
|
{
|
|
Data = i_Value;
|
|
}
|
|
|
|
public CQSJsonNode GetMapValue(string i_sKey)
|
|
{
|
|
CQSJsonNode res = null;
|
|
if (null == Data)
|
|
{
|
|
Data = new Dictionary<string, CQSJsonNode>();
|
|
}
|
|
Dictionary<string, CQSJsonNode> dic = Data as Dictionary<string, CQSJsonNode>;
|
|
if (false == dic.Keys.Contains(i_sKey))
|
|
{
|
|
res = new CQSJsonNode();
|
|
dic.Add(i_sKey, res);
|
|
}
|
|
else
|
|
{
|
|
res = dic[i_sKey];
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public CQSJsonNode GetListItem(int i_n)
|
|
{
|
|
CQSJsonNode res = null;
|
|
if (null == Data)
|
|
{
|
|
Data = new List<CQSJsonNode>();
|
|
}
|
|
|
|
List<CQSJsonNode> l = Data as List<CQSJsonNode>;
|
|
if (i_n >= l.Count)
|
|
{
|
|
res = new CQSJsonNode();
|
|
l.Add(res);
|
|
}
|
|
else
|
|
{
|
|
res = l[i_n];
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
public CQSJsonNode GetMapItemByKey(string i_sKey)
|
|
{
|
|
CQSJsonNode nRes = null;
|
|
if (Data != null && Data is Dictionary<string, CQSJsonNode>)
|
|
{
|
|
Dictionary<string, CQSJsonNode> dic = Data as Dictionary<string, CQSJsonNode>;
|
|
nRes = dic[i_sKey];
|
|
}
|
|
return nRes;
|
|
}
|
|
|
|
public CQSJsonNode GetListItemByIndex(int i_nIdx)
|
|
{
|
|
CQSJsonNode nRes = null;
|
|
if (Data != null && Data is List<CQSJsonNode>)
|
|
{
|
|
List<CQSJsonNode> l = Data as List<CQSJsonNode>;
|
|
nRes = l[i_nIdx];
|
|
}
|
|
return nRes;
|
|
}
|
|
}
|
|
|
|
public static bool IsNumeric(string inputString)
|
|
{
|
|
|
|
return Regex.IsMatch(inputString, "^[0-9]+$");
|
|
|
|
}
|
|
|
|
|
|
public static string Convert(IEnumerable<KeyValuePair<string, string>> i_iKeyValue)
|
|
{
|
|
CQSJsonNode rootNode = new CQSJsonNode();
|
|
CQSJsonNode curNode = null;
|
|
int nCount = 0;
|
|
foreach (KeyValuePair<string, string> kp in i_iKeyValue)
|
|
{
|
|
CQSJsonNode v = new CQSJsonNode() { Data = kp.Value };
|
|
|
|
string[] saToken = kp.Key.Split(new char[] { '[', ']', '.' });
|
|
curNode = rootNode;
|
|
nCount = 0;
|
|
|
|
foreach (string sToken in saToken)
|
|
{
|
|
if ("" == sToken)
|
|
{
|
|
nCount = nCount + 1;
|
|
continue;
|
|
}
|
|
bool isList = IsNumeric(sToken);
|
|
|
|
if ((saToken.Length - 1) == nCount)
|
|
{
|
|
if (false == isList)
|
|
{
|
|
curNode.AddKeyValue(sToken, v);
|
|
}
|
|
else
|
|
{
|
|
curNode.AppendList(v);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
if (false == isList)
|
|
{
|
|
curNode = curNode.GetMapValue(sToken);
|
|
}
|
|
else
|
|
{
|
|
curNode = curNode.GetListItem(Int32.Parse(sToken));
|
|
}
|
|
}
|
|
nCount = nCount + 1;
|
|
}
|
|
}
|
|
return rootNode.ToString();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Abstract decleare
|
|
protected abstract string HandleRequest(CRequestMessage i_jo, HttpRequestMessage i_rRequest);
|
|
protected abstract CResponseMessage HandleRequest2(CRequestMessage i_jo, HttpRequestMessage i_rRequest);
|
|
#endregion
|
|
|
|
#region Attribute
|
|
|
|
#endregion
|
|
|
|
#region helper function
|
|
|
|
public AApiServiceBase()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Converter for http query string to JObject
|
|
/// </summary>
|
|
/// <param name="i_iKeyValue"></param>
|
|
/// <returns></returns>
|
|
protected CRequestMessage httpQueryStringToRequest(IEnumerable<KeyValuePair<string, string>> i_iKeyValue)
|
|
{
|
|
string sJaon = CQS2Json.Convert(i_iKeyValue);
|
|
|
|
return JsonConvert.DeserializeObject<CRequestMessage>(sJaon);
|
|
}
|
|
|
|
|
|
protected string getHttpRequestValue(IEnumerable<KeyValuePair<string, string>> i_iKeyValue, string i_sTarget)
|
|
{
|
|
string sRes = null;
|
|
foreach (KeyValuePair<string, string> kp in i_iKeyValue)
|
|
{
|
|
if (i_sTarget == kp.Key)
|
|
{
|
|
sRes = kp.Value;
|
|
break;
|
|
}
|
|
}
|
|
return sRes;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Create json message
|
|
/// </summary>
|
|
/// <param name="i_o"></param>
|
|
/// <returns></returns>
|
|
protected string makeMessage(object i_o)
|
|
{
|
|
return JsonConvert.SerializeObject(i_o);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create general error
|
|
/// </summary>
|
|
/// <param name="i_sMsg"></param>
|
|
/// <returns></returns>
|
|
protected string makeErrorReturn(CRequestMessage i_OriRequest, string i_sMsg)
|
|
{
|
|
return makeMessage(new CErrorResponseMessage(i_sMsg, i_OriRequest));
|
|
}
|
|
|
|
public string ProgessJson(string i_sJson, HttpRequestMessage i_rRequest)
|
|
{
|
|
CRequestMessage crm = null;
|
|
|
|
if(null != i_sJson)
|
|
{
|
|
crm = JsonConvert.DeserializeObject<CRequestMessage>(i_sJson);
|
|
}
|
|
else
|
|
{
|
|
crm = new CRequestMessage() { TYPE = "LITE" };
|
|
}
|
|
crm.ClientIP = GetClientIp(i_rRequest);
|
|
return HandleRequest(crm, i_rRequest);
|
|
}
|
|
|
|
public CResponseMessage ProgessJson2(string i_sJson, HttpRequestBase i_rRequest)
|
|
{
|
|
CRequestMessage crm = null;
|
|
|
|
if (null != i_sJson)
|
|
{
|
|
crm = JsonConvert.DeserializeObject<CRequestMessage>(i_sJson);
|
|
}
|
|
else
|
|
{
|
|
crm = new CRequestMessage() { TYPE = "LITE" };
|
|
}
|
|
//crm.ClientIP = GetClientIp(i_rRequest);
|
|
crm.ClientIP = i_rRequest.UserHostAddress;
|
|
return HandleRequest2(crm, null);
|
|
}
|
|
|
|
|
|
private string GetClientIp(HttpRequestMessage i_rRequest)
|
|
{
|
|
|
|
if (i_rRequest.Properties.ContainsKey("MS_HttpContext"))
|
|
{
|
|
return ((HttpContextWrapper)i_rRequest.Properties["MS_HttpContext"]).Request.UserHostAddress;
|
|
}
|
|
/*else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
|
|
{
|
|
RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name];
|
|
return prop.Address;
|
|
}*/
|
|
else if (HttpContext.Current != null)
|
|
{
|
|
return HttpContext.Current.Request.UserHostAddress;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
/*
|
|
public string ProcessData(IEnumerable<KeyValuePair<string, string>> i_iKeyValue)
|
|
{
|
|
string sRes = "";
|
|
string sCallback = getHttpRequestValue(i_iKeyValue, _TAG_CALLBACK);
|
|
|
|
CRequestMessage crm = httpQueryStringToRequest(i_iKeyValue);
|
|
string sHandle = HandleRequest(crm, JsonConvert.SerializeObject(crm));
|
|
if (false == string.IsNullOrWhiteSpace(sCallback))
|
|
{
|
|
sRes = string.Format("{0}({1});", sCallback, sHandle);
|
|
}
|
|
else
|
|
{
|
|
sRes = sHandle;
|
|
}
|
|
|
|
return sRes;
|
|
}*/
|
|
#endregion
|
|
}
|
|
|
|
}
|