}
if (null == response) {
throw new NullPointerException("Response parameter is null");
}
if (!isInitialized()) {
throw new BridgeException("JSF Portlet bridge is not initialized");
}
if (log.isLoggable(Level.FINE)) {
log.fine("Start bridge render request processing for portlet "
+ getPortletName());
}
BufferedRenderResponseWrapper wrappedResponse = new BufferedRenderResponseWrapper(
response);
initRequest(request, wrappedResponse, Bridge.PortletPhase.RenderPhase);
String namespace = wrappedResponse.getNamespace();
StateId stateId = getStateHolder().getStateId(getPortletName(),
request, namespace);
PortletWindowState windowState = getStateHolder().getWindowState(
stateId);
if (null == windowState) {
windowState = new PortletWindowState() {
@Override
public BridgeConfig getBridgeConfig() {
return AjaxPortletBridge.this;
}
};
getStateHolder().addWindowState(stateId, windowState);
}
PortletBridgeContext bridgeContext = createBridgeContext(request,
windowState);
bridgeContext.setStateId(stateId);
FacesContext facesContext = getFacesContext(request, wrappedResponse);
try {
windowState.restoreRequest(facesContext, true);
// If we're using RichFaces, setup proper parameters for this render
// request
if (RICHFACES_ENABLED) {
setupAjaxParams(facesContext, stateId.toString(), namespace);
}
// set portletbridge title if its set.
ResourceBundle bundle = portletConfig.getResourceBundle(request
.getLocale());
if (bundle != null) {
String title = null;
try {
title = bundle.getString("javax.portlet.title");
wrappedResponse.setTitle(title);
} catch (Exception e) {
// Ignore MissingResourceException
}
}
try {
renderResponse(facesContext, windowState);
// TODO - detect redirect case. Reset response, clear request
// variables as far as Seam state.
// Perform new render phase with a new ViewId.
String redirectViewId = bridgeContext.getRedirectViewId();
if (null != redirectViewId) {
windowState.reset();
windowState.setViewId(redirectViewId);
Map<String, String[]> redirectParams = bridgeContext
.getRedirectRequestParameters();
// release old FacesContext.
facesContext.release();
// Reset attributes to initial state
Set<String> initialAttributes = bridgeContext
.getInitialRequestAttributeNames();
List<String> currentAttributes = Collections.list(request
.getAttributeNames());
currentAttributes.removeAll(initialAttributes);
for (Object newAttribute : currentAttributes) {
request.removeAttribute((String) newAttribute);
}
if (redirectParams != null) {
windowState.setRequestParameters(redirectParams);
}
// Create new FacesContext
facesContext = getFacesContext(request, wrappedResponse);
ViewHandler viewHandler = facesContext.getApplication()
.getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(facesContext,
redirectViewId);
facesContext.setViewRoot(viewRoot);
renderResponse(facesContext, windowState);
}
windowState.setViewId(facesContext.getViewRoot().getViewId());
} catch (Exception e) {
wrappedResponse.reset();
log.log(Level.SEVERE, "Error processing execute lifecycle", e);
exceptionHandler.processRenderException(facesContext,
windowState, e);
}
// Set important Portal parameters to window state.
String viewId = facesContext.getViewRoot().getViewId();
//
String actionURL = facesContext.getApplication().getViewHandler()
.getActionURL(facesContext, viewId);
actionURL = facesContext.getExternalContext().encodeActionURL(
actionURL);
windowState.setPortalActionURL(new PortalActionURL(actionURL));
PortletURL portletURL = wrappedResponse.createRenderURL();
portletURL.setParameter(PortletStateHolder.STATE_ID_PARAMETER,
stateId.toString());
String renderUrl = portletURL.toString();
windowState.setPortalRenderURL(new PortalActionURL(renderUrl));
windowState.setNamespace(namespace);
windowState.setPortletLocale(request.getLocale());
// TODO - encode request attributes, portlet mode and windowId, as
// required by JSR-301 5.3.3
String portletModeName = request.getPortletMode().toString();
PortalActionURL historyViewId = new PortalActionURL(viewId);
historyViewId.setParameter(PortletStateHolder.STATE_ID_PARAMETER,
stateId.toString());
historyViewId.setParameter(Bridge.PORTLET_MODE_PARAMETER,
portletModeName);
facesContext.getExternalContext().getSessionMap().put(
VIEWID_HISTORY_PREFIX + portletModeName,
historyViewId.toString());
// writer.println("</div>");
windowState.saveSeamConversationId(facesContext);
// PortletSession portletSession = request.getPortletSession(true);
PortletSession portletSession = (PortletSession) facesContext
.getExternalContext().getSession(true);
WindowIDRetriver idRetriver = (WindowIDRetriver) portletSession
.getAttribute(PortletStateHolder.WINDOW_ID_RETRIVER);
if (null != idRetriver) {
windowState.setWindowId(idRetriver.getWindowID());
}
PortletBridgePrincipal security = new PortletBridgePrincipal(request,userRoles);
// TODO - get user roles, defined in the portletbridge.xml ( ???
// parse it ??? ), and store all values for a
// "isUserInRole(roleName)" calls
portletSession.setAttribute(
AbstractExternalContext.PORTAL_USER_PRINCIPAL,
security, PortletSession.APPLICATION_SCOPE);
if (log.isLoggable(Level.FINE)) {
log.fine("Finish rendering portletbridge for namespace "
+ namespace);
}
// Disable portletbridge caching.
// TODO - detect ajax components on page, static views can be
// cached.
wrappedResponse.setProperty(RenderResponse.EXPIRATION_CACHE, "0");
if (RICHFACES_ENABLED) {
richFacesHelper
.getCurrentInstance();
Object headEvents = request.getAttribute(AjaxContext.HEAD_EVENTS_PARAMETER);
if (headEvents != null) {
Node[] nodes = (Node[]) headEvents;
Properties xhtmlProperties = OutputPropertiesFactory
.getDefaultMethodProperties(Method.XHTML);
Serializer serializer = SerializerFactory
.getSerializer(xhtmlProperties);
if (wrappedResponse.isUseWriter()) {
serializer.setWriter(response.getWriter());
} else {
serializer.setOutputStream(response
.getPortletOutputStream());
}
ContentHandler contentHandler = serializer
.asContentHandler();
TreeWalker treeWalker = new TreeWalker(contentHandler);
contentHandler.startDocument();
for (Node node : nodes) {
treeWalker.traverseFragment(node);
}
contentHandler.endDocument();
}
}
wrappedResponse.writeBufferedData();
} catch (Exception e) {
throw new BridgeException(e);
} finally {
facesContext.release();
}
}