You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mc...@apache.org on 2007/10/24 19:13:13 UTC

svn commit: r587932 - in /myfaces/trinidad/trunk/trinidad: trinidad-api/ trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/ trinidad-impl/src/main/java/org/apache/myfaces...

Author: mcooper
Date: Wed Oct 24 10:13:11 2007
New Revision: 587932

URL: http://svn.apache.org/viewvc?rev=587932&view=rev
Log:
TRINIDAD-666: Make ExternalContextUtils part of the public API

Thank you Scott O'Bryan for the patch.

Added:
    myfaces/trinidad/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java
Removed:
    myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/util/ExternalContextUtils.java
Modified:
    myfaces/trinidad/trunk/trinidad/trinidad-api/pom.xml
    myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java
    myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/dispatch/DispatchResponseConfiguratorImpl.java
    myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java
    myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/RequestContextImpl.java
    myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/util/MultipartFormHandler.java

Modified: myfaces/trinidad/trunk/trinidad/trinidad-api/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-api/pom.xml?rev=587932&r1=587931&r2=587932&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-api/pom.xml (original)
+++ myfaces/trinidad/trunk/trinidad/trinidad-api/pom.xml Wed Oct 24 10:13:11 2007
@@ -150,6 +150,11 @@
     </dependency>
 
     <dependency>
+      <groupId>portlet-api</groupId>
+      <artifactId>portlet-api</artifactId>
+    </dependency>
+
+    <dependency>
       <groupId>org.apache.myfaces.core</groupId>
       <artifactId>myfaces-api</artifactId>
     </dependency>

Added: myfaces/trinidad/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java?rev=587932&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java (added)
+++ myfaces/trinidad/trunk/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java Wed Oct 24 10:13:11 2007
@@ -0,0 +1,266 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.myfaces.trinidad.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.faces.context.ExternalContext;
+import javax.portlet.ActionRequest;
+import javax.servlet.ServletRequest;
+
+import org.apache.myfaces.trinidad.logging.TrinidadLogger;
+
+/**
+ * This provides some functionality for determining some things about the
+ * native request object that is not provided by the base utils.
+ *
+ * @version $Revision$ $Date$
+ */
+public class ExternalContextUtils
+{
+  /**
+   * Returns the character encoding or null if there isn't any
+   *
+   * @param externalContext
+   * @return
+   */
+  public static String getCharacterEncoding(final ExternalContext externalContext)
+  {
+    if (isAction(externalContext))
+    {
+      try
+      {
+        if (isPortlet(externalContext))
+        {
+          // Allows us to not have the portal api's in the classpath
+          return _getPortletCharacterEncoding(externalContext.getRequest());
+        }
+        else
+        {
+          return ((ServletRequest) externalContext.getRequest()).getCharacterEncoding();
+        }
+      }
+      catch (final ClassCastException e)
+      {
+        _LOG.severe(e);
+      }
+    }
+
+    return null;
+  }
+
+  /**
+   * Returns the content length or -1 if the unknown.
+   *
+   * @param externalContext
+   *          the ExternalContext
+   * @return the length or -1
+   */
+  public static int getContentLength(final ExternalContext externalContext)
+  {
+    if (isAction(externalContext))
+    {
+      try
+      {
+        if (isPortlet(externalContext))
+        {
+          // Allows us to not have the portal api's in the classpath
+          _getPortletContentLength(externalContext.getRequest());
+        }
+        else
+        {
+          return ((ServletRequest) externalContext.getRequest()).getContentLength();
+        }
+      }
+      catch (final ClassCastException e)
+      {
+        _LOG.severe(e);
+      }
+    }
+    return -1;
+  }
+
+  /**
+   * Returns the content type from the current externalContext or <code>null</code> if unknown.
+   *
+   * @param externalContext
+   *          the ExternalContext
+   * @return the content type or <code>null</code>
+   */
+  public static String getContentType(final ExternalContext externalContext)
+  {
+    if (isAction(externalContext))
+    {
+      try
+      {
+        if (isPortlet(externalContext))
+        {
+          // Allows us to not have the portal api's in the classpath
+          return _getPortletContentType(externalContext.getRequest());
+        }
+        else
+        {
+          return ((ServletRequest) externalContext.getRequest()).getContentType();
+        }
+      }
+      catch (final ClassCastException e)
+      {
+        // probably won't happen, but it could if we don't have a portlet OR a servlet container.
+        _LOG.severe(e);
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Returns the request input stream if one is available
+   *
+   * @param externalContext
+   * @return
+   * @throws IOException
+   */
+  public static InputStream getRequestInputStream(final ExternalContext externalContext)
+      throws IOException
+  {
+    if (isAction(externalContext))
+    {
+      try
+      {
+        if (isPortlet(externalContext))
+        {
+          // Allows us to not have the portal api's in the classpath
+          return _getPortletInputStream(externalContext.getRequest());
+        }
+        else
+        {
+          return ((ServletRequest) externalContext.getRequest()).getInputStream();
+        }
+      }
+      catch (final ClassCastException e)
+      {
+        _LOG.severe(e);
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Returns <code>true</code> if this externalContext represents an "action". An action request
+   * is any ServletRequest or a portlet ActionRequest. It is assumed that the ExternalContext
+   *
+   * @return a boolean of <code>true</code> if this is a Portlet ActionRequest or an non-portlet
+   *         request.
+   */
+  public static boolean isAction(final ExternalContext externalContext)
+  {
+    final Object request = externalContext.getRequest();
+
+    if (_PORTLET_ACTION_REQUEST_CLASS == null)
+    {
+      _LOG
+          .fine("Portlet API's are not on the classpath so isAction will only check for servlet request.");
+      return request instanceof ServletRequest;
+    }
+
+    return request instanceof ServletRequest || _PORTLET_ACTION_REQUEST_CLASS.isInstance(request);
+  }
+
+  /**
+   * Returns whether or not this external context is from a Portlet or a Servlet.
+   *
+   * @param externalContext
+   *          the ExternalContext to check
+   *
+   * @return <code>true</code> if this is a portlet RenderRequest or ActionRequest and
+   *         <code>false<code> if it is not.
+   */
+  public static boolean isPortlet(final ExternalContext externalContext)
+  {
+    if (_PORTLET_CONTEXT_CLASS == null)
+    {
+      _LOG.fine("Portlet API's are not on the classpath therefore isPortlet is false.");
+      return false;
+    }
+
+    return _PORTLET_CONTEXT_CLASS.isInstance(externalContext.getContext());
+  }
+
+  private static final String _getPortletCharacterEncoding(final Object request)
+  {
+    if (!(request instanceof ActionRequest))
+      return null;
+
+    return ((ActionRequest) request).getCharacterEncoding();
+  }
+
+  private static final int _getPortletContentLength(final Object request)
+  {
+    if (!(request instanceof ActionRequest))
+      return -1;
+
+    return ((ActionRequest) request).getContentLength();
+  }
+
+  private static final String _getPortletContentType(final Object request)
+  {
+    if (!(request instanceof ActionRequest))
+      return null;
+
+    return ((ActionRequest) request).getContentType();
+  }
+
+  private static final InputStream _getPortletInputStream(final Object request) throws IOException
+  {
+    return ((ActionRequest) request).getPortletInputStream();
+  }
+
+  // prevent this from being instantiated
+  private ExternalContextUtils()
+  {}
+
+  private static final TrinidadLogger _LOG = TrinidadLogger
+                                               .createTrinidadLogger(ExternalContextUtils.class);
+
+  // =-= Scott O'Bryan =-=
+  // Performance enhancement. These will be needed anyway, let's not get them every time.
+  private static final Class<?>       _PORTLET_ACTION_REQUEST_CLASS;
+  private static final Class<?>       _PORTLET_CONTEXT_CLASS;
+
+  static
+  {
+    Class<?> context;
+    Class<?> actionRequest;
+    try
+    {
+      context = ClassLoaderUtils.loadClass("javax.portlet.PortletContext");
+      actionRequest = ClassLoaderUtils.loadClass("javax.portlet.ActionRequest");
+    }
+    catch (final ClassNotFoundException e)
+    {
+      _LOG
+          .fine("Portlet API is not available on the classpath.  Portlet configurations are disabled.");
+      context = null;
+      actionRequest = null;
+    }
+
+    _PORTLET_CONTEXT_CLASS = context;
+    _PORTLET_ACTION_REQUEST_CLASS = actionRequest;
+  }
+}

Modified: myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java?rev=587932&r1=587931&r2=587932&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java Wed Oct 24 10:13:11 2007
@@ -34,7 +34,7 @@
 import org.apache.myfaces.trinidadinternal.context.RequestContextFactoryImpl;
 import org.apache.myfaces.trinidadinternal.skin.SkinFactoryImpl;
 import org.apache.myfaces.trinidadinternal.skin.SkinUtils;
-import org.apache.myfaces.trinidadinternal.util.ExternalContextUtils;
+import org.apache.myfaces.trinidad.util.ExternalContextUtils;
 
 /**
  * This is the implementation of the Trinidad's Global configurator. It provides the entry point for

Modified: myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/dispatch/DispatchResponseConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/dispatch/DispatchResponseConfiguratorImpl.java?rev=587932&r1=587931&r2=587932&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/dispatch/DispatchResponseConfiguratorImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/dispatch/DispatchResponseConfiguratorImpl.java Wed Oct 24 10:13:11 2007
@@ -33,7 +33,7 @@
 
 import org.apache.myfaces.trinidad.config.Configurator;
 import org.apache.myfaces.trinidad.context.ExternalContextDecorator;
-import org.apache.myfaces.trinidadinternal.util.ExternalContextUtils;
+import org.apache.myfaces.trinidad.util.ExternalContextUtils;
 import org.apache.myfaces.trinidadinternal.webapp.wrappers.PortletContextWrapper;
 
 /**

Modified: myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java?rev=587932&r1=587931&r2=587932&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/upload/FileUploadConfiguratorImpl.java Wed Oct 24 10:13:11 2007
@@ -33,7 +33,7 @@
 import org.apache.myfaces.trinidad.model.UploadedFile;
 import org.apache.myfaces.trinidadinternal.share.util.MultipartFormHandler;
 import org.apache.myfaces.trinidadinternal.share.util.MultipartFormItem;
-import org.apache.myfaces.trinidadinternal.util.ExternalContextUtils;
+import org.apache.myfaces.trinidad.util.ExternalContextUtils;
 
 /**
  * This configurator will handle the FileUploads for Trinidad.
@@ -97,8 +97,7 @@
     // as possible is a good thing
     //Process MultipartForm if need be
     if (MultipartFormHandler.isMultipartRequest(externalContext) &&
-       (externalContext.getRequest() instanceof HttpServletRequest ||
-          ExternalContextUtils.isPortlet(externalContext)))
+       (externalContext.getRequest() instanceof HttpServletRequest || ExternalContextUtils.isPortlet(externalContext)))
     {
       try
       {

Modified: myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/RequestContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/RequestContextImpl.java?rev=587932&r1=587931&r2=587932&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/RequestContextImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/RequestContextImpl.java Wed Oct 24 10:13:11 2007
@@ -64,7 +64,7 @@
 import org.apache.myfaces.trinidadinternal.renderkit.core.CoreRenderKit;
 import org.apache.myfaces.trinidadinternal.share.config.UIXCookie;
 import org.apache.myfaces.trinidadinternal.ui.expl.ColorPaletteUtils;
-import org.apache.myfaces.trinidadinternal.util.ExternalContextUtils;
+import org.apache.myfaces.trinidad.util.ExternalContextUtils;
 import org.apache.myfaces.trinidadinternal.util.nls.LocaleUtils;
 import org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl;
 

Modified: myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/util/MultipartFormHandler.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/util/MultipartFormHandler.java?rev=587932&r1=587931&r2=587932&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/util/MultipartFormHandler.java (original)
+++ myfaces/trinidad/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/share/util/MultipartFormHandler.java Wed Oct 24 10:13:11 2007
@@ -28,7 +28,7 @@
 
 import javax.faces.context.ExternalContext;
 
-import org.apache.myfaces.trinidadinternal.util.ExternalContextUtils;
+import org.apache.myfaces.trinidad.util.ExternalContextUtils;
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 
 /**
@@ -54,7 +54,7 @@
    */
   static public boolean isMultipartRequest(final ExternalContext externalContext)
   {
-    final String contentType =   ExternalContextUtils.getContentType(externalContext);
+    final String contentType = ExternalContextUtils.getContentType(externalContext);
     
     if (contentType == null)
     {