コード例 #1
0
        public void HandleRequest(WebRequest request, out HttpStatusCode?statusCode, out string responseHeaders, out string responsePayload)
        {
            statusCode      = null;
            responseHeaders = string.Empty;
            responsePayload = string.Empty;

            var    reqHttp    = request as HttpWebRequest;
            string filename   = string.Empty;
            string requestURL = request.RequestUri.AbsoluteUri.ToLower().TrimEnd('/');

            try
            {
                if (!string.IsNullOrEmpty(reqHttp.Accept) &&
                    reqHttp.Accept.Equals(URL_PathConstants.URL_Path_AcceptHeader_StreamingTrue))
                {
                    statusCode = HttpStatusCode.NotAcceptable;
                    filename   = site.Properties["Response_Path"] + "OnlyStreaming.txt";
                }
                else if (URL2FileName.ContainsKey(requestURL))
                {
                    filename = this.URL2FileName[requestURL];

                    if (filename == "ServiceDocument" &&
                        !string.IsNullOrEmpty(reqHttp.Accept) &&
                        reqHttp.Accept.Contains(FormatConstants.V4FormatJsonFullMetadata))
                    {
                        filename += "_Full";
                    }
                    else if (filename == "Entity_People_Minimal" &&
                             !string.IsNullOrEmpty(reqHttp.Accept) &&
                             reqHttp.Accept.Contains(FormatConstants.V4FormatJsonFullMetadata))
                    {
                        filename = "Entity_People_Full";
                    }
                    else if (filename == "EntitySet_People_Minimal" &&
                             !string.IsNullOrEmpty(reqHttp.Accept) &&
                             reqHttp.Accept.Contains(FormatConstants.V4FormatJsonFullMetadata))
                    {
                        filename = "EntitySet_People_Full";
                    }
                    else if (filename.Contains("EntitySet") &&
                             !string.IsNullOrEmpty(reqHttp.Accept) &&
                             reqHttp.Accept.Contains(FormatConstants.V4FormatJsonNoMetadata))
                    {
                        filename = "EntitySet_People_None";
                    }

                    filename = site.Properties["Response_Path"] + filename + ".txt";
                }
                else
                {
                    if (!requestURL.StartsWith(URL_SrvDocConstants.URL_SrvDoc_TripPin))
                    {
                        using (WebResponse resp = request.GetResponse())
                        {
                            statusCode = WebHelper.ParseResponse(RuleEngineSetting.Instance().DefaultMaximumPayloadSize, resp, out responseHeaders, out responsePayload);
                        }
                    }
                    return;
                }

                if (!string.IsNullOrEmpty(filename))
                {
                    try
                    {
                        string   txtFileContent = Utility.ReadFile(filename);
                        string[] lines          = txtFileContent.Split(new string[] { "\r\n\r\n" }, 2, StringSplitOptions.None);

                        responseHeaders = lines[0];
                        responsePayload = lines[1];
                        statusCode      = HttpStatusCode.OK;
                        var serviceStatus = ServiceStatus.GetInstance(requestURL);
                        if (responsePayload.IsMetadata())
                        {
                            ServiceStatus.ReviseMetadata(responsePayload);
                        }

                        return;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Read file '{0}' gets exception: {1}!", filename, ex.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Handle Request '{0}' gets exception: {1}.", request.RequestUri.AbsoluteUri, ex.Message);
            }
            finally
            {
                try
                {
                    reqHttp.Abort();
                }
                catch (Exception reqEx)
                {
                    System.Diagnostics.Debug.WriteLine("Abort Request '{0}' gets exception: {1}.", request.RequestUri.AbsoluteUri, reqEx.Message);
                }
            }
        }