You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by so...@apache.org on 2011/03/14 09:58:52 UTC

svn commit: r1081289 - /myfaces/trinidad/branches/trinidad-1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java

Author: sobryan
Date: Mon Mar 14 08:58:52 2011
New Revision: 1081289

URL: http://svn.apache.org/viewvc?rev=1081289&view=rev
Log:
TRINIDAD-2062 - Added a getWriter method to the ExternalContextUtils.

Modified:
    myfaces/trinidad/branches/trinidad-1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java

Modified: myfaces/trinidad/branches/trinidad-1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/trinidad-1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java?rev=1081289&r1=1081288&r2=1081289&view=diff
==============================================================================
--- myfaces/trinidad/branches/trinidad-1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java (original)
+++ myfaces/trinidad/branches/trinidad-1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/util/ExternalContextUtils.java Mon Mar 14 08:58:52 2011
@@ -21,6 +21,9 @@ package org.apache.myfaces.trinidad.util
 import java.io.IOException;
 import java.io.InputStream;
 
+import java.io.Writer;
+
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
 import javax.faces.context.ExternalContext;
@@ -267,6 +270,51 @@ public final class ExternalContextUtils
       return null;
     }
   }
+  
+  /**
+   * Returns the writer appropriate for the current response or <code>null</code> if one is
+   * not available.  This will always be available in a servlet request, but will only be available
+   * for resource or render responses in a portal environments
+   * 
+   * @param ec the current externalContext
+   * @return a writer appropriate for the current response
+   */
+  public static Writer getResponseWriter(ExternalContext ec) throws IOException
+  {
+    if(isResponseWritable(ec))
+    { 
+      Object response = ec.getResponse();
+      
+      Method writerMethod;
+      try
+      {
+        writerMethod = response.getClass().getMethod("getWriter");
+        return (Writer)writerMethod.invoke(response);
+      }
+      catch (NoSuchMethodException e)
+      {
+        _LOG.severe(e);
+      }
+      catch (IllegalAccessException e)
+      {
+        _LOG.severe(e);
+      }
+      catch (InvocationTargetException e)
+      {
+        Throwable cause = e.getCause();
+        
+        //Throws the IOException as per the contract
+        if(cause instanceof IOException)
+        {
+          throw (IOException)cause;
+        }
+        
+        _LOG.severe(e);
+      }
+    }
+    
+    return null;
+  }
 
   /**
    * Returns the character encoding or <code>null</code> if there isn't any