/// <summary> /// Serialiaztion of string valued objects /// </summary> /// <param name="obj">Object to serialize</param> /// <param name="abstractHessianOutput">HessianOutput - Instance</param> public override void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput) { if (obj == null) abstractHessianOutput.WriteNull(); else { abstractHessianOutput.WriteMapBegin(obj.GetType().FullName); abstractHessianOutput.WriteString("value"); abstractHessianOutput.WriteString(obj.ToString()); abstractHessianOutput.WriteMapEnd(); } }
/// <summary> /// Invoke the object with the request from the input stream. /// </summary> /// <param name="inHessian">the Hessian input stream</param> /// <param name="outHessian">the Hessian output stream</param> public void invoke(AbstractHessianInput inHessian, AbstractHessianOutput outHessian) { inHessian.StartCall(); MethodInfo methodInf = getMethodInfo(inHessian.Method); //If the method doesn't exist if (methodInf == null) { outHessian.StartReply(); outHessian.WriteFault("NoSuchMethodException", "The service has no method named: " + inHessian.Method, null); outHessian.CompleteReply(); return; } ParameterInfo[] paramInfo = methodInf.GetParameters(); Object[] valuesParam = new Object[paramInfo.Length]; for (int i = 0; i < paramInfo.Length; i++) { valuesParam[i] = inHessian.ReadObject(paramInfo[i].ParameterType); } inHessian.CompleteCall(); Object result = null; try { result = methodInf.Invoke(m_Service, valuesParam); } catch (Exception e) { //TODO: Exception besser behandeln if (e.GetType() == typeof(System.Reflection.TargetInvocationException)) { if (e.InnerException != null) { e = e.InnerException; } } outHessian.StartReply(); outHessian.WriteFault("ServiceException", e.Message, e); outHessian.CompleteReply(); return; } outHessian.StartReply(); outHessian.WriteObject(result); outHessian.CompleteReply(); }
/// <summary> /// Serialiaztion of string valued objects /// </summary> /// <param name="obj">Object to serialize</param> /// <param name="abstractHessianOutput">HessianOutput - Instance</param> public override void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput) { if (obj == null) { abstractHessianOutput.WriteNull(); } else { abstractHessianOutput.WriteMapBegin(obj.GetType().FullName); abstractHessianOutput.WriteString("value"); abstractHessianOutput.WriteString(obj.ToString()); abstractHessianOutput.WriteMapEnd(); } }
/// <summary> /// Writes Instance of the DateTime class /// </summary> /// <param name="objDate">Instance of the DateTime class</param> /// <param name="abstractHessianOutput">HessianOutput - Stream</param> public override void WriteObject(Object objDate, AbstractHessianOutput abstractHessianOutput) { if (objDate == null) abstractHessianOutput.WriteNull(); else { /*Type type = objDate.GetType(); abstractHessianOutput.WriteMapBegin(type.FullName); abstractHessianOutput.WriteString("value"); abstractHessianOutput.WriteUTCDate(((((DateTime) objDate).Ticks - ticksDifference) / ticksDivider) - (long) TimeZone.CurrentTimeZone.GetUtcOffset(((DateTime) objDate)).TotalMilliseconds); abstractHessianOutput.WriteMapEnd(); */ abstractHessianOutput.WriteUTCDate(MakeJavaDate((DateTime)objDate)); //abstractHessianOutput.WriteUTCDate(((DateTime)objDate).ToFileTimeUtc()); } }
/// <summary> /// Serialization of stream valued objects /// </summary> /// <param name="obj">Object to serialize</param> /// <param name="abstractHessianOutput">HessianOutput - Instance</param> public override void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput) { Stream inStream = (Stream) obj; if (inStream == null) abstractHessianOutput.WriteNull(); else { byte[] buf = new byte[1024]; int len; while ((len = inStream.Read(buf, 0, buf.Length)) > 0) { abstractHessianOutput.WriteByteBufferPart(buf, 0, len); } abstractHessianOutput.WriteByteBufferEnd(buf, 0, 0); } }
/// <summary> /// Writes list objects (That extends from ICollection-Interfaces) /// </summary> /// <param name="objList">List object</param> /// <param name="abstractHessianOutput">HessianOutput - Instance</param> public override void WriteObject(object objList, AbstractHessianOutput abstractHessianOutput) { if (abstractHessianOutput.AddRef(objList)) return ; // TODO auch generische Listen schreiben ICollection collection = ( ICollection) objList; Type type = objList.GetType(); if (type.Equals(typeof(ArrayList))) abstractHessianOutput.WriteListBegin(collection.Count, null); else abstractHessianOutput.WriteListBegin(collection.Count, objList.GetType().FullName); IEnumerator enumerator = collection.GetEnumerator(); while (enumerator.MoveNext()) { Object value = enumerator.Current; abstractHessianOutput.WriteObject(value); } abstractHessianOutput.WriteListEnd(); }
/// <summary> /// Writes enum to the output stream /// </summary> /// <param name="obj"> Enum to write</param> /// <param name="abstractHessianOutput">Instance of the hessian output</param> public override void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput) { // immer den Referenzzähler hochzählen! // (manche enums mit nullable funktionieren nicht) object unrefobj = new object(); if (abstractHessianOutput.AddRef(unrefobj)) return; if (obj == null) { abstractHessianOutput.WriteNull(); } else { Type enumType = obj.GetType(); string name = //Enum.GetName(enumType, obj); ((Enum)obj).ToString(); // das geht auch mit .NET Mobile abstractHessianOutput.WriteMapBegin(enumType.FullName); abstractHessianOutput.WriteObject("name"); abstractHessianOutput.WriteObject(name); abstractHessianOutput.WriteMapEnd(); } }
/// <summary> /// Serialiaztion of objects /// </summary> /// <param name="obj">Object to serialize</param> /// <param name="abstractHessianOutput">HessianOutput - Instance</param> public abstract void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput);
public override void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput) { throw new NotImplementedException(); }
/// <summary> /// Serialiaztion of objects /// </summary> /// <param name="obj">Object to serialize</param> /// <param name="abstractHessianOutput">HessianOutput - Instance</param> public override void WriteObject(object obj, AbstractHessianOutput abstractHessianOutput) { if (abstractHessianOutput.AddRef(obj)) return; Type type = obj.GetType(); abstractHessianOutput.WriteMapBegin(type.FullName); ArrayList serFields = GetSerializableFieldList(); for (int i = 0; i < serFields.Count; i++) { FieldInfo field = (FieldInfo)serFields[i]; abstractHessianOutput.WriteString(field.Name); abstractHessianOutput.WriteObject(field.GetValue(obj)); } abstractHessianOutput.WriteMapEnd(); }
/// <summary> /// Invoke the object with the request from the input stream. /// </summary> /// <param name="inHessian">the Hessian input stream</param> /// <param name="outHessian">the Hessian output stream</param> public void invoke(AbstractHessianInput inHessian, AbstractHessianOutput outHessian) { inHessian.SkipOptionalCall(); // Hessian 1.0 backward compatibility String header; while ((header = inHessian.ReadHeader()) != null) { Object value = inHessian.ReadObject(); //context.addHeader(header, value); } String methodName = inHessian.ReadMethod(); int argLength = inHessian.ReadMethodArgLength(); MethodInfo methodInf = getMethodInfo(methodName + "__" + argLength); if (methodInf == null) { methodInf = getMethodInfo(methodName); } //If the method doesn't exist if (methodInf == null) { outHessian.WriteFault("NoSuchMethodException", EscapeMessage("The service has no method named: " + methodName), null); outHessian.CompleteReply(); return; } ParameterInfo[] paramInfo = methodInf.GetParameters(); Object[] valuesParam = new Object[paramInfo.Length]; if (argLength != paramInfo.Length && argLength >= 0) { outHessian.WriteFault("NoSuchMethod", EscapeMessage("method " + methodInf + " argument length mismatch, received length=" + argLength), null); outHessian.Flush(); return; } for (int i = 0; i < paramInfo.Length; i++) { valuesParam[i] = inHessian.ReadObject(paramInfo[i].ParameterType); } inHessian.CompleteCall(); Object result = null; try { result = methodInf.Invoke(m_Service, valuesParam); } catch (Exception e) { //TODO: Exception besser behandeln //if (e.GetType() == typeof(System.Reflection.TargetInvocationException)) //{ // if (e.InnerException != null) // { // e = e.InnerException; // } //} InvokeErrorCallBack?.Invoke(this, e); //多层InnerException使用GetBaseException()更好。 e = e.GetBaseException(); //outHessian.StartReply(); outHessian.WriteFault("ServiceException", e.Message, e.ToString()); outHessian.Flush(); //outHessian.CompleteReply(); return; } outHessian.StartReply(); outHessian.WriteObject(result); outHessian.CompleteReply(); }
/// <summary> /// This method wrapps an instance call to the hessian /// requests, sends it to the hessian service and translates the reply of this call to the C# - data type /// </summary> /// <param name="methodInfo">The method to call</param> /// <param name="arrMethodArgs">The arguments to the method call</param> /// <returns>Invocation result</returns> public object DoHessianMethodCall(object[] arrMethodArgs, MethodInfo methodInfo) { Type[] argumentTypes = GetArgTypes(arrMethodArgs); Stream sInStream = null; Stream sOutStream = null; try { var methodUri = new Uri(string.Format("{0}?{1}", m_uriHessianServiceUri.ToString(), HttpUtility.UrlEncode(methodInfo.Name)), UriKind.RelativeOrAbsolute); WebRequest webRequest = this.OpenConnection(methodUri); #if COMPACT_FRAMEWORK #else try { webRequest.Headers.Add(m_CHessianProxyFactory.Headers); //webRequest.Headers HttpWebRequest req = webRequest as HttpWebRequest; //Preserve cookies to allow for session affinity between remote server and client if (HttpContext.Current != null) { if (HttpContext.Current.Session["SessionCookie"] == null) { HttpContext.Current.Session.Add("SessionCookie", new CookieContainer()); } req.CookieContainer = (CookieContainer)HttpContext.Current.Session["SessionCookie"]; } } catch { // throw e; //log4net.LogManager.GetLogger(GetType()).Error("Error in setting cookie on request", e); } #endif webRequest.ContentType = "application/octet-stream"; webRequest.Method = "POST"; #if COMPACT_FRAMEWORK #else //Add custom headers if ((HttpContext.Current != null) && (HttpContext.Current.Session != null)) { AddCustomHeadersToRequest(webRequest, HttpContext.Current.Session); } #endif MemoryStream memoryStream = new MemoryStream(2048); //sOutStream = webRequest.GetRequestStream(); //BufferedStream bs = new BufferedStream(sOutStream); AbstractHessianOutput cHessianOutput = m_CHessianProxyFactory.GetHessianOutput(memoryStream); string strMethodName = methodInfo.Name; if (m_CHessianProxyFactory.IsOverloadEnabled) { if (arrMethodArgs != null) { strMethodName = strMethodName + "__" + arrMethodArgs.Length; } else { strMethodName = strMethodName + "__0"; } } cHessianOutput.Call(strMethodName, arrMethodArgs); try { webRequest.ContentLength = memoryStream.ToArray().Length; sOutStream = webRequest.GetRequestStream(); memoryStream.WriteTo(sOutStream); } catch (Exception e) { throw new CHessianException("Exception by sending request to the service with URI:\n" + this.URI.ToString() + "\n" + e.Message); } sOutStream.Flush(); sOutStream.Close(); HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse.StatusCode != HttpStatusCode.OK) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); int chTemp; sInStream = webResponse.GetResponseStream(); if (sInStream != null) { while ((chTemp = sInStream.ReadByte()) >= 0) { sb.Append((char)chTemp); } sInStream.Close(); } throw new CHessianException(sb.ToString()); } sInStream = webResponse.GetResponseStream(); //#if COMPACT_FRAMEWORK // AbstractHessianInput hessianInput = this.GetHessianInput(sInStream); //#else // System.IO.BufferedStream bStream = new BufferedStream(sInStream, 2048); // AbstractHessianInput hessianInput = this.GetHessianInput(bStream); //#endif // return hessianInput.ReadReply(methodInfo.ReturnType); System.IO.BufferedStream bStream = new BufferedStream(sInStream, 2048); AbstractHessianInput hessianInput; int code = bStream.ReadByte(); if (code == 'H') { int major = bStream.ReadByte(); int minor = bStream.ReadByte(); hessianInput = m_CHessianProxyFactory.GetHessian2Input(bStream); Object value = hessianInput.ReadReply(methodInfo.ReturnType); return(value); } else if (code == 'r') { int major = bStream.ReadByte(); int minor = bStream.ReadByte(); hessianInput = m_CHessianProxyFactory.GetHessian1Input(bStream); hessianInput.StartReplyBody(); Object value = hessianInput.ReadObject(methodInfo.ReturnType); //if (value instanceof InputStream) { // value = new ResultInputStream(conn, bStream, in, (InputStream) value); // is = null; // conn = null; //} //else hessianInput.CompleteReply(); return(value); } else { throw new CHessianException("'" + (char)code + "' is an unknown code"); } } catch (WebException e) { var httpResponse = e.Response as HttpWebResponse; if (httpResponse == null) { throw new CHessianException(e.Message, e); } throw new CHessianException(string.Format("Code:{0},Message:{1}", (int)httpResponse.StatusCode, System.Web.HttpUtility.UrlDecode(httpResponse.StatusDescription)), e); } catch (Exception e) { if (e.GetType().Equals(typeof(CHessianException))) { if ((e as CHessianException).FaultWrapper) { // nur ein Wrapper throw e.InnerException; } else { throw e; } } else { throw new CHessianException("Exception by proxy call\n" + e.Message, e); } } finally { if (sInStream != null) { sInStream.Close(); } if (sOutStream != null) { sOutStream.Close(); } } }