/// <summary> /// Deserializes an XML-RPC response from a <see cref="System.Xml.XmlReader"/>. /// </summary> /// <param name="reader">the <see cref="System.Xml.XmlReader"/> to read</param> /// <param name="config">the context configuration</param> /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param> /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcResponse"/> read from the reader</returns> /// <exception cref="System.Xml.XmlException">failed parsing the response XML</exception> public IXmlRpcResponse ReadResponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory) { IXmlRpcResponse response = null; do { reader.Read(); if (reader.NodeType == XmlNodeType.Element) { if (!String.IsNullOrEmpty(reader.NamespaceURI) || !XmlRpcSpec.METHOD_RESPONSE_TAG.Equals(reader.LocalName)) { throw new XmlException("Expected root element methodResponse, got " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI)); } RecursiveTypeSerializer.ReadToElement(reader); if (String.IsNullOrEmpty(reader.NamespaceURI) && XmlRpcSpec.FAULT_TAG.Equals(reader.LocalName)) { response = ReadFaultResponse(reader, config, typeSerializerFactory); } else if (String.IsNullOrEmpty(reader.NamespaceURI) && XmlRpcSpec.PARAMS_TAG.Equals(reader.LocalName)) { response = ReadParamReponse(reader, config, typeSerializerFactory); } } } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.METHOD_RESPONSE_TAG.Equals(reader.LocalName))); if (response == null) { throw new XmlRpcException("Invalid XML-RPC response."); } return(response); }
private static IXmlRpcResponse ReadParamReponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory) { IList list = RecursiveTypeSerializer.ReadParams(reader, config, typeSerializerFactory); XmlRpcResponse response = new XmlRpcResponse(list.Count > 0 ? list[0] : null); return(response); }
/// <summary> /// Serializes an XML-RPC request to a <see cref="System.Xml.XmlWriter"/>. /// </summary> /// <param name="writer">the <see cref="System.Xml.XmlWriter"/> to write</param> /// <param name="request">the <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> to serialize</param> /// <param name="config">the context configuration</param> /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param> /// <exception cref="System.Xml.XmlException">failed writing the request XML</exception> public void WriteRequest(XmlWriter writer, IXmlRpcRequest request, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory) { writer.WriteStartDocument(); writer.WriteStartElement(XmlRpcSpec.METHOD_CALL_TAG); writer.WriteElementString(XmlRpcSpec.METHOD_NAME_TAG, request.MethodName); RecursiveTypeSerializer.WriteParams(writer, config, typeSerializerFactory, request.Parameters); writer.WriteEndElement(); writer.WriteEndDocument(); }
/// <summary> /// Serializes an XML-RPC response to a <see cref="System.Xml.XmlWriter"/>. /// </summary> /// <param name="writer">the <see cref="System.Xml.XmlWriter"/> to write</param> /// <param name="response">the <see cref="LX.EasyWeb.XmlRpc.IXmlRpcResponse"/> to serialize</param> /// <param name="config">the context configuration</param> /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param> /// <exception cref="System.Xml.XmlException">failed writing the response XML</exception> public void WriteResponse(XmlWriter writer, IXmlRpcResponse response, IXmlRpcStreamRequestConfig config, ITypeSerializerFactory typeSerializerFactory) { writer.WriteStartDocument(); writer.WriteStartElement(XmlRpcSpec.METHOD_RESPONSE_TAG); if (response.Fault == null) { RecursiveTypeSerializer.WriteParams(writer, config, typeSerializerFactory, response.Result); } else { WriteFaultResponse(writer, response.Fault, config, typeSerializerFactory); } writer.WriteEndElement(); writer.WriteEndDocument(); }
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(); }
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."); }
/// <summary> /// Deserializes an XML-RPC request from a <see cref="System.Xml.XmlReader"/>. /// </summary> /// <param name="reader">the <see cref="System.Xml.XmlReader"/> to read</param> /// <param name="config">the context configuration</param> /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param> /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> read from the reader</returns> /// <exception cref="System.Xml.XmlException">failed parsing the request XML</exception> public IXmlRpcRequest ReadRequest(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory) { String methodName = null; IList args = null; do { reader.Read(); if (reader.NodeType == XmlNodeType.Element) { if (!String.IsNullOrEmpty(reader.NamespaceURI) || !XmlRpcSpec.METHOD_CALL_TAG.Equals(reader.LocalName)) { throw new XmlException("Expected root element methodCall, got " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI)); } RecursiveTypeSerializer.ReadToElement(reader); methodName = ReadMethodName(reader); RecursiveTypeSerializer.ReadToElement(reader); args = RecursiveTypeSerializer.ReadParams(reader, config, typeSerializerFactory); } } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.METHOD_CALL_TAG.Equals(reader.LocalName))); return(new XmlRpcRequest(methodName, args == null ? null : ToArray(args))); }
private static String ReadMethodName(XmlReader reader) { RecursiveTypeSerializer.CheckTag(reader, XmlRpcSpec.METHOD_NAME_TAG); return(reader.ReadElementString()); }