Exemplo n.º 1
0
		protected override void OnLoad(EventArgs e)
		{
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			Response.Clear();
			base.OnLoad(e);
			string action = Request.QueryString["a"];
			if (!string.IsNullOrEmpty(action))
			{
				ProcessUtilRequest(action);
				ProcessNormalRequest(action);
			}
			string xml = Request.Form["xml"];
			if (string.IsNullOrEmpty(xml))
			{
				xml = Request.QueryString["xml"];
			}
			if (!string.IsNullOrEmpty(xml))
			{
				JsRequest r = xml.FromXml<JsRequest>();
				HandleJsRequest(r);
			}
			else if (string.IsNullOrEmpty(action))
			{
				JsRequest r = new JsRequest();
				r.Methods.Add(new Method { Name = "Content" });
				r.Methods[0].Params.Add(new Param { Name = "Arg1", Value = "a" });
				r.Methods[0].Params.Add(new Param { Name = "Arg2", Value = "b" });
				Response.Write(r.ToXml());
			}
			AuthCodePage.Update();
			Response.End();
		}
Exemplo n.º 2
0
		protected override void OnLoad(EventArgs e)
		{
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			Response.Clear();
			base.OnLoad(e);
			string action = Request.QueryString["a"];
			if (!string.IsNullOrEmpty(action))
			{
				if (string.Equals(action, "util", StringComparison.OrdinalIgnoreCase))
				{
					string type = Request.QueryString["t"];
					if (!string.IsNullOrEmpty(type))
					{
						if ("Escape".Equals(type))
						{
							Response.Write("<textarea id='box' style='width:800px; height:500px;'></textarea><br /><script>var b = document.getElementById('box');</script><input type='button' onmousedown='box.value = unescape(box.value);' value='UnEscape' /><input type='button' onmousedown='box.value = escape(box.value);' value='Escape' />");
							Response.End();
						}
					}
				}
				JsRequest r = new JsRequest();
				string[] actions = action.Split(',');
				foreach (string a in actions)
				{
					Method m = r.AddMethod(a);
					if (!string.IsNullOrEmpty(a))
					{
						string param = Request.QueryString[a];
						if (!string.IsNullOrEmpty(param))
						{
							string[] args = param.Split(',');
							foreach (string i in args)
							{
								m.AddParam(i);
							}
						}
					}
				}
				HandleJsRequest(r);
			}
			string xml = Request.Form["xml"];
			if (!string.IsNullOrEmpty(xml))
			{
				JsRequest r = xml.FromXml<JsRequest>();
				HandleJsRequest(r);
			}
			else if (string.IsNullOrEmpty(action))
			{
				JsRequest r = new JsRequest();
				r.Methods.Add(new Method { Name = "GetData" });
				r.Methods[0].Params.Add(new Param { Name = "Arg1", Value = "a" });
				r.Methods[0].Params.Add(new Param { Name = "Arg2", Value = "b" });
				Response.Write(r.ToXml());
			}
			AuthCodePage.Update();
			Response.End();
		}
Exemplo n.º 3
0
		private void ProcessNormalRequest(string action)
		{
			JsRequest r = new JsRequest();
			string[] actions = action.Split(',');
			foreach (string a in actions)
			{
				Method m = r.AddMethod(a);
				if (!string.IsNullOrEmpty(a))
				{
					string param = Request.QueryString[a];
					if (!string.IsNullOrEmpty(param))
					{
						string[] args = param.Split(',');
						foreach (string i in args)
						{
							m.AddParam(i);
						}
					}
				}
			}
			HandleJsRequest(r);
		}
Exemplo n.º 4
0
		protected override void OnLoad(EventArgs e)
		{
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			Response.Clear();
			base.OnLoad(e);
			string action = Request.QueryString["a"];
			if (!string.IsNullOrEmpty(action))
			{
				ProcessUtilRequest(action);
				ProcessNormalRequest(action);
			}
			bool processed = ProcessJsonRequest() || ProcessXmlRequest();
			if (!processed && string.IsNullOrEmpty(action))
			{
				JsRequest r = new JsRequest();
				r.Methods.Add(new Method { Name = "Content" });
				r.Methods[0].Params.Add(new Param { Name = "Arg1", Value = "a" });
				r.Methods[0].Params.Add(new Param { Name = "Arg2", Value = "b" });
				Response.Write(r.ToXml());
			}
			AuthCodePage.Update();
			Response.End();
		}
Exemplo n.º 5
0
		private void HandleNormalRequest()
		{
			JsRequest r = new JsRequest();
			MethodInfo mi = Intent;
			InvokeResult ir = null;
			if (mi != null)
			{
				ir = InvokeMethod(r, mi);
			}
			else
			{
				r.SetException(new ArgumentException(ServerErrorCode.InvalidNormalRequestMethod));
			}
			if (ir == null || !ir.IsPageResponse)
			{
				OutputResponse(r);
			}
		}
Exemplo n.º 6
0
		private static void OutputResponse(JsRequest r)
		{
			string output = r.ToJson();
			HttpContext.Current.Response.Write(output);
		}  
Exemplo n.º 7
0
		private InvokeResult InvokeMethod(JsRequest r, MethodInfo mi)
		{
			InvokeResult rlt = new InvokeResult();
			try
			{
				if (mi != null)
				{
					mi.ValidateServiceMethod(rlt);
					r.AddMethod(mi.Name);
					if (mi.ReturnType == typeof (void))
					{
						if (Params != null && Params.Length > 0)
						{
							object[] args = Params.ConvertType(0, mi.GetParameters());
							mi.Invoke(this, args);
							r.Methods[0].MethodReturnValue = null;
						}
						else
						{
							mi.Invoke(this, null);
							r.Methods[0].MethodReturnValue = null;
						}
					}
					else
					{
					    object rv = null;
						if (Params != null && Params.Length > 0)
						{
							object[] args = Params.ConvertType(0, mi.GetParameters());
							rv = mi.Invoke(this, args);
						}
						else
						{
                            rv = mi.Invoke(this, null);
						}
                        r.Methods[0].MethodReturnValue = rv;
					    if (rv is ResultBase)
					    {
					        ResultBase result = (ResultBase)rv;
					        if (!result.IsNoException)
					        {
					            r.SetException(result.LastError, mi.Name);
					        }
					    }
					}
				}
				else
				{
					throw new ArgumentNullException(ServerErrorCode.MethodNull);
				}
			}
			catch (Exception ex)
			{
				rlt.LastError = ex;
				if (mi != null)
				{
					r.SetException(ex, mi.Name);
				}
				else
				{
					r.SetException(ex);
				}
			}
			return rlt;
		}
Exemplo n.º 8
0
		private void ProcessJsRequest(JsRequest r)
		{
			Type type = GetType();
			ParameterInfo currentParameterInfo = null;
			MethodInfo currentMethod = null;
			foreach (Method m in r.Methods)
			{
				if (m != null)
				{
					try
					{
						MethodInfo mi = type.GetMethod(m.Name, BindingFlags.Public | BindingFlags.Instance);
						currentMethod = mi;
						InvokeJsMethod(ref currentParameterInfo, m, mi);
					}
					catch (ArgumentNullException err)
					{
						ErrorHandler.Handle(err);
						HandleInvalidInputException(currentParameterInfo, m);
					}
					catch (InvalidCastException err)
					{
						ErrorHandler.Handle(err);
						HandleInvalidInputException(currentParameterInfo, m);
					}
					catch (FormatException err)
					{
						ErrorHandler.Handle(err);
						HandleInvalidInputException(currentParameterInfo, m);
					}
					catch (TargetInvocationException err)
					{
						Exception innerException = ErrorHandler.Handle(err);
						if (innerException is CustomException)
						{
							HandleUnknownException(m, ErrorHandler.Handle(err));
						}
						else if (!(innerException is IgnorableException))
						{
							HandleInvokeException(currentMethod, m);
						}
					}
					catch (Exception err)
					{
						HandleUnknownException(m, err);
					}
				}
			}
			OutputResponse(r);
		}
Exemplo n.º 9
0
		private void HandleRestfulRequest(HttpContext context)
		{
			string method = context.Request.HttpMethod;
			JsRequest r = new JsRequest();
			Method m = r.AddMethod(method);
			if (method.IndexOf("get", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				m.MethodReturnValue = Get(context);
			}
			else if (method.IndexOf("post", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				m.MethodReturnValue = Post(context);
			}
			else if (method.IndexOf("put", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				m.MethodReturnValue = Put(context);
			}
			else if (method.IndexOf("delete", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				m.MethodReturnValue = Delete(context);
			}
			else
			{
				r.SetException(new ArgumentException(ServerErrorCode.InvalidHttpMethod));
			}
			OutputResponse(r);
		}
Exemplo n.º 10
0
		private void HandleJsRequest(JsRequest r)
		{
			if (r == null)
			{
				return;
			}
			Type type = this.GetType();
			ParameterInfo currentParameterInfo = null;
			MethodInfo currentMethod = null;
			for (int i = 0; i < r.Methods.Count; i++)
			{
				Method m = r.Methods[i];
				if (m != null)
				{
					try
					{
						MethodInfo mi = type.GetMethod(m.Name, BindingFlags.Public | BindingFlags.Instance);
						currentMethod = mi;
						if (mi != null && mi.IsPublic)
						{
							object[] args = m.ToArray();

							ParameterInfo[] pis = mi.GetParameters();
							if (pis != null)
							{
								if (pis.Length == 1 && args.Length > 1)
								{
									args = new object[] { args };
								}
								for (int ii = 0; ii < pis.Length; ii++)
								{
									ParameterInfo pi = pis[ii];
									currentParameterInfo = pi;
									Type ptype = pi.ParameterType;
									args[ii] = Convert.ChangeType(args[ii], ptype);
								}
							}
							if (mi.ReturnType != typeof(void))
							{
								m.MethodReturnValue = mi.Invoke(this, args);
							}
							else
							{
								mi.Invoke(this, args);
							}
						}
					}
					catch (ArgumentNullException err)
					{
						ErrorHandler.Handle(err);
						HandleInvalidInputException(currentParameterInfo, m);
					}
					catch (InvalidCastException err)
					{
						ErrorHandler.Handle(err);
						HandleInvalidInputException(currentParameterInfo, m);
					}
					catch (FormatException err)
					{
						ErrorHandler.Handle(err);
						HandleInvalidInputException(currentParameterInfo, m);
					}
					catch (TargetInvocationException err)
					{
						Exception innerException = ErrorHandler.Handle(err);
						if (innerException is CustomException)
						{
							HandleUnknownException(m, ErrorHandler.Handle(err));
						}
						else if (!(innerException is IgnorableException))
						{
							HandleInvokeException(currentMethod, m);
						}
					}
					catch (Exception err)
					{
						HandleUnknownException(m, err);
					}
				}
			}
			string output = r.ToJson();
			Response.Write(output);
		}