/** * Return a new PageContext for storing component-scoped variables. * @return a new PageContext */ protected PageContext createPageContext() { return new PageContext(); }
/** * @param html * @return a ContextMap to access the parameters of the inbound Http request. */ public static ContextMap getParameters(HtmlCanvas html) { return html.getPageContext().getContextMap(PageContext.REQUEST_PARAMETERS); } /**
/** * * @param html * @return the actual path of the inbound Http request. */ public static String getPath(HtmlCanvas html) { return html.getPageContext().getString(PageContext.REQUEST_PATH); } /**
/** * Render the component using the receiver. If available, prepare the * pageContext for accessing variables by the components. * * @param component a Renderable instance | null * @return the receiver this <code>HtmlCanvas</code> instance. * @throws IOException in case of an I/O error. */ public HtmlCanvas render(Renderable component) throws IOException { // Allow undefined component as argument. This is a noop. if (component == null) return this; if (pageContext != null) { pageContext.beginRender(); component.renderOn(this); pageContext.endRender(); } else { component.renderOn(this); } return this; } /**
static void setupPageContext(HttpServletRequest request, PageContext context) { String q = request.getQueryString(); context.withObject(PageContext.REQUEST_PATH, request.getPathTranslated()); context.withObject(PageContext.REQUEST_URIQ, request.getRequestURI() + (q == null ? "" : "?" + q)); context.withObject(PageContext.SESSION, new SessionAttributesMap(request)); context.withObject(PageContext.REQUEST_PARAMETERS, new RequestParametersMap(request)); context.withObject(PageContext.REQUEST_HEADERS, new RequestHeadersMap(request)); } }
@Override public PageContext withBoolean(String key, Boolean trueOrFalse) { LOG.fine("PageContext.["+this.attributes.getDepth()+"].set(" + key + "," + trueOrFalse + ")"); return super.withBoolean(key, trueOrFalse); } }
@Override public PageContext withInteger(String key, Integer number) { LOG.fine("PageContext.["+this.attributes.getDepth()+"].set(" + key + "," + number + ")"); return super.withInteger(key, number); }
public void renderOn(HtmlCanvas html) throws IOException {// @formatter:off // no need to inspect an exception if (target instanceof RenderException) { target.renderOn(html); return; } html .div(class_("rendersnake-inspector")) .write(target.getClass().getName()); html.getPageContext().renderForInpectorOn(this,html); target.renderOn(html); html._div(); } // @formatter:on
/** * Render the receiver on a html to provide debugging information about the exception. */ public void renderOn(HtmlCanvas html) throws IOException { html.div(style("color:red")); if (isEmptyStack) { html.write("[Render Error] unexpected close()"); return; } if (expectedTag != null) { // tag mismatch html.write(String.format("[Render Error] expected [%s] actual [%s]", expectedTag, closingTag)); } if (exception != null) { // general error html.write(String.format("[Render Error] general exception [%s]", exception.getMessage())); } // if debugging, the error component is available Renderable errorComponent = (Renderable)(html.getPageContext().getObject("renderable.error")); if (errorComponent != null) { html.write(" in ").write(errorComponent.getClass().getName()); } else { html.a(href("?inspect")).write(" Inspect details")._a(); this.renderStackTraceOn(html); } html._div(); } /**
@Override public PageContext withObject(String key, Object value) { LOG.fine("PageContext.["+this.attributes.getDepth()+"].set(" + key + "," + value + ")"); return super.withObject(key, value); }
/** * * @param html * @return a ContextMap to access the Cookies passed in by the inbound Http request. */ public static ContextMap getCookies(HtmlCanvas html) { return html.getPageContext().getContextMap(PageContext.REQUEST_COOKIES); } /**
/** * Dispatch an incoming request to a Renderable component * by looking for a named component that matches the path. */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uri = request.getPathInfo(); Renderable component = this.dispatchMap.get(uri); if (component == null) { response.sendError(404); } else { HtmlCanvas canvas = HtmlCanvasFactory.createCanvas(request,response,response.getWriter()); PageContext context = canvas.getPageContext(); String q = request.getQueryString(); context.withObject(PageContext.REQUEST_PATH, request.getPathTranslated()); context.withObject(PageContext.REQUEST_URIQ, request.getRequestURI() + (q == null ? "" : "?" + q)); context.withObject(PageContext.SESSION, new SessionAttributesMap(request)); context.withObject(PageContext.REQUEST_PARAMETERS, new RequestParametersMap(request)); context.withObject(PageContext.REQUEST_HEADERS, new RequestHeadersMap(request)); canvas.render(component); } }
/** * Return the full undecoded URL. * @param html * @return */ public static String getRequestURLAndQuery(HtmlCanvas html) { return html.getPageContext().getString(PageContext.REQUEST_URIQ); } }
/** * * @param html * @return a ContextMap to access the session associated to the inbound request. */ public static ContextMap getSession(HtmlCanvas html) { return html.getPageContext().getContextMap(PageContext.SESSION); } /**
@Override public HtmlCanvas render(Renderable component) throws IOException { if (null == component) return this; // For debugging, we hide the exception component to reveal the offending component if (!(component instanceof RenderException)) this.getPageContext().withObject(RenderException.KEY_PAGECONTEXT,component); if (enabled) { renderCount++; if (insideBody) { super.render(new Inspector(component)); } else { super.render(component); } } else { super.render(component); } return this; }
/** * use RequestUtils.getPathParameters(html) instead. * @return */ @Deprecated public ContextMap getPathParameters() { return this.getPageContext().getContextMap(PageContext.REQUEST_PATH); } /**
/** * * @param html * @param name * @return aString | null which is the value of a header of the inbound Http request. */ public static String getHeaderValue(HtmlCanvas html, String name) { ContextMap map = html.getPageContext().getContextMap(PageContext.REQUEST_HEADERS); if (null == map) return null; return map.getString(name); } /**