Exemplo n.º 1
0
        public override bool TryRespond(IHttpContext context)
        {
            try
            {
                RequestWrapper  request  = context.Request as RequestWrapper;
                ResponseWrapper response = context.Response as ResponseWrapper;
                string          appName  = ApplicationNameResolver.ResolveApplicationName(context);

                bool responded = false;

                if (request != null && response != null)
                {
                    string path = request.Url.AbsolutePath.ToLowerInvariant();

                    if (path.StartsWith("/{0}"._Format(ResponderSignificantName.ToLowerInvariant())))
                    {
                        if (path.StartsWith(MethodFormPrefixFormat._Format(ResponderSignificantName).ToLowerInvariant()))
                        {
                            responded = SendMethodForm(context);
                        }
                        else
                        {
                            responded = SendProxyCode(context);
                        }
                    }
                    else
                    {
                        ExecutionRequest execRequest = ResolveExecutionRequest(context, appName);
                        responded = execRequest.Execute();
                        if (responded)
                        {
                            // TODO: make this configurable
                            response.AddHeader("Access-Control-Allow-Origin", "*");
                            response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
                            response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
                            // ---
                            RenderResult(appName, path, execRequest);
                        }
                    }
                }
                if (responded)
                {
                    OnResponded(context);
                }
                else
                {
                    OnNotResponded(context);
                }
                return(responded);
            }
            catch (Exception ex)
            {
                Logger.AddEntry("An error occurred in {0}.{1}: {2}", ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.Message);
                OnNotResponded(context);
                return(false);
            }
        }
Exemplo n.º 2
0
        public override bool TryRespond(IHttpContext context)
        {
            try
            {
                RequestWrapper  request  = context.Request as RequestWrapper;
                ResponseWrapper response = context.Response as ResponseWrapper;
                string          appName  = AppConf.AppNameFromUri(request.Url, BamConf.AppConfigs);

                bool responded = false;

                if (request != null && response != null)
                {
                    string path = request.Url.AbsolutePath.ToLowerInvariant();

                    if (path.StartsWith("/{0}"._Format(ResponderSignificantName.ToLowerInvariant())))
                    {
                        if (path.StartsWith(MethodFormPrefixFormat._Format(ResponderSignificantName).ToLowerInvariant()))
                        {
                            responded = SendMethodForm(context, appName);
                        }
                        else
                        {
                            responded = SendProxyCode(request, response, path);
                        }
                    }
                    else
                    {
                        ExecutionRequest execRequest = CreateExecutionRequest(context, appName);
                        responded = execRequest.Execute();
                        if (responded)
                        {
                            RenderResult(appName, path, execRequest);
                        }
                    }
                }
                if (responded)
                {
                    OnResponded(context);
                }
                else
                {
                    OnNotResponded(context);
                }
                return(responded);
            }
            catch (Exception ex)
            {
                Logger.AddEntry("An error occurred in {0}.{1}: {2}", ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.Message);
                OnNotResponded(context);
                return(false);
            }
        }
Exemplo n.º 3
0
        private bool SendMethodForm(IHttpContext context, string appName)
        {
            bool     result          = false;
            IRequest request         = context.Request;
            string   path            = request.Url.AbsolutePath;
            string   prefix          = MethodFormPrefixFormat._Format(ResponderSignificantName.ToLowerInvariant());
            string   partsICareAbout = path.TruncateFront(prefix.Length);

            string[] segments = partsICareAbout.DelimitSplit("/", "\\");

            if (segments.Length == 2)
            {
                Incubator         providers;
                List <ProxyAlias> aliases;
                GetServiceProxies(appName, out providers, out aliases);
                string className  = segments[0];
                string methodName = segments[1];
                Type   type       = providers[className];
                if (type == null)
                {
                    ProxyAlias alias = aliases.FirstOrDefault(a => a.Alias.Equals(className));
                    if (alias != null)
                    {
                        type = providers[alias.ClassName];
                    }
                }

                if (type != null)
                {
                    InputFormBuilder            builder    = new InputFormBuilder(type);
                    QueryStringParameter[]      parameters = request.Url.Query.DelimitSplit("?", "&").ToQueryStringParameters();
                    Dictionary <string, object> defaults   = new Dictionary <string, object>();
                    foreach (QueryStringParameter param in parameters)
                    {
                        defaults.Add(param.Name, param.Value);
                    }
                    TagBuilder  form        = builder.MethodForm(methodName, defaults);
                    LayoutModel layoutModel = GetLayoutModel(appName);
                    layoutModel.PageContent = form.ToMvcHtml().ToString();
                    ContentResponder.CommonTemplateRenderer.RenderLayout(layoutModel, context.Response.OutputStream);
                    result = true;
                }
            }

            return(result);
        }