public static UIComponent addTransient( FacesContext context, ServletRequest req, UIComponent parent, String prevId, Class childClass) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); BodyContent body = parentTag.getBodyContent(); if (body != null) addVerbatim(parent, body); } UIComponent child = null; ; if (child == null) child = (UIComponent) childClass.newInstance(); child.setTransient(true); addChild(parent, prevId, child); return child; }
public static UIComponent addFacet( FacesContext context, ServletRequest req, UIComponent parent, String facetName, ValueExpression binding, Class childClass) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); } UIComponent child = null; if (binding != null) child = (UIComponent) binding.getValue(context.getELContext()); if (child == null) child = (UIComponent) childClass.newInstance(); if (parent != null) parent.getFacets().put(facetName, child); if (binding != null) binding.setValue(context.getELContext(), child); return child; }
public static UIComponent addPersistent( FacesContext context, ServletRequest req, UIComponent parent, ValueExpression binding, Class childClass) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); BodyContent body = parentTag.getBodyContent(); addVerbatim(parent, body); } UIComponent child = null; if (binding != null) child = (UIComponent) binding.getValue(context.getELContext()); if (child == null) { child = (UIComponent) childClass.newInstance(); // jsf/3251 if (binding != null) binding.setValue(context.getELContext(), child); } if (parent != null) parent.getChildren().add(child); return child; }
public void render(FacesContext context) throws FacesException { if (context.getResponseComplete()) return; Application app = context.getApplication(); ViewHandler view = app.getViewHandler(); beforePhase(context, PhaseId.RENDER_RESPONSE); try { if (log.isLoggable(Level.FINER)) log.finer(context.getViewRoot() + " before render view"); view.renderView(context, context.getViewRoot()); } catch (java.io.IOException e) { if (sendError(context, "renderView", e)) return; throw new FacesException(e); } catch (RuntimeException e) { if (sendError(context, "renderView", e)) return; throw e; } finally { afterPhase(context, PhaseId.RENDER_RESPONSE); logMessages(context); } }
private void restoreView(FacesContext context) throws FacesException { Application app = context.getApplication(); if (app instanceof ApplicationImpl) ((ApplicationImpl) app).initRequest(); ViewHandler view = app.getViewHandler(); view.initView(context); UIViewRoot viewRoot = context.getViewRoot(); if (viewRoot != null) { ExternalContext extContext = context.getExternalContext(); viewRoot.setLocale(extContext.getRequestLocale()); doSetBindings(context.getELContext(), viewRoot); return; } String viewId = calculateViewId(context); String renderKitId = view.calculateRenderKitId(context); RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); RenderKit renderKit = renderKitFactory.getRenderKit(context, renderKitId); ResponseStateManager stateManager = renderKit.getResponseStateManager(); if (stateManager.isPostback(context)) { viewRoot = view.restoreView(context, viewId); if (viewRoot != null) { doSetBindings(context.getELContext(), viewRoot); } else { // XXX: backward compat issues with ViewHandler and StateManager // throw new ViewExpiredException(L.l("{0} is an expired view", viewId)); context.renderResponse(); viewRoot = view.createView(context, viewId); context.setViewRoot(viewRoot); } context.setViewRoot(viewRoot); } else { context.renderResponse(); viewRoot = view.createView(context, viewId); context.setViewRoot(viewRoot); } }
static ValueExpression restore(Object value, Class type, FacesContext context) { if (value == null) return null; String expr = (String) value; Application app = context.getApplication(); ExpressionFactory factory = app.getExpressionFactory(); return factory.createValueExpression(context.getELContext(), expr, type); }
private String calculateViewId(FacesContext context) { Map map = context.getExternalContext().getRequestMap(); String viewId = (String) map.get(RequestDispatcher.INCLUDE_PATH_INFO); if (viewId == null) viewId = context.getExternalContext().getRequestPathInfo(); if (viewId == null) viewId = (String) map.get(RequestDispatcher.INCLUDE_SERVLET_PATH); if (viewId == null) viewId = context.getExternalContext().getRequestServletPath(); return viewId; }
static ValueExpression restoreWithType(Object value, FacesContext context) { if (value == null) return null; Object[] state = (Object[]) value; String expr = (String) state[0]; Class type = (Class) state[1]; Application app = context.getApplication(); ExpressionFactory factory = app.getExpressionFactory(); return factory.createValueExpression(context.getELContext(), expr, type); }
public static UIComponent findPersistent( FacesContext context, ServletRequest req, UIComponent parent, String id) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); BodyContent body = null; if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); body = parentTag.getBodyContent(); } if (parent != null) { List<UIComponent> children = parent.getChildren(); int size = children.size(); String prevId = null; for (int i = 0; i < size; i++) { UIComponent child = children.get(i); if (id.equals(child.getId())) { if (body != null) addVerbatim(parent, prevId, body); return child; } if (child.getId() != null) prevId = child.getId(); } } return null; }
static final String evalString(ValueExpression expr, FacesContext context) { try { return (String) expr.getValue(context.getELContext()); } catch (ELException e) { throw new FacesException(e); } }
static final int evalInt(ValueExpression expr, FacesContext context) { try { return (Integer) expr.getValue(context.getELContext()); } catch (ELException e) { throw new FacesException(e); } }
private void logMessages(FacesContext context) { UIViewRoot root = context.getViewRoot(); String viewId = ""; if (root != null) viewId = root.getViewId(); Iterator<FacesMessage> iter = context.getMessages(); while (iter != null && iter.hasNext()) { FacesMessage msg = iter.next(); if (log.isLoggable(Level.FINE)) { if (msg.getDetail() != null) log.fine( viewId + " [ " + msg.getSeverity() + "] " + msg.getSummary() + " " + msg.getDetail()); else log.fine(viewId + " [ " + msg.getSeverity() + "] " + msg.getSummary()); } } }
/** Decodes the data from the form. */ @Override public void decode(FacesContext context, UIComponent component) { String clientId = component.getClientId(context); ExternalContext ext = context.getExternalContext(); Map<String, String> paramMap = ext.getRequestParameterMap(); String value = paramMap.get(clientId); if (value != null) ((EditableValueHolder) component).setSubmittedValue(value); }
static final boolean evalBoolean(ValueExpression expr, FacesContext context) { try { Object value = expr.getValue(context.getELContext()); if (value == null) return false; else if (value instanceof Boolean) return ((Boolean) value).booleanValue(); else if (value instanceof String) return "true".equalsIgnoreCase((String) value); else return false; } catch (ELException e) { throw new FacesException(e); } }
public static UIViewRoot findRoot(FacesContext context, ServletRequest req, Object etag) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); UIViewRoot root = context.getViewRoot(); if (root == null) throw new NullPointerException(L.l("f:view can't find current in FacesContext")); Object oldETag = root.getAttributes().get("caucho.etag"); if (oldETag != null && !oldETag.equals(etag)) { // clear view on JSP change root.getChildren().clear(); root.getFacets().clear(); } root.getAttributes().put("caucho.etag", etag); return root; }
public static UIComponent findFacet( FacesContext context, ServletRequest req, UIComponent parent, String facetName) throws Exception { if (context == null) FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); } if (parent != null) return parent.getFacet(facetName); else return null; }
private static String getMessage(FacesContext context, String messageId, String defaultMessage) { Application app = context.getApplication(); String bundleName = app.getMessageBundle(); if (bundleName == null) return defaultMessage; ResourceBundle bundle = app.getResourceBundle(context, bundleName); if (bundle == null) return defaultMessage; String msg = bundle.getString(messageId); if (msg != null) return msg; else return defaultMessage; }
/** Renders the open tag for the text. */ @Override public void encodeBegin(FacesContext context, UIComponent component) throws IOException { ResponseWriter out = context.getResponseWriter(); String id = component.getId(); String accesskey; String dir; boolean disabled; String disabledClass; String enabledClass; String lang; String onblur; String onchange; String onclick; String ondblclick; String onfocus; String onkeydown; String onkeypress; String onkeyup; String onmousedown; String onmousemove; String onmouseout; String onmouseover; String onmouseup; String onselect; boolean readonly; String style; String styleClass; String tabindex; String title; Object value; if (component instanceof HtmlSelectOneMenu) { HtmlSelectOneMenu htmlComponent = (HtmlSelectOneMenu) component; accesskey = htmlComponent.getAccesskey(); dir = htmlComponent.getDir(); disabled = htmlComponent.isDisabled(); disabledClass = htmlComponent.getDisabledClass(); enabledClass = htmlComponent.getEnabledClass(); lang = htmlComponent.getLang(); onblur = htmlComponent.getOnblur(); onchange = htmlComponent.getOnchange(); onclick = htmlComponent.getOnclick(); ondblclick = htmlComponent.getOndblclick(); onfocus = htmlComponent.getOnfocus(); onkeydown = htmlComponent.getOnkeydown(); onkeypress = htmlComponent.getOnkeypress(); onkeyup = htmlComponent.getOnkeyup(); onmousedown = htmlComponent.getOnmousedown(); onmousemove = htmlComponent.getOnmousemove(); onmouseout = htmlComponent.getOnmouseout(); onmouseover = htmlComponent.getOnmouseover(); onmouseup = htmlComponent.getOnmouseup(); onselect = htmlComponent.getOnselect(); readonly = htmlComponent.isReadonly(); style = htmlComponent.getStyle(); styleClass = htmlComponent.getStyleClass(); tabindex = htmlComponent.getTabindex(); title = htmlComponent.getTitle(); value = htmlComponent.getValue(); } else { Map<String, Object> attrMap = component.getAttributes(); accesskey = (String) attrMap.get("accesskey"); dir = (String) attrMap.get("dir"); disabled = Boolean.TRUE.equals(attrMap.get("disabled")); disabledClass = (String) attrMap.get("disabledClass"); enabledClass = (String) attrMap.get("enabledClass"); lang = (String) attrMap.get("lang"); onblur = (String) attrMap.get("onblur"); onchange = (String) attrMap.get("onchange"); onclick = (String) attrMap.get("onclick"); ondblclick = (String) attrMap.get("ondblclick"); onfocus = (String) attrMap.get("onfocus"); onkeydown = (String) attrMap.get("onkeydown"); onkeypress = (String) attrMap.get("onkeypress"); onkeyup = (String) attrMap.get("onkeyup"); onmousedown = (String) attrMap.get("onmousedown"); onmousemove = (String) attrMap.get("onmousemove"); onmouseout = (String) attrMap.get("onmouseout"); onmouseover = (String) attrMap.get("onmouseover"); onmouseup = (String) attrMap.get("onmouseup"); onselect = (String) attrMap.get("onselect"); readonly = Boolean.TRUE.equals(attrMap.get("readonly")); style = (String) attrMap.get("style"); styleClass = (String) attrMap.get("styleClass"); tabindex = (String) attrMap.get("tabindex"); title = (String) attrMap.get("title"); value = attrMap.get("value"); } UIViewRoot viewRoot = context.getViewRoot(); out.startElement("select", component); if (style != null) out.writeAttribute("style", style, "style"); if (styleClass != null) out.writeAttribute("class", styleClass, "class"); String clientId = component.getClientId(context); out.writeAttribute("name", clientId, "name"); if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) out.writeAttribute("id", clientId, "id"); if (disabled) out.writeAttribute("disabled", "disabled", "disabled"); if (accesskey != null) out.writeAttribute("accesskey", accesskey, "accesskey"); if (dir != null) out.writeAttribute("dir", dir, "dir"); if (lang != null) out.writeAttribute("lang", lang, "lang"); if (onblur != null) out.writeAttribute("onblur", onblur, "onblur"); if (onchange != null) out.writeAttribute("onchange", onchange, "onchange"); if (onclick != null) out.writeAttribute("onclick", onclick, "onclick"); if (ondblclick != null) out.writeAttribute("ondblclick", ondblclick, "ondblclick"); if (onfocus != null) out.writeAttribute("onfocus", onfocus, "onfocus"); if (onkeydown != null) out.writeAttribute("onkeydown", onkeydown, "onkeydown"); if (onkeypress != null) out.writeAttribute("onkeypress", onkeypress, "onkeypress"); if (onkeyup != null) out.writeAttribute("onkeyup", onkeyup, "onkeyup"); if (onmousedown != null) out.writeAttribute("onmousedown", onmousedown, "onmousedown"); if (onmousemove != null) out.writeAttribute("onmousemove", onmousemove, "onmousemove"); if (onmouseout != null) out.writeAttribute("onmouseout", onmouseout, "onmouseout"); if (onmouseover != null) out.writeAttribute("onmouseover", onmouseover, "onmouseover"); if (onmouseup != null) out.writeAttribute("onmouseup", onmouseup, "onmouseup"); if (onselect != null) out.writeAttribute("onselect", onselect, "onselect"); if (readonly) out.writeAttribute("readonly", "readonly", "readonly"); if (tabindex != null) out.writeAttribute("tabindex", tabindex, "tabindex"); if (title != null) out.writeAttribute("title", title, "title"); out.writeAttribute("size", "1", "size"); out.write("\n"); encodeOneChildren(out, context, component, value, enabledClass, disabledClass); out.endElement("select"); out.write("\n"); for (UIComponent child : component.getChildren()) { if (child instanceof UIComponent) child.encodeAll(context); } }
public void execute(FacesContext context) throws FacesException { boolean isFiner = log.isLoggable(Level.FINER); if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.RESTORE_VIEW); try { if (isFiner) log.finer("JSF[] before restore view"); restoreView(context); } finally { afterPhase(context, PhaseId.RESTORE_VIEW); } if (context.getResponseComplete() || context.getRenderResponse()) return; UIViewRoot viewRoot = context.getViewRoot(); beforePhase(context, PhaseId.APPLY_REQUEST_VALUES); try { if (isFiner) log.finer(context.getViewRoot() + " before process decodes"); viewRoot.processDecodes(context); } catch (RuntimeException e) { log.log(Level.WARNING, e.toString(), e); } finally { afterPhase(context, PhaseId.APPLY_REQUEST_VALUES); } // // Process Validations (processValidators) // if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.PROCESS_VALIDATIONS); try { if (isFiner) log.finer(context.getViewRoot() + " before process validators"); viewRoot.processValidators(context); } finally { afterPhase(context, PhaseId.PROCESS_VALIDATIONS); } // // Update Model Values (processUpdates) // if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.UPDATE_MODEL_VALUES); try { if (isFiner) log.finer(context.getViewRoot() + " before process updates"); viewRoot.processUpdates(context); } catch (RuntimeException e) { if (sendError(context, "processUpdates", e)) return; } finally { afterPhase(context, PhaseId.UPDATE_MODEL_VALUES); } // // Invoke Application (processApplication) // if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.INVOKE_APPLICATION); try { if (isFiner) log.finer(context.getViewRoot() + " before process application"); viewRoot.processApplication(context); } finally { afterPhase(context, PhaseId.INVOKE_APPLICATION); } }
private boolean sendError(FacesContext context, String lifecycle, Exception e) { for (Throwable cause = e; cause != null; cause = cause.getCause()) { if (cause instanceof DisplayableException) { if (e instanceof RuntimeException) throw (RuntimeException) e; else throw new FacesException(e); } else if (cause instanceof ServletException) throw new FacesException(e); else if (cause instanceof JspException) throw new FacesException(e); } ExternalContext extContext = context.getExternalContext(); Object response = extContext.getResponse(); if (!(response instanceof HttpServletResponse)) { context.renderResponse(); if (e instanceof RuntimeException) throw (RuntimeException) e; else throw new RuntimeException(e); } log.log(Level.WARNING, e.toString(), e); HttpServletResponse res = (HttpServletResponse) response; try { context.renderResponse(); context.responseComplete(); res.setStatus(500, "JSF Exception"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<body>"); out.println("<h3>JSF exception detected in " + lifecycle + " phase</h3>"); String msg = e.getMessage(); out.println("<span style='color:red;font:bold'>" + Html.escapeHtml(msg) + "</span><br/>"); out.println("<h3>Context: " + context.getViewRoot() + "</h3>"); out.println("<code><pre>"); String errorId = null; if (e instanceof FacesException && msg.startsWith("id=")) { int p = msg.indexOf(' '); errorId = msg.substring(3, p); } printComponentTree(out, errorId, context, context.getViewRoot(), 0); out.println("</pre></code>"); if (!Alarm.isTest()) { out.println("<h3>Stack Trace</h3>"); out.println("<pre>"); if (e.getCause() != null) e.getCause().printStackTrace(out); else e.printStackTrace(out); out.println("</pre>"); } out.println("</body>"); // clear, so we don't just loop Application app = context.getApplication(); ViewHandler view = app.getViewHandler(); UIViewRoot viewRoot = context.getViewRoot(); viewRoot = view.createView(context, viewRoot.getViewId()); context.setViewRoot(viewRoot); // view.writeState(context); // XXX: no need to output state, but review. return true; } catch (IOException e1) { throw new RuntimeException(e); } }