Ejemplo n.º 1
0
        internal CacheExecutionResult ExecuteIfCached(Type channel, MethodInfo method, HttpListenerRequest request, HttpListenerResponse response, IChannelHeuristicContext heurContext)
        {
            CacheExecutionResult result = new CacheExecutionResult();

            result.Executed = false;
            bool isCacheEnabled = false;

            try
            {
                isCacheEnabled = IsCached(method);
            }
            catch (InvalidChannelMethodTargetException ex)
            {
                StreamWriter writer = new StreamWriter(response.OutputStream);
                _msgService.ExceptionHandler(writer, ex, response);
                writer.Close();
                result.Executed = true;
            }

            if (isCacheEnabled)
            {
                HeuristicsInfo hInfo    = new HeuristicsInfo();
                bool           isCached = _heuristics.IsMethodCached(channel, method, out hInfo);
                if (isCached)
                {
                    ChannelMethodHeuristicOptions hOptions = new ChannelMethodHeuristicOptions
                    {
                        Channel       = channel,
                        ChannelMethod = method,
                        Request       = request,
                        Response      = response
                    };
                    return(_heuristics.Execute(hOptions, hInfo));
                }
                else
                {
                    heurContext.ExpectsAdding = true;
                    heurContext.Channel       = channel;
                    heurContext.MethodInfo    = method;
                    result.Executed           = false;
                    result.DataProcessed      = false;
                }
            }

            return(result);
        }
        internal bool TryInvokeGetRequest(Type channel,
                                          MethodInfo method,
                                          HttpListenerRequest request,
                                          HttpListenerResponse response,
                                          ChannelMethodInfo methodDescription,
                                          ChannelConfigurationInfo channelConfig,
                                          List <object> channelRequestBody,
                                          bool authenticated,
                                          CacheExecutionResult cacheExecutionResult)
        {
            try
            {
                if (request.QueryString.AllKeys.Length > 0)
                {
                    SetupAndInvokeGetRequest(channel, method, request, response, methodDescription, channelConfig, channelRequestBody, authenticated, true, cacheExecutionResult);
                }
                else if (request.QueryString.AllKeys.Length == 0)
                {
                    if (methodDescription.Parameters.Count > 0)
                    {
                        StreamWriter writer = new StreamWriter(response.OutputStream);
                        _msgService.ExceptionHandler(writer, new TargetParameterCountException(), response);
                        writer.Close();
                        return(false);
                    }

                    SetupAndInvokeGetRequest(channel, method, request, response, methodDescription, channelConfig, channelRequestBody, authenticated, false, cacheExecutionResult);
                }
                return(true);
            }
            catch (Exception ex)
            {
                using (StreamWriter writer = new StreamWriter(response.OutputStream))
                {
                    _msgService.ExceptionHandler(writer, ex, response);
                    LogChannel.Write(LogSeverity.Fatal, ex.Message);
                }

                return(true);
            }
        }
        internal void SetupAndInvokeGetRequest(Type channel,
                                               MethodInfo method,
                                               HttpListenerRequest request,
                                               HttpListenerResponse response,
                                               ChannelMethodInfo methodDescription,
                                               ChannelConfigurationInfo channelConfig,
                                               List <object> channelRequestBody,
                                               bool authenticated,
                                               bool hasParams,
                                               CacheExecutionResult cacheExecutionResult)
        {
            //Context should be initialized before invoking the method because ChannelBase relies on Context
            InitChannelMethodContext(channelConfig.Endpoint, request, response, authenticated, channelConfig.HttpMethod, channelRequestBody);
            if (hasParams)
            {
                //Since request body will be processed in Heuristics if cache is enabled
                //Data is already stored in Data.Parameters property of CacheExecutionResult
                if (!cacheExecutionResult.DataProcessed)
                {
                    _dsrFactory        = new ChannelMethodDeserializerFactory(request.QueryString);
                    channelRequestBody = _dsrFactory.DeserializeFromQueryParameters(methodDescription);
                }
                else
                {
                    channelRequestBody = cacheExecutionResult.Data.Parameters;
                }

                _requestActivator.GetActivateWithParameters(channel, method, channelRequestBody, response);
            }
            else
            {
                _requestActivator.GetActivateWithoutParameters(channel, method, response);
            }

            _contextProvider.DestroyChannelMethodContext(channelConfig.Endpoint);
        }
        internal void TryInvokePostRequest(Type channel,
                                           MethodInfo method,
                                           HttpListenerRequest request,
                                           HttpListenerResponse response,
                                           List <object> channelRequestBody,
                                           ChannelMethodInfo methodDescription,
                                           ChannelConfigurationInfo channelConfig,
                                           bool authenticated,
                                           CacheExecutionResult cacheExecutionResult)
        {
            StreamWriter writer = new StreamWriter(response.OutputStream);

            try
            {
                //Since request body will be processed in Heuristics if cache is enabled
                //InputStream is flushed and data is already stored in Data.Parameters
                //property of CacheExecutionResult
                if (!cacheExecutionResult.DataProcessed)
                {
                    _dsrFactory        = new ChannelMethodDeserializerFactory(request.InputStream);
                    channelRequestBody = _dsrFactory.DeserializeFromBody(methodDescription, request.ContentType);
                }
                else
                {
                    channelRequestBody = cacheExecutionResult.Data.Parameters;
                }

                InitChannelMethodContext(channelConfig.Endpoint, request, response, authenticated, channelConfig.HttpMethod, channelRequestBody);
                _requestActivator.PostActivate(channel, method, channelRequestBody, response);
            }
            catch (ChannelMethodContentTypeException cEx)
            {
                response.StatusCode = 400;
                _msgService.ExceptionHandler(writer, cEx, response);
                LogChannel.Write(LogSeverity.Error, cEx.Message);
            }
            catch (ChannelMethodParameterException pEx)
            {
                response.StatusCode = 400;
                _msgService.ExceptionHandler(writer, pEx, response);
                LogChannel.Write(LogSeverity.Error, pEx.Message);
            }
            catch (TargetParameterCountException tEx)
            {
                response.StatusCode = 400;
                _msgService.ExceptionHandler(writer, tEx, response);
                LogChannel.Write(LogSeverity.Error, tEx.Message);
            }
            catch (Exception ex)
            {
                response.StatusCode = 500;
                _msgService.ExceptionHandler(writer, ex, response);
                LogChannel.Write(LogSeverity.Fatal, ex.Message);
            }
            finally
            {
                _contextProvider.DestroyChannelMethodContext(channelConfig.Endpoint);
                writer.Flush();
                writer.Close();
            }
        }
        public void Start()
        {
            State     = EntityState.Starting;
            StateName = Enum.GetName(typeof(EntityState), EntityState.Starting);

            _httpListener = new HttpListener();
            ChannelConfigurationInfo channelConfig = _configuration.Configure(_httpListener, _channel, _method, _baseURL);

            Url = channelConfig.MethodUrl;
            //Keep the ChannelMethod open for new requests
            while (true)
            {
                try
                {
                    _httpListener.Start();
                    State     = EntityState.Running;
                    StateName = Enum.GetName(typeof(EntityState), EntityState.Running);
                }
                catch (HttpListenerException hle)
                {
                    Console.WriteLine($"System.Net.HttpListenerException encountered on {channelConfig.MethodUrl} with reason :{hle.Message}");
                    return;
                }

                if (!_isManaged)
                {
                    Console.WriteLine($"Listening on {Url}");
                }

                HttpListenerContext      context       = _httpListener.GetContext();
                HttpListenerRequest      request       = context.Request;
                HttpListenerResponse     response      = context.Response;
                IChannelHeuristicContext heuristicsCtx = _services.Get <IChannelHeuristicContext>();

                LogChannel.Write(LogSeverity.Info, $"Request coming to {channelConfig.Endpoint.Name}");
                LogChannel.Write(LogSeverity.Info, $"HttpMethod:{request.HttpMethod}");

                ChannelAuthenticationInspector authInspector = new ChannelAuthenticationInspector(_authenticationService, _msgService, _settings, _session, _basicAuthenticationMethod, _tokenAuthenticationMethod);

                //Even if method is cached check authenticaion first
                bool authFailed = authInspector.AuthenticationFailedIfRequired(context, request, response, channelConfig, out bool authenticated);
                if (authFailed)
                {
                    goto EndRequest;
                }

                List <object> channelRequestBody = null;

                ChannelMethodCacheInspector cacheInspector       = new ChannelMethodCacheInspector(_msgService, _heuristics);
                CacheExecutionResult        cacheExecutionResult = cacheInspector.ExecuteIfCached(_channel, _method, request, response, heuristicsCtx);

                if (cacheExecutionResult.Executed)
                {
                    heuristicsCtx.Clear();
                    goto EndRequest;
                }

                if (channelConfig.HttpMethod.ToString() != request.HttpMethod && channelConfig.HttpMethod != ChannelHttpMethod.Unknown)
                {
                    _msgService.WrongHttpMethod(response, channelConfig.HttpMethod);
                    LogChannel.Write(LogSeverity.Error, "Wrong HttpMethod... Closing request");
                    goto EndRequest;
                }

                ChannelMethodInfo   methodDescription   = _channelMethodDescriptor.GetMethodDescription(_method);
                ChannelMethodCaller channelMethodCaller = new ChannelMethodCaller(_msgService, _contextProvider, _requestActivator);

                if (request.HttpMethod == "GET")
                {
                    channelMethodCaller.TryInvokeGetRequest(_channel, _method, request, response, methodDescription, channelConfig, channelRequestBody, authenticated, cacheExecutionResult);
                    goto EndRequest;
                }

                //Enter only if Request Body is supplied with POST Method
                if (request.HasEntityBody == true && request.HttpMethod == "POST")
                {
                    channelMethodCaller.TryInvokePostRequest(_channel, _method, request, response, channelRequestBody, methodDescription, channelConfig, authenticated, cacheExecutionResult);
                }

EndRequest:
                LogChannel.Write(LogSeverity.Debug, "Request finished...");
                LogChannel.Write(LogSeverity.Debug, "Closing the response");
                response.Close();
            }
        }