Example #1
0
 private void AssertRequestWasSuccessful <T>(IResponseAdapter <T> responseAdapter)
 {
     if (responseAdapter.GetStatus() != ResponseStatus.OK)
     {
         throw new IOException(responseAdapter.GetErrorMessage());
     }
 }
 public EnergyTankModule(
     ICacheAdapter appCache,
     ISessionAdapter session,
     IResponseAdapter response) :
     base(appCache, session, response)
 {
 }
 public AccountModule(
     ICacheAdapter appCache,
     ISessionAdapter session,
     IResponseAdapter response) :
     base(appCache, session, response)
 {
 }
Example #4
0
 public CharacterModule(
     ICacheAdapter appCache,
     ISessionAdapter session,
     IResponseAdapter response) :
     base(appCache, session, response)
 {
 }
Example #5
0
 public GameModule(
     ICacheAdapter appCache,
     ISessionAdapter session,
     IResponseAdapter response)
     : base(appCache, session, response)
 {
 }
 public EnergyTankModule(
     ICacheAdapter appCache,
     ISessionAdapter session,
     IResponseAdapter response)
     : base(appCache, session, response)
 {
 }
Example #7
0
 public RestModule()
 {
     // I don't expect this case to get called
     this.Application = null;
     this.Session     = null;
     this.Response    = null;
 }
Example #8
0
 public RestModule(
     ICacheAdapter appCache,
     ISessionAdapter session, 
     IResponseAdapter response)
 {
     this.Application = appCache;
     this.Session = session;
     this.Response = response;
 }
Example #9
0
 public RestModule(
     ICacheAdapter appCache,
     ISessionAdapter session,
     IResponseAdapter response)
 {
     this.Application = appCache;
     this.Session     = session;
     this.Response    = response;
 }
Example #10
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();
        }