/// <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); } }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Javax.Servlet.ServletException"/> public override void DoFilter(HttpServletRequest request, HttpServletResponse response , FilterChain chain) { response.SetCharacterEncoding("UTF-8"); string uri = HtmlQuoting.QuoteHtmlChars(request.GetRequestURI()); if (uri == null) { uri = "/"; } RMWebApp rmWebApp = injector.GetInstance <RMWebApp>(); rmWebApp.CheckIfStandbyRM(); if (rmWebApp.IsStandby() && ShouldRedirect(rmWebApp, uri)) { string redirectPath = rmWebApp.GetRedirectPath(); if (redirectPath != null && !redirectPath.IsEmpty()) { redirectPath += uri; string redirectMsg = "This is standby RM. The redirect url is: " + redirectPath; PrintWriter @out = response.GetWriter(); @out.WriteLine(redirectMsg); response.SetHeader("Location", redirectPath); response.SetStatus(HttpServletResponse.ScTemporaryRedirect); return; } else { bool doRetry = true; string retryIntervalStr = request.GetParameter(YarnWebParams.NextRefreshInterval); int retryInterval = 0; if (retryIntervalStr != null) { try { retryInterval = System.Convert.ToInt32(retryIntervalStr.Trim()); } catch (FormatException) { doRetry = false; } } int next = CalculateExponentialTime(retryInterval); string redirectUrl = AppendOrReplaceParamter(path + uri, YarnWebParams.NextRefreshInterval + "=" + (retryInterval + 1)); if (redirectUrl == null || next > MaxSleepTime) { doRetry = false; } string redirectMsg = doRetry ? "Can not find any active RM. Will retry in next " + next + " seconds." : "There is no active RM right now."; redirectMsg += "\nHA Zookeeper Connection State: " + rmWebApp.GetHAZookeeperConnectionState (); PrintWriter @out = response.GetWriter(); @out.WriteLine(redirectMsg); if (doRetry) { response.SetHeader("Refresh", next + ";url=" + redirectUrl); response.SetStatus(HttpServletResponse.ScTemporaryRedirect); } } return; } base.DoFilter(request, response, chain); }