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);
}
}
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);
}
}
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());
}
}
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);
}
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);
}
}
Aggregations