Author: alexsmirnov
Date: 2007-10-31 21:00:16 -0400 (Wed, 31 Oct 2007)
New Revision: 3660
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/AjaxPortletBridge.java
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewHandler.java
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/AbstractExternalContext.java
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/PortletContextImpl.java
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/ServletContextImpl.java
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/lifecycle/PortalPhaseListener.java
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/ContextFactoryTest.java
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/PortletExternalContextTest.java
Log:
Continue to implement JSR-301 bridge. Finish requrements from PLT 5.
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/AjaxPortletBridge.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/AjaxPortletBridge.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/AjaxPortletBridge.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -4,6 +4,7 @@
package org.ajax4jsf.portlet;
import java.io.PrintWriter;
+import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.FacesException;
@@ -35,7 +36,7 @@
public class AjaxPortletBridge extends AbstractAjaxBridge implements Bridge {
private static final Log log = LogFactory.getLog(AjaxPortletBridge.class);
- private PortletConfig portletConfig;
+ private PortletConfig portletConfig;
/*
* (non-Javadoc)
@@ -76,14 +77,17 @@
throws BridgeException {
initRequest(request, response, Bridge.PortletPhase.ActionPhase);
FacesContext facesContext = getFacesContext(request, response);
- PortletViewState windowState =
PortletStateHolder.getInstance(facesContext).getWindowState(facesContext);
- request.setAttribute(Bridge.IS_POSTBACK_ATTRIBUTE, Boolean.valueOf(null !=
windowState.getViewId()));
+ PortletViewState windowState = PortletStateHolder.getInstance(
+ facesContext).getWindowState(facesContext);
+ windowState.restoreRequest(facesContext, false);
+ // request.setAttribute(Bridge.IS_POSTBACK_ATTRIBUTE,
+ // Boolean.valueOf(null != windowState.getViewId()));
try {
execute(facesContext);
- facesContext.getApplication().getStateManager().saveSerializedView(facesContext);
- // save request scope variables and Faces Messages.
- windowState.saveMessages(facesContext);
- windowState.saveBeans(facesContext);
+ facesContext.getApplication().getStateManager().saveSerializedView(
+ facesContext);
+ // save request scope variables and Faces Messages.
+ windowState.saveRequest(facesContext);
// saveView(facesContext);
} catch (Exception e) {
log.error("Error processing execute lifecycle", e);
@@ -110,46 +114,46 @@
response.setContentType(contenttype);
}
- // set portlet title if its set.
- ResourceBundle bundle =
- portletConfig.getResourceBundle(request.getLocale());
- if (bundle != null) {
- String title = null;
- try {
- title = bundle.getString("javax.portlet.title");
- response.setTitle(title);
- } catch (Exception e) {
- // Ignore MissingResourceException
- }
- }
-// PrintWriter writer = response.getWriter();
+ // set portlet title if its set.
+ ResourceBundle bundle = portletConfig.getResourceBundle(request
+ .getLocale());
+ if (bundle != null) {
+ String title = null;
+ try {
+ title = bundle.getString("javax.portlet.title");
+ response.setTitle(title);
+ } catch (Exception e) {
+ // Ignore MissingResourceException
+ }
+ }
+ // PrintWriter writer = response.getWriter();
String namespace = response.getNamespace();
// Write anchor for update portlet pages by ajax.
// TODO - configure html element and style/class.
-// writer.println("<div id='" + namespace + "'>");
+ // writer.println("<div id='" + namespace + "'>");
// writer.flush();
+ PortletViewState windowState = PortletStateHolder.getInstance(
+ facesContext).getWindowState(facesContext);
+ windowState.restoreRequest(facesContext, true);
+ if (null == facesContext.getViewRoot()) {
+ execute(facesContext);
+ }
+ //
AjaxContext ajaxContext = AjaxContext
.getCurrentInstance(facesContext);
- ajaxContext.getCommonAjaxParameters().put(
- AbstractExternalContext.ACTION__PARAMETER,
+ Map commonAjaxParameters = ajaxContext.getCommonAjaxParameters();
+ commonAjaxParameters.put(AbstractExternalContext.ACTION__PARAMETER,
response.createActionURL().toString());
- ajaxContext.getCommonAjaxParameters().put(
- AbstractExternalContext.PORTLET_MODE_PARAMETER,
- request.getPortletMode().toString());
- ajaxContext.getCommonAjaxParameters().put(
+ commonAjaxParameters.put(
+ AbstractExternalContext.PORTLET_MODE_PARAMETER, request
+ .getPortletMode().toString());
+ commonAjaxParameters.put(
AbstractExternalContext.NAMESPACE_PARAMETER, namespace);
- ajaxContext.getCommonAjaxParameters().put(
- AbstractExternalContext.PORTLET_NAME_PARAMETER,
getPortletConfig().getPortletName());
- PortletViewState windowState =
PortletStateHolder.getInstance(facesContext).getWindowState(facesContext);
- request.setAttribute(Bridge.IS_POSTBACK_ATTRIBUTE, Boolean.valueOf(null !=
windowState.getViewId()));
- windowState.restoreMessages(facesContext);
- windowState.restoreBeans(facesContext);
- if(null == facesContext.getViewRoot()){
- execute(facesContext);
- }
-//
+ commonAjaxParameters.put(
+ AbstractExternalContext.PORTLET_NAME_PARAMETER,
+ getPortletConfig().getPortletName());
render(facesContext);
-// writer.println("</div>");
+ // writer.println("</div>");
PortletSession portletSession = request.getPortletSession(true);
// Store namespace value in portlet scope session.
// ServletContext, in case of ajax requests,
@@ -182,11 +186,12 @@
protected void initRequest(PortletRequest request,
PortletResponse response, PortletPhase actionPhase)
throws BridgeException {
- if( null == request.getAttribute(Bridge.DEFAULT_VIEWID)){
- throw new BridgeDefaultViewNotSpecifiedException();
- }
- request.setAttribute(Bridge.PORTLET_LIFECYCLE_PHASE, actionPhase);
- request.setAttribute(AbstractExternalContext.PORTLET_CONFIG_ATTRIBUTE,
getPortletConfig());
+ if (null == request.getAttribute(Bridge.DEFAULT_VIEWID)) {
+ throw new BridgeDefaultViewNotSpecifiedException();
+ }
+ request.setAttribute(Bridge.PORTLET_LIFECYCLE_PHASE, actionPhase);
+ request.setAttribute(AbstractExternalContext.PORTLET_CONFIG_ATTRIBUTE,
+ getPortletConfig());
}
/**
@@ -205,8 +210,9 @@
@Override
protected String getInitParameter(String name) {
String initParameter = portletConfig.getInitParameter(name);
- if(null == initParameter){
- initParameter = portletConfig.getPortletContext().getInitParameter(name);
+ if (null == initParameter) {
+ initParameter = portletConfig.getPortletContext().getInitParameter(
+ name);
}
return initParameter;
}
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewHandler.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewHandler.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewHandler.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -4,15 +4,21 @@
package org.ajax4jsf.portlet.application;
import java.io.IOException;
+import java.io.Writer;
import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
+import javax.faces.application.StateManager;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
-import javax.portlet.PortletURL;
+import javax.faces.context.ResponseWriter;
+import javax.faces.render.RenderKit;
+import javax.faces.render.RenderKitFactory;
import javax.portlet.RenderResponse;
import javax.portlet.faces.Bridge;
+import javax.servlet.ServletContext;
import org.ajax4jsf.application.AjaxViewHandler;
import org.ajax4jsf.component.AjaxViewRoot;
@@ -50,17 +56,7 @@
return root;
}
- public void renderView(FacesContext context, UIViewRoot root)
- throws IOException, FacesException {
- super.renderView(context, root);
- // Save view to use in portlet rendering phases
- // PortletViewState state = new PortletViewState();
- // state.save(context);
- // context.getExternalContext().getSessionMap().put(
- // PortletViewState.SAVED_VIEW_ATTRIBUTE, state);
- }
-
public String getActionURL(FacesContext context, String url) {
String actionURL = super.getActionURL(context, url);
if( null !=
context.getExternalContext().getRequestMap().get(Bridge.PORTLET_LIFECYCLE_PHASE)){
@@ -72,4 +68,270 @@
protected synchronized void fillChain(FacesContext context) {
// Do nothing - chain must be filled in AjaxViewHandler
}
+
+ // TODO - create own implementation. Now, this code are copied from MyFaces
implementation.
+ @Override
+ public void renderView(FacesContext context, UIViewRoot viewToRender) throws
IOException,
+ FacesException
+ {
+ // Get the renderPolicy from the requestScope
+ Bridge.BridgeRenderPolicy renderPolicy = (Bridge.BridgeRenderPolicy) context
+
.getExternalContext()
+
.getRequestMap()
+ .get(
+
AbstractExternalContext.RENDER_POLICY_ATTRIBUTE);
+
+ if (renderPolicy == null)
+ {
+ renderPolicy = Bridge.BridgeRenderPolicy.valueOf("DEFAULT");
+ }
+
+ if (context.getExternalContext().getContext() instanceof ServletContext
+ || renderPolicy == Bridge.BridgeRenderPolicy.ALWAYS_DELEGATE)
+ {
+ super.renderView(context, viewToRender);
+ return;
+ }
+ else if (renderPolicy == Bridge.BridgeRenderPolicy.DEFAULT)
+ {
+ try
+ {
+ super.renderView(context, viewToRender);
+ return;
+ }
+ catch (Throwable t)
+ {
+ // catch all throws and swallow -- falling through to our own
+ // render
+ }
+ }
+
+ // suppress rendering if "rendered" property on the component is
+ // false
+ if (!viewToRender.isRendered())
+ {
+ return;
+ }
+
+ ExternalContext extContext = context.getExternalContext();
+ RenderResponse renderResponse = (RenderResponse) extContext.getResponse();
+
+ try
+ {
+
+ // set request attribute indicating we can deal with content
+ // that is supposed to be delayed until after JSF tree is ouput.
+ extContext.getRequestMap().put(Bridge.RENDER_CONTENT_AFTER_VIEW, Boolean.TRUE);
+ // TODO JSF 1.2 - executePageToBuildView() creates
+ // ViewHandlerResponseWrapper
+ // to handle error page and text that exists after the <f:view> tag
+ // among other things which have lots of servlet dependencies -
+ // we're skipping this for now for portlet
+ extContext.dispatch(viewToRender.getViewId());
+ /*
+ * if (executePageToBuildView(context, viewToRender)) { response.flushBuffer();
return; }
+ */
+ }
+ catch (IOException e)
+ {
+ throw new FacesException(e);
+ }
+
+ // set up the ResponseWriter
+ RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder
+
.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
+ RenderKit renderKit = renderFactory.getRenderKit(context,
viewToRender.getRenderKitId());
+
+ ResponseWriter oldWriter = context.getResponseWriter();
+ StringBuilderWriter strWriter = new StringBuilderWriter(context, 4096);
+ ResponseWriter newWriter;
+ if (null != oldWriter)
+ {
+ newWriter = oldWriter.cloneWithWriter(strWriter);
+ }
+ else
+ {
+ newWriter = renderKit.createResponseWriter(strWriter, null,
+
renderResponse.getCharacterEncoding());
+ }
+ context.setResponseWriter(newWriter);
+
+ newWriter.startDocument();
+
+ doRenderView(context, viewToRender);
+
+ newWriter.endDocument();
+
+ // replace markers in the body content and write it to response.
+
+ ResponseWriter responseWriter;
+ if (null != oldWriter)
+ {
+ responseWriter = oldWriter.cloneWithWriter(renderResponse.getWriter());
+ }
+ else
+ {
+ responseWriter = newWriter.cloneWithWriter(renderResponse.getWriter());
+ }
+ context.setResponseWriter(responseWriter);
+
+ strWriter.write(responseWriter);
+
+ if (null != oldWriter)
+ {
+ context.setResponseWriter(oldWriter);
+ }
+
+ Object content = extContext.getRequestMap().get(Bridge.AFTER_VIEW_CONTENT);
+ if (content != null)
+ {
+ if (content instanceof char[])
+ {
+ renderResponse.getWriter().write(new String((byte[]) content));
+ }
+ else if (content instanceof byte[])
+ {
+ renderResponse.getWriter().write(new String((char[]) content));
+ }
+ else
+ {
+ throw new IOException("PortletViewHandlerImpl: invalid" +
"AFTER_VIEW_CONTENT buffer type");
+ }
+ }
+ renderResponse.flushBuffer();
+ }
+
+ /**
+ * <p>
+ * This is a separate method to account for handling the content after the view tag.
+ * </p>
+ *
+ * <p>
+ * Create a new ResponseWriter around this response's Writer. Set it into the
FacesContext, saving
+ * the old one aside.
+ * </p>
+ *
+ * <p>
+ * call encodeBegin(), encodeChildren(), encodeEnd() on the argument
<code>UIViewRoot</code>.
+ * </p>
+ *
+ * <p>
+ * Restore the old ResponseWriter into the FacesContext.
+ * </p>
+ *
+ * <p>
+ * Write out the after view content to the response's writer.
+ * </p>
+ *
+ * <p>
+ * Flush the response buffer, and remove the after view content from the request
scope.
+ * </p>
+ *
+ * @param context
+ * the <code>FacesContext</code> for the current request
+ * @param viewToRender
+ * the view to render
+ * @throws IOException
+ * if an error occurs rendering the view to the client
+ */
+ private void doRenderView(FacesContext context, UIViewRoot viewToRender) throws
IOException,
+
FacesException
+ {
+ ExternalContext extContext = context.getExternalContext();
+ viewToRender.encodeAll(context);
+ }
+
+ private static final class StringBuilderWriter extends Writer
+ {
+ private StringBuilder mBuilder;
+ private FacesContext mContext;
+
+ // TODO: These bridge needs to use it's own constants here. This will
+ // confine
+ // us to only work with the R.I.
+ private static final String SAVESTATE_FIELD_MARKER =
"~com.sun.faces.saveStateFieldMarker~";
+
+ public StringBuilderWriter(FacesContext context, int initialCapacity)
+ {
+ if (initialCapacity < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ mBuilder = new StringBuilder(initialCapacity);
+ mContext = context;
+ }
+
+ @Override
+ public void write(char[] cbuf, int off, int len) throws IOException
+ {
+ if (off < 0 || off > cbuf.length || len < 0 || off + len > cbuf.length
|| off + len < 0)
+ {
+ throw new IndexOutOfBoundsException();
+ }
+ else if (len == 0)
+ {
+ return;
+ }
+ mBuilder.append(cbuf, off, len);
+ }
+
+ @Override
+ public void flush() throws IOException
+ {
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ }
+
+ /**
+ * Write a string.
+ *
+ * @param str
+ * String to be written
+ */
+ @Override
+ public void write(String str)
+ {
+ mBuilder.append(str);
+ }
+
+ @Override
+ public void write(String str, int off, int len)
+ {
+ write(str.substring(off, off + len));
+ }
+
+ public StringBuilder getBuffer()
+ {
+ return mBuilder;
+ }
+
+ @Override
+ public String toString()
+ {
+ return mBuilder.toString();
+ }
+
+ public void write(Writer writer) throws IOException
+ {
+ // TODO: Buffer?
+ StateManager stateManager = mContext.getApplication().getStateManager();
+ Object stateToWrite = stateManager.saveView(mContext);
+ int markLen = SAVESTATE_FIELD_MARKER.length();
+ int pos = 0;
+ int tildeIdx = mBuilder.indexOf(SAVESTATE_FIELD_MARKER);
+ while (tildeIdx > 0)
+ {
+ writer.write(mBuilder.substring(pos, (tildeIdx - pos)));
+ stateManager.writeState(mContext, stateToWrite);
+ pos += tildeIdx + markLen;
+ tildeIdx = mBuilder.indexOf(SAVESTATE_FIELD_MARKER, pos);
+ }
+ writer.write(mBuilder.substring(pos));
+ }
+ }
+
+
}
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/application/PortletViewState.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -14,9 +14,15 @@
import javax.faces.application.FacesMessage;
import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.render.ResponseStateManager;
+import javax.portlet.faces.Bridge;
+import javax.portlet.faces.BridgeDefaultViewNotSpecifiedException;
+import org.ajax4jsf.context.AjaxContext;
+import org.ajax4jsf.portlet.context.AbstractExternalContext;
+
/**
* @author asmirnov
*
@@ -45,7 +51,8 @@
javax.servlet.ServletContext.class,
javax.servlet.ServletRequest.class,
javax.servlet.ServletResponse.class,
- javax.servlet.http.HttpSession.class };
+ javax.servlet.http.HttpSession.class,
+ AjaxContext.class};
private static final String[] excludedRequestAttributes = {
"javax.servlet.include", ResponseStateManager.VIEW_STATE_PARAM };
@@ -246,8 +253,8 @@
beans.put(entry.getKey(), entry.getValue());
}
}
- _requestParameters = facesContext.getExternalContext()
- .getRequestParameterValuesMap();
+ _requestParameters = new HashMap<String, String[]>(facesContext
+ .getExternalContext().getRequestParameterValuesMap());
}
public void restoreBeans(FacesContext facesContext) {
@@ -259,4 +266,37 @@
requestMap.put(REQUEST_PARAMETERS_ATTRIBUTE, _requestParameters);
}
+ public void restoreRequest(FacesContext facesContext, boolean b) {
+ ExternalContext externalContext = facesContext.getExternalContext();
+ Map<String, Object> requestMap = externalContext.getRequestMap();
+ if (b) {
+ restoreMessages(facesContext);
+ restoreBeans(facesContext);
+ }
+ String viewId = getViewId();
+ if(null == viewId &&
Bridge.PortletPhase.ActionPhase.equals(requestMap.get(Bridge.PORTLET_LIFECYCLE_PHASE))){
+ viewId = (String)
externalContext.getRequestParameterMap().get(AbstractExternalContext.VIEW_ID_PARAMETER);
+ }
+ if (null == viewId) {
+ viewId = (String) requestMap.get(Bridge.DEFAULT_VIEWID);
+ if (null == viewId) {
+ throw new BridgeDefaultViewNotSpecifiedException("could'n determine portlet
view id");
+ }
+ } else {
+ requestMap.put(Bridge.IS_POSTBACK_ATTRIBUTE, Boolean.TRUE);
+ }
+ setViewId(viewId);
+ requestMap.put(AbstractExternalContext.VIEW_ID_PARAMETER, viewId);
+
+ }
+
+ public void saveRequest(FacesContext facesContext) {
+ UIViewRoot root = facesContext.getViewRoot();
+ if (null != root) {
+ setViewId(root.getViewId());
+ }
+ saveBeans(facesContext);
+ saveMessages(facesContext);
+ }
+
}
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/AbstractExternalContext.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/AbstractExternalContext.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/AbstractExternalContext.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -53,12 +53,16 @@
*/
package org.ajax4jsf.portlet.context;
+import java.net.MalformedURLException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
+import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.portlet.faces.Bridge;
/**
*
@@ -82,395 +86,423 @@
*
*/
public abstract class AbstractExternalContext extends ExternalContext {
-
- /**
- * Request parameter to store current View Id.
- */
- public static final String VIEW_ID_PARAMETER
="org.ajax4jsf.portlet.VIEWID";
- /**
- * Name of request parameter to store namsepace of the current portlet instance.
- */
- public static final String NAMESPACE_PARAMETER
="org.ajax4jsf.portlet.NAMESPACE";
- public static final String ACTION__PARAMETER =
"org.ajax4jsf.portlet.ACTION_URL";
- public static final Object PORTLET_MODE_PARAMETER =
"org.ajax4jsf.portlet.MODE";
- public static final Object PORTLET_NAME_PARAMETER =
"org.ajax4jsf.portlet.NAME";
-
- protected static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Request parameter to store current View Id.
+ */
+ public static final String VIEW_ID_PARAMETER = "org.ajax4jsf.portlet.VIEWID";
+ /**
+ * Name of request parameter to store namsepace of the current portlet
+ * instance.
+ */
+ public static final String NAMESPACE_PARAMETER =
"org.ajax4jsf.portlet.NAMESPACE";
+ public static final String ACTION__PARAMETER =
"org.ajax4jsf.portlet.ACTION_URL";
+ public static final Object PORTLET_MODE_PARAMETER =
"org.ajax4jsf.portlet.MODE";
+ public static final Object PORTLET_NAME_PARAMETER =
"org.ajax4jsf.portlet.NAME";
+
+ protected static final String[] EMPTY_STRING_ARRAY = new String[0];
public static final String PORTLET_CONFIG_ATTRIBUTE =
"org.ajax4jsf.portlet.CONFIG";
- // TODO - optimization.
- private Map applicationMap;
+ public static final Object RENDER_POLICY_ATTRIBUTE =
"org.ajax4jsf.portlet.RENDER_POLICY";
+ // TODO - optimization.
+ private Map applicationMap;
- private Map initParameterMap;
+ private Map initParameterMap;
- private Map requestHeaderMap = null;
+ private Map requestHeaderMap = null;
- private Map requestHeaderValues;
+ private Map requestHeaderValues;
- private Map requestMap;
+ private Map requestMap;
- private Map requestParameterMap;
+ private Map requestParameterMap;
- private Map requestParameterValuesMap;
+ private Map requestParameterValuesMap;
- private Map sessionMap;
+ private Map sessionMap;
- private Object request;
+ private Object request;
- private Object response;
+ private Object response;
- private boolean http;
+ private boolean http;
- private Map actionSettings;
+ private Map actionSettings;
- private Object context;
-
+ private Object context;
- /**
- *
- * @param response
- *
- * @param request
- *
- * @param context
- *
- * @param defaultContext -
- *
- * default implementation of <code>ExternalFacesContext</code>.
- *
- */
- public AbstractExternalContext(Object context, Object request,
- Object response) {
- super();
- this.context = context;
- this.request = request;
- this.response = response;
- }
+ /**
+ *
+ * @param context
+ * @param request
+ * @param response
+ * @param defaultContext -
+ *
+ * default implementation of <code>ExternalFacesContext</code>.
+ *
+ */
+ public AbstractExternalContext(Object context, Object request,
+ Object response) {
+ super();
+ this.context = context;
+ this.request = request;
+ this.response = response;
+ }
- protected abstract String getNamespace();
-
-
- public String encodeNamespace(String name) {
-
- return getNamespace()+name;
- }
- /*
- *
- * (non-Javadoc)
- *
- *
- *
- * @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
- *
- */
- public Map getApplicationMap() {
- if (this.applicationMap == null) {
- this.applicationMap = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return getContextAttributeNames();
- }
+ protected abstract String getNamespace();
- protected Object getAttribute(String name) {
- return getContextAttribute(name);
- }
+ public String encodeNamespace(String name) {
- protected void setAttribute(String name, Object value) {
- setContextAttribute(name, value);
- }
+ return getNamespace() + name;
+ }
- protected void removeAttribute(String name) {
- removeContextAttribute(name);
+ /*
+ *
+ * (non-Javadoc)
+ *
+ *
+ *
+ * @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
+ *
+ */
+ public Map getApplicationMap() {
+ if (this.applicationMap == null) {
+ this.applicationMap = new ContextAttributesMap() {
+ protected Enumeration getEnumeration() {
+ return getContextAttributeNames();
+ }
+
+ protected Object getAttribute(String name) {
+ return getContextAttribute(name);
+ }
+
+ protected void setAttribute(String name, Object value) {
+ setContextAttribute(name, value);
+ }
+
+ protected void removeAttribute(String name) {
+ removeContextAttribute(name);
+ }
+ };
}
- };
+ return this.applicationMap;
}
- return this.applicationMap;
- }
- protected abstract void removeContextAttribute(String name);
+ protected abstract void removeContextAttribute(String name);
- protected abstract void setContextAttribute(String name, Object value);
+ protected abstract void setContextAttribute(String name, Object value);
- protected abstract Object getContextAttribute(String name);
+ protected abstract Object getContextAttribute(String name);
- protected abstract Enumeration getContextAttributeNames();
+ protected abstract Enumeration getContextAttributeNames();
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getAuthType()
- */
- public Object getContext() {
- return this.context;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getAuthType()
+ */
+ public Object getContext() {
+ return this.context;
+ }
- /**
- * @param context
- */
- public void setContext(Object context) {
- this.context = context;
- }
+ /**
+ * @param context
+ */
+ public void setContext(Object context) {
+ this.context = context;
+ }
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getInitParameter(java.lang.String)
- */
- public Map getInitParameterMap() {
- if (this.initParameterMap == null) {
- this.initParameterMap = new ContextAttributesMap() {
- protected Object getAttribute(String name) {
- return getInitParameter(name);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getInitParameter(java.lang.String)
+ */
+ public Map getInitParameterMap() {
+ if (this.initParameterMap == null) {
+ this.initParameterMap = new ContextAttributesMap() {
+ protected Object getAttribute(String name) {
+ return getInitParameter(name);
+ }
- protected void setAttribute(String name, Object value) {
- throw new UnsupportedOperationException();
- }
+ protected void setAttribute(String name, Object value) {
+ throw new UnsupportedOperationException();
+ }
- protected Enumeration getEnumeration() {
- return getInitParametersNames();
+ protected Enumeration getEnumeration() {
+ return getInitParametersNames();
+ }
+ };
}
- };
+ return this.initParameterMap;
}
- return this.initParameterMap;
- }
- /**
- * Hoock method for initialization parameters.
- *
- * @return
- */
- protected abstract Enumeration getInitParametersNames();
+ /**
+ * Hoock method for initialization parameters.
+ *
+ * @return
+ */
+ protected abstract Enumeration getInitParametersNames();
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getRequest()
- */
- public Object getRequest() {
- return this.request;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getRequest()
+ */
+ public Object getRequest() {
+ return this.request;
+ }
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#setRequest(java.lang.Object)
- */
- public void setRequest(Object request) {
- this.request = request;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#setRequest(java.lang.Object)
+ */
+ public void setRequest(Object request) {
+ this.request = request;
+ }
- public Map getRequestCookieMap() {
- // Portlet environment don't have methods to use cookies.
- return Collections.EMPTY_MAP;
- }
+ public Map getRequestCookieMap() {
+ // Portlet environment don't have methods to use cookies.
+ return Collections.EMPTY_MAP;
+ }
- /*
- *
- * (non-Javadoc)
- *
- *
- *
- * @see javax.faces.context.ExternalContext#getRequestHeaderMap()
- *
- */
- public Map getRequestHeaderMap() {
- if (this.requestHeaderMap == null) {
- this.requestHeaderMap = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return getRequestHeaderNames();
- }
+ /*
+ *
+ * (non-Javadoc)
+ *
+ *
+ *
+ * @see javax.faces.context.ExternalContext#getRequestHeaderMap()
+ *
+ */
+ public Map getRequestHeaderMap() {
+ if (this.requestHeaderMap == null) {
+ this.requestHeaderMap = new ContextAttributesMap() {
+ protected Enumeration getEnumeration() {
+ return getRequestHeaderNames();
+ }
- protected Object getAttribute(String name) {
- return getRequestHeader(name);
- }
+ protected Object getAttribute(String name) {
+ return getRequestHeader(name);
+ }
- protected void setAttribute(String name, Object value) {
- throw new UnsupportedOperationException();
+ protected void setAttribute(String name, Object value) {
+ throw new UnsupportedOperationException();
+ }
+ };
}
- };
+ return this.requestHeaderMap;
}
- return this.requestHeaderMap;
- }
- protected abstract Object getRequestHeader(String name);
+ protected abstract Object getRequestHeader(String name);
- protected abstract Enumeration getRequestHeaderNames();
+ protected abstract Enumeration getRequestHeaderNames();
- public Map getRequestHeaderValuesMap() {
- //
- if (this.requestHeaderValues == null) {
- this.requestHeaderValues = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return getRequestHeaderNames();
- }
+ public Map getRequestHeaderValuesMap() {
+ //
+ if (this.requestHeaderValues == null) {
+ this.requestHeaderValues = new ContextAttributesMap() {
+ protected Enumeration getEnumeration() {
+ return getRequestHeaderNames();
+ }
- protected Object getAttribute(String name) {
- return getRequestHeaderValues(name);
- }
+ protected Object getAttribute(String name) {
+ return getRequestHeaderValues(name);
+ }
- protected void setAttribute(String name, Object value) {
- throw new UnsupportedOperationException();
+ protected void setAttribute(String name, Object value) {
+ throw new UnsupportedOperationException();
+ }
+ };
}
- };
+ return this.requestHeaderValues;
}
- return this.requestHeaderValues;
- }
- protected abstract String[] getRequestHeaderValues(String name);
+ protected abstract String[] getRequestHeaderValues(String name);
- public Map getRequestMap() {
- if (this.requestMap == null) {
- this.requestMap = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return getRequestAttributeNames();
- }
+ public Map getRequestMap() {
+ if (this.requestMap == null) {
+ this.requestMap = new ContextAttributesMap() {
+ protected Enumeration getEnumeration() {
+ return getRequestAttributeNames();
+ }
- protected Object getAttribute(String name) {
- return getRequestAttribute(name);
- }
+ protected Object getAttribute(String name) {
+ return getRequestAttribute(name);
+ }
- protected void setAttribute(String name, Object value) {
- setRequestAttribute(name, value);
- }
+ protected void setAttribute(String name, Object value) {
+ setRequestAttribute(name, value);
+ }
- protected void removeAttribute(String name) {
- removeRequestAttribute(name);
+ protected void removeAttribute(String name) {
+ removeRequestAttribute(name);
+ }
+ };
}
- };
+ return this.requestMap;
}
- return this.requestMap;
- }
- protected abstract void removeRequestAttribute(String name);
+ protected abstract void removeRequestAttribute(String name);
- protected abstract void setRequestAttribute(String name, Object value);
+ protected abstract void setRequestAttribute(String name, Object value);
- protected abstract Object getRequestAttribute(String name);
+ protected abstract Object getRequestAttribute(String name);
- protected abstract Enumeration getRequestAttributeNames();
+ protected abstract Enumeration getRequestAttributeNames();
- public Map getRequestParameterMap() {
- //
- if (this.requestParameterMap == null) {
- this.requestParameterMap = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return enumerateRequestParameterNames();
- }
+ public Map getRequestParameterMap() {
+ //
+ if (this.requestParameterMap == null) {
+ this.requestParameterMap = new ContextAttributesMap() {
+ protected Enumeration getEnumeration() {
+ return enumerateRequestParameterNames();
+ }
- protected Object getAttribute(String name) {
- return getRequestParameter(name);
- }
+ protected Object getAttribute(String name) {
+ return getRequestParameter(name);
+ }
- protected void setAttribute(String name, Object value) {
- throw new UnsupportedOperationException();
+ protected void setAttribute(String name, Object value) {
+ throw new UnsupportedOperationException();
+ }
+ };
}
- };
+ return this.requestParameterMap;
}
- return this.requestParameterMap;
- }
- protected abstract Object getRequestParameter(String name);
+ protected abstract Object getRequestParameter(String name);
- protected abstract Enumeration enumerateRequestParameterNames();
+ protected abstract Enumeration enumerateRequestParameterNames();
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getRequestParameterNames()
- *
- */
- public Iterator getRequestParameterNames() {
- return new EnumerationIterator(enumerateRequestParameterNames());
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getRequestParameterNames()
+ *
+ */
+ public Iterator getRequestParameterNames() {
+ return new EnumerationIterator(enumerateRequestParameterNames());
+ }
- /*
- *
- * (non-Javadoc)
- *
- *
- *
- * @see javax.faces.context.ExternalContext#getRequestParameterValuesMap()
- *
- */
- public Map getRequestParameterValuesMap() {
- if (this.requestParameterValuesMap == null) {
- this.requestParameterValuesMap = new ContextAttributesMap() {
+ /*
+ *
+ * (non-Javadoc)
+ *
+ *
+ *
+ * @see javax.faces.context.ExternalContext#getRequestParameterValuesMap()
+ *
+ */
+ public Map getRequestParameterValuesMap() {
+ if (this.requestParameterValuesMap == null) {
+ this.requestParameterValuesMap = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return enumerateRequestParameterNames();
- }
+ protected Enumeration getEnumeration() {
+ return enumerateRequestParameterNames();
+ }
- protected Object getAttribute(String name) {
- return getRequestParameterValues(name);
- }
+ protected Object getAttribute(String name) {
+ return getRequestParameterValues(name);
+ }
- protected void setAttribute(String name, Object value) {
- throw new UnsupportedOperationException();
+ protected void setAttribute(String name, Object value) {
+ throw new UnsupportedOperationException();
+ }
+ };
}
- };
+ return this.requestParameterValuesMap;
}
- return this.requestParameterValuesMap;
- }
- protected abstract Object getRequestParameterValues(String name);
+ protected abstract Object getRequestParameterValues(String name);
- /*
- *
- * (non-Javadoc)
- *
- *
- *
- * @see javax.faces.context.ExternalContext#getResponse()
- *
- */
- public void setResponse(Object response) {
- this.response = response;
- }
+ /*
+ *
+ * (non-Javadoc)
+ *
+ *
+ *
+ * @see javax.faces.context.ExternalContext#getResponse()
+ *
+ */
+ public void setResponse(Object response) {
+ this.response = response;
+ }
- public Object getResponse() {
- return this.response;
- }
+ public Object getResponse() {
+ return this.response;
+ }
- /*
- *
- * (non-Javadoc)
- *
- *
- *
- * @see javax.faces.context.ExternalContext#getSessionMap()
- *
- */
- public Map getSessionMap() {
- if (this.sessionMap == null) {
- this.sessionMap = new ContextAttributesMap() {
+ /*
+ *
+ * (non-Javadoc)
+ *
+ *
+ *
+ * @see javax.faces.context.ExternalContext#getSessionMap()
+ *
+ */
+ public Map getSessionMap() {
+ if (this.sessionMap == null) {
+ this.sessionMap = new ContextAttributesMap() {
- protected Enumeration getEnumeration() {
- return getSessionAttributeNames();
- }
+ protected Enumeration getEnumeration() {
+ return getSessionAttributeNames();
+ }
- protected Object getAttribute(String name) {
- return getSessionAttribute(name);
- }
+ protected Object getAttribute(String name) {
+ return getSessionAttribute(name);
+ }
- protected void setAttribute(String name, Object value) {
- setSessionAttribute(name, value);
+ protected void setAttribute(String name, Object value) {
+ setSessionAttribute(name, value);
+ }
+
+ protected void removeAttribute(String name) {
+ removeSessionAttribute(name);
+ }
+
+ };
}
-
- protected void removeAttribute(String name) {
- removeSessionAttribute(name);
- }
-
-
- };
+ return this.sessionMap;
}
- return this.sessionMap;
- }
+ protected abstract void removeSessionAttribute(String name);
- protected abstract void removeSessionAttribute(String name);
+ protected abstract void setSessionAttribute(String name, Object value);
- protected abstract void setSessionAttribute(String name, Object value);
+ protected abstract Object getSessionAttribute(String name);
- protected abstract Object getSessionAttribute(String name);
+ protected abstract Enumeration getSessionAttributeNames();
- protected abstract Enumeration getSessionAttributeNames();
+ protected abstract String createActionUrl(Map parameters);
+
+ public String encodeActionURL(String url) {
+ if (null == url) {
+ throw new NullPointerException();
+ }
+ String actionUrl = url;
+ if (!actionUrl.startsWith("#")) {
+ try {
+ PortalActionURL portalUrl = new PortalActionURL(url);
+ String directLink = portalUrl.getParameter(Bridge.DIRECT_LINK);
+ boolean inContext = portalUrl
+ .isInContext(getRequestContextPath());
+ if (inContext
+ && (null == directLink || false == Boolean
+ .parseBoolean(directLink))) {
+ if (null != directLink) {
+ portalUrl.removeParameter(Bridge.DIRECT_LINK);
+
+ }
+ String viewId = portalUrl.getParameter(VIEW_ID_PARAMETER);
+ actionUrl = createActionUrl(portalUrl.getParameters());
+ }
+ } catch (MalformedURLException e) {
+ throw new FacesException(e);
+ }
+ }
+ return encodeURL(actionUrl);
+ }
+
+ protected abstract String encodeURL(String actionUrl);
}
\ No newline at end of file
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/PortletContextImpl.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/PortletContextImpl.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/PortletContextImpl.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -18,7 +18,6 @@
import java.util.Set;
import java.util.regex.Pattern;
-import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContextFactory;
import javax.portlet.ActionRequest;
@@ -31,8 +30,9 @@
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
-import javax.portlet.faces.Bridge;
+import org.ajax4jsf.webapp.WebXml;
+
/**
* Version of the {@link ExternalContext} for a Portlet request. {@link
FacesContextFactory} will create instance of this class
* for a portal <code>action</code> phase.
@@ -46,6 +46,10 @@
public PortletContextImpl(PortletContext context, PortletRequest request,
PortletResponse response) {
super(context, request, response);
+ WebXml webXml = (WebXml) context.getAttribute(WebXml.CONTEXT_ATTRIBUTE);
+ if(null != webXml){
+ _servletPath = webXml.getFacesServletPrefix();
+ }
}
public void setResponseCharacterEncoding(String encoding) {
@@ -221,56 +225,24 @@
private static final Pattern directLink = Pattern.compile("");
private static final Pattern viewIdParam = Pattern.compile("");
- /* (non-Javadoc)
- * @see javax.faces.context.ExternalContext#encodeActionURL(java.lang.String)
- * JSR-301 requirements, PLT 6.1.3.1 :
- * Return the inputURL, after performing any rewriting needed to ensure that it will
correctly identify an addressable URL in the current application. The inputURL is
expected to conform to standard URI syntax or more specifically not be the result of
calling PortletURL.toString(). When called during the RenderPhase, the inputURL is
transformed, encoded, and returned so it can be used as markup into the response. When
called during the ActionPhase, the inputURL is transformed and encoded into the
ActionResponse as the Faces navigation which resulted from processing the action. To
process such an inputURL correctly, this method must:
- o If the inputURL starts with the # character or the inputURL is an absolute
path external to this portlet application return the inputURL unchanged.
- o If the inputURL contains the parameter javax.portlet.faces.DirectLink and its
value is true then return it without removing this parameter. If the inputURL contains
the parameter javax.portlet.faces.DirectLink and its value is false then remove the
javax.portlet.faces.DirectLink parameter and value from the queryString and continue
processing.
- o Otherwise:
- + Identify the viewId within the inputURL.
- # If the inputURL contains a bridge encoded viewId then extract it
from the URL and use this as the viewId.
- # Otherwise if the inputURL is a full path within the portlet
application, use the servlet definitions from the web application's web.xml to
determine both how the Faces servlet is mapped and whether this URL resolves to the Faces
servlet. Derive the view identifier that corresponds to this URL, as follows:
- Note: all resulting viewIds start with a "/".
- * If prefix mapping (such as “/faces/*”) is used for
FacesServlet, the viewId is set from the extra path information of the request URI. This
corresponds to the path that follows the prefix mapping.
- * If suffix mapping (such as “*.faces”) is used for
FacesServlet, the viewId is set from the servlet path information of the request URI,
after replacing the suffix with the value of the context initialization parameter named by
the symbolic constant ViewHandler.DEFAULT_SUFFIX_PARAM_NAME (if no such context
initialization parameter is present, use the value of the symbolic constant
ViewHandler.DEFAULT_SUFFIX as the replacement suffix). This corresponds to the path that
follows the context path.
- * If the URL doesn't reference a FacesServlet mapping
then the viewId is the path that follows the context path.
- + Process the viewId based URL
- # If executed during the ActionPhase, encode the viewId and any
additional querystring parameters in the ActionResponse in a way that not only ensures
that the appropriate subsequent render will be based on these but also that inappropriate
subsequent renders will not. (See section 5.1 concerning request scopes). Return a
non-null URL string whose value isn't equal to the inputURL.
- # If executed during the RenderPhase, construct an actionURL by
calling getResponse().createActionURL(). Encode the viewId and any additional querystring
parameters in this actionURL in a way that not only ensures that the appropriate
subsequent render will be based on these parameters but also that inappropriate subsequent
renders will not. (See section 5.1 concerning request scopes).. And return
actionURL.toString().
-
- Note: the result must not contain doubly encoded querystring
parameter values and hence additional processing is required to detect and undo such
double encoding when the inputURL is already encoded and the actionURL.toString() does
reencoding.
-
- */
- public String encodeActionURL(String url) {
- if (null == url) {
- throw new NullPointerException();
- }
- String actionUrl = url;
- if(!actionUrl.startsWith("#") ){
- try {
- PortalActionURL portalUrl = new PortalActionURL(url);
- if(portalUrl.isInContext(getRequestContextPath()) ||
!(Boolean.parseBoolean(portalUrl.getParameter(Bridge.DIRECT_LINK)))){
- portalUrl.removeParameter(Bridge.DIRECT_LINK);
- String viewId = portalUrl.getParameter(VIEW_ID_PARAMETER);
- actionUrl = createActionUrl(portalUrl.getParameters());
- }
- } catch (MalformedURLException e) {
- throw new FacesException(e);
- }
- }
- return getPortletResponse().encodeURL(actionUrl);
- }
-
/**
* @param parameters
* @return
*/
+ @Override
protected String createActionUrl(Map parameters){
return "/ajax4jsfPortletBridge/actionUrl/do/nothitg";
}
public String encodeResourceURL(String url) {
+ return encodeURL(url);
+ }
+
+ /**
+ * @param url
+ * @return
+ */
+ protected String encodeURL(String url) {
return getPortletResponse().encodeURL(url);
}
@@ -294,6 +266,8 @@
return new EnumerationIterator(getPortletRequest().getLocales());
}
+ private String _pathInfo = null;
+
public String getRequestPathInfo() {
String pathInfo = (String) getRequestParameter(VIEW_ID_PARAMETER);
if (null == pathInfo) {
@@ -302,8 +276,10 @@
return pathInfo;
}
+ private String _servletPath = null;
+
public String getRequestServletPath() {
- return null;
+ return _servletPath;
}
public Object getSession(boolean create) {
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/ServletContextImpl.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/ServletContextImpl.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/context/ServletContextImpl.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -13,6 +13,7 @@
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
+import java.util.Map;
import java.util.Set;
import javax.faces.FacesException;
@@ -26,6 +27,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import org.ajax4jsf.context.AjaxContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -34,301 +36,322 @@
*
*/
public class ServletContextImpl extends AbstractExternalContext {
-
- private static final Log _log = LogFactory.getLog(ServletContextImpl.class);
-
- private String namespace;
- /**
- * @param context
- * @param request
- * @param response
- */
- public ServletContextImpl(ServletContext context, HttpServletRequest request,
HttpServletResponse response) {
- super(context, request, response);
- }
-
- public void setResponseCharacterEncoding(String encoding) {
-
- getHttpResponse().setCharacterEncoding(encoding);
- }
-
- public String getResponseCharacterEncoding() {
- return getHttpResponse().getCharacterEncoding();
- }
-
-
- public String getResponseContentType() {
- return getHttpResponse().getContentType();
- }
+ private static final Log _log = LogFactory.getLog(ServletContextImpl.class);
- public void setRequestCharacterEncoding(String encoding)
- throws UnsupportedEncodingException {
- getHttpRequest().setCharacterEncoding(encoding);
- }
-
- public String getRequestCharacterEncoding() {
- return getHttpRequest().getCharacterEncoding();
- }
-
-
- public String getRequestContentType() {
- return getHttpRequest().getContentType();
- }
+ private String namespace;
- private HttpServletRequest getHttpRequest() {
- return (HttpServletRequest) getRequest();
- }
+ /**
+ * @param context
+ * @param request
+ * @param response
+ */
+ public ServletContextImpl(ServletContext context,
+ HttpServletRequest request, HttpServletResponse response) {
+ super(context, request, response);
+ }
- private ServletContext getServletContext() {
- return (ServletContext) getContext();
- }
+ public void setResponseCharacterEncoding(String encoding) {
- private HttpServletResponse getHttpResponse() {
- return (HttpServletResponse) getResponse();
- }
+ getHttpResponse().setCharacterEncoding(encoding);
+ }
- protected String getNamespace() {
- if (null == namespace) {
- Object requestParameter = getRequestParameter(NAMESPACE_PARAMETER);
- if (null != requestParameter) {
- namespace = (String) requestParameter;
- if (_log.isDebugEnabled()) {
- _log.debug("Namespace for a portlet instance is "+namespace);
+ public String getResponseCharacterEncoding() {
+ return getHttpResponse().getCharacterEncoding();
+ }
+
+ public String getResponseContentType() {
+ return getHttpResponse().getContentType();
+ }
+
+ public void setRequestCharacterEncoding(String encoding)
+ throws UnsupportedEncodingException {
+ getHttpRequest().setCharacterEncoding(encoding);
+ }
+
+ public String getRequestCharacterEncoding() {
+ return getHttpRequest().getCharacterEncoding();
+ }
+
+ public String getRequestContentType() {
+ return getHttpRequest().getContentType();
+ }
+
+ private HttpServletRequest getHttpRequest() {
+ return (HttpServletRequest) getRequest();
+ }
+
+ private ServletContext getServletContext() {
+ return (ServletContext) getContext();
+ }
+
+ private HttpServletResponse getHttpResponse() {
+ return (HttpServletResponse) getResponse();
+ }
+
+ protected String getNamespace() {
+ if (null == namespace) {
+ Object requestParameter = getRequestParameter(NAMESPACE_PARAMETER);
+ if (null != requestParameter) {
+ namespace = (String) requestParameter;
+ if (_log.isDebugEnabled()) {
+ _log.debug("Namespace for a portlet instance is "
+ + namespace);
+ }
+ } else {
+ throw new FacesException(
+ "AJAX call to portlet without namespace parameter");
+ }
}
- } else {
- throw new FacesException("AJAX call to portlet without namespace
parameter");
- }
+ return namespace;
}
- return namespace;
- }
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getInitParameter(java.lang.String)
- */
- public String getInitParameter(String name) {
- return getServletContext().getInitParameter(name);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getInitParameter(java.lang.String)
+ */
+ public String getInitParameter(String name) {
+ return getServletContext().getInitParameter(name);
+ }
- protected Enumeration getInitParametersNames() {
- return getServletContext().getInitParameterNames();
- }
+ protected Enumeration getInitParametersNames() {
+ return getServletContext().getInitParameterNames();
+ }
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getResource(java.lang.String)
- */
- public URL getResource(String path) throws MalformedURLException {
- return getServletContext().getResource(path);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getResource(java.lang.String)
+ */
+ public URL getResource(String path) throws MalformedURLException {
+ return getServletContext().getResource(path);
+ }
- /*
- * (non-Javadoc)
- *
- * @see
javax.faces.context.ExternalContext#getResourceAsStream(java.lang.String)
- */
- public InputStream getResourceAsStream(String path) {
- return getServletContext().getResourceAsStream(path);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getResourceAsStream(java.lang.String)
+ */
+ public InputStream getResourceAsStream(String path) {
+ return getServletContext().getResourceAsStream(path);
+ }
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
- */
- public Set getResourcePaths(String path) {
- return getServletContext().getResourcePaths(path);
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
+ */
+ public Set getResourcePaths(String path) {
+ return getServletContext().getResourcePaths(path);
+ }
- protected Object getContextAttribute(String name) {
- return getServletContext().getAttribute(name);
- }
+ protected Object getContextAttribute(String name) {
+ return getServletContext().getAttribute(name);
+ }
- protected Enumeration getContextAttributeNames() {
- return getServletContext().getAttributeNames();
- }
+ protected Enumeration getContextAttributeNames() {
+ return getServletContext().getAttributeNames();
+ }
- protected void setContextAttribute(String name, Object value) {
- getServletContext().setAttribute(name, value);
- }
+ protected void setContextAttribute(String name, Object value) {
+ getServletContext().setAttribute(name, value);
+ }
- public String getAuthType() {
- return getHttpRequest().getAuthType();
- }
+ public String getAuthType() {
+ return getHttpRequest().getAuthType();
+ }
- public String getRemoteUser() {
- return getHttpRequest().getRemoteUser();
- }
+ public String getRemoteUser() {
+ return getHttpRequest().getRemoteUser();
+ }
- public String getRequestContextPath() {
- return getHttpRequest().getContextPath();
- }
+ public String getRequestContextPath() {
+ return getHttpRequest().getContextPath();
+ }
- public String getRequestPathInfo() {
- return getHttpRequest().getPathInfo();
- }
+ public String getRequestPathInfo() {
+ return getHttpRequest().getPathInfo();
+ }
- public String getRequestServletPath() {
- return getHttpRequest().getServletPath();
- }
+ public String getRequestServletPath() {
+ return getHttpRequest().getServletPath();
+ }
- protected Enumeration enumerateRequestParameterNames() {
- return getHttpRequest().getParameterNames();
- }
+ protected Enumeration enumerateRequestParameterNames() {
+ return getHttpRequest().getParameterNames();
+ }
- protected Object getRequestAttribute(String name) {
- return getHttpRequest().getAttribute(name);
- }
+ protected Object getRequestAttribute(String name) {
+ return getHttpRequest().getAttribute(name);
+ }
- protected Enumeration getRequestAttributeNames() {
- return getHttpRequest().getAttributeNames();
- }
+ protected Enumeration getRequestAttributeNames() {
+ return getHttpRequest().getAttributeNames();
+ }
- protected Object getRequestParameterValues(String name) {
- return getHttpRequest().getParameterValues(name);
- }
+ protected Object getRequestParameterValues(String name) {
+ return getHttpRequest().getParameterValues(name);
+ }
- protected Object getRequestHeader(String name) {
- return getHttpRequest().getHeader(name);
- }
+ protected Object getRequestHeader(String name) {
+ return getHttpRequest().getHeader(name);
+ }
- protected Enumeration getRequestHeaderNames() {
- return getHttpRequest().getHeaderNames();
- }
+ protected Enumeration getRequestHeaderNames() {
+ return getHttpRequest().getHeaderNames();
+ }
- protected String[] getRequestHeaderValues(String name) {
- Enumeration values = getHttpRequest().getHeaders(name);
- ArrayList valuesList = new ArrayList();
- while (values.hasMoreElements()) {
- valuesList.add(values.nextElement());
+ protected String[] getRequestHeaderValues(String name) {
+ Enumeration values = getHttpRequest().getHeaders(name);
+ ArrayList valuesList = new ArrayList();
+ while (values.hasMoreElements()) {
+ valuesList.add(values.nextElement());
+ }
+ return (String[]) valuesList.toArray(EMPTY_STRING_ARRAY);
}
- return (String[]) valuesList.toArray(EMPTY_STRING_ARRAY);
- }
- protected Object getRequestParameter(String name) {
- return getHttpRequest().getParameter(name);
- }
-
- private String sessionPrefix;
-
- private String getSessionPrefix(){
- if (sessionPrefix == null) {
- HttpSession session = getHttpRequest().getSession(false);
- String namespase = getNamespace();
- if(null == namespase || null == session){
- throw new FacesException("JSF request called without portlet namespace
parameter");
- }
- Enumeration attributeNames = session.getAttributeNames();
- while (attributeNames.hasMoreElements() && null == sessionPrefix) {
- String name = (String) attributeNames.nextElement();
- Object attribute = session.getAttribute(name);
- if (PortletSessionUtil.decodeScope(name) == PortletSession.PORTLET_SCOPE
- && PortletSessionUtil.decodeAttributeName(name).equals(
- NAMESPACE_PARAMETER)
- && namespase.equals(attribute)) {
- sessionPrefix = name.substring(0, name.length()-NAMESPACE_PARAMETER.length());
- if (_log.isDebugEnabled()) {
- _log.debug("Prefix for a PORTLET_SCOPE session attributes:
"+sessionPrefix);
- }
+ protected Object getRequestParameter(String name) {
+ return getHttpRequest().getParameter(name);
+ }
+
+ private String sessionPrefix;
+
+ private String getSessionPrefix() {
+ if (sessionPrefix == null) {
+ HttpSession session = getHttpRequest().getSession(false);
+ String namespase = getNamespace();
+ if (null == namespase || null == session) {
+ throw new FacesException(
+ "JSF request called without portlet namespace parameter");
+ }
+ Enumeration attributeNames = session.getAttributeNames();
+ while (attributeNames.hasMoreElements() && null == sessionPrefix) {
+ String name = (String) attributeNames.nextElement();
+ Object attribute = session.getAttribute(name);
+ if (PortletSessionUtil.decodeScope(name) == PortletSession.PORTLET_SCOPE
+ && PortletSessionUtil.decodeAttributeName(name).equals(
+ NAMESPACE_PARAMETER)
+ && namespase.equals(attribute)) {
+ sessionPrefix = name.substring(0, name.length()
+ - NAMESPACE_PARAMETER.length());
+ if (_log.isDebugEnabled()) {
+ _log
+ .debug("Prefix for a PORTLET_SCOPE session attributes: "
+ + sessionPrefix);
+ }
+ }
+ }
+ if (null == sessionPrefix) {
+ throw new FacesException(
+ "Prefix for attributes in portlet session scope not found");
+ }
}
- }
- if(null == sessionPrefix){
- throw new FacesException("Prefix for attributes in portlet session scope not
found");
- }
+ return sessionPrefix;
}
- return sessionPrefix;
- }
- protected Object getSessionAttribute(String name) {
- return getHttpRequest().getSession(true).getAttribute(getSessionPrefix()+name);
- }
+ protected Object getSessionAttribute(String name) {
+ return getHttpRequest().getSession(true).getAttribute(
+ getSessionPrefix() + name);
+ }
- protected Enumeration getSessionAttributeNames() {
- return new
SessionAttributesNames(getHttpRequest().getSession(true).getAttributeNames(),getSessionPrefix());
- }
+ protected Enumeration getSessionAttributeNames() {
+ return new SessionAttributesNames(getHttpRequest().getSession(true)
+ .getAttributeNames(), getSessionPrefix());
+ }
- protected void removeSessionAttribute(String name) {
- getHttpRequest().getSession(true).removeAttribute(getSessionPrefix()+name);
- }
+ protected void removeSessionAttribute(String name) {
+ getHttpRequest().getSession(true).removeAttribute(
+ getSessionPrefix() + name);
+ }
- protected void setSessionAttribute(String name, Object value) {
- getHttpRequest().getSession(true).setAttribute(getSessionPrefix()+name, value);
- }
+ protected void setSessionAttribute(String name, Object value) {
+ getHttpRequest().getSession(true).setAttribute(
+ getSessionPrefix() + name, value);
+ }
- protected void removeContextAttribute(String name) {
- getHttpRequest().getSession(true).removeAttribute(name);
- }
+ protected void removeContextAttribute(String name) {
+ getHttpRequest().getSession(true).removeAttribute(name);
+ }
- protected void removeRequestAttribute(String name) {
- getHttpRequest().removeAttribute(name);
- }
+ protected void removeRequestAttribute(String name) {
+ getHttpRequest().removeAttribute(name);
+ }
- protected void setRequestAttribute(String name, Object value) {
- getHttpRequest().setAttribute(name, value);
- }
+ protected void setRequestAttribute(String name, Object value) {
+ getHttpRequest().setAttribute(name, value);
+ }
- public void dispatch(String path) throws IOException {
- RequestDispatcher requestDispatcher =
getHttpRequest().getRequestDispatcher(path);
- if (requestDispatcher == null) {
- (getHttpResponse()).
- sendError(HttpServletResponse.SC_NOT_FOUND);
- return;
- }
- try {
- requestDispatcher.forward(getHttpRequest(), getHttpResponse());
- } catch (IOException ioe) {
- throw ioe;
- } catch (ServletException se) {
- throw new FacesException(se);
- }
+ public void dispatch(String path) throws IOException {
+ RequestDispatcher requestDispatcher = getHttpRequest()
+ .getRequestDispatcher(path);
+ if (requestDispatcher == null) {
+ (getHttpResponse()).sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+ try {
+ requestDispatcher.forward(getHttpRequest(), getHttpResponse());
+ } catch (IOException ioe) {
+ throw ioe;
+ } catch (ServletException se) {
+ throw new FacesException(se);
+ }
- }
+ }
- public String encodeActionURL(String url) {
- return getHttpResponse().encodeURL(url);
- }
+ @Override
+ protected String createActionUrl(Map parameters) {
+ String actionURL = getHttpRequest().getParameter(ACTION__PARAMETER);
+ return actionURL;
+ }
- public String encodeResourceURL(String url) {
- return getHttpResponse().encodeURL(url);
- }
+ public String encodeResourceURL(String url) {
+ return encodeURL(url);
+ }
- public Locale getRequestLocale() {
- return getHttpRequest().getLocale();
- }
+ /**
+ * @param url
+ * @return
+ */
+ protected String encodeURL(String url) {
+ return getHttpResponse().encodeURL(url);
+ }
- public Iterator getRequestLocales() {
- return new EnumerationIterator(getHttpRequest().getLocales());
- }
+ public Locale getRequestLocale() {
+ return getHttpRequest().getLocale();
+ }
- public Object getSession(boolean create) {
- HttpSession session = getHttpRequest().getSession(create);
- if(null != session){
- session = new ServletSessionWrapper(session,getSessionPrefix());
+ public Iterator getRequestLocales() {
+ return new EnumerationIterator(getHttpRequest().getLocales());
}
- return session;
- }
- public Principal getUserPrincipal() {
- return getHttpRequest().getUserPrincipal();
- }
+ public Object getSession(boolean create) {
+ HttpSession session = getHttpRequest().getSession(create);
+ if (null != session) {
+ session = new ServletSessionWrapper(session, getSessionPrefix());
+ }
+ return session;
+ }
- public boolean isUserInRole(String role) {
- return getHttpRequest().isUserInRole(role);
- }
+ public Principal getUserPrincipal() {
+ return getHttpRequest().getUserPrincipal();
+ }
- public void log(String message) {
- getServletContext().log(message);
- }
+ public boolean isUserInRole(String role) {
+ return getHttpRequest().isUserInRole(role);
+ }
- public void log(String message, Throwable exception) {
- getServletContext().log(message, exception);
- }
+ public void log(String message) {
+ getServletContext().log(message);
+ }
- public void redirect(String url) throws IOException {
- getHttpResponse().sendRedirect(url);
- FacesContext.getCurrentInstance().responseComplete();
- }
+ public void log(String message, Throwable exception) {
+ getServletContext().log(message, exception);
+ }
+
+ public void redirect(String url) throws IOException {
+ getHttpResponse().sendRedirect(url);
+ FacesContext.getCurrentInstance().responseComplete();
+ }
+
}
Modified:
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/lifecycle/PortalPhaseListener.java
===================================================================
---
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/lifecycle/PortalPhaseListener.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/main/java/org/ajax4jsf/portlet/lifecycle/PortalPhaseListener.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -3,18 +3,22 @@
*/
package org.ajax4jsf.portlet.lifecycle;
+import java.util.Map;
+
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.portlet.faces.Bridge;
+import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.portlet.application.PortletStateHolder;
import org.ajax4jsf.portlet.application.PortletViewState;
+import org.ajax4jsf.portlet.context.AbstractExternalContext;
/**
* @author asmirnov
- *
+ *
*/
public class PortalPhaseListener implements PhaseListener {
@@ -23,39 +27,79 @@
*/
private static final long serialVersionUID = -4023885603543145666L;
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
*/
public void afterPhase(PhaseEvent event) {
PhaseId phaseId = event.getPhaseId();
FacesContext context = event.getFacesContext();
- if(phaseId.equals(PhaseId.RESTORE_VIEW)){
- Object portletPhase =
context.getExternalContext().getRequestMap().get(Bridge.PORTLET_LIFECYCLE_PHASE);
- if(Bridge.PortletPhase.RenderPhase.equals(portletPhase)){
+ if (phaseId.equals(PhaseId.RESTORE_VIEW)) {
+ Object portletPhase = context.getExternalContext().getRequestMap()
+ .get(Bridge.PORTLET_LIFECYCLE_PHASE);
+ if (null == portletPhase) {
+ // For a servlet phase, put all portlet-related parameters back to ajaxContext.
+ Map<String, String> requestParameters = context
+ .getExternalContext().getRequestParameterMap();
+
+ AjaxContext ajaxContext = AjaxContext
+ .getCurrentInstance(context);
+ Map commonAjaxParameters = ajaxContext
+ .getCommonAjaxParameters();
+ commonAjaxParameters
+ .put(
+ AbstractExternalContext.ACTION__PARAMETER,
+ requestParameters
+ .get(AbstractExternalContext.ACTION__PARAMETER));
+ commonAjaxParameters
+ .put(
+ AbstractExternalContext.PORTLET_MODE_PARAMETER,
+ requestParameters
+ .get(AbstractExternalContext.PORTLET_MODE_PARAMETER));
+
+ commonAjaxParameters
+ .put(
+ AbstractExternalContext.NAMESPACE_PARAMETER,
+ requestParameters
+ .get(AbstractExternalContext.NAMESPACE_PARAMETER));
+
+ commonAjaxParameters
+ .put(
+ AbstractExternalContext.PORTLET_NAME_PARAMETER,
+ requestParameters
+ .get(AbstractExternalContext.PORTLET_NAME_PARAMETER));
+
+ } else if (Bridge.PortletPhase.RenderPhase.equals(portletPhase)) {
context.renderResponse();
}
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
*/
public void beforePhase(PhaseEvent event) {
PhaseId phaseId = event.getPhaseId();
FacesContext context = event.getFacesContext();
- if(phaseId.equals(PhaseId.RENDER_RESPONSE)){
- Object portletPhase =
context.getExternalContext().getRequestMap().get(Bridge.PORTLET_LIFECYCLE_PHASE);
- if(null == portletPhase){
- // save request scope variables and Faces Messages.
- PortletViewState windowState =
PortletStateHolder.getInstance(context).getWindowState(context);
- windowState.saveMessages(context);
- windowState.saveBeans(context);
+ if (phaseId.equals(PhaseId.RENDER_RESPONSE)) {
+ Object portletPhase = context.getExternalContext().getRequestMap()
+ .get(Bridge.PORTLET_LIFECYCLE_PHASE);
+ if (null == portletPhase) {
+ // save request scope variables and Faces Messages.
+ PortletViewState windowState = PortletStateHolder.getInstance(
+ context).getWindowState(context);
+ windowState.saveRequest(context);
}
}
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see javax.faces.event.PhaseListener#getPhaseId()
*/
public PhaseId getPhaseId() {
Modified:
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/ContextFactoryTest.java
===================================================================
---
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/ContextFactoryTest.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/ContextFactoryTest.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -66,7 +66,7 @@
FacesContext context = factory.getFacesContext(portletContext,
portletRequest, portletResponse, lifecycle);
assertTrue((context instanceof FacesContextImpl));
- assertTrue((context.getExternalContext() instanceof PortletContextImpl));
+ assertTrue((context.getExternalContext() instanceof AbstractExternalContext));
assertSame(FacesContext.getCurrentInstance(),context);
}
Modified:
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/PortletExternalContextTest.java
===================================================================
---
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/PortletExternalContextTest.java 2007-10-31
21:57:40 UTC (rev 3659)
+++
trunk/extensions/portlet/src/test/java/org/ajax4jsf/portlet/context/PortletExternalContextTest.java 2007-11-01
01:00:16 UTC (rev 3660)
@@ -16,7 +16,7 @@
*
*/
public class PortletExternalContextTest extends AbstractAjax4JsfTestCase {
- private PortletContextImpl portletContextImpl;
+ private AbstractExternalContext portletContextImpl;
private FacesContextImpl portalFacesContext;
private MockPortletContext portletContext;
private MockActionRequest portletRequest;