use of org.apache.myfaces.push.cdi.WebsocketViewBean 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());
}
}
Aggregations