aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Common-Source/XmlRpcCS/XmlRpcSystemObject.cs
blob: 5f799514c67e5a818f2aa25609527bdf220003d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
namespace Nwc.XmlRpc
{
    using System;
    using System.Collections;
    using System.Reflection;

    /// <summary> XML-RPC System object implementation of extended specifications.</summary>
    [XmlRpcExposed]
    public class XmlRpcSystemObject
    {
        private XmlRpcServer _server;
        static private IDictionary _methodHelp = new Hashtable();

        /// <summary>Static <c>IDictionary</c> to hold mappings of method name to associated documentation String</summary>
        static public IDictionary MethodHelp
        {
            get { return _methodHelp; }
        }

        /// <summary>Constructor.</summary>
        /// <param name="server"><c>XmlRpcServer</c> server to be the system object for.</param>
        public XmlRpcSystemObject(XmlRpcServer server)
        {
            _server = server;
            server.Add("system", this);
            _methodHelp.Add(this.GetType().FullName + ".methodHelp", "Return a string description.");
        }

        /// <summary>Invoke a method on a given object.</summary>
        /// <remarks>Using reflection, and respecting the <c>XmlRpcExposed</c> attribute,
        /// invoke the <paramref>methodName</paramref> method on the <paramref>target</paramref>
        /// instance with the <paramref>parameters</paramref> provided. All this packages other <c>Invoke</c> methods 
        /// end up calling this.</remarks>
        /// <returns><c>Object</c> the value the invoked method returns.</returns>
        /// <exception cref="XmlRpcException">If method does not exist, is not exposed, parameters invalid, or invocation
        /// results in an exception. Note, the <c>XmlRpcException.Code</c> will indicate cause.</exception>
        static public Object Invoke(Object target, String methodName, IList parameters)
        {
            if (target == null)
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                              XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Invalid target object.");

            Type type = target.GetType();
            MethodInfo method = type.GetMethod(methodName);

            try
            {
                if (!XmlRpcExposedAttribute.ExposedMethod(target, methodName))
                    throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                              XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": Method " + methodName + " is not exposed.");
            }
            catch (MissingMethodException me)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                              XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG + ": " + me.Message);
            }

            Object[] args = new Object[parameters.Count];

            int index = 0;
            foreach (Object arg in parameters)
            {
                args[index] = arg;
                index++;
            }

            try
            {
                Object retValue = method.Invoke(target, args);
                if (retValue == null)
                    throw new XmlRpcException(XmlRpcErrorCodes.APPLICATION_ERROR,
                              XmlRpcErrorCodes.APPLICATION_ERROR_MSG + ": Method returned NULL.");
                return retValue;
            }
            catch (XmlRpcException e)
            {
                throw e;
            }
            catch (ArgumentException ae)
            {
                Logger.WriteEntry(XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": " + ae.Message,
                          LogLevel.Information);
                String call = methodName + "( ";
                foreach (Object o in args)
                {
                    call += o.GetType().Name;
                    call += " ";
                }
                call += ")";
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_PARAMS,
                              XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": Arguement type mismatch invoking " + call);
            }
            catch (TargetParameterCountException tpce)
            {
                Logger.WriteEntry(XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": " + tpce.Message,
                          LogLevel.Information);
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_PARAMS,
                              XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG + ": Arguement count mismatch invoking " + methodName);
            }
            catch (TargetInvocationException tie)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.APPLICATION_ERROR,
                              XmlRpcErrorCodes.APPLICATION_ERROR_MSG + " Invoked method " + methodName + ": " + tie.Message);
            }
        }

        /// <summary>List methods available on all handlers of this server.</summary>
        /// <returns><c>IList</c> An array of <c>Strings</c>, each <c>String</c> will have form "object.method".</returns>
        [XmlRpcExposed]
        public IList listMethods()
        {
            IList methods = new ArrayList();
            Boolean considerExposure;

            foreach (DictionaryEntry handlerEntry in _server)
            {
                considerExposure = XmlRpcExposedAttribute.IsExposed(handlerEntry.Value.GetType());

                foreach (MemberInfo mi in handlerEntry.Value.GetType().GetMembers())
                {
                    if (mi.MemberType != MemberTypes.Method)
                        continue;

                    if (!((MethodInfo)mi).IsPublic)
                        continue;

                    if (considerExposure && !XmlRpcExposedAttribute.IsExposed(mi))
                        continue;

                    methods.Add(handlerEntry.Key + "." + mi.Name);
                }
            }

            return methods;
        }

        /// <summary>Given a method name return the possible signatures for it.</summary>
        /// <param name="name"><c>String</c> The object.method name to look up.</param>
        /// <returns><c>IList</c> Of arrays of signatures.</returns>
        [XmlRpcExposed]
        public IList methodSignature(String name)
        {
            IList signatures = new ArrayList();
            int index = name.IndexOf('.');

            if (index < 0)
                return signatures;

            String oName = name.Substring(0, index);
            Object obj = _server[oName];

            if (obj == null)
                return signatures;

            MemberInfo[] mi = obj.GetType().GetMember(name.Substring(index + 1));

            if (mi == null || mi.Length != 1) // for now we want a single signature
                return signatures;

            MethodInfo method;

            try
            {
                method = (MethodInfo)mi[0];
            }
            catch (Exception e)
            {
                Logger.WriteEntry("Attempted methodSignature call on " + mi[0] + " caused: " + e,
                          LogLevel.Information);
                return signatures;
            }

            if (!method.IsPublic)
                return signatures;

            IList signature = new ArrayList();
            signature.Add(method.ReturnType.Name);

            foreach (ParameterInfo param in method.GetParameters())
            {
                signature.Add(param.ParameterType.Name);
            }


            signatures.Add(signature);

            return signatures;
        }

        /// <summary>Help for given method signature. Not implemented yet.</summary>
        /// <param name="name"><c>String</c> The object.method name to look up.</param>
        /// <returns><c>String</c> help text. Rich HTML text.</returns>
        [XmlRpcExposed]
        public String methodHelp(String name)
        {
            String help = null;

            try
            {
                help = (String)_methodHelp[_server.MethodName(name)];
            }
            catch (XmlRpcException e)
            {
                throw e;
            }
            catch (Exception) { /* ignored */ };

            if (help == null)
                help = "No help available for: " + name;

            return help;
        }

        /// <summary>Boxcarring support method.</summary>
        /// <param name="calls"><c>IList</c> of calls</param>
        /// <returns><c>ArrayList</c> of results/faults.</returns>
        [XmlRpcExposed]
        public IList multiCall(IList calls)
        {
            IList responses = new ArrayList();
            XmlRpcResponse fault = new XmlRpcResponse();

            foreach (IDictionary call in calls)
            {
                try
                {
                    XmlRpcRequest req = new XmlRpcRequest((String)call[XmlRpcXmlTokens.METHOD_NAME],
                                          (ArrayList)call[XmlRpcXmlTokens.PARAMS]);
                    Object results = _server.Invoke(req);
                    IList response = new ArrayList();
                    response.Add(results);
                    responses.Add(response);
                }
                catch (XmlRpcException e)
                {
                    fault.SetFault(e.FaultCode, e.FaultString);
                    responses.Add(fault.Value);
                }
                catch (Exception e2)
                {
                    fault.SetFault(XmlRpcErrorCodes.APPLICATION_ERROR,
                               XmlRpcErrorCodes.APPLICATION_ERROR_MSG + ": " + e2.Message);
                    responses.Add(fault.Value);
                }
            }

            return responses;
        }

    }
}