public void SerializeResponse(Stream stm, XmlRpcResponse response) { Object ret = response.retVal; if (ret is XmlRpcFaultException) { SerializeFaultResponse(stm, (XmlRpcFaultException)ret); return; } XmlWriter xtw = XmlRpcXmlWriter.Create(stm, base.XmlRpcFormatSettings); xtw.WriteStartDocument(); xtw.WriteStartElement("", "methodResponse", ""); xtw.WriteStartElement("", "params", ""); xtw.WriteStartElement("", "param", ""); var mappingActions = new MappingActions(); mappingActions = GetTypeMappings(response.MethodInfo, mappingActions); mappingActions = GetReturnMappingActions(response, mappingActions); try { Serialize(xtw, ret, mappingActions); } catch (XmlRpcUnsupportedTypeException ex) { throw new XmlRpcInvalidReturnType(string.Format( "Return value is of, or contains an instance of, type {0} which " + "cannot be mapped to an XML-RPC type", ex.UnsupportedType)); } WriteFullEndElement(xtw); WriteFullEndElement(xtw); WriteFullEndElement(xtw); xtw.Flush(); }
public void PaoloLiveraniProblem() { try { XmlRpcResponse resp = new XmlRpcResponse(new DataSet()); Stream responseStream = new MemoryStream(); XmlRpcSerializer serializer = new XmlRpcSerializer(); serializer.SerializeResponse(responseStream, resp); } catch(XmlRpcInvalidReturnType ex) { string s = ex.Message; } }
public void SerializeResponseOnMethod() { var deserializer = new XmlRpcResponseSerializer(); var response = new XmlRpcResponse(IntEnum.One, GetType().GetMethod("MappingReturnOnMethod")); var stm = new MemoryStream(); deserializer.SerializeResponse(stm, response); stm.Position = 0; var tr = new StreamReader(stm); var reqstr = tr.ReadToEnd(); reqstr.ShouldBe( @"<?xml version=""1.0""?> <methodResponse> <params> <param> <value> <string>One</string> </value> </param> </params> </methodResponse>"); }
public void SerializeResponseOnType() { var deserializer = new XmlRpcResponseSerializer(); var proxy = XmlRpcProxyGen.Create<TestMethods2>(); MethodInfo mi = proxy.GetType().GetMethod("Bar"); var response = new XmlRpcResponse(IntEnum.Three, mi); var stm = new MemoryStream(); deserializer.SerializeResponse(stm, response); stm.Position = 0; TextReader tr = new StreamReader(stm); string reqstr = tr.ReadToEnd(); Assert.AreEqual( @"<?xml version=""1.0""?> <methodResponse> <params> <param> <value> <string>Three</string> </value> </param> </params> </methodResponse>", reqstr); }
public XmlRpcResponse DeserializeResponse(XmlDocument xdoc, Type returnType) { XmlRpcResponse response = new XmlRpcResponse(); Object retObj = null; XmlNode methodResponseNode = xdoc.SelectSingleNode("./methodResponse"); if (methodResponseNode == null) { throw new XmlRpcInvalidXmlRpcException( "Response XML not valid XML-RPC - missing methodResponse element."); } // check for fault response XmlNode faultNode = methodResponseNode.SelectSingleNode("./fault"); if (faultNode != null) { ParseStack parseStack = new ParseStack("fault response"); // TODO: use global action setting MappingAction mappingAction = MappingAction.Error; XmlRpcFaultException faultEx = ParseFault(faultNode, parseStack, mappingAction); throw faultEx; } XmlNode paramsNode = methodResponseNode.SelectSingleNode("./params"); if (paramsNode == null && returnType != null) { if (returnType == typeof(void)) return null; else throw new XmlRpcInvalidXmlRpcException( "Response XML not valid XML-RPC - missing params element."); } XmlNode paramNode = paramsNode.SelectSingleNode("./param"); if (paramNode == null && returnType != null) { if (returnType == typeof(void)) return null; else throw new XmlRpcInvalidXmlRpcException( "Response XML not valid XML-RPC - missing params element."); } XmlNode valueNode = paramNode.SelectSingleNode("./value"); if (valueNode == null) { throw new XmlRpcInvalidXmlRpcException( "Response XML not valid XML-RPC - missing value element."); } if (returnType == typeof(void)) { retObj = null; } else { ParseStack parseStack = new ParseStack("response"); // TODO: use global action setting MappingAction mappingAction = MappingAction.Error; XmlNode node = valueNode.SelectSingleNode("./*"); if (node == null) node = valueNode.FirstChild; retObj = ParseValue(node, returnType, parseStack, mappingAction); } response.retVal = retObj; return response; }
public void SerializeResponse(Stream stm, XmlRpcResponse response) { Object ret = response.retVal; if (ret is XmlRpcFaultException) { SerializeFaultResponse(stm, (XmlRpcFaultException)ret); return; } XmlTextWriter xtw = new XmlTextWriter(stm, m_encoding); ConfigureXmlFormat(xtw); xtw.WriteStartDocument(); xtw.WriteStartElement("", "methodResponse", ""); xtw.WriteStartElement("", "params", ""); // "void" methods actually return an empty string value if (ret == null) { ret = ""; } xtw.WriteStartElement("", "param", ""); // TODO: use global action setting MappingAction mappingAction = MappingAction.Error; try { Serialize(xtw, ret, mappingAction); } catch (XmlRpcUnsupportedTypeException ex) { throw new XmlRpcInvalidReturnType(string.Format( "Return value is of, or contains an instance of, type {0} which " + "cannot be mapped to an XML-RPC type", ex.UnsupportedType)); } xtw.WriteEndElement(); xtw.WriteEndElement(); xtw.WriteEndElement(); xtw.Flush(); }
MappingActions GetReturnMappingActions(XmlRpcResponse response, MappingActions mappingActions) { var ri = response.MethodInfo != null ? response.MethodInfo.ReturnParameter : null; return GetMappingActions(ri, mappingActions); }
public XmlRpcResponse DeserializeResponse(XmlReader rdr, Type returnType) { try { IEnumerator<Node> iter = new XmlRpcParser().ParseResponse(rdr).GetEnumerator(); iter.MoveNext(); if (iter.Current is FaultNode) { var xmlRpcException = DeserializeFault(iter); throw xmlRpcException; } if (returnType == typeof(void) || !iter.MoveNext()) return new XmlRpcResponse { retVal = null }; var valueNode = iter.Current as ValueNode; object retObj = MapValueNode(iter, returnType, new MappingStack("response"), MappingAction.Error); var response = new XmlRpcResponse { retVal = retObj }; return response; } catch (XmlException ex) { throw new XmlRpcIllFormedXmlException("Response contains invalid XML", ex); } }
void SerializeResponse( IMessage responseMsg, ref ITransportHeaders responseHeaders, ref Stream responseStream) { XmlRpcSerializer serializer = new XmlRpcSerializer(); responseStream = new MemoryStream(); responseHeaders = new TransportHeaders(); ReturnMessage retMsg = (ReturnMessage)responseMsg; if (retMsg.Exception == null) { XmlRpcResponse xmlRpcResp = new XmlRpcResponse(retMsg.ReturnValue); serializer.SerializeResponse(responseStream, xmlRpcResp); } else if (retMsg.Exception is XmlRpcFaultException) { serializer.SerializeFaultResponse(responseStream, (XmlRpcFaultException)retMsg.Exception); } else { serializer.SerializeFaultResponse(responseStream, new XmlRpcFaultException(1, retMsg.Exception.Message)); } responseHeaders["Content-Type"] = "text/xml; charset=\"utf-8\""; }
public XmlRpcResponse Invoke(XmlRpcRequest request) { MethodInfo mi = null; if (request.mi != null) { mi = request.mi; } else { mi = this.GetType().GetMethod(request.method); } // exceptions thrown during an MethodInfo.Invoke call are // package as inner of Object reto; try { reto = mi.Invoke(this, request.args); } catch(Exception ex) { if (ex.InnerException != null) throw ex.InnerException; throw ex; } XmlRpcResponse response = new XmlRpcResponse(reto); return response; }
public XmlRpcResponse Invoke(XmlRpcRequest request) { MethodInfo mi = null; if (request.mi != null) { mi = request.mi; } else { mi = this.GetType().GetMethod(request.method); } // exceptions thrown during an MethodInfo.Invoke call are // package as inner of Object reto; try { reto = mi.Invoke(this, request.args); } catch(Exception ex) { if (ex.InnerException != null) throw ex.InnerException; throw ex; } // methods which have void return type always return integer 0 // because XML-RPC doesn't support no return type (could use nil // but want to maintain backwards compatibility in this area) if (mi != null && mi.ReturnType == typeof(void)) reto = 0; XmlRpcResponse response = new XmlRpcResponse(reto); return response; }
public object Invoke( Object clientObj, MethodInfo mi, params object[] parameters) { #if (!COMPACT_FRAMEWORK && !SILVERLIGHT) _responseHeaders = null; _responseCookies = null; #endif WebRequest webReq = null; object reto = null; try { string useUrl = GetEffectiveUrl(clientObj); webReq = GetWebRequest(new Uri(useUrl)); XmlRpcRequest req = MakeXmlRpcRequest(webReq, mi, parameters, clientObj, _xmlRpcMethod, _id); SetProperties(webReq); #if (!SILVERLIGHT) SetRequestHeaders(_headers, webReq); #endif #if (!COMPACT_FRAMEWORK && !SILVERLIGHT) SetClientCertificates(_clientCertificates, webReq); #endif Stream serStream = null; Stream reqStream = null; bool logging = (RequestEvent != null); if (!logging) { serStream = reqStream = webReq.GetRequestStream(); } else { serStream = new MemoryStream(2000); } try { XmlRpcSerializer serializer = new XmlRpcSerializer(); if (_xmlEncoding != null) { serializer.XmlEncoding = _xmlEncoding; } serializer.UseIndentation = _useIndentation; serializer.Indentation = _indentation; serializer.NonStandard = _nonStandard; serializer.UseStringTag = _useStringTag; serializer.UseIntTag = _useIntTag; serializer.UseEmptyParamsTag = _useEmptyParamsTag; serializer.SerializeRequest(serStream, req); if (logging) { reqStream = webReq.GetRequestStream(); serStream.Position = 0; Util.CopyStream(serStream, reqStream); reqStream.Flush(); serStream.Position = 0; OnRequest(new XmlRpcRequestEventArgs(req.proxyId, req.number, serStream)); } } finally { if (reqStream != null) { reqStream.Close(); } } HttpWebResponse webResp = GetWebResponse(webReq) as HttpWebResponse; #if (!COMPACT_FRAMEWORK && !SILVERLIGHT) _responseCookies = webResp.Cookies; #endif #if (!COMPACT_FRAMEWORK && !SILVERLIGHT) _responseHeaders = webResp.Headers; #endif Stream respStm = null; Stream deserStream; logging = (ResponseEvent != null); try { respStm = webResp.GetResponseStream(); if (!logging) { deserStream = respStm; } else { deserStream = new MemoryStream(2000); Util.CopyStream(respStm, deserStream); deserStream.Flush(); deserStream.Position = 0; } #if (!COMPACT_FRAMEWORK && !SILVERLIGHT) deserStream = MaybeDecompressStream((HttpWebResponse)webResp, deserStream); #endif try { XmlRpcResponse resp = ReadResponse(req, webResp, deserStream, null); reto = resp.retVal; } finally { if (logging) { deserStream.Position = 0; OnResponse(new XmlRpcResponseEventArgs(req.proxyId, req.number, deserStream)); } } } finally { if (respStm != null) { respStm.Close(); } } } finally { if (webReq != null) { webReq = null; } } return(reto); }