Search in sources :

Example 1 with FacesWrapper

use of jakarta.faces.FacesWrapper in project myfaces by apache.

the class ApplicationImpl method createBehavior.

@Override
public Behavior createBehavior(String behaviorId) throws FacesException {
    Assert.notEmpty(behaviorId, "behaviorId");
    final Class<? extends Behavior> behaviorClass = getObjectFromClassMap(behaviorId, _behaviorClassMap);
    if (behaviorClass == null) {
        throw new FacesException("Could not find any registered behavior-class for behaviorId : " + behaviorId);
    }
    try {
        if (!_cdiManagedBehaviorMap.containsKey(behaviorClass)) {
            FacesBehavior annotation = behaviorClass.getAnnotation(FacesBehavior.class);
            if (annotation != null && annotation.managed()) {
                _cdiManagedBehaviorMap.put(behaviorClass, true);
            } else {
                _cdiManagedBehaviorMap.put(behaviorClass, false);
            }
        }
        boolean managed = _cdiManagedBehaviorMap.get(behaviorClass);
        Behavior behavior = null;
        if (managed) {
            if (ClientBehavior.class.isAssignableFrom(behaviorClass)) {
                behavior = new FacesClientBehaviorCDIWrapper((Class<ClientBehavior>) behaviorClass, behaviorId);
            } else {
                behavior = new FacesBehaviorCDIWrapper(behaviorClass, behaviorId);
            }
            Behavior innerBehavior = ((FacesWrapper<Behavior>) behavior).getWrapped();
            FacesContext facesContext = getFacesContext();
            _handleAttachedResourceDependencyAnnotations(facesContext, innerBehavior);
            if (innerBehavior instanceof ClientBehaviorBase) {
                ClientBehaviorBase clientBehavior = (ClientBehaviorBase) innerBehavior;
                String renderType = clientBehavior.getRendererType();
                if (renderType != null) {
                    ClientBehaviorRenderer cbr = facesContext.getRenderKit().getClientBehaviorRenderer(renderType);
                    _handleAttachedResourceDependencyAnnotations(facesContext, cbr);
                }
            }
        } else {
            behavior = behaviorClass.newInstance();
            FacesContext facesContext = getFacesContext();
            _handleAttachedResourceDependencyAnnotations(facesContext, behavior);
            if (behavior instanceof ClientBehaviorBase) {
                ClientBehaviorBase clientBehavior = (ClientBehaviorBase) behavior;
                String renderType = clientBehavior.getRendererType();
                if (renderType != null) {
                    ClientBehaviorRenderer cbr = facesContext.getRenderKit().getClientBehaviorRenderer(renderType);
                    _handleAttachedResourceDependencyAnnotations(facesContext, cbr);
                }
            }
        }
        return behavior;
    } catch (Exception e) {
        log.log(Level.SEVERE, "Could not instantiate behavior " + behaviorClass, e);
        throw new FacesException("Could not instantiate behavior: " + behaviorClass, e);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FacesBehaviorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesBehaviorCDIWrapper) FacesWrapper(jakarta.faces.FacesWrapper) FacesException(jakarta.faces.FacesException) MissingResourceException(java.util.MissingResourceException) NamingException(javax.naming.NamingException) ELException(jakarta.el.ELException) FacesException(jakarta.faces.FacesException) ClientBehaviorRenderer(jakarta.faces.render.ClientBehaviorRenderer) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase) FacesClientBehaviorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesClientBehaviorCDIWrapper) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior) Behavior(jakarta.faces.component.behavior.Behavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior)

Example 2 with FacesWrapper

use of jakarta.faces.FacesWrapper in project myfaces by apache.

the class FlowHandlerImpl method getActiveFlows.

public static List<Flow> getActiveFlows(FacesContext facesContext, FlowHandler fh) {
    FlowHandler flowHandler = fh;
    while (flowHandler != null) {
        if (flowHandler instanceof FlowHandlerImpl) {
            break;
        } else if (flowHandler instanceof FacesWrapper) {
            flowHandler = ((FacesWrapper<FlowHandler>) flowHandler).getWrapped();
        } else {
            flowHandler = null;
        }
    }
    if (flowHandler == null) {
        // Use the standard form
        Flow curFlow = fh.getCurrentFlow(facesContext);
        if (curFlow != null) {
            List<Flow> activeFlows = new ArrayList<Flow>();
            while (curFlow != null) {
                activeFlows.add(curFlow);
                fh.pushReturnMode(facesContext);
                curFlow = fh.getCurrentFlow(facesContext);
            }
            for (int i = 0; i < activeFlows.size(); i++) {
                fh.popReturnMode(facesContext);
            }
            return activeFlows;
        } else {
            return Collections.emptyList();
        }
    } else {
        FlowHandlerImpl flowHandlerImpl = (FlowHandlerImpl) flowHandler;
        return flowHandlerImpl.getActiveFlows(facesContext);
    }
}
Also used : FacesWrapper(jakarta.faces.FacesWrapper) ArrayList(java.util.ArrayList) FlowHandler(jakarta.faces.flow.FlowHandler) Flow(jakarta.faces.flow.Flow)

Example 3 with FacesWrapper

use of jakarta.faces.FacesWrapper in project myfaces by apache.

the class WebsocketComponentRenderer method encodeEnd.

@Override
public void encodeEnd(FacesContext facesContext, UIComponent c) throws IOException {
    // check for NP
    super.encodeEnd(facesContext, c);
    UIWebsocket component = (UIWebsocket) c;
    WebsocketInit init = (WebsocketInit) facesContext.getViewRoot().findComponent((String) component.getAttributes().get(_WebsocketInit.ATTRIBUTE_COMPONENT_ID));
    ResponseWriter writer = facesContext.getResponseWriter();
    String channel = component.getChannel();
    // TODO: use a single bean and entry point for this algorithm.
    BeanManager beanManager = CDIUtils.getBeanManager(facesContext);
    WebsocketChannelTokenBuilderBean channelTokenBean = CDIUtils.get(beanManager, WebsocketChannelTokenBuilderBean.class);
    // This bean is required because you always need to register the token, so it can be properly destroyed
    WebsocketViewBean viewTokenBean = CDIUtils.get(beanManager, WebsocketViewBean.class);
    WebsocketSessionBean sessionTokenBean = CDIUtils.get(beanManager, WebsocketSessionBean.class);
    // Create channel token
    // TODO: Use ResponseStateManager to create the token
    String scope = component.getScope() == null ? "application" : component.getScope();
    WebsocketChannelMetadata metadata = new WebsocketChannelMetadata(channel, scope, component.getUser(), component.isConnected());
    String channelToken = null;
    // creation
    if (!component.isConnected()) {
        channelToken = viewTokenBean.getChannelToken(metadata);
    }
    if (channelToken == null) {
        // No channel token found for that combination, create a new token for this view
        channelToken = channelTokenBean.createChannelToken(facesContext, channel);
        // Register channel in view scope to chain discard view algorithm using @PreDestroy
        viewTokenBean.registerToken(channelToken, metadata);
        // Register channel in session scope to allow validation on handshake ( WebsocketConfigurator )
        sessionTokenBean.registerToken(channelToken, metadata);
    }
    // Ask these two scopes
    WebsocketApplicationBean appTokenBean = CDIUtils.get(beanManager, WebsocketApplicationBean.class, false);
    // Register token and metadata in the proper bean
    if (scope.equals("view")) {
        viewTokenBean.registerWebsocketSession(channelToken, metadata);
    } else if (scope.equals("session")) {
        sessionTokenBean = (sessionTokenBean != null) ? sessionTokenBean : CDIUtils.get(CDIUtils.getBeanManager(facesContext), WebsocketSessionBean.class);
        sessionTokenBean.registerWebsocketSession(channelToken, metadata);
    } else {
        // Default application
        appTokenBean = (appTokenBean != null) ? appTokenBean : CDIUtils.get(CDIUtils.getBeanManager(facesContext), WebsocketApplicationBean.class);
        appTokenBean.registerWebsocketSession(channelToken, metadata);
    }
    writer.startElement(HTML.SCRIPT_ELEM, component);
    HtmlRendererUtils.renderScriptType(facesContext, writer);
    StringBuilder sb = new StringBuilder(50);
    sb.append("faces.push.init(");
    sb.append('\'');
    sb.append(component.getClientId());
    sb.append('\'');
    sb.append(',');
    sb.append('\'');
    sb.append(facesContext.getExternalContext().encodeWebsocketURL(facesContext.getApplication().getViewHandler().getWebsocketURL(facesContext, component.getChannel() + '?' + channelToken)));
    sb.append('\'');
    sb.append(',');
    sb.append('\'');
    sb.append(component.getChannel());
    sb.append('\'');
    sb.append(',');
    sb.append(component.getOnopen());
    sb.append(',');
    sb.append(component.getOnmessage());
    sb.append(',');
    sb.append(component.getOnerror());
    sb.append(',');
    sb.append(component.getOnclose());
    sb.append(',');
    sb.append(getBehaviorScripts(facesContext, component));
    sb.append(',');
    sb.append(component.isConnected());
    sb.append(");");
    writer.write(sb.toString());
    writer.endElement(HTML.SCRIPT_ELEM);
    if (!facesContext.getPartialViewContext().isAjaxRequest()) {
        ResponseWriter responseWriter = facesContext.getResponseWriter();
        while (!(responseWriter instanceof HtmlBufferResponseWriterWrapper) && responseWriter instanceof FacesWrapper) {
            responseWriter = (ResponseWriter) ((FacesWrapper) responseWriter).getWrapped();
        }
        HtmlBufferResponseWriterWrapper htmlBufferResponseWritter = (HtmlBufferResponseWriterWrapper) responseWriter;
        init.getUIWebsocketMarkupList().add(htmlBufferResponseWritter.toString());
        facesContext.setResponseWriter(htmlBufferResponseWritter.getInitialWriter());
    }
}
Also used : WebsocketApplicationBean(org.apache.myfaces.push.cdi.WebsocketApplicationBean) WebsocketChannelTokenBuilderBean(org.apache.myfaces.push.cdi.WebsocketChannelTokenBuilderBean) WebsocketChannelMetadata(org.apache.myfaces.push.cdi.WebsocketChannelMetadata) FacesWrapper(jakarta.faces.FacesWrapper) WebsocketViewBean(org.apache.myfaces.push.cdi.WebsocketViewBean) UIWebsocket(jakarta.faces.component.UIWebsocket) ResponseWriter(jakarta.faces.context.ResponseWriter) WebsocketSessionBean(org.apache.myfaces.push.cdi.WebsocketSessionBean) BeanManager(jakarta.enterprise.inject.spi.BeanManager)

Example 4 with FacesWrapper

use of jakarta.faces.FacesWrapper in project myfaces by apache.

the class StateCacheServerSide method saveSerializedViewInSession.

protected void saveSerializedViewInSession(FacesContext context, Object serializedView) {
    Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
    SerializedViewCollection viewCollection = (SerializedViewCollection) sessionMap.get(SERIALIZED_VIEW_SESSION_ATTR);
    if (viewCollection == null) {
        viewCollection = sessionViewStorageFactory.createSerializedViewCollection(context);
        sessionMap.put(SERIALIZED_VIEW_SESSION_ATTR, viewCollection);
    }
    Map<Object, Object> attributeMap = context.getAttributes();
    SerializedViewKey key = null;
    if (numberOfSequentialViewsInSession > 0) {
        key = (SerializedViewKey) attributeMap.get(RESTORED_VIEW_KEY_REQUEST_ATTR);
        if (key == null) {
            // Check if clientWindow is enabled and if the last view key is stored
            // into session, so we can use it to chain the precedence in GET-GET
            // cases.
            ClientWindow clientWindow = context.getExternalContext().getClientWindow();
            if (clientWindow != null) {
                key = (SerializedViewKey) viewCollection.getLastWindowKey(context, clientWindow.getId());
            } else if (useFlashScopePurgeViewsInSession && Boolean.TRUE.equals(context.getExternalContext().getRequestMap().get(FlashImpl.FLASH_PREVIOUS_REQUEST_REDIRECT))) {
                key = (SerializedViewKey) context.getExternalContext().getFlash().get(RESTORED_VIEW_KEY_REQUEST_ATTR);
            }
        }
    }
    SerializedViewKey nextKey = sessionViewStorageFactory.createSerializedViewKey(context, context.getViewRoot().getViewId(), getNextViewSequence(context));
    // Get viewScopeMapId
    ViewScopeProxyMap viewScopeProxyMap = null;
    Object viewMap = context.getViewRoot().getViewMap(false);
    if (viewMap != null) {
        while (viewMap != null) {
            if (viewMap instanceof ViewScopeProxyMap) {
                viewScopeProxyMap = (ViewScopeProxyMap) viewMap;
                break;
            } else if (viewMap instanceof FacesWrapper) {
                viewMap = ((FacesWrapper) viewMap).getWrapped();
            }
        }
    }
    if (viewScopeProxyMap != null) {
        viewCollection.put(context, serializeView(context, serializedView), nextKey, key, viewScopeProxyMap.getViewScopeId());
    } else {
        viewCollection.put(context, serializeView(context, serializedView), nextKey, key);
    }
    ClientWindow clientWindow = context.getExternalContext().getClientWindow();
    if (clientWindow != null) {
        // Update the last key generated for the current windowId in session map
        viewCollection.putLastWindowKey(context, clientWindow.getId(), nextKey);
    }
    // replace the value to notify the container about the change
    sessionMap.put(SERIALIZED_VIEW_SESSION_ATTR, viewCollection);
}
Also used : ViewScopeProxyMap(org.apache.myfaces.view.ViewScopeProxyMap) ClientWindow(jakarta.faces.lifecycle.ClientWindow) FacesWrapper(jakarta.faces.FacesWrapper)

Example 5 with FacesWrapper

use of jakarta.faces.FacesWrapper in project myfaces by apache.

the class FacesConfigurator method _callInjectAndPostConstruct.

private void _callInjectAndPostConstruct(Object instance) {
    try {
        // invoke the injection over the inner one first
        if (instance instanceof FacesWrapper) {
            Object innerInstance = ((FacesWrapper) instance).getWrapped();
            if (innerInstance != null) {
                _callInjectAndPostConstruct(innerInstance);
            }
        }
        List<BeanEntry> injectedBeanStorage = (List<BeanEntry>) _externalContext.getApplicationMap().get(FacesInitializerImpl.INJECTED_BEAN_STORAGE_KEY);
        Object creationMetaData = getInjectionProvider().inject(instance);
        injectedBeanStorage.add(new BeanEntry(instance, creationMetaData));
        getInjectionProvider().postConstruct(instance, creationMetaData);
    } catch (InjectionProviderException ex) {
        log.log(Level.INFO, "Exception on Injection or PostConstruct", ex);
    }
}
Also used : InjectionProviderException(org.apache.myfaces.spi.InjectionProviderException) FacesWrapper(jakarta.faces.FacesWrapper) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BeanEntry(org.apache.myfaces.cdi.util.BeanEntry)

Aggregations

FacesWrapper (jakarta.faces.FacesWrapper)5 ArrayList (java.util.ArrayList)2 ELException (jakarta.el.ELException)1 BeanManager (jakarta.enterprise.inject.spi.BeanManager)1 FacesException (jakarta.faces.FacesException)1 UIWebsocket (jakarta.faces.component.UIWebsocket)1 Behavior (jakarta.faces.component.behavior.Behavior)1 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)1 ClientBehaviorBase (jakarta.faces.component.behavior.ClientBehaviorBase)1 FacesBehavior (jakarta.faces.component.behavior.FacesBehavior)1 FacesContext (jakarta.faces.context.FacesContext)1 ResponseWriter (jakarta.faces.context.ResponseWriter)1 Flow (jakarta.faces.flow.Flow)1 FlowHandler (jakarta.faces.flow.FlowHandler)1 ClientWindow (jakarta.faces.lifecycle.ClientWindow)1 ClientBehaviorRenderer (jakarta.faces.render.ClientBehaviorRenderer)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 NamingException (javax.naming.NamingException)1