1 | /* |
---|
2 | XML-RPC.NET library |
---|
3 | Copyright (c) 2001-2006, Charles Cook <charlescook@cookcomputing.com> |
---|
4 | |
---|
5 | Permission is hereby granted, free of charge, to any person |
---|
6 | obtaining a copy of this software and associated documentation |
---|
7 | files (the "Software"), to deal in the Software without restriction, |
---|
8 | including without limitation the rights to use, copy, modify, merge, |
---|
9 | publish, distribute, sublicense, and/or sell copies of the Software, |
---|
10 | and to permit persons to whom the Software is furnished to do so, |
---|
11 | subject to the following conditions: |
---|
12 | |
---|
13 | The above copyright notice and this permission notice shall be |
---|
14 | included in all copies or substantial portions of the Software. |
---|
15 | |
---|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
---|
18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
23 | DEALINGS IN THE SOFTWARE. |
---|
24 | */ |
---|
25 | |
---|
26 | namespace CookComputing.XmlRpc |
---|
27 | { |
---|
28 | using System; |
---|
29 | using System.Collections; |
---|
30 | using System.IO; |
---|
31 | using System.Reflection; |
---|
32 | using System.Text; |
---|
33 | using System.Text.RegularExpressions; |
---|
34 | |
---|
35 | public class XmlRpcServerProtocol : SystemMethodsBase |
---|
36 | { |
---|
37 | public Stream Invoke(Stream requestStream) |
---|
38 | { |
---|
39 | try |
---|
40 | { |
---|
41 | XmlRpcSerializer serializer = new XmlRpcSerializer(); |
---|
42 | Type type = this.GetType(); |
---|
43 | XmlRpcServiceAttribute serviceAttr = (XmlRpcServiceAttribute) |
---|
44 | Attribute.GetCustomAttribute(this.GetType(), |
---|
45 | typeof(XmlRpcServiceAttribute)); |
---|
46 | if (serviceAttr != null) |
---|
47 | { |
---|
48 | if (serviceAttr.XmlEncoding != null) |
---|
49 | serializer.XmlEncoding = Encoding.GetEncoding(serviceAttr.XmlEncoding); |
---|
50 | serializer.UseIntTag = serviceAttr.UseIntTag; |
---|
51 | serializer.UseStringTag = serviceAttr.UseStringTag; |
---|
52 | serializer.UseIndentation = serviceAttr.UseIndentation; |
---|
53 | serializer.Indentation = serviceAttr.Indentation; |
---|
54 | } |
---|
55 | XmlRpcRequest xmlRpcReq |
---|
56 | = serializer.DeserializeRequest(requestStream, this.GetType()); |
---|
57 | XmlRpcResponse xmlRpcResp = Invoke(xmlRpcReq); |
---|
58 | Stream responseStream = new MemoryStream(); |
---|
59 | serializer.SerializeResponse(responseStream, xmlRpcResp); |
---|
60 | responseStream.Seek(0, SeekOrigin.Begin); |
---|
61 | return responseStream; |
---|
62 | } |
---|
63 | catch (Exception ex) |
---|
64 | { |
---|
65 | XmlRpcFaultException fex; |
---|
66 | if (ex is XmlRpcException) |
---|
67 | fex = new XmlRpcFaultException(0, ((XmlRpcException)ex).Message); |
---|
68 | else if (ex is XmlRpcFaultException) |
---|
69 | fex = (XmlRpcFaultException)ex; |
---|
70 | else |
---|
71 | fex = new XmlRpcFaultException(0, ex.Message); |
---|
72 | XmlRpcSerializer serializer = new XmlRpcSerializer(); |
---|
73 | Stream responseStream = new MemoryStream(); |
---|
74 | serializer.SerializeFaultResponse(responseStream, fex); |
---|
75 | responseStream.Seek(0, SeekOrigin.Begin); |
---|
76 | return responseStream; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | public XmlRpcResponse Invoke(XmlRpcRequest request) |
---|
81 | { |
---|
82 | MethodInfo mi = null; |
---|
83 | if (request.mi != null) |
---|
84 | { |
---|
85 | mi = request.mi; |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | mi = this.GetType().GetMethod(request.method); |
---|
90 | } |
---|
91 | // exceptions thrown during an MethodInfo.Invoke call are |
---|
92 | // package as inner of |
---|
93 | Object reto; |
---|
94 | try |
---|
95 | { |
---|
96 | reto = mi.Invoke(this, request.args); |
---|
97 | } |
---|
98 | catch(Exception ex) |
---|
99 | { |
---|
100 | if (ex.InnerException != null) |
---|
101 | throw ex.InnerException; |
---|
102 | throw ex; |
---|
103 | } |
---|
104 | XmlRpcResponse response = new XmlRpcResponse(reto); |
---|
105 | return response; |
---|
106 | } |
---|
107 | |
---|
108 | bool IsVisibleXmlRpcMethod(MethodInfo mi) |
---|
109 | { |
---|
110 | bool ret = false; |
---|
111 | Attribute attr = Attribute.GetCustomAttribute(mi, |
---|
112 | typeof(XmlRpcMethodAttribute)); |
---|
113 | if (attr != null) |
---|
114 | { |
---|
115 | XmlRpcMethodAttribute mattr = (XmlRpcMethodAttribute)attr; |
---|
116 | ret = !mattr.Hidden; |
---|
117 | } |
---|
118 | return ret; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|