Exemple #1
0
        void ListenerCallback(IAsyncResult asyncRequest)
        {
            HttpListenerContext context = m_httpListener.EndGetContext(asyncRequest);
            RestRequest request = new RestRequest(context.Request);
            RestResponse response = new RestResponse(context.Response);
            Type restModuleType = null;
            string result = "";

            bool isNewSession = false;
            RestSession session = m_sessionManager.GetSession(request, out isNewSession);

            m_logger.LogInfo("Server received: " + request.ToString());

            if (m_restModuleTypes.TryGetValue(request.RESTModuleName, out restModuleType))
            {
                // Find the method on the module by name
                MethodInfo method = restModuleType.GetMethod(request.RESTMethodName);

                if (method != null)
                {
                    // Attempt to convert convert all string arguments over to the corresponding native types
                    try
                    {
                        ParameterInfo[] methodParameters= method.GetParameters();
                        object[] parameters = new object[methodParameters.Length];

                        for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
                        {
                            Type parameterType = methodParameters[parameterIndex].ParameterType;
                            string parameterName = methodParameters[parameterIndex].Name;
                            string stringParameter = request.RESTMethodParameters[parameterName];

                            parameters[parameterIndex] = Convert.ChangeType(stringParameter, parameterType);
                        }

                        // Attempt to call the REST method
                        try
                        {
                            ICacheAdapter applicationCacheAdapter = new DictionaryCacheAdapter(m_applicationCache);
                            ISessionAdapter sessionAdapter = session;
                            IResponseAdapter responseAdapter = response;

                            RestModule restModule=
                                (RestModule)Activator.CreateInstance(
                                    restModuleType,
                                    applicationCacheAdapter,
                                    sessionAdapter,
                                    responseAdapter);

                            object methodResult = method.Invoke(restModule, parameters);

                            result = methodResult.ToString();
                        }
                        catch (System.Exception ex)
                        {
                            result = "Failed to invoke REST Method: " + ex.Message;
                            m_logger.LogError(result);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        result = "Malformed REST Parameters: " + ex.Message;
                        m_logger.LogError(result);
                    }
                }
                else
                {
                    result = "Unknown REST Method: " + request.RESTMethodName;
                    m_logger.LogError(result);
                }
            }
            else
            {
                result = "Unknown REST Module: " + request.RESTModuleName;
                m_logger.LogError(result);
            }

            // If the module wants the session to be abandoned free it from the session manager
            if (session.IsAbandoned)
            {
                m_sessionManager.FreeSession(session);
            }
            // If this is a new session, give the client the session cookie
            else if (isNewSession)
            {
                response.AppendSessionIdCookie(session);
            }

            // If there was any result string from the rest method, assign it to the result
            if (result.Length > 0)
            {
                response.SetBody(result);
            }

            response.Close();
        }
        void ListenerCallback(IAsyncResult asyncRequest)
        {
            HttpListenerContext context  = m_httpListener.EndGetContext(asyncRequest);
            RestRequest         request  = new RestRequest(context.Request);
            RestResponse        response = new RestResponse(context.Response);
            Type   restModuleType        = null;
            string result = "";

            bool        isNewSession = false;
            RestSession session      = m_sessionManager.GetSession(request, out isNewSession);

            m_logger.LogInfo("Server received: " + request.ToString());

            if (m_restModuleTypes.TryGetValue(request.RESTModuleName, out restModuleType))
            {
                // Find the method on the module by name
                MethodInfo method = restModuleType.GetMethod(request.RESTMethodName);

                if (method != null)
                {
                    // Attempt to convert convert all string arguments over to the corresponding native types
                    try
                    {
                        ParameterInfo[] methodParameters = method.GetParameters();
                        object[]        parameters       = new object[methodParameters.Length];

                        for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
                        {
                            Type   parameterType   = methodParameters[parameterIndex].ParameterType;
                            string parameterName   = methodParameters[parameterIndex].Name;
                            string stringParameter = request.RESTMethodParameters[parameterName];

                            parameters[parameterIndex] = Convert.ChangeType(stringParameter, parameterType);
                        }

                        // Attempt to call the REST method
                        try
                        {
                            ICacheAdapter    applicationCacheAdapter = new DictionaryCacheAdapter(m_applicationCache);
                            ISessionAdapter  sessionAdapter          = session;
                            IResponseAdapter responseAdapter         = response;

                            RestModule restModule =
                                (RestModule)Activator.CreateInstance(
                                    restModuleType,
                                    applicationCacheAdapter,
                                    sessionAdapter,
                                    responseAdapter);

                            object methodResult = method.Invoke(restModule, parameters);

                            result = methodResult.ToString();
                        }
                        catch (System.Exception ex)
                        {
                            result = "Failed to invoke REST Method: " + ex.Message;
                            m_logger.LogError(result);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        result = "Malformed REST Parameters: " + ex.Message;
                        m_logger.LogError(result);
                    }
                }
                else
                {
                    result = "Unknown REST Method: " + request.RESTMethodName;
                    m_logger.LogError(result);
                }
            }
            else
            {
                result = "Unknown REST Module: " + request.RESTModuleName;
                m_logger.LogError(result);
            }

            // If the module wants the session to be abandoned free it from the session manager
            if (session.IsAbandoned)
            {
                m_sessionManager.FreeSession(session);
            }
            // If this is a new session, give the client the session cookie
            else if (isNewSession)
            {
                response.AppendSessionIdCookie(session);
            }

            // If there was any result string from the rest method, assign it to the result
            if (result.Length > 0)
            {
                response.SetBody(result);
            }

            response.Close();
        }