Exemplo n.º 1
0
        /// <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();
                }
            }
        }