Search in sources :

Example 1 with UIWebsocket

use of jakarta.faces.component.UIWebsocket in project myfaces by apache.

the class WebsocketHandler method onComponentCreated.

@Override
public void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent) {
    UIWebsocket component = (UIWebsocket) c;
    component.getAttributes().put(_WebsocketInit.ATTRIBUTE_COMPONENT_ID, ComponentSupport.getViewRoot(ctx, parent).createUniqueId() + "_wsinit");
}
Also used : UIWebsocket(jakarta.faces.component.UIWebsocket)

Example 2 with UIWebsocket

use of jakarta.faces.component.UIWebsocket in project myfaces by apache.

the class WebsocketComponentRenderer method processEvent.

@Override
public void processEvent(ComponentSystemEvent event) {
    if (event instanceof PostAddToViewEvent) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIWebsocket component = (UIWebsocket) event.getComponent();
        WebsocketInit initComponent = (WebsocketInit) facesContext.getViewRoot().findComponent((String) component.getAttributes().get(_WebsocketInit.ATTRIBUTE_COMPONENT_ID));
        if (initComponent == null) {
            initComponent = (WebsocketInit) facesContext.getApplication().createComponent(facesContext, WebsocketInit.COMPONENT_TYPE, WebsocketInit.COMPONENT_TYPE);
            initComponent.setId((String) component.getAttributes().get(_WebsocketInit.ATTRIBUTE_COMPONENT_ID));
            facesContext.getViewRoot().addComponentResource(facesContext, initComponent, "body");
        }
    }
}
Also used : PostAddToViewEvent(jakarta.faces.event.PostAddToViewEvent) UIWebsocket(jakarta.faces.component.UIWebsocket) FacesContext(jakarta.faces.context.FacesContext)

Example 3 with UIWebsocket

use of jakarta.faces.component.UIWebsocket 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 UIWebsocket

use of jakarta.faces.component.UIWebsocket in project mojarra by eclipse-ee4j.

the class WebsocketFacesListener method processEvent.

/**
 * If the websocket has just switched its <code>rendered</code> or <code>connected</code> attribute, then render either
 * the <code>open()</code> script or the <code>close()</code> script. During an ajax request with partial rendering,
 * it's added as <code>&lt;eval&gt;</code> by partial response writer, else it's just added as a script component with
 * <code>target="body"</code>.
 */
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
    if (!(event instanceof PreRenderViewEvent)) {
        return;
    }
    FacesContext context = ((ComponentSystemEvent) event).getFacesContext();
    Map<String, Boolean> initializedWebsockets = getInitializedWebsockets(context);
    if (!context.getPartialViewContext().isAjaxRequest()) {
        initializedWebsockets.clear();
    }
    for (Entry<String, Boolean> initializedWebsocket : initializedWebsockets.entrySet()) {
        String clientId = initializedWebsocket.getKey();
        UIWebsocket websocket = (UIWebsocket) context.getViewRoot().findComponent(clientId);
        boolean connected = websocket.isRendered() && websocket.isConnected();
        boolean previouslyConnected = initializedWebsocket.setValue(connected);
        if (previouslyConnected != connected) {
            String script = String.format(connected ? SCRIPT_OPEN : SCRIPT_CLOSE, clientId);
            PartialViewContext pvc = context.getPartialViewContext();
            if (pvc.isAjaxRequest() && !pvc.isRenderAll()) {
                context.getPartialViewContext().getEvalScripts().add(script);
            } else {
                UIOutput outputScript = new UIOutput();
                outputScript.setRendererType("jakarta.faces.resource.Script");
                UIOutput content = new UIOutput();
                content.setValue(script);
                outputScript.getChildren().add(content);
                context.getViewRoot().addComponentResource(context, outputScript, "body");
            }
        }
    }
}
Also used : UIWebsocket(jakarta.faces.component.UIWebsocket) FacesContext(jakarta.faces.context.FacesContext) UIOutput(jakarta.faces.component.UIOutput) ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) PartialViewContext(jakarta.faces.context.PartialViewContext) PreRenderViewEvent(jakarta.faces.event.PreRenderViewEvent)

Example 5 with UIWebsocket

use of jakarta.faces.component.UIWebsocket in project mojarra by eclipse-ee4j.

the class WebsocketRenderer method encodeEnd.

/**
 * Render <code>faces.push.init()</code> function if necessary.
 */
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    UIWebsocket websocket = (UIWebsocket) component;
    if (WebsocketFacesListener.isNew(context, websocket)) {
        WebsocketChannelManager websocketChannelManager = getBeanReference(WebsocketChannelManager.class);
        String clientId = websocket.getClientId(context);
        String channel = websocket.getChannel();
        String url = websocketChannelManager.register(context, channel, websocket.getScope(), websocket.getUser());
        String functions = websocket.getOnopen() + "," + websocket.getOnmessage() + "," + websocket.getOnerror() + "," + websocket.getOnclose();
        String behaviors = getBehaviorScripts(context, websocket);
        boolean connected = websocket.isConnected();
        RenderKitUtils.renderFacesJsIfNecessary(context);
        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("script", component);
        writer.writeAttribute("id", clientId, "id");
        writer.write(String.format(SCRIPT_INIT, clientId, url, channel, functions, behaviors, connected));
        writer.endElement("script");
    }
}
Also used : UIWebsocket(jakarta.faces.component.UIWebsocket) ResponseWriter(jakarta.faces.context.ResponseWriter) WebsocketChannelManager(com.sun.faces.push.WebsocketChannelManager)

Aggregations

UIWebsocket (jakarta.faces.component.UIWebsocket)5 FacesContext (jakarta.faces.context.FacesContext)2 ResponseWriter (jakarta.faces.context.ResponseWriter)2 WebsocketChannelManager (com.sun.faces.push.WebsocketChannelManager)1 BeanManager (jakarta.enterprise.inject.spi.BeanManager)1 FacesWrapper (jakarta.faces.FacesWrapper)1 UIOutput (jakarta.faces.component.UIOutput)1 PartialViewContext (jakarta.faces.context.PartialViewContext)1 ComponentSystemEvent (jakarta.faces.event.ComponentSystemEvent)1 PostAddToViewEvent (jakarta.faces.event.PostAddToViewEvent)1 PreRenderViewEvent (jakarta.faces.event.PreRenderViewEvent)1 WebsocketApplicationBean (org.apache.myfaces.push.cdi.WebsocketApplicationBean)1 WebsocketChannelMetadata (org.apache.myfaces.push.cdi.WebsocketChannelMetadata)1 WebsocketChannelTokenBuilderBean (org.apache.myfaces.push.cdi.WebsocketChannelTokenBuilderBean)1 WebsocketSessionBean (org.apache.myfaces.push.cdi.WebsocketSessionBean)1 WebsocketViewBean (org.apache.myfaces.push.cdi.WebsocketViewBean)1