Example #1
0
        private static void WriteFaultResponse(XmlWriter writer, IXmlRpcFault fault, IXmlRpcStreamRequestConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            writer.WriteStartElement(XmlRpcSpec.FAULT_TAG);

            ArrayList nestvedObjs = new ArrayList();

            if (config != null && config.EnabledForExceptions)
            {
                Hashtable   ht  = new Hashtable(3);
                XmlRpcFault xpf = fault as XmlRpcFault;
                ht.Add(XmlRpcSpec.FAULT_CODE_TAG, fault.FaultCode);
                ht.Add(XmlRpcSpec.FAULT_STRING_TAG, fault.FaultString);
                ht.Add("faultCause", fault is XmlRpcFault ? ((XmlRpcFault)fault).Exception : null);
                RecursiveTypeSerializer.WriteValue(writer, ht, config, typeSerializerFactory, nestvedObjs);
            }
            else
            {
                RecursiveTypeSerializer.WriteValue(writer, fault, config, typeSerializerFactory, nestvedObjs);
            }

            writer.WriteEndElement();
        }
Example #2
0
        private static IXmlRpcResponse ReadFaultResponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IDictionary dict = null;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    dict = (IDictionary)RecursiveTypeSerializer.ReadValue(reader, config, typeSerializerFactory);
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.FAULT_TAG.Equals(reader.LocalName));

            if (dict != null)
            {
                if (!dict.Contains(XmlRpcSpec.FAULT_CODE_TAG))
                {
                    throw new XmlRpcException("Invalid XML-RPC response: missing fault code");
                }
                if (!dict.Contains(XmlRpcSpec.FAULT_STRING_TAG))
                {
                    throw new XmlRpcException("Invalid XML-RPC response: missing fault string");
                }

                try
                {
                    XmlRpcFault    fault    = new XmlRpcFault((Int32)dict[XmlRpcSpec.FAULT_CODE_TAG], (String)dict[XmlRpcSpec.FAULT_STRING_TAG]);
                    XmlRpcResponse response = new XmlRpcResponse(null);
                    response.Fault = fault;
                    return(response);
                }
                catch (Exception ex)
                {
                    throw new XmlRpcException("Invalid XML-RPC response: " + ex.Message);
                }
            }

            throw new XmlRpcException("Invalid XML-RPC response.");
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of <see cref="XmlRpc"/> class.
 /// </summary>
 /// <param name="fault"></param>
 public XmlRpcFaultResponse(XmlRpcFault fault)
 {
     this.fault = fault;
 }
        /// <summary>
        /// Call multiple methods without multiple round-trip times.
        /// </summary>
        /// <param name="multicall">MultiCall object containing the calls to perform.</param>
        /// <returns>An array of results for each call.</returns>
        public async Task <object[]> MultiCallAsync(MultiCall multicall)
        {
            List <XmlRpcBaseType> calls = new();

            // build the call
            foreach (var call in multicall.MethodCalls)
            {
                string methodName = call.MethodName;
                if (methodName.EndsWith("Async"))
                {
                    methodName = methodName.Substring(0, methodName.Length - 5);
                }

                XmlRpcBaseType[] args       = MethodArgs(call.Arguments);
                XmlRpcStruct     callStruct = new(new Struct()
                {
                    { "methodName", new XmlRpcString(methodName) },
                    { "params", new XmlRpcArray(args) }
                });

                calls.Add(callStruct);
            }

            // run the call
            XmlRpcArray multicallArgs = new(calls.ToArray());
            var         msg           = await CallAsync("system.multicall", multicallArgs);

            if (msg.IsFault)
            {
                logger.Error("Multicall failed with reason: {message}", (XmlRpcFault)msg.ResponseData);
                throw new XmlRpcFaultException((XmlRpcFault)msg.ResponseData);
            }

            // convert response to native values, should always be array if no error
            XmlRpcArray results = (XmlRpcArray)msg.ResponseData;

            object[] converted = new object[results.Values.Length];

            for (int i = 0; i < converted.Length; i++)
            {
                if (results.Values[i] is XmlRpcStruct)
                {
                    // if struct we have a fault instead
                    XmlRpcStruct faultStruct = (XmlRpcStruct)results.Values[i];

                    converted[i] = new XmlRpcFault(
                        ((XmlRpcInteger)faultStruct.Fields["faultCode"]).Value,
                        ((XmlRpcString)faultStruct.Fields["faultString"]).Value
                        );
                }
                else
                {
                    // else normal resutl
                    XmlRpcArray resultArr = (XmlRpcArray)results.Values[i];

                    if (resultArr.Values.Length == 0)
                    {
                        converted[i] = null;
                    }
                    else
                    {
                        converted[i] = XmlRpcTypes.ToNativeValue <object>(resultArr.Values[0]);
                    }
                }
            }

            return(converted);
        }
 /// <summary>
 /// Creates a new instance of the exception using the
 /// provided fault info.
 /// </summary>
 /// <param name="fault">Object containing info about the fault.</param>
 public XmlRpcFaultException(XmlRpcFault fault)
 {
     Fault = fault;
 }
        private static IXmlRpcResponse ReadFaultResponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IDictionary dict = null;
            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    dict = (IDictionary)RecursiveTypeSerializer.ReadValue(reader, config, typeSerializerFactory);
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.FAULT_TAG.Equals(reader.LocalName));

            if (dict != null)
            {
                if (!dict.Contains(XmlRpcSpec.FAULT_CODE_TAG))
                    throw new XmlRpcException("Invalid XML-RPC response: missing fault code");
                if (!dict.Contains(XmlRpcSpec.FAULT_STRING_TAG))
                    throw new XmlRpcException("Invalid XML-RPC response: missing fault string");

                try
                {
                    XmlRpcFault fault = new XmlRpcFault((Int32)dict[XmlRpcSpec.FAULT_CODE_TAG], (String)dict[XmlRpcSpec.FAULT_STRING_TAG]);
                    XmlRpcResponse response = new XmlRpcResponse(null);
                    response.Fault = fault;
                    return response;
                }
                catch (Exception ex)
                {
                    throw new XmlRpcException("Invalid XML-RPC response: " + ex.Message);
                }
            }

            throw new XmlRpcException("Invalid XML-RPC response.");
        }