Search in sources :

Example 1 with RenderKit

use of jakarta.faces.render.RenderKit in project myfaces by apache.

the class FacesContextImpl method isPostback.

@Override
public boolean isPostback() {
    assertNotReleased();
    RenderKit renderKit = getRenderKit();
    FacesContext facesContext = getCurrentFacesContext();
    if (renderKit == null) {
        // NullPointerException with StateManager, because
        // to restore state it first restore structure,
        // then fill it and in the middle of the two previous
        // process there is many calls from _ComponentChildrenList.childAdded
        // to facesContext.isPostback, and getViewRoot is null.
        // 
        // Setting a "phantom" UIViewRoot calling facesContext.setViewRoot(viewRoot)
        // to avoid it is bad, because this is work of RestoreViewExecutor,
        // and theorically ViewHandler.restoreView must return an UIViewRoot
        // instance.
        // 
        // The problem with this is if the user changes the renderkit directly
        // using f:view renderKitId param, the ResponseStateManager returned
        // will be the one tied to faces-config selected RenderKit. But the usual
        // method to check if a request is a postback, is always detect the param
        // jakarta.faces.ViewState, so there is no problem after all.
        String renderKitId = facesContext.getApplication().getViewHandler().calculateRenderKitId(facesContext);
        if (_renderKitFactory == null) {
            _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
        }
        renderKit = _renderKitFactory.getRenderKit(facesContext, renderKitId);
    }
    return renderKit.getResponseStateManager().isPostback(facesContext);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) RenderKit(jakarta.faces.render.RenderKit)

Example 2 with RenderKit

use of jakarta.faces.render.RenderKit in project myfaces by apache.

the class PartialViewContextImpl method getPartialResponseWriter.

@Override
public PartialResponseWriter getPartialResponseWriter() {
    assertNotReleased();
    if (_partialResponseWriter == null) {
        ResponseWriter responseWriter = context.getResponseWriter();
        if (responseWriter == null) {
            // ResponseWriter from the RenderKit and then wrap if necessary.
            try {
                RenderKit renderKit = context.getRenderKit();
                if (renderKit == null) {
                    // If the viewRoot was set to null by some reason, or there is no
                    // renderKitId on that view, this could be still an ajax redirect,
                    // so we have to try to calculate the renderKitId and return a
                    // RenderKit instance, to send the response.
                    String renderKitId = context.getApplication().getViewHandler().calculateRenderKitId(context);
                    RenderKitFactory rkf = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
                    renderKit = rkf.getRenderKit(context, renderKitId);
                }
                responseWriter = renderKit.createResponseWriter(context.getExternalContext().getResponseOutputWriter(), "text/xml", context.getExternalContext().getRequestCharacterEncoding());
            } catch (IOException e) {
                throw new IllegalStateException("Cannot create Partial Response Writer", e);
            }
        }
        // createResponseWriter,  so we should cast here for it and prevent double wrapping.
        if (responseWriter instanceof PartialResponseWriter) {
            _partialResponseWriter = (PartialResponseWriter) responseWriter;
        } else {
            _partialResponseWriter = new PartialResponseWriterImpl(responseWriter);
        }
    }
    return _partialResponseWriter;
}
Also used : PartialResponseWriterImpl(org.apache.myfaces.context.PartialResponseWriterImpl) ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) RenderKit(jakarta.faces.render.RenderKit) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) IOException(java.io.IOException) RenderKitFactory(jakarta.faces.render.RenderKitFactory)

Example 3 with RenderKit

use of jakarta.faces.render.RenderKit in project myfaces by apache.

the class FacesConfigurator method configureRenderKits.

private void configureRenderKits() {
    RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
    FacesConfigData dispenser = getDispenser();
    for (String renderKitId : dispenser.getRenderKitIds()) {
        Collection<String> renderKitClass = dispenser.getRenderKitClasses(renderKitId);
        if (renderKitClass.isEmpty()) {
            renderKitClass = new ArrayList<>(1);
            renderKitClass.add(DEFAULT_RENDER_KIT_CLASS);
        }
        RenderKit renderKit = (RenderKit) ClassUtils.buildApplicationObject(RenderKit.class, renderKitClass, null);
        // If the default html RenderKit instance is wrapped, the top level object will not implement
        // LazyRenderKit and all renderers will be added using the standard form.
        boolean lazyRenderKit = renderKit instanceof LazyRenderKit;
        for (Renderer element : dispenser.getRenderers(renderKitId)) {
            jakarta.faces.render.Renderer renderer;
            if (element.getRendererClass() != null) {
                if (lazyRenderKit) {
                    // Add renderer using LazyRenderKit interface. This will have the effect of improve startup
                    // time avoiding load renderer classes that are not used.
                    ((LazyRenderKit) renderKit).addRenderer(element.getComponentFamily(), element.getRendererType(), element.getRendererClass());
                } else {
                    // Use standard form
                    try {
                        renderer = (jakarta.faces.render.Renderer) ClassUtils.newInstance(element.getRendererClass());
                    } catch (Throwable e) {
                        // ignore the failure so that the render kit is configured
                        log.log(Level.SEVERE, "failed to configure class " + element.getRendererClass(), e);
                        continue;
                    }
                    if (renderer != null) {
                        renderKit.addRenderer(element.getComponentFamily(), element.getRendererType(), renderer);
                    } else {
                        log.log(Level.INFO, "Renderer instance cannot be created for " + element.getRendererClass() + ", ignoring..." + element.getRendererClass());
                    }
                }
            } else {
                log.log(Level.INFO, "Renderer element with no rendererClass found, ignoring..." + element.getRendererClass());
            }
        }
        Collection<ClientBehaviorRenderer> clientBehaviorRenderers = dispenser.getClientBehaviorRenderers(renderKitId);
        for (ClientBehaviorRenderer clientBehaviorRenderer : clientBehaviorRenderers) {
            try {
                jakarta.faces.render.ClientBehaviorRenderer behaviorRenderer = (jakarta.faces.render.ClientBehaviorRenderer) ClassUtils.newInstance(clientBehaviorRenderer.getRendererClass());
                renderKit.addClientBehaviorRenderer(clientBehaviorRenderer.getRendererType(), behaviorRenderer);
            } catch (Throwable e) {
                if (log.isLoggable(Level.SEVERE)) {
                    log.log(Level.SEVERE, "failed to configure client behavior renderer class " + clientBehaviorRenderer.getRendererClass(), e);
                }
            }
        }
        renderKitFactory.addRenderKit(renderKitId, renderKit);
    }
}
Also used : FacesConfigData(org.apache.myfaces.config.element.FacesConfigData) LazyRenderKit(org.apache.myfaces.renderkit.LazyRenderKit) ClientBehaviorRenderer(org.apache.myfaces.config.element.ClientBehaviorRenderer) LazyRenderKit(org.apache.myfaces.renderkit.LazyRenderKit) RenderKit(jakarta.faces.render.RenderKit) Renderer(org.apache.myfaces.config.element.Renderer) ClientBehaviorRenderer(org.apache.myfaces.config.element.ClientBehaviorRenderer) RenderKitFactory(jakarta.faces.render.RenderKitFactory)

Example 4 with RenderKit

use of jakarta.faces.render.RenderKit in project myfaces by apache.

the class UIComponentBase method getRenderer.

@Override
protected Renderer getRenderer(FacesContext context) {
    Assert.notNull(context, "context");
    Renderer renderer = getCachedRenderer();
    if (renderer != null) {
        return renderer;
    }
    String rendererType = getRendererType();
    if (rendererType == null) {
        return null;
    }
    RenderKit renderKit = context.getRenderKit();
    renderer = renderKit.getRenderer(getFamily(), rendererType);
    if (renderer == null) {
        String location = getComponentLocation(this);
        String logStr = "No Renderer found for component " + ComponentUtils.getPathToComponent(this) + " (component-family=" + getFamily() + ", renderer-type=" + rendererType + ')' + (location != null ? " created from: " + location : "");
        getFacesContext().getExternalContext().log(logStr);
        log.warning(logStr);
    }
    return renderer;
}
Also used : Renderer(jakarta.faces.render.Renderer) RenderKit(jakarta.faces.render.RenderKit)

Example 5 with RenderKit

use of jakarta.faces.render.RenderKit in project myfaces by apache.

the class RestoreViewExecutor method checkViewProtection.

private void checkViewProtection(FacesContext facesContext, ViewHandler viewHandler, String viewId, UIViewRoot root) throws ProtectedViewException {
    boolean valid = true;
    if (getViewIdSupport(facesContext).isViewProtected(facesContext, viewId)) {
        // "... Obtain the value of the value of the request parameter whose
        // name is given by the value of ResponseStateManager.NON_POSTBACK_VIEW_TOKEN_PARAM.
        // If there is no value, throw ProtectedViewException ..."
        String token = (String) facesContext.getExternalContext().getRequestParameterMap().get(ResponseStateManager.NON_POSTBACK_VIEW_TOKEN_PARAM);
        if (token != null && token.length() > 0) {
            String renderKitId = null;
            if (root != null) {
                renderKitId = root.getRenderKitId();
            }
            if (renderKitId == null) {
                renderKitId = viewHandler.calculateRenderKitId(facesContext);
            }
            RenderKit renderKit = getRenderKitFactory().getRenderKit(facesContext, renderKitId);
            ResponseStateManager rsm = renderKit.getResponseStateManager();
            String storedToken = rsm.getCryptographicallyStrongTokenFromSession(facesContext);
            if (token.equals(storedToken)) {
                if (!ExternalContextUtils.isPortlet(facesContext.getExternalContext())) {
                    // Any check beyond this point only has sense for servlet requests.
                    String referer = facesContext.getExternalContext().getRequestHeaderMap().get("Referer");
                    if (referer != null) {
                        valid = valid && checkRefererOrOriginHeader(facesContext, viewHandler, referer);
                    }
                    String origin = facesContext.getExternalContext().getRequestHeaderMap().get("Origin");
                    if (valid && origin != null) {
                        valid = valid && checkRefererOrOriginHeader(facesContext, viewHandler, origin);
                    }
                }
            } else {
                valid = false;
            }
        } else {
            valid = false;
        }
    }
    if (!valid) {
        throw new ProtectedViewException();
    }
}
Also used : ResponseStateManager(jakarta.faces.render.ResponseStateManager) RenderKit(jakarta.faces.render.RenderKit) ProtectedViewException(jakarta.faces.application.ProtectedViewException)

Aggregations

RenderKit (jakarta.faces.render.RenderKit)73 RenderKitFactory (jakarta.faces.render.RenderKitFactory)40 PrintWriter (java.io.PrintWriter)34 Renderer (jakarta.faces.render.Renderer)18 UIComponent (jakarta.faces.component.UIComponent)14 UIViewRoot (jakarta.faces.component.UIViewRoot)12 ResponseWriter (jakarta.faces.context.ResponseWriter)7 ClientBehaviorRenderer (jakarta.faces.render.ClientBehaviorRenderer)7 IOException (java.io.IOException)7 MockRenderKit (com.sun.faces.mock.MockRenderKit)6 ExternalContext (jakarta.faces.context.ExternalContext)5 ResponseStateManager (jakarta.faces.render.ResponseStateManager)5 FacesContext (jakarta.faces.context.FacesContext)4 ViewHandler (jakarta.faces.application.ViewHandler)3 ServletException (jakarta.servlet.ServletException)3 StringWriter (java.io.StringWriter)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 MessageUtils.getExceptionMessageString (com.sun.faces.util.MessageUtils.getExceptionMessageString)2 Bean (jakarta.enterprise.inject.spi.Bean)2