Exemple #1
0
        protected void RegisterPublicMethods(String key, Type type)
        {
            XmlRpcServiceAttribute serviceAttr = (XmlRpcServiceAttribute)Attribute.GetCustomAttribute(type, typeof(XmlRpcServiceAttribute), true);

            if (key == null)
            {
                key = (serviceAttr == null) ? type.Name : serviceAttr.Name;
            }

            Dictionary <String, MethodInfo[]> map = new Dictionary <String, MethodInfo[]>();

            foreach (Type itf in type.GetInterfaces())
            {
                if (itf == typeof(IXmlRpcHandler))
                {
                    _handlers[key] = new DefaultXmlRpcHandler(this, TargetProviderFactory == null ? null : TargetProviderFactory.GetTargetProvider(type));
                    continue;
                }

                foreach (MethodInfo mi in itf.GetMethods())
                {
                    RegisterMethod(map, mi, key, type);
                }
            }

            foreach (MethodInfo mi in type.GetMethods())
            {
                RegisterMethod(map, mi, key, type);
            }

            foreach (var pair in map)
            {
                _handlers[pair.Key] = NewXmlRpcHandler(type, pair.Value);
            }
        }
        void SetSerializerSettings(XmlRpcServiceAttribute serviceAttr, XmlRpcSerializer serializer)
        {
            if (serviceAttr.XmlEncoding != null)
            {
                serializer.Configuration.XmlEncoding = Encoding.GetEncoding(serviceAttr.XmlEncoding);
            }

            serializer.Configuration.UseIntTag      = serviceAttr.UseIntTag;
            serializer.Configuration.UseStringTag   = serviceAttr.UseStringTag;
            serializer.Configuration.UseIndentation = serviceAttr.UseIndentation;
            serializer.Configuration.Indentation    = serviceAttr.Indentation;
        }
 public Stream Invoke(Stream requestStream)
 {
     try
     {
         XmlRpcSerializer       serializer  = new XmlRpcSerializer();
         Type                   type        = this.GetType();
         XmlRpcServiceAttribute serviceAttr = (XmlRpcServiceAttribute)
                                              Attribute.GetCustomAttribute(this.GetType(),
                                                                           typeof(XmlRpcServiceAttribute));
         if (serviceAttr != null)
         {
             if (serviceAttr.XmlEncoding != null)
             {
                 serializer.XmlEncoding = Encoding.GetEncoding(serviceAttr.XmlEncoding);
             }
             serializer.UseIntTag      = serviceAttr.UseIntTag;
             serializer.UseStringTag   = serviceAttr.UseStringTag;
             serializer.UseIndentation = serviceAttr.UseIndentation;
             serializer.Indentation    = serviceAttr.Indentation;
         }
         XmlRpcRequest xmlRpcReq
             = serializer.DeserializeRequest(requestStream, this.GetType());
         XmlRpcResponse xmlRpcResp     = Invoke(xmlRpcReq);
         Stream         responseStream = new MemoryStream();
         serializer.SerializeResponse(responseStream, xmlRpcResp);
         responseStream.Seek(0, SeekOrigin.Begin);
         return(responseStream);
     }
     catch (Exception ex)
     {
         XmlRpcFaultException fex;
         if (ex is XmlRpcException)
         {
             fex = new XmlRpcFaultException(0, ((XmlRpcException)ex).Message);
         }
         else if (ex is XmlRpcFaultException)
         {
             fex = (XmlRpcFaultException)ex;
         }
         else
         {
             fex = new XmlRpcFaultException(0, ex.Message);
         }
         XmlRpcSerializer serializer     = new XmlRpcSerializer();
         Stream           responseStream = new MemoryStream();
         serializer.SerializeFaultResponse(responseStream, fex);
         responseStream.Seek(0, SeekOrigin.Begin);
         return(responseStream);
     }
 }
        public void HandleHttpRequestMy(IHttpRequest httpReq, IHttpResponse httpResp)
        {
            // GET has its own handler because it can be used to return a
            // HTML description of the service
            if (httpReq.HttpMethod == "GET")
            {
                XmlRpcServiceAttribute svcAttr = (XmlRpcServiceAttribute)
                                                 Attribute.GetCustomAttribute(GetType(), typeof(XmlRpcServiceAttribute));
                if (svcAttr != null && svcAttr.AutoDocumentation == false)
                {
                    HandleUnsupportedMethod(httpReq, httpResp);
                }
                else
                {
                    bool autoDocVersion = true;
                    if (svcAttr != null)
                    {
                        autoDocVersion = svcAttr.AutoDocVersion;
                    }
                    HandleGET(httpReq, httpResp, autoDocVersion);
                }
                return;
            }
            if (httpReq.HttpMethod == "OPTIONS")
            {
                HandleOPTIONS(httpReq, httpResp);
                return;
            }
            // calls on service methods are via POST
            if (httpReq.HttpMethod != "POST")
            {
                HandleUnsupportedMethod(httpReq, httpResp);
                return;
            }
            //Context.Response.AppendHeader("Server", "XML-RPC.NET");
            // process the request
            Stream responseStream = Invoke(httpReq.InputStream);

            httpResp.ContentType   = "text/xml";
            httpResp.ContentLength = responseStream.Length;

            Stream respStm = httpResp.OutputStream;

            Util.CopyStream(responseStream, respStm);
            respStm.Flush();
        }
Exemple #5
0
        public string XmlRpcInvoke(Stream requestStream)
        {
            try
            {
                XmlRpcSerializer xmlRpcSerializer = new XmlRpcSerializer();

                XmlRpcServiceAttribute xmlRpcServiceAttribute = (XmlRpcServiceAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(XmlRpcServiceAttribute));
                if (xmlRpcServiceAttribute != null)
                {
                    if (xmlRpcServiceAttribute.XmlEncoding != null)
                    {
                        xmlRpcSerializer.XmlEncoding = Encoding.GetEncoding(xmlRpcServiceAttribute.XmlEncoding);
                    }
                    xmlRpcSerializer.UseIntTag      = xmlRpcServiceAttribute.UseIntTag;
                    xmlRpcSerializer.UseStringTag   = xmlRpcServiceAttribute.UseStringTag;
                    xmlRpcSerializer.UseIndentation = xmlRpcServiceAttribute.UseIndentation;
                    xmlRpcSerializer.Indentation    = xmlRpcServiceAttribute.Indentation;
                }

                var bodyStream = new StreamReader(requestStream);
                bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);

                XmlRpcRequest  request  = xmlRpcSerializer.DeserializeRequest(bodyStream, this.GetType());
                XmlRpcResponse response = this.Invoke(request);
                Stream         stream   = new MemoryStream();
                xmlRpcSerializer.SerializeResponse(stream, response);
                stream.Seek(0L, SeekOrigin.Begin);


                StreamReader reader = new StreamReader(stream);
                return(reader.ReadToEnd());
            }
            catch (Exception ex)
            {
                XmlRpcFaultException faultEx           = (!(ex is XmlRpcException)) ? ((!(ex is XmlRpcFaultException)) ? new XmlRpcFaultException(0, ex.Message) : ((XmlRpcFaultException)ex)) : new XmlRpcFaultException(0, ((XmlRpcException)ex).Message);
                XmlRpcSerializer     xmlRpcSerializer2 = new XmlRpcSerializer();
                Stream stream2 = new MemoryStream();
                xmlRpcSerializer2.SerializeFaultResponse(stream2, faultEx);
                stream2.Seek(0L, SeekOrigin.Begin);

                StreamReader reader2 = new StreamReader(stream2);
                return(reader2.ReadToEnd());
            }
        }
        public static XmlRpcServiceInfo CreateServiceInfo(Type type)
        {
            XmlRpcServiceInfo svcInfo = new XmlRpcServiceInfo();
            // extract service info
            XmlRpcServiceAttribute svcAttr = (XmlRpcServiceAttribute)
                                             Attribute.GetCustomAttribute(type, typeof(XmlRpcServiceAttribute));

            if (svcAttr != null && svcAttr.Description != "")
            {
                svcInfo.doc = svcAttr.Description;
            }
            if (svcAttr != null && svcAttr.Name != "")
            {
                svcInfo.Name = svcAttr.Name;
            }
            else
            {
                svcInfo.Name = type.Name;
            }
            // extract method info
            Hashtable methods = new Hashtable();

            foreach (Type itf in type.GetInterfaces())
            {
                XmlRpcServiceAttribute itfAttr = (XmlRpcServiceAttribute)
                                                 Attribute.GetCustomAttribute(itf, typeof(XmlRpcServiceAttribute));
                if (itfAttr != null)
                {
                    svcInfo.doc = itfAttr.Description;
                }
#if (!COMPACT_FRAMEWORK)
                InterfaceMapping imap = type.GetInterfaceMap(itf);
                foreach (MethodInfo mi in imap.InterfaceMethods)
                {
                    ExtractMethodInfo(methods, mi, itf);
                }
#else
                foreach (MethodInfo mi in itf.GetMethods())
                {
                    ExtractMethodInfo(methods, mi, itf);
                }
#endif
            }

            foreach (MethodInfo mi in type.GetMethods())
            {
                ArrayList mthds = new ArrayList();
                mthds.Add(mi);
                MethodInfo curMi = mi;
                while (true)
                {
                    MethodInfo baseMi = curMi.GetBaseDefinition();
                    if (baseMi.DeclaringType == curMi.DeclaringType)
                    {
                        break;
                    }
                    mthds.Insert(0, baseMi);
                    curMi = baseMi;
                }
                foreach (MethodInfo mthd in mthds)
                {
                    ExtractMethodInfo(methods, mthd, type);
                }
            }
            svcInfo.methodInfos = new XmlRpcMethodInfo[methods.Count];
            methods.Values.CopyTo(svcInfo.methodInfos, 0);
            Array.Sort(svcInfo.methodInfos);
            return(svcInfo);
        }