Пример #1
0
        public void SerializeFaultResponse(
            Stream stm,
            XmlRpcFaultException faultEx)
        {
            FaultStruct fs;

            fs.faultCode   = faultEx.FaultCode;
            fs.faultString = faultEx.FaultString;


            XmlWriter xtw = XmlRpcXmlWriter.Create(stm, XmlRpcFormatSettings);

            try
            {
                xtw.WriteStartDocument();
            }
            catch
            {
                const string startDoc = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                byte[]       data     = XmlRpcFormatSettings.XmlEncoding.GetBytes(startDoc);
                stm.Write(data, 0, data.Length);
            }
            xtw.WriteStartElement("", "methodResponse", "");
            xtw.WriteStartElement("", "fault", "");
            Serialize(xtw, fs, new MappingActions {
                NullMappingAction = NullMappingAction.Error
            });
            WriteFullEndElement(xtw);
            WriteFullEndElement(xtw);
            xtw.Flush();
        }
Пример #2
0
        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();
        }
Пример #3
0
        public static XmlReader Serialize(string testName, object obj, Encoding encoding, MappingActions actions)
        {
            Stream stm = new MemoryStream();

            var xtw = XmlRpcXmlWriter.Create(stm, new XmlRpcFormatSettings());

            xtw.WriteStartDocument();
            var ser = new XmlRpcSerializer();

            ser.Serialize(xtw, obj, actions);
            xtw.Flush();
            stm.Position = 0;

            return(XmlRpcXmlReader.Create(stm));
        }
Пример #4
0
        public static string SerializeValue(object value, bool indent)
        {
            var memStm = new MemoryStream();
            var writer = XmlRpcXmlWriter.Create(
                memStm,
                new XmlRpcFormatSettings {
                OmitXmlDeclaration = true, UseIndentation = indent
            });
            var serializer = new XmlRpcSerializer();

            serializer.Serialize(writer, value, new MappingActions {
                NullMappingAction = NullMappingAction.Error
            });
            writer.Flush();
            memStm.Position = 0;

            return(new StreamReader(memStm).ReadToEnd());
        }
Пример #5
0
        public void SerializeFaultResponse(Stream stm, XmlRpcFaultException faultEx)
        {
            FaultStruct fs;

            fs.FaultCode   = faultEx.FaultCode;
            fs.FaultString = faultEx.FaultString;
            var xtw = XmlRpcXmlWriter.Create(stm, XmlRpcFormatSettings);

            xtw.WriteStartDocument();
            xtw.WriteStartElement(string.Empty, "methodResponse", string.Empty);
            xtw.WriteStartElement(string.Empty, "fault", string.Empty);
            Serialize(xtw, fs, new MappingActions {
                NullMappingAction = NullMappingAction.Error
            });
            WriteFullEndElement(xtw);
            WriteFullEndElement(xtw);
            xtw.Flush();
        }
Пример #6
0
        public void SerializeRequest(Stream stm, XmlRpcRequest request)
        {
            var xtw = XmlRpcXmlWriter.Create(stm, XmlRpcFormatSettings);

            xtw.WriteStartDocument();
            xtw.WriteStartElement(string.Empty, "methodCall", string.Empty);
            {
                var mappingActions = new MappingActions();
                mappingActions = GetTypeMappings(request.Mi, mappingActions);
                mappingActions = GetMappingActions(request.Mi, mappingActions);
                WriteFullElementString(xtw, "methodName", request.Method);
                if (request.Args.Length > 0 || UseEmptyParamsTag)
                {
                    xtw.WriteStartElement("params");
                    try
                    {
                        if (!IsStructParamsMethod(request.Mi))
                        {
                            SerializeParams(xtw, request, mappingActions);
                        }
                        else
                        {
                            SerializeStructParams(xtw, request, mappingActions);
                        }
                    }
                    catch (XmlRpcUnsupportedTypeException ex)
                    {
                        throw new XmlRpcUnsupportedTypeException(
                                  ex.UnsupportedType,
                                  string.Format(
                                      "A parameter is of, or contains an instance of, type {0} which cannot be mapped to an XML-RPC type",
                                      ex.UnsupportedType));
                    }
                    WriteFullEndElement(xtw);
                }
            }
            WriteFullEndElement(xtw);
            xtw.Flush();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="stm"></param>
        /// <param name="request"></param>
        public void SerializeRequest(Stream stm, XmlRpcRequest request)
        {
            XmlWriter xtw = XmlRpcXmlWriter.Create(stm, base.XmlRpcFormatSettings);

            xtw.WriteStartDocument();
            xtw.WriteStartElement("", "methodCall", "");
            {
                // TODO: use global action setting
                NullMappingAction mappingAction = NullMappingAction.Error;
                WriteFullElementString(xtw, "methodName", request.method);
                if (request.args.Length > 0 || UseEmptyParamsTag)
                {
                    xtw.WriteStartElement("params");
                    try
                    {
                        if (!IsStructParamsMethod(request.mi))
                        {
                            SerializeParams(xtw, request, mappingAction);
                        }
                        else
                        {
                            SerializeStructParams(xtw, request, mappingAction);
                        }
                    }
                    catch (XmlRpcUnsupportedTypeException ex)
                    {
                        throw new XmlRpcUnsupportedTypeException(ex.UnsupportedType,
                                                                 String.Format("A parameter is of, or contains an instance of, "
                                                                               + "type {0} which cannot be mapped to an XML-RPC type",
                                                                               ex.UnsupportedType));
                    }
                    WriteFullEndElement(xtw);
                }
            }
            WriteFullEndElement(xtw);
            xtw.Flush();
        }
Пример #8
0
        public void SerializeFaultResponse(
            Stream stm,
            XmlRpcFaultException faultEx)
        {
            FaultStruct fs = new FaultStruct();

            fs.faultCode   = faultEx.FaultCode;
            fs.faultString = faultEx.FaultString;
            XmlWriter xtw = XmlRpcXmlWriter.Create(stm, XmlRpcFormatSettings);

            xtw.WriteStartDocument(false);
            xtw.WriteStartElement("", "methodResponse", "");
            xtw.WriteStartElement("", "fault", "");
            Serialize(xtw, fs, new MappingActions {
                NullMappingAction = NullMappingAction.Error
            });
            WriteFullEndElement(xtw);
            WriteFullEndElement(xtw);
            xtw.Flush();


            #region ×¢ÊÍ

            /*
             * FaultStruct fs=new FaultStruct();
             * fs.faultCode = faultEx.FaultCode;
             * fs.faultString = faultEx.FaultString;
             *
             * XmlWriter xw = XmlWriter.Create(stm);
             * xw.WriteStartDocument();
             *
             * xw.WriteStartElement("", "methodResponse", "");
             * xw.WriteStartElement("", "fault", "");
             * xw.WriteStartElement("value");
             *
             * xw.WriteStartElement("struct");
             *
             * //Member ״̬¿ªÊ¼
             * xw.WriteStartElement("member");
             * xw.WriteStartElement("name");
             * xw.WriteValue("faultCode");
             * xw.WriteEndElement();
             *
             * xw.WriteStartElement("value");
             * xw.WriteStartElement("int");
             * xw.WriteValue(faultEx.FaultCode);
             * xw.WriteEndElement();
             * xw.WriteEndElement();
             *
             * //Member½áÊø
             * xw.WriteEndElement();
             *
             * //Member×Ö·û¿ªÊ¼
             * xw.WriteStartElement("member");
             * xw.WriteStartElement("name");
             * xw.WriteValue("faultString");
             * xw.WriteEndElement();
             *
             * xw.WriteStartElement("value");
             * xw.WriteStartElement("string");
             * xw.WriteValue(faultEx.FaultString);
             * xw.WriteEndElement();
             * xw.WriteEndElement();
             *
             * //Member½áÊø
             * xw.WriteEndElement();
             *
             * //struct½áÊø
             * xw.WriteEndElement();
             *
             * //value½áÊø
             * xw.WriteEndElement();
             *
             *
             * //fault½áÊø
             * xw.WriteEndElement();
             *
             * //methodResponse½áÊø
             * xw.WriteEndElement();
             *
             *
             * // xw.WriteAttributeString("faultCode", fs.faultCode.ToString());
             * //xw.WriteAttributeString("faultString", fs.faultString);
             * // xw.WriteEndElement();
             * // xw.WriteEndElement();
             *
             * xw.WriteEndDocument();
             * xw.Flush();
             *
             * /*
             * <?xml version="1.0" encoding="UTF-8"?>
             * <methodResponse>
             * <fault>
             * <value>
             *  <struct>
             *      <member>
             *          <name>faultCode</name>
             *          <value><int>´íÎóºÅ</int></value>
             *      </member>
             *      <member>
             *          <name>faultString</name>
             *          <value><string>ʧ°ÜÔ­Òò</string></value>
             *      </member>
             *  </struct>
             * </value>
             * </fault>
             * </methodResponse>
             */
            #endregion
        }