Exemplo n.º 1
0
        private bool IsValidUser(string username, string password, out Exception exception)
        {
            if (!authenticator.AuthenticateAndLogin(username, password))
            {
                exception = new XmlRpcFaultException(Convert.ToInt32(HttpStatusCode.Unauthorized), "Unauthorized user!");
                return(false);
            }

            exception = null;
            return(true);
        }
Exemplo n.º 2
0
 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);
     }
 }
Exemplo n.º 3
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());
            }
        }
Exemplo n.º 4
0
        public void SerializeFaultResponse(Stream stm, XmlRpcFaultException faultEx)
        {
            var fs = new FaultStruct
            {
                faultCode   = faultEx.FaultCode,
                faultString = faultEx.FaultString
            };

            var xtw = new XmlTextWriter(stm, Configuration.XmlEncoding);

            Configuration.ConfigureXmlFormat(xtw);

            xtw.WriteStartDocument();
            xtw.WriteStartElement("", "methodResponse", "");
            xtw.WriteStartElement("", "fault", "");

            Serialize(xtw, fs);

            xtw.WriteEndElement();
            xtw.WriteEndElement();
            xtw.Flush();
        }
Exemplo n.º 5
0
        protected internal static OdooException GetException(XmlRpcFaultException e)
        {
            string message = string.Empty;

            string[] messages = e.Message.Split('\n');
            if (messages.Length >= 3)
            {
                try
                {
                    message = string.Join("\n", messages.Skip(2));
                }
                catch (Exception)
                {
                    message = e.Message;
                }
            }
            else
            {
                message = e.Message;
            }
            return(new OdooException(message, e));
        }
Exemplo n.º 6
0
        Stream CreateExceptionResponse(Exception exception)
        {
            XmlRpcFaultException fex;

            if (exception is XmlRpcException)
            {
                fex = new XmlRpcFaultException(0, ((XmlRpcException)exception).Message);
            }
            else if (exception is XmlRpcFaultException)
            {
                fex = (XmlRpcFaultException)exception;
            }
            else
            {
                fex = new XmlRpcFaultException(0, exception.Message);
            }

            var serializer     = new XmlRpcSerializer();
            var responseStream = new MemoryStream();

            serializer.SerializeFaultResponse(responseStream, fex);
            responseStream.Seek(0, SeekOrigin.Begin);
            return(responseStream);
        }
Exemplo n.º 7
0
        private static XmlRpcFaultException NewXmlRpcFaultWithMessage(string msg, Exception ex)
        {
            XmlRpcFaultException xmlEx = new XmlRpcFaultException(0, String.Format("{0}.  Message: {1}.", msg, ex.Message));

            return(xmlEx);
        }
 public InfusionSoftException(XmlRpcFaultException e) : base(e.Message, e)
 {
 }
Exemplo n.º 9
0
        /**
         * This method converts the objects compatible in AdrConvertor to
         * objects that compatible in XMLRPC.NET library.
         * byte[], MemBlock -> byte[] -> string(base64)
         * other array -> array ( elements converted)
         * IDictionary -> XmlRpcStruct (key -> string)
         * IList -> array
         * float -> double
         * long, ulong -> string
         * AdrException -> XmlRpcFaultException (w/ errorCode added)
         * Exception -> Exception (converted by XmlRpcFaultException by xmlrpc.net)
         * ISender -> string (using ToString())
         * short, ushort, uint, byte, sbyte -> int
         * null -> string.Empty
         */
        public static object Adr2XmlRpc(object o, out bool modified)
        {
            object retval;

            if (o == null)
            {
                /*
                 * If null is returned when the method is not recursively called by itself,
                 * it is OK because XmlRpc.Net will convert it to string.Empty.
                 * If not, the null element's outer data structure like Array and IDictionary,
                 * which themselves allow null elements, might not be handled correctly:
                 * XmlRpc.Net can't serialize IDictionary and Array with null elements.
                 * So we return s.Empty directly from here
                 */
                retval   = string.Empty;
                modified = true;
                return(retval);
            }

            System.Type t = o.GetType();
            // byte arrays are converted to base64 strings in XmlRpc
            // so we treat it as a special case of array
            if (t == typeof(byte[]))
            {
                retval   = o;
                modified = false;
            }
            // convert each element
            else if (t.IsArray)
            {
                ArrayList list = new ArrayList((ICollection)o);
                bool      m;
                modified = false;
                for (int i = 0; i < list.Count; i++)
                {
                    list[i] = Adr2XmlRpc(list[i], out m);
                    if (m == true)
                    {
                        modified = true;
                    }
                }
                retval = list.ToArray();
            }
            //IDictionary -> XmlRpcStruct (string key)
            else if (o is IDictionary)
            {
                modified = true;
                XmlRpcStruct xrs  = new XmlRpcStruct();
                IDictionary  dict = o as IDictionary;

                IDictionaryEnumerator my_en = dict.GetEnumerator();
                while (my_en.MoveNext())
                {
                    object key = Adr2XmlRpc(my_en.Key);

                    /*
                     * XmlRpcStruct requires keys to be strings, we just use ToString() to generate
                     * strings.
                     */
                    string str_key = key.ToString();
                    object val     = Adr2XmlRpc(my_en.Value);
                    xrs.Add(str_key, val);
                }
                retval = xrs;
            }
            //XmlRpcSerializer doesn't recognize lists
            //IList -> Array
            else if (o is IList)
            {
                modified = true; //list -> array
                ArrayList list = new ArrayList((ICollection)o);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i] = Adr2XmlRpc(list[i]);
                }
                retval = list.ToArray();
            }
            //Memblock -> byte[]
            else if (o is MemBlock)
            {
                modified = true;
                MemBlock mb = (MemBlock)o;
                byte[]   b  = new byte[mb.Length];
                mb.CopyTo(b, 0);
                retval = b;
            }
            //float -> double
            else if (t == typeof(Single))
            {
                retval   = Convert.ToDouble(o);
                modified = true;
            }
            else if (t == typeof(short) || t == typeof(ushort) || t == typeof(uint) || t == typeof(byte) || t == typeof(sbyte))
            {
                retval   = Convert.ToInt32(o);
                modified = true;
            }
            //long-> string
            else if (t == typeof(long) || t == typeof(ulong))
            {
                retval   = Convert.ToString(o);
                modified = true;
            }
            //AdrException is different from others that it has a code that can
            //be assigned to XmlRpcFaultException
            else if (o is AdrException)
            {
                AdrException  e  = (AdrException)o;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(string.Format("{0}", e.ToString()));
                retval   = new XmlRpcFaultException(e.Code, sb.ToString());
                modified = true;
            }
            //Still exceptions, XmlRpc.net converts it to XmlRpcFaultException
            else if (o is Exception)
            {
                Exception     e  = (Exception)o;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(string.Format("{0}", e.ToString()));
                retval   = new Exception(sb.ToString());
                modified = true;
            }
            else if (o is ISender)
            {
                ISender s = (ISender)o;
                retval   = s.ToString();
                modified = true;
            }
            else
            {
                retval   = o;
                modified = false;
            }
            return(retval);
        }