Beispiel #1
0
 private int SetCookieParams(Controller.RequestContext rc, HttpServletRequest req)
 {
     Cookie[] cookies = req.GetCookies();
     if (cookies != null)
     {
         foreach (Cookie cookie in cookies)
         {
             rc.Cookies()[cookie.GetName()] = cookie;
         }
         return(cookies.Length);
     }
     return(0);
 }
Beispiel #2
0
 public virtual Controller.RequestContext Context()
 {
     if (context == null)
     {
         if (injector == null)
         {
             // One of the downsides of making injection in subclasses optional.
             throw new WebAppException(StringHelper.Join("Error accessing RequestContext from\n"
                                                         , "a child constructor, either move the usage of the Controller\n", "methods out of the constructor or inject the RequestContext\n"
                                                         , "into the constructor"));
         }
         context = injector.GetInstance <Controller.RequestContext>();
     }
     return(context);
 }
Beispiel #3
0
 // /path/foo/bar with /path/:arg1/:arg2 will set {arg1=>foo, arg2=>bar}
 private void SetMoreParams(Controller.RequestContext rc, string pathInfo, Router.Dest
                            dest)
 {
     Preconditions.CheckState(pathInfo.StartsWith(dest.prefix), "prefix should match");
     if (dest.pathParams.Count == 0 || dest.prefix.Length == pathInfo.Length)
     {
         return;
     }
     string[] parts = Iterables.ToArray <string>(WebApp.pathSplitter.Split(Sharpen.Runtime.Substring
                                                                               (pathInfo, dest.prefix.Length)));
     Log.Debug("parts={}, params={}", parts, dest.pathParams);
     for (int i = 0; i < dest.pathParams.Count && i < parts.Length; ++i)
     {
         string key = dest.pathParams[i];
         if (key[0] == ':')
         {
             rc.MoreParams()[Sharpen.Runtime.Substring(key, 1)] = parts[i];
         }
     }
 }
Beispiel #4
0
        /// <exception cref="Javax.Servlet.ServletException"/>
        /// <exception cref="System.IO.IOException"/>
        protected override void Service(HttpServletRequest req, HttpServletResponse res)
        {
            res.SetCharacterEncoding("UTF-8");
            string uri = HtmlQuoting.QuoteHtmlChars(req.GetRequestURI());

            if (uri == null)
            {
                uri = "/";
            }
            if (devMode && uri.Equals("/__stop"))
            {
                // quick hack to restart servers in dev mode without OS commands
                res.SetStatus(res.ScNoContent);
                Log.Info("dev mode restart requested");
                PrepareToExit();
                return;
            }
            // if they provide a redirectPath go there instead of going to
            // "/" so that filters can differentiate the webapps.
            if (uri.Equals("/"))
            {
                string redirectPath = webApp.GetRedirectPath();
                if (redirectPath != null && !redirectPath.IsEmpty())
                {
                    res.SendRedirect(redirectPath);
                    return;
                }
            }
            string method = req.GetMethod();

            if (method.Equals("OPTIONS"))
            {
                DoOptions(req, res);
                return;
            }
            if (method.Equals("TRACE"))
            {
                DoTrace(req, res);
                return;
            }
            if (method.Equals("HEAD"))
            {
                DoGet(req, res);
                // default to bad request
                return;
            }
            string pathInfo = req.GetPathInfo();

            if (pathInfo == null)
            {
                pathInfo = "/";
            }
            Controller.RequestContext rc = injector.GetInstance <Controller.RequestContext>();
            if (SetCookieParams(rc, req) > 0)
            {
                Cookie ec = rc.Cookies()[ErrorCookie];
                if (ec != null)
                {
                    rc.SetStatus(System.Convert.ToInt32(rc.Cookies()[StatusCookie].GetValue()));
                    RemoveErrorCookies(res, uri);
                    rc.Set(Params.ErrorDetails, ec.GetValue());
                    Render(typeof(ErrorPage));
                    return;
                }
            }
            rc.prefix = webApp.Name();
            Router.Dest dest = null;
            try
            {
                dest = router.Resolve(method, pathInfo);
            }
            catch (WebAppException e)
            {
                rc.error = e;
                if (!e.Message.Contains("not found"))
                {
                    rc.SetStatus(res.ScInternalServerError);
                    Render(typeof(ErrorPage));
                    return;
                }
            }
            if (dest == null)
            {
                rc.SetStatus(res.ScNotFound);
                Render(typeof(ErrorPage));
                return;
            }
            rc.devMode = devMode;
            SetMoreParams(rc, pathInfo, dest);
            Controller controller = injector.GetInstance(dest.controllerClass);

            try
            {
                // TODO: support args converted from /path/:arg1/...
                dest.action.Invoke(controller, (object[])null);
                if (!rc.rendered)
                {
                    if (dest.defaultViewClass != null)
                    {
                        Render(dest.defaultViewClass);
                    }
                    else
                    {
                        if (rc.status == 200)
                        {
                            throw new InvalidOperationException("No view rendered for 200");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("error handling URI: " + uri, e);
                // Page could be half rendered (but still not flushed). So redirect.
                RedirectToErrorPage(res, e, uri, devMode);
            }
        }
Beispiel #5
0
 internal ViewContext(Controller.RequestContext ctx)
 {
     rc = ctx;
 }
Beispiel #6
0
 public Controller(Controller.RequestContext ctx)
 {
     // Makes injection in subclasses optional.
     // Time will tell if this buy us more than the NPEs :)
     context = ctx;
 }