You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2013/10/22 21:24:56 UTC

svn commit: r1534740 - in /myfaces/tobago/trunk: tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/ tobago-core/src/main/java/org/apache/myfaces/tobago/portlet/ tobago-core/src/main/java/org/apache/myfaces/tobago/util/ tobago-core/src/m...

Author: lofwyr
Date: Tue Oct 22 19:24:55 2013
New Revision: 1534740

URL: http://svn.apache.org/r1534740
Log:
TOBAGO-633: Portlet support
 - working on portlet support

Modified:
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxResponseRenderer.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/portlet/PortletUtils.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/RequestUtils.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/Secret.java
    myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/application/ViewHandlerImpl.java
    myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/lifecycle/RestoreViewExecutor.java

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxResponseRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxResponseRenderer.java?rev=1534740&r1=1534739&r2=1534740&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxResponseRenderer.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/ajax/AjaxResponseRenderer.java Tue Oct 22 19:24:55 2013
@@ -20,26 +20,27 @@
 package org.apache.myfaces.tobago.internal.ajax;
 
 import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import org.apache.myfaces.tobago.component.Attributes;
 import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
 import org.apache.myfaces.tobago.internal.util.ResponseUtils;
 import org.apache.myfaces.tobago.internal.webapp.JsonResponseWriter;
+import org.apache.myfaces.tobago.portlet.PortletUtils;
 import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.apache.myfaces.tobago.util.EncodeAjaxCallback;
 import org.apache.myfaces.tobago.util.RequestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.faces.FactoryFinder;
 import javax.faces.application.StateManager;
 import javax.faces.component.UIComponent;
 import javax.faces.component.UIViewRoot;
-import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
 import javax.faces.render.RenderKit;
 import javax.faces.render.RenderKitFactory;
+import javax.portlet.PortletResponse;
+import javax.portlet.ResourceResponse;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -169,14 +170,13 @@ public class AjaxResponseRenderer {
   }
 
   private PrintWriter getPrintWriter(FacesContext facesContext) throws IOException {
-    ExternalContext externalContext = facesContext.getExternalContext();
-    //TODO: fix this to work in PortletRequest as well
-    if (externalContext.getResponse() instanceof HttpServletResponse) {
-      final HttpServletResponse httpServletResponse
-          = (HttpServletResponse) externalContext.getResponse();
-      return httpServletResponse.getWriter();
+    final Object response = facesContext.getExternalContext().getResponse();
+    if (response instanceof HttpServletResponse) {
+      return ((HttpServletResponse) response).getWriter();
+    } else if (PortletUtils.isPortletApiAvailable() && response instanceof PortletResponse) {
+      return ((ResourceResponse) response).getWriter();
     }
-    throw new IOException("No ResponseWriter found for ExternalContext " + externalContext);
+    throw new IOException("No ResponseWriter found for response " + response);
   }
 
   private JsonResponseWriter getJsonResponseWriter(RenderKit renderKit, PrintWriter writer) {

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/portlet/PortletUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/portlet/PortletUtils.java?rev=1534740&r1=1534739&r2=1534740&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/portlet/PortletUtils.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/portlet/PortletUtils.java Tue Oct 22 19:24:55 2013
@@ -19,36 +19,23 @@
 
 package org.apache.myfaces.tobago.portlet;
 
-import javax.faces.context.FacesContext;
-import javax.portlet.ActionRequest;
-import javax.portlet.PortletContext;
 import javax.portlet.PortletRequest;
-import javax.portlet.PortletSession;
-import javax.portlet.PortletURL;
-import javax.portlet.RenderResponse;
-import java.io.UnsupportedEncodingException;
-
 
 /**
  * Static utility class for portlet-related operations.
  */
 public final class PortletUtils {
 
-  private static final boolean PORTLET_API_AVAILABLE = portletApiAvailable();
-
-  /**
-   * This flag is imbedded in the request.
-   * It signifies that the request is coming from a portlet.
-   */
-//  public static final String PORTLET_REQUEST = PortletUtils.class.getName() + ".PORTLET_REQUEST";
-  private static final String VIEW_ID = PortletUtils.class.getName() + ".VIEW_ID";
+  private static final boolean PORTLET_API_AVAILABLE;
 
-  private static boolean portletApiAvailable() {
+  static {
+    boolean result;
     try {
-      return PortletRequest.class != null; // never false
+      result = PortletRequest.class != null;
     } catch (NoClassDefFoundError e) {
-      return false;
+      result = false;
     }
+    PORTLET_API_AVAILABLE = result;
   }
 
   private PortletUtils() {
@@ -56,58 +43,13 @@ public final class PortletUtils {
   }
 
   /**
-   * Determine if we are processing a portlet RenderResponse.
+   * The Portlet API is optional in the class path. We are only allowed to check instance of e. g. PortletRequest,
+   * if the API exists.
    *
-   * @param facesContext The current FacesContext.
-   * @return <code>true</code> if we are processing a RenderResponse,
-   * <code>false</code> otherwise.
+   * @return Are the Portlet classes available?
    */
-  public static boolean isRenderResponse(FacesContext facesContext) {
-    return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getResponse() instanceof RenderResponse;
-  }
-
-  /**
-   * Determine if we are running as a portlet.
-   *
-   * @param facesContext The current FacesContext.
-   * @return <code>true</code> if we are running as a portlet,
-   * <code>false</code> otherwise.
-   */
-//  public static boolean isPortletRequest(FacesContext facesContext) {
-//    return facesContext.getExternalContext().getSessionMap().get(PORTLET_REQUEST) != null;
-//  }
-  public static boolean isPortletRequest(FacesContext facesContext) {
-    return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getContext() instanceof PortletContext;
-  }
-
-  public static String getViewId(FacesContext facesContext) {
-    PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest();
-    return request.getParameter(PortletUtils.VIEW_ID);
+  public static boolean isPortletApiAvailable() {
+    return PORTLET_API_AVAILABLE;
   }
 
-  /**
-   * @return The action url.
-   */
-  public static String setViewIdForUrl(FacesContext facesContext, String viewId) {
-    RenderResponse response = (RenderResponse) facesContext.getExternalContext().getResponse();
-    PortletURL url = response.createActionURL();
-    url.setParameter(VIEW_ID, viewId);
-    return url.toString();
-  }
-
-  public static void ensureEncoding(FacesContext facesContext) throws UnsupportedEncodingException {
-    ActionRequest request = (ActionRequest) facesContext.getExternalContext().getRequest();
-    if (request.getCharacterEncoding() == null) {
-      request.setCharacterEncoding("UTF-8");
-    }
-  }
-
-  public static Object getAttributeFromSessionForApplication(Object session, String name) {
-
-    if (PORTLET_API_AVAILABLE && session instanceof PortletSession) {
-      return ((PortletSession) session).getAttribute(name, PortletSession.APPLICATION_SCOPE);
-    } else {
-      throw new IllegalArgumentException("Unknown session type: " + session.getClass().getName());
-    }
-  }
 }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/RequestUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/RequestUtils.java?rev=1534740&r1=1534739&r2=1534740&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/RequestUtils.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/RequestUtils.java Tue Oct 22 19:24:55 2013
@@ -19,11 +19,12 @@
 
 package org.apache.myfaces.tobago.util;
 
+import org.apache.myfaces.tobago.portlet.PortletUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.apache.myfaces.tobago.portlet.PortletUtils;
 
 import javax.faces.context.FacesContext;
+import javax.portlet.ClientDataRequest;
 import javax.servlet.http.HttpServletRequest;
 import java.io.UnsupportedEncodingException;
 
@@ -34,19 +35,21 @@ public class RequestUtils {
   private RequestUtils() {
   }
 
-  public static void ensureEncoding(FacesContext facesContext) {
-    Object requestObject = facesContext.getExternalContext().getRequest();
+  public static void ensureEncoding(final FacesContext facesContext) {
+    final Object request = facesContext.getExternalContext().getRequest();
     try {
-      if (requestObject instanceof HttpServletRequest) {
-        HttpServletRequest request = (HttpServletRequest) requestObject;
-        if (request.getCharacterEncoding() == null) {
-          request.setCharacterEncoding("UTF-8");
+      if (request instanceof HttpServletRequest) {
+        final HttpServletRequest servletRequest = (HttpServletRequest) request;
+        if (servletRequest.getCharacterEncoding() == null) {
+          servletRequest.setCharacterEncoding("UTF-8");
+        }
+      } else if (PortletUtils.isPortletApiAvailable() && request instanceof ClientDataRequest) {
+        final ClientDataRequest portletRequest = (ClientDataRequest) request;
+        if (portletRequest.getCharacterEncoding() == null) {
+          portletRequest.setCharacterEncoding("UTF-8");
         }
-      } else if (PortletUtils.isPortletRequest(facesContext)) {
-        PortletUtils.ensureEncoding(facesContext);
       }
-
-    } catch (UnsupportedEncodingException e) {
+    } catch (final UnsupportedEncodingException e) {
       LOG.error("" + e, e);
     }
   }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/Secret.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/Secret.java?rev=1534740&r1=1534739&r2=1534740&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/Secret.java (original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/webapp/Secret.java Tue Oct 22 19:24:55 2013
@@ -26,6 +26,7 @@ import org.apache.myfaces.tobago.renderk
 import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
 
 import javax.faces.context.FacesContext;
+import javax.portlet.PortletSession;
 import javax.servlet.http.HttpSession;
 import java.io.IOException;
 import java.io.Serializable;
@@ -84,8 +85,10 @@ public class Secret implements Serializa
     final Secret secret;
     if (session instanceof HttpSession) {
       secret = (Secret) ((HttpSession) session).getAttribute(Secret.KEY);
+    } else if (PortletUtils.isPortletApiAvailable() && session instanceof PortletSession) {
+      secret = (Secret) ((PortletSession) session).getAttribute(Secret.KEY, PortletSession.APPLICATION_SCOPE);
     } else {
-      secret = (Secret) PortletUtils.getAttributeFromSessionForApplication(session, Secret.KEY);
+      throw new IllegalArgumentException("Unknown session type: " + session);
     }
     return secret != null && secret.secret.equals(fromRequest);
   }
@@ -102,8 +105,10 @@ public class Secret implements Serializa
     final Secret secret;
     if (session instanceof HttpSession) {
       secret = (Secret) ((HttpSession) session).getAttribute(Secret.KEY);
+    } else if (PortletUtils.isPortletApiAvailable() && session instanceof PortletSession) {
+      secret = (Secret) ((PortletSession) session).getAttribute(Secret.KEY, PortletSession.APPLICATION_SCOPE);
     } else {
-      secret = (Secret) PortletUtils.getAttributeFromSessionForApplication(session, Secret.KEY);
+      throw new IllegalArgumentException("Unknown session type: " + session);
     }
     writer.writeAttribute(HtmlAttributes.VALUE, secret.secret, false);
     writer.endElement(HtmlElements.INPUT);

Modified: myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/application/ViewHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/application/ViewHandlerImpl.java?rev=1534740&r1=1534739&r2=1534740&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/application/ViewHandlerImpl.java (original)
+++ myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/application/ViewHandlerImpl.java Tue Oct 22 19:24:55 2013
@@ -19,11 +19,10 @@
 
 package org.apache.myfaces.tobago.internal.application;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.myfaces.tobago.portlet.PortletUtils;
 import org.apache.myfaces.tobago.util.DebugUtils;
 import org.apache.myfaces.tobago.util.RequestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.faces.FacesException;
 import javax.faces.application.ViewHandler;
@@ -82,10 +81,12 @@ public class ViewHandlerImpl extends Vie
 
   public String getActionURL(FacesContext facesContext, String viewId) {
 
+/*
     if (PortletUtils.isRenderResponse(facesContext)) {
       return PortletUtils.setViewIdForUrl(facesContext, viewId);
     }
 
+*/
     return base.getActionURL(facesContext, viewId);
   }
 

Modified: myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/lifecycle/RestoreViewExecutor.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/lifecycle/RestoreViewExecutor.java?rev=1534740&r1=1534739&r2=1534740&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/lifecycle/RestoreViewExecutor.java (original)
+++ myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/internal/lifecycle/RestoreViewExecutor.java Tue Oct 22 19:24:55 2013
@@ -21,7 +21,6 @@ package org.apache.myfaces.tobago.intern
 
 import org.apache.myfaces.tobago.config.TobagoConfig;
 import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
-import org.apache.myfaces.tobago.portlet.PortletUtils;
 import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.apache.myfaces.tobago.webapp.Secret;
 import org.slf4j.Logger;
@@ -163,10 +162,12 @@ class RestoreViewExecutor implements Pha
   private static String deriveViewId(FacesContext facesContext) {
     ExternalContext externalContext = facesContext.getExternalContext();
 
+/*
     if (PortletUtils.isPortletRequest(facesContext)) {
       return PortletUtils.getViewId(facesContext);
     }
 
+*/
     String viewId = externalContext.getRequestPathInfo(); // getPathInfo
     if (viewId == null) {
       // No extra path info found, so it is probably extension mapping