You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2010/02/05 16:27:42 UTC
svn commit: r906968 - in
/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util:
ClassLoaderUtils.java ExternalContextUtils.java RequestType.java
Author: matzew
Date: Fri Feb 5 15:27:42 2010
New Revision: 906968
URL: http://svn.apache.org/viewvc?rev=906968&view=rev
Log:
added some util: Externalcontext (required the more fine grainded type|ClassLoaderUtil). Code take
from Trinidad
Added:
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ClassLoaderUtils.java
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/RequestType.java
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ClassLoaderUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ClassLoaderUtils.java?rev=906968&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ClassLoaderUtils.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ClassLoaderUtils.java Fri Feb 5 15:27:42 2010
@@ -0,0 +1,343 @@
+/*
+ * 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.shared.util;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.IOException;
+
+import java.io.InputStreamReader;
+
+import java.net.URL;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.List;
+
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Utility methods for accessing classes and resources using an appropriate
+ * class loader.
+ *
+ * @version $Revision: $ $Date: $
+ */
+public final class ClassLoaderUtils
+{
+ // Utility class only, no instances
+ private ClassLoaderUtils()
+ {
+ }
+
+ /**
+ * Loads the class with the specified name. For Java 2 callers, the
+ * current thread's context class loader is preferred, falling back on the
+ * system class loader of the caller when the current thread's context is not
+ * set, or the caller is pre Java 2.
+ *
+ * @param name the name of the class
+ * @return the resulting <code>Class</code> object
+ * @exception ClassNotFoundException if the class was not found
+ */
+ public static Class<?> loadClass(
+ String name) throws ClassNotFoundException
+ {
+ return loadClass(name, null);
+ }
+
+ /**
+ * Locates the resource with the specified name. For Java 2 callers, the
+ * current thread's context class loader is preferred, falling back on the
+ * system class loader of the caller when the current thread's context is not
+ * set, or the caller is pre Java 2.
+ *
+ * @param name the name of the resource
+ * @return the resulting <code>URL</code> object
+ */
+ public static URL getResource(
+ String name)
+ {
+ return getResource(name, null);
+ }
+
+ /**
+ * Locates the stream resource with the specified name. For Java 2 callers,
+ * the current thread's context class loader is preferred, falling back on
+ * the system class loader of the caller when the current thread's context is
+ * not set, or the caller is pre Java 2.
+ *
+ * @param name the name of the resource
+ * @return the resulting <code>InputStream</code> object
+ */
+ public static InputStream getResourceAsStream(
+ String name)
+ {
+ return getResourceAsStream(name, null);
+ }
+
+ /**
+ * Loads the class with the specified name. For Java 2 callers, the
+ * current thread's context class loader is preferred, falling back on the
+ * class loader of the caller when the current thread's context is not set,
+ * or the caller is pre Java 2. If the callerClassLoader is null, then
+ * fall back on the system class loader.
+ *
+ * @param name the name of the class
+ * @param callerClassLoader the calling class loader context
+ * @return the resulting <code>Class</code> object
+ * @exception ClassNotFoundException if the class was not found
+ */
+ public static Class<?> loadClass(
+ String name,
+ ClassLoader callerClassLoader) throws ClassNotFoundException
+ {
+ Class<?> clazz = null;
+
+ try
+ {
+ ClassLoader loader = getContextClassLoader();
+
+ if (loader != null)
+ clazz = loader.loadClass(name);
+ }
+ catch (ClassNotFoundException e)
+ {
+ // treat as though loader not set
+ ;
+ }
+
+ if (clazz == null)
+ {
+ if (callerClassLoader != null)
+ clazz = callerClassLoader.loadClass(name);
+ else
+ clazz = Class.forName(name);
+ }
+
+ return clazz;
+ }
+
+ /**
+ * Locates the resource with the specified name. For Java 2 callers, the
+ * current thread's context class loader is preferred, falling back on the
+ * class loader of the caller when the current thread's context is not set,
+ * or the caller is pre Java 2. If the callerClassLoader is null, then
+ * fall back on the system class loader.
+ *
+ * @param name the name of the resource
+ * @param callerClassLoader the calling class loader context
+ * @return the resulting <code>URL</code> object
+ */
+ public static URL getResource(
+ String name,
+ ClassLoader callerClassLoader)
+ {
+ _checkResourceName(name);
+
+ URL url = null;
+
+ ClassLoader loader = getContextClassLoader();
+
+ if (loader != null)
+ url = loader.getResource(name);
+
+ if (url == null)
+ {
+ if (callerClassLoader != null)
+ url = callerClassLoader.getResource(name);
+ else
+ url = ClassLoader.getSystemResource(name);
+ }
+
+ return url;
+ }
+
+ /**
+ * Locates the resource stream with the specified name. For Java 2 callers,
+ * the current thread's context class loader is preferred, falling back on
+ * the class loader of the caller when the current thread's context is not
+ * set, or the caller is pre Java 2. If the callerClassLoader is null, then
+ * fall back on the system class loader.
+ *
+ * @param name the name of the resource
+ * @param callerClassLoader the calling class loader context
+ * @return the resulting <code>InputStream</code> object
+ */
+ public static InputStream getResourceAsStream(
+ String name,
+ ClassLoader callerClassLoader)
+ {
+ _checkResourceName(name);
+
+ InputStream stream = null;
+
+ ClassLoader loader = getContextClassLoader();
+
+ if (loader != null)
+ stream = loader.getResourceAsStream(name);
+
+ if (stream == null)
+ {
+ if (callerClassLoader != null)
+ stream = callerClassLoader.getResourceAsStream(name);
+ else
+ stream = ClassLoader.getSystemResourceAsStream(name);
+ }
+
+ return stream;
+ }
+
+ /**
+ * Dynamically accesses the current context class loader.
+ * Returns null if there is no per-thread context class loader.
+ */
+ public static ClassLoader getContextClassLoader()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+
+ /**
+ * Instantiate a service from a file in /META-INF/services.
+ * <P>
+ * The following is an excerpt from the JAR File specification:
+ * A service provider identifies itself by placing a provider-configuration file
+ * in the resource directory META-INF/services.
+ * The file's name should consist of the fully-qualified name of the abstract service class.
+ * The file should contain a newline-separated list of unique concrete provider-class names.
+ * Space and tab characters, as well as blank lines, are ignored. The comment character is '#' (0x23);
+ * on each line all characters following the first comment character are ignored.
+ * The file must be encoded in UTF-8.
+ *
+ * @param service the classname of the abstract service class.
+ * eg: javax.servlet.Filter
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> List<T> getServices(String service)
+ {
+ String serviceUri ="META-INF/services/" + service;
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ Enumeration<URL> urls = loader.getResources(serviceUri);
+ if (urls.hasMoreElements())
+ {
+ List<T> services = new ArrayList<T>(1);
+ Set<String> keys = new HashSet<String>(20);
+ do
+ {
+ URL url = urls.nextElement();
+
+ if (_LOG.isLoggable(Level.FINEST))
+ {
+ _LOG.finest("Processing: " + url);
+ }
+ try
+ {
+ BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
+ try
+ {
+ while(true)
+ {
+ String line = in.readLine();
+ if (line == null)
+ break;
+
+ String className = _parseLine(line);
+
+ if(className!=null && keys.add(className))
+ {
+ T instance = (T) _getClass(loader, className);
+ services.add(instance);
+ }
+ }
+ }
+ finally
+ {
+ in.close();
+ }
+ }
+ catch (Exception e)
+ {
+ if (_LOG.isLoggable(Level.WARNING))
+ {
+ _LOG.log(Level.WARNING, "Error parsing URL: " + url, e);
+ }
+ }
+ }
+ while(urls.hasMoreElements());
+
+ if (services.size() == 1)
+ return Collections.singletonList(services.get(0));
+
+ return Collections.unmodifiableList(services);
+ }
+ }
+ catch (IOException e)
+ {
+ if (_LOG.isLoggable(Level.SEVERE))
+ {
+ _LOG.log(Level.SEVERE, "Error loading Resource: " + serviceUri, e);
+ }
+ }
+
+ return Collections.emptyList();
+ }
+
+ private static String _parseLine(String line)
+ {
+ // Eliminate any comments
+ int hashIndex = line.indexOf('#');
+ if (hashIndex >= 0)
+ line = line.substring(0, hashIndex);
+
+ // and any whitespace
+ line = line.trim();
+ if (line.length() > 0)
+ {
+ return line;
+ }
+
+ return null;
+ }
+
+ private static Object _getClass(ClassLoader loader, String className)
+ throws ClassNotFoundException, InstantiationException,
+ IllegalAccessException
+ {
+ Class<?> clazz = loader.loadClass(className);
+ return clazz.newInstance();
+ }
+
+ private static void _checkResourceName(String name)
+ {
+ if ((name != null) && name.startsWith("/"))
+ {
+ if (_LOG.isLoggable(Level.WARNING))
+ {
+ _LOG.log(Level.WARNING, "Resource name not portable: " +name);
+ }
+ }
+ }
+
+ private static final Logger _LOG = Logger.getLogger(ClassLoaderUtils.class.getName());
+}
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java?rev=906968&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java Fri Feb 5 15:27:42 2010
@@ -0,0 +1,577 @@
+/*
+ * 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.shared.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.lang.reflect.Method;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.faces.context.ExternalContext;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * This provides some functionality for determining some things about the
+ * native request object that is not provided by the base utils.
+ *
+ * @version 2.0
+ */
+public final class ExternalContextUtils
+{
+
+ /**
+ * Returns <code>true</code> if a particular class relating to the supplied
+ * request type is available on the current classpath or <code>false</code>
+ * if it is not. This class assumes that all containers have a servlet type
+ * request available, but the portlet request types are all dependant on the
+ * portlet container being used.
+ *
+ * @param type the RequestType to test
+ * @return a boolean value of <code>true</code> if the container contains the
+ * request type in the classpath
+ * @since 2.0
+ */
+ public static final boolean isRequestTypeAvailable(RequestType type)
+ {
+ switch (type)
+ {
+ case SERVLET:
+ return true;
+
+ case ACTION:
+ case RENDER:
+ return _PORTLET_CONTEXT_CLASS != null;
+
+ case RESOURCE:
+ case EVENT:
+ return _PORTLET_RENDER_REQUEST_CLASS != null;
+
+ default:
+ return false;
+ }
+ }
+
+ /**
+ * Returns <code>true</code> if a particular request type is supported by the
+ * container. For a request type to be supported, the required objects must
+ * be on the classpath AND and, in the case of Portlet RequestTypes, an
+ * appropriate bridge must be avaialble which supports those objects. This
+ * means that if the supplied RequestType is RESOURCE, the
+ * javax.portlet.ResourceRequest object must be available on the classpath AND
+ * a bridge which supports the Portlet 2.0 specification would also need to be
+ * available.
+ *
+ * @param type the RequestType to test
+ * @return a boolean value of <code>true</code> if the container supports the
+ * current request type
+ * @since 2.0
+ */
+ public static final boolean isRequestTypeSupported(RequestType type)
+ {
+ switch (type)
+ {
+ case SERVLET:
+ return true;
+
+ case ACTION:
+ case RENDER:
+ return _PORTLET_10_SUPPORTED;
+
+ case RESOURCE:
+ case EVENT:
+ return _PORTLET_20_SUPPORTED;
+
+ default:
+ return false;
+ }
+ }
+
+ /**
+ * Returns the requestType of this ExternalContext.
+ *
+ * @param ec the current external context
+ * @return the appropriate RequestType for this external context
+ * @see RequestType
+ * @since 2.0
+ */
+ public static final RequestType getRequestType(ExternalContext ec)
+ {
+ // Stuff is laid out strangely in this class in order to optimize
+ // performance. We want to do as few instanceof's as possible so
+ // things are laid out according to the expected frequency of the
+ // various requests occurring.
+ if(_PORTLET_10_SUPPORTED)
+ {
+ if (_PORTLET_CONTEXT_CLASS.isInstance(ec.getContext()))
+ {
+ //We are inside of a portlet container
+ Object request = ec.getRequest();
+
+ if(_PORTLET_RENDER_REQUEST_CLASS.isInstance(request))
+ {
+ return RequestType.RENDER;
+ }
+
+ if(_PORTLET_RESOURCE_REQUEST_CLASS != null)
+ {
+ if(_PORTLET_ACTION_REQUEST_CLASS.isInstance(request))
+ {
+ return RequestType.ACTION;
+ }
+
+ // We are in a JSR-286 container
+ if(_PORTLET_RESOURCE_REQUEST_CLASS.isInstance(request))
+ {
+ return RequestType.RESOURCE;
+ }
+
+ return RequestType.EVENT;
+ }
+
+ return RequestType.ACTION;
+ }
+ }
+
+ return RequestType.SERVLET;
+ }
+
+ /**
+ * Returns the current active session id or <code>null</code> if there is
+ * none. If a session is not already created, this method will create one
+ * for you.
+ *
+ * @param ec the current external context
+ * @return a string containing the requestedSessionId
+ */
+ public static String getSessionId(ExternalContext ec)
+ {
+ return getSessionId(ec, true);
+ }
+
+ /**
+ * Returns the current active session id or <code>null</code> if there is
+ * none.
+ *
+ * @param ec the current external context
+ * @param create create a new session if one is not created
+ * @return a string containing the requestedSessionId
+ */
+ public static String getSessionId(ExternalContext ec, boolean create)
+ {
+ Object session = ec.getSession(create);
+ return (null!=session)?(String)_runMethod(session, "getId"):null;
+ }
+
+ /**
+ * Returns the session ID for the client, or <code>null</code> if there is none.
+ *
+ * @param ec the current external context
+ * @return a string containing the requestedSessionId
+ */
+ public static String getRequestedSessionId(ExternalContext ec)
+ {
+ return (String) _runMethod(ec.getRequest(), "getRequestedSessionId");
+ }
+
+ /**
+ * Checks if the requested session ID is still valid.
+ *
+ * @param ec the current external context
+ * @return a boolean containing <code>true</code> if the request session is
+ * valid or <code>false</code> if it is not
+ */
+ public static boolean isRequestedSessionIdValid(ExternalContext ec)
+ {
+ return (Boolean) _runMethod(ec.getRequest(), "isRequestedSessionIdValid");
+ }
+
+ /**
+ * Returns the contextPath of the ServletContext or <code>null</code> for portlets
+ *
+ * @param ec the current external context
+ * @return a String containing the servletContextPath
+ */
+ public static String getServletContextPath(ExternalContext ec)
+ {
+ if(!isPortlet(ec))
+ {
+ return ((ServletContext) ec.getContext()).getContextPath();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Returns the contextPath of the ServletRequest or <code>null</code> for portlet requests
+ *
+ * @param ec the current external context
+ * @return a String containing the request context path
+ * @see ExternalContext#getRequestContextPath()
+ *
+ * @deprecated use ExternalContext.getRequestContextPath() as of JSF 1.2. This method
+ * does not appropriately handle portlet environments, but the functionality
+ * is maintained to prevent needing to change the contract.
+ */
+ @Deprecated
+ public static String getRequestContextPath(ExternalContext ec)
+ {
+ if(!isPortlet(ec))
+ {
+ return ec.getRequestContextPath();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Returns the requestURI of the HttpServletRequest or <code>null</code> for
+ * portlet requests
+ *
+ * @param ec the current external context
+ * @return A string containing the current request uri
+ */
+ public static String getRequestURI(ExternalContext ec)
+ {
+ if (!isPortlet(ec))
+ {
+ return ((HttpServletRequest) ec.getRequest()).getRequestURI();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Returns the character encoding or <code>null</code> if there isn't any
+ *
+ * @param ec the current external context
+ * @return a string containing the request's character encoding
+ * @see ExternalContext#getRequestCharacterEncoding()
+ *
+ * @deprecated replaced by an API in JSF. Use ExternalContext.getRequestCharacterEncoding()
+ */
+ @Deprecated
+ public static String getCharacterEncoding(ExternalContext ec)
+ {
+ return ec.getRequestCharacterEncoding();
+ }
+
+ /**
+ * Returns the name of the underlying context or <code>null</code> if something
+ * went wrong in trying to retrieve the context.
+ *
+ * @param ec the current external context
+ * @return a String containing the context name
+ */
+ public static String getContextName(ExternalContext ec)
+ {
+ try
+ {
+ if (isPortlet(ec))
+ {
+ return (String) _runMethod(ec.getContext(), "getPortletContextName");
+ }
+ else
+ {
+ return ((ServletContext) ec.getContext()).getServletContextName();
+ }
+ }
+ catch (final ClassCastException e)
+ {
+ _LOG.severe(e.getMessage());
+ }
+ return null;
+ }
+
+ /**
+ * Returns the content length or -1 if the unknown.
+ *
+ * @param ec the current external context
+ * @return the length or -1 if the length is unknown
+ */
+ public static int getContentLength(ExternalContext ec)
+ {
+ if(isRequestFromClient(ec))
+ {
+ return (Integer) _runMethod(ec.getRequest(), "getContentLength");
+ }
+
+ return -1;
+ }
+
+ /**
+ * Returns the content type from the current externalContext or
+ * <code>null</code> if unknown.
+ *
+ * @param ec the current external context
+ * @return a String contining the the content type or <code>null</code>
+ * @see ExternalContext#getRequestContentType()
+ *
+ * @deprecated use ExternalContext.getRequestContentType()
+ */
+ @Deprecated
+ public static String getContentType(ExternalContext ec)
+ {
+ return ec.getRequestContentType();
+ }
+
+ /**
+ * Returns the request input stream if one is available
+ *
+ * @param ec the current external context
+ * @return the request's input stream
+ * @throws IOException if there was a problem getting the input stream
+ */
+ public static InputStream getRequestInputStream(ExternalContext ec)
+ throws IOException
+ {
+ RequestType type = getRequestType(ec);
+ if(type.isRequestFromClient())
+ {
+ Object req = ec.getRequest();
+ if(type.isPortlet())
+ {
+ return (InputStream)_runMethod(req, "getPortletInputStream");
+ }
+ else
+ {
+ return ((ServletRequest) ec.getRequest()).getInputStream();
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Returns <code>true</code> if this externalContext represents an "action".
+ * An action request is any ServletRequest or a portlet ActionRequest or
+ * ResourceRequest.
+ *
+ * @param ec the current external context
+ * @return a boolean of <code>true</code> if this request is an action-type
+ * request.
+ * @see #isRequestFromClient(ExternalContext)
+ *
+ * @deprecated replaced with {@link #isRequestFromClient(ExternalContext)}
+ */
+ @Deprecated
+ public static boolean isAction(ExternalContext ec)
+ {
+ return isRequestFromClient(ec);
+ }
+
+ /**
+ * Returns the value of {@link RequestType#isPortlet()} for the current
+ * RequestType. This is a convenience function designed to perform a quick
+ * check of the current request. If more capabilities need to be tested for
+ * the given request, then it is more efficient to pull this information from
+ * the RequestType itself.
+ *
+ * @param ec the current external context
+ * @return a boolean value of <code>true</code> if the current RequestType
+ * is a portlet request.
+ *
+ * @see RequestType#isPortlet()
+ * @see #getRequestType(ExternalContext)
+ */
+ public static boolean isPortlet(ExternalContext ec)
+ {
+ return getRequestType(ec).isPortlet();
+ }
+
+ /**
+ * Returns the value of {@link RequestType#isResponseWritable()} for the
+ * current RequestType. This is a convenience function designed to perform a
+ * quick check of the current request. If more capabilities need to be tested
+ * for the given request, then it is more efficient to pull this information
+ * from the RequestType itself.
+ *
+ * @param ec the current external context
+ * @return a boolean value of <code>true</code> if the current RequestType
+ * is a "render" type response.
+ *
+ * @see RequestType#isResponseWritable()
+ * @see #getRequestType(ExternalContext)
+ * @since 2.0
+ */
+ public static final boolean isResponseWritable(ExternalContext ec)
+ {
+ return getRequestType(ec).isResponseWritable();
+ }
+
+ /**
+ * Returns the value of {@link RequestType#isRequestFromClient()} for the
+ * current RequestType. This is a convenience function designed to perform a
+ * quick check of the current request. If more capabilities need to be tested
+ * for the given request, then it is more efficient to pull this information
+ * from the RequestType itself.
+ *
+ * @param ec the current external context
+ * @return a boolean value of <code>true</code> if the current RequestType
+ * represents a request from the client.
+ *
+ * @see RequestType#isResponseWritable()
+ * @see #getRequestType(ExternalContext)
+ * @since 2.0
+ */
+ public static final boolean isRequestFromClient(ExternalContext ec)
+ {
+ return getRequestType(ec).isRequestFromClient();
+ }
+
+ /**
+ * Returns wherther of not this external context represents a true HttpServletRequest or
+ * not. Some portal containers implement the PortletRequest/Response objects as
+ * HttpServletRequestWrappers, and those objects should not be treated as an
+ * HttpServlerRequest. As such, this method first tests to see if the request is
+ * a portlet request and, if not, then tests to see if the request is an instanceof
+ * HttpServletRequest.
+ *
+ * @param ec the current external context
+ * @return a boolean value of <code>true</code> if the current request is an
+ * HttpServletRequest
+ * @since 1.1
+ */
+ public static boolean isHttpServletRequest(ExternalContext ec)
+ {
+ return (!isPortlet(ec) && (ec.getRequest() instanceof HttpServletRequest));
+ }
+
+ /**
+ * Runs a method on an object and returns the result
+ *
+ * @param obj the object to run the method on
+ * @param methodName the name of the method
+ * @return the results of the method run
+ */
+ private static Object _runMethod(Object obj, String methodName)
+ {
+ try
+ {
+ Method sessionIdMethod = obj.getClass().getMethod(methodName);
+ return sessionIdMethod.invoke(obj);
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+
+ }
+
+ // prevent this from being instantiated
+ private ExternalContextUtils()
+ {}
+
+ private static final Logger _LOG = Logger.getLogger(ExternalContextUtils.class.getName());
+
+ // =-= 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_RENDER_REQUEST_CLASS;
+ private static final Class<?> _PORTLET_RESOURCE_REQUEST_CLASS;
+ private static final Class<?> _PORTLET_CONTEXT_CLASS;
+ private static final boolean _PORTLET_10_SUPPORTED;
+ private static final boolean _PORTLET_20_SUPPORTED;
+
+ static
+ {
+ Class<?> context;
+ Class<?> actionRequest;
+ Class<?> renderRequest;
+ Class<?> resourceRequest;
+ boolean portlet20Supported = false;
+ boolean portlet10Supported = false;
+
+ try
+ {
+ context = ClassLoaderUtils.loadClass("javax.portlet.PortletContext");
+ actionRequest = ClassLoaderUtils.loadClass("javax.portlet.ActionRequest");
+ renderRequest = ClassLoaderUtils.loadClass("javax.portlet.RenderRequest");
+
+ try
+ {
+ resourceRequest = ClassLoaderUtils.loadClass("javax.portlet.ResourceRequest");
+ }
+ catch (ClassNotFoundException e)
+ {
+ _LOG.fine("Portlet 2.0 API is not available on classpath. Portlet 2.0 functionality is disabled");
+ resourceRequest = null;
+ }
+ }
+ catch (final ClassNotFoundException e)
+ {
+ _LOG
+ .fine("Portlet API is not available on the classpath. Portlet configurations are disabled.");
+ context = null;
+ actionRequest = null;
+ renderRequest = null;
+ resourceRequest = null;
+ }
+
+ //Find bridge to tell if portal is supported
+ if(context != null)
+ {
+ try
+ {
+ Class<?> bridge = ClassLoaderUtils.loadClass("javax.portlet.faces.Bridge");
+
+ if(bridge != null)
+ {
+ portlet10Supported = true;
+
+ //Standard bridge defines a spec name which can be used to
+ //determine Portlet 2.0 Support.
+ String specName = bridge.getPackage().getSpecificationTitle();
+ _LOG.fine("Found Bridge: " + specName);
+ if(specName != null && specName.startsWith("Portlet 2"))
+ {
+ portlet20Supported = true;
+ }
+
+ if(_LOG.isLoggable(Level.INFO))
+ {
+ String ver = (portlet20Supported)?"2.0":"1.0";
+ _LOG.info("Portlet Environment Detected: " + ver);
+ }
+ }
+ }
+ catch (ClassNotFoundException e)
+ {
+ _LOG.fine("Portlet API is present but bridge is not. Portlet configurations are disabled.");
+ }
+ }
+
+ _PORTLET_CONTEXT_CLASS = context;
+ _PORTLET_ACTION_REQUEST_CLASS = actionRequest;
+ _PORTLET_RENDER_REQUEST_CLASS = renderRequest;
+ _PORTLET_RESOURCE_REQUEST_CLASS = resourceRequest;
+ _PORTLET_10_SUPPORTED = portlet10Supported;
+ _PORTLET_20_SUPPORTED = portlet20Supported;
+ }
+}
\ No newline at end of file
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/RequestType.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/RequestType.java?rev=906968&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/RequestType.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/RequestType.java Fri Feb 5 15:27:42 2010
@@ -0,0 +1,126 @@
+/*
+ * 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.shared.util;
+
+/**
+ * Represents the type of request currently in the ExternalContext.
+ * All servlet requests will be of the SERVLET requestType whereas
+ * all of the other RequestTypes will be portlet type requests. There
+ * are a number of convenience methods on the RequestType enumeration
+ * which can be used to determine the capabilities of the current request.
+ */
+public enum RequestType
+{
+ /**
+ * The type for all servlet requests. SERVLET request types are
+ * both client requests and response writable.
+ */
+ SERVLET(true, true, false),
+
+ /**
+ * The type for a portlet RenderRequest. RENDER request types are
+ * for portlets and are response writable but are NOT client
+ * requests.
+ */
+ RENDER(false, true, true),
+
+ /**
+ * The type for a portlet ActionRequest. ACTION request types are
+ * for portlets and are client requests but are NOT response
+ * writable.
+ */
+ ACTION(true, false, true),
+
+ /**
+ * The type for a portlet ResourceRequest. RESOURCE request types
+ * are for portlets and are both client requests and response
+ * writable. RESOURCE request types will only be returned in a
+ * Portlet 2.0 portlet container.
+ */
+ RESOURCE(true, true, true),
+
+ /**
+ * The type for a portlet EventRequest. EVENT request types
+ * are for portlets and are neither client requests nor response
+ * writable. EVENT request types will only be returned in a
+ * Portlet 2.0 portlet container.
+ */
+ EVENT(false, false, true);
+
+ private boolean _client;
+ private boolean _writable;
+ private boolean _portlet;
+
+ RequestType(boolean client, boolean writable, boolean portlet)
+ {
+ _client = client;
+ _writable = writable;
+ _portlet = portlet;
+ }
+
+ /**
+ * Returns <code>true</code> if this request was a direct
+ * result of a call from the client. This implies that
+ * the current application is the "owner" of the current
+ * request and that it has access to the inputStream, can
+ * get and set character encodings, etc. Currently all
+ * SERVLET, ACTION, and RESOURCE RequestTypes are client
+ * requests.
+ *
+ * @return <code>true</code> if the current request is a
+ * client data type request and <code>false</code>
+ * if it is not.
+ */
+ public boolean isRequestFromClient()
+ {
+ return _client;
+ }
+
+ /**
+ * Returns <code>true</code> if the response for this
+ * RequestType is intended to produce output to the client.
+ * Currently the SERVLET, RENDER, and RESOURCE request are
+ * response writable.
+ *
+ * @return <code>true</code> if the current request is
+ * intended to produce output and <code>false</code>
+ * if it is not.
+ */
+ public boolean isResponseWritable()
+ {
+ return _writable;
+ }
+
+ /**
+ * Returns <code>true</code> if the response for this
+ * RequestType originated from a JSR-168 or JSR-286
+ * portlet container. Currently RENDER, ACTION,
+ * RESOURCE, and EVENT RequestTypes are all portlet
+ * requests.
+ *
+ * @return <code>true</code> if the current request
+ * originated inside of a JSR-168 or JSR-286
+ * Portlet Container or <code>false</code> if
+ * it did not.
+ */
+ public boolean isPortlet()
+ {
+ return _portlet;
+ }
+}
\ No newline at end of file