Search in sources :

Example 1 with WebsocketSessionBean

use of org.apache.myfaces.push.cdi.WebsocketSessionBean 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 2 with WebsocketSessionBean

use of org.apache.myfaces.push.cdi.WebsocketSessionBean in project myfaces by apache.

the class WebsocketConfigurator method modifyHandshake.

@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
    if (this.maxIdleTimeout != null) {
        sec.getUserProperties().put(MAX_IDLE_TIMEOUT, this.maxIdleTimeout);
    }
    String channelToken = request.getQueryString();
    if (channelToken == null) {
        String uri = request.getRequestURI().toString();
        if (uri.indexOf('?') >= 0) {
            channelToken = uri.substring(uri.indexOf('?') + 1);
        } else {
            channelToken = uri.substring(uri.lastIndexOf('/') + 1);
        }
    }
    BeanManager beanManager = CDI.current().getBeanManager();
    WebsocketSessionBean websocketSessionBean = CDIUtils.get(beanManager, WebsocketSessionBean.class, false);
    if (websocketSessionBean != null) {
        Serializable user = websocketSessionBean.getUserFromChannelToken(channelToken);
        if (user != null) {
            sec.getUserProperties().put(WEBSOCKET_USER, user);
        }
        sec.getUserProperties().put(WEBSOCKET_VALID, websocketSessionBean.isTokenValid(channelToken));
    } else {
        // Cannot validate session, mark as invalid
        sec.getUserProperties().put(WEBSOCKET_VALID, false);
    }
}
Also used : Serializable(java.io.Serializable) WebsocketSessionBean(org.apache.myfaces.push.cdi.WebsocketSessionBean) BeanManager(jakarta.enterprise.inject.spi.BeanManager)

Aggregations

BeanManager (jakarta.enterprise.inject.spi.BeanManager)2 WebsocketSessionBean (org.apache.myfaces.push.cdi.WebsocketSessionBean)2 FacesWrapper (jakarta.faces.FacesWrapper)1 UIWebsocket (jakarta.faces.component.UIWebsocket)1 ResponseWriter (jakarta.faces.context.ResponseWriter)1 Serializable (java.io.Serializable)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 WebsocketViewBean (org.apache.myfaces.push.cdi.WebsocketViewBean)1