XmlRpcResponse.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. namespace Nwc.XmlRpc
  29. {
  30. using System;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Xml;
  34. /// <summary>Class designed to represent an XML-RPC response.</summary>
  35. public class XmlRpcResponse
  36. {
  37. private Object _value;
  38. /// <summary><c>bool</c> indicating if this response represents a fault.</summary>
  39. public bool IsFault;
  40. /// <summary>Basic constructor</summary>
  41. public XmlRpcResponse()
  42. {
  43. Value = null;
  44. IsFault = false;
  45. }
  46. /// <summary>Constructor for a fault.</summary>
  47. /// <param name="code"><c>int</c> the numeric faultCode value.</param>
  48. /// <param name="message"><c>String</c> the faultString value.</param>
  49. public XmlRpcResponse(int code, String message)
  50. : this()
  51. {
  52. SetFault(code, message);
  53. }
  54. /// <summary>The data value of the response, may be fault data.</summary>
  55. public Object Value
  56. {
  57. get { return _value; }
  58. set
  59. {
  60. IsFault = false;
  61. _value = value;
  62. }
  63. }
  64. /// <summary>The faultCode if this is a fault.</summary>
  65. public int FaultCode
  66. {
  67. get
  68. {
  69. if (!IsFault)
  70. return 0;
  71. else
  72. return (int)((Hashtable)_value)[XmlRpcXmlTokens.FAULT_CODE];
  73. }
  74. }
  75. /// <summary>The faultString if this is a fault.</summary>
  76. public String FaultString
  77. {
  78. get
  79. {
  80. if (!IsFault)
  81. return "";
  82. else
  83. return (String)((Hashtable)_value)[XmlRpcXmlTokens.FAULT_STRING];
  84. }
  85. }
  86. /// <summary>Set this response to be a fault.</summary>
  87. /// <param name="code"><c>int</c> the numeric faultCode value.</param>
  88. /// <param name="message"><c>String</c> the faultString value.</param>
  89. public void SetFault(int code, String message)
  90. {
  91. Hashtable fault = new Hashtable();
  92. fault.Add("faultCode", code);
  93. fault.Add("faultString", message);
  94. Value = fault;
  95. IsFault = true;
  96. }
  97. /// <summary>Form a useful string representation of the object, in this case the XML response.</summary>
  98. /// <returns><c>String</c> The XML serialized XML-RPC response.</returns>
  99. override public String ToString()
  100. {
  101. return XmlRpcResponseSerializer.Singleton.Serialize(this);
  102. }
  103. }
  104. }