[seam-commits] Seam SVN: r15373 - in branches/community/Seam_2_3/jboss-seam/src/main: java/org/jboss/seam/mock and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Nov 28 06:59:36 EST 2012


Author: manaRH
Date: 2012-11-28 06:59:36 -0500 (Wed, 28 Nov 2012)
New Revision: 15373

Added:
   branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/jsf/SeamExceptionHandlerFactory.java
Modified:
   branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/mock/MockExternalContext.java
   branches/community/Seam_2_3/jboss-seam/src/main/resources/META-INF/faces-config.xml
Log:
JBSEAM-5045 Ajax handling for ExceptionFilter

Added: branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/jsf/SeamExceptionHandlerFactory.java
===================================================================
--- branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/jsf/SeamExceptionHandlerFactory.java	                        (rev 0)
+++ branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/jsf/SeamExceptionHandlerFactory.java	2012-11-28 11:59:36 UTC (rev 15373)
@@ -0,0 +1,49 @@
+package org.jboss.seam.jsf;
+
+import javax.faces.context.ExceptionHandler;
+import javax.faces.context.ExceptionHandlerFactory;
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.web.ExceptionFilter;
+
+import com.sun.faces.application.ApplicationAssociate;
+import com.sun.faces.context.AjaxExceptionHandlerImpl;
+import com.sun.faces.context.ExceptionHandlerImpl;
+
+
+
+/**
+ * Factory not to be used AjaxExceptionHandlerImpl class and 
+ * always be an exception to be thrown by capturad ExceptionFilter
+ * 
+ * @see AjaxExceptionHandlerImpl
+ * @see ExceptionFilter  
+ * @author Tiago Peruzzo
+ * 
+ */
+public class SeamExceptionHandlerFactory extends ExceptionHandlerFactory {
+
+	private ApplicationAssociate associate;
+
+
+	@Override
+    public ExceptionHandler getExceptionHandler() {
+        FacesContext fc = FacesContext.getCurrentInstance();
+        ApplicationAssociate associate = getAssociate(fc);
+        return new ExceptionHandlerImpl(((associate != null) ? associate.isErrorPagePresent() : Boolean.TRUE));
+    }
+
+
+    // --------------------------------------------------------- Private Methods
+
+    private ApplicationAssociate getAssociate(FacesContext ctx) {
+        if (associate == null) {
+            associate = ApplicationAssociate.getCurrentInstance();
+            if (associate == null) {
+                associate = ApplicationAssociate.getInstance(ctx.getExternalContext());
+            }
+        }
+        return associate;
+    }
+	
+}

Modified: branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/mock/MockExternalContext.java
===================================================================
--- branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/mock/MockExternalContext.java	2012-11-28 11:12:35 UTC (rev 15372)
+++ branches/community/Seam_2_3/jboss-seam/src/main/java/org/jboss/seam/mock/MockExternalContext.java	2012-11-28 11:59:36 UTC (rev 15373)
@@ -8,6 +8,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.Principal;
@@ -21,20 +22,26 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
+import java.util.logging.Level;
 
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
+import javax.faces.context.PartialResponseWriter;
+import javax.faces.context.ResponseWriter;
 import javax.servlet.ServletContext;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
 import org.jboss.seam.util.EnumerationIterator;
 
 /**
  * @author Gavin King
  * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
+ * @author Marek Novotny
  * @version $Revision: 13963 $
  */
 public class MockExternalContext extends ExternalContext
@@ -44,6 +51,8 @@
    private HttpServletRequest request;
 
    private HttpServletResponse response;
+   
+   private static final LogProvider log = Logging.getLogProvider( MockExternalContext.class );
 
    public MockExternalContext()
    {
@@ -525,12 +534,64 @@
    @Override
    public void redirect(String url) throws IOException
    {
-      response.sendRedirect(url);
+      if ("partial/ajax".equals(this.request.getHeader("Faces-Request")))
+      {
+         this.response.setContentType("text/xml");
+         this.response.setCharacterEncoding("UTF-8");
+         this.response.addHeader("Cache-Control", "no-cache");
+         this.response.setStatus(HttpServletResponse.SC_OK);
+         
+         FacesContext facesContext = FacesContext.getCurrentInstance();
+         PartialResponseWriter pwriter = facesContext.getPartialViewContext().getPartialResponseWriter();
+         
+         if (pwriter == null)
+         {
+            pwriter = createPartialResponseWriter();
+         }
+         pwriter.startDocument();
+         pwriter.redirect(url);
+         pwriter.endDocument();
+      }
+      else
+      {
+         this.response.sendRedirect(url);
+      }
+
       FacesContext.getCurrentInstance().responseComplete();
    }
    
-   
+   private PartialResponseWriter createPartialResponseWriter()
+   {
+      FacesContext facesCtx = FacesContext.getCurrentInstance();
+      ExternalContext externalCtx = facesCtx.getExternalContext();
+      String encoding = externalCtx.getRequestCharacterEncoding();
+      externalCtx.setResponseCharacterEncoding(encoding);
+      ResponseWriter responseWriter = null;
+      Writer outputWriter = null;
+      try
+      {
+         outputWriter = externalCtx.getResponseOutputWriter();
+      }
+      catch (IOException ioe)
+      {
+         log.error("couldn't get ResponseOutputWriter for Partial Ajax request", ioe);
+      }
 
+      if (outputWriter != null)
+      {
+         responseWriter = facesCtx.getRenderKit().createResponseWriter(outputWriter, "text/xml", encoding);
+      }
+      if (responseWriter instanceof PartialResponseWriter)
+      {
+         return (PartialResponseWriter) responseWriter;
+      }
+      else
+      {
+         return new PartialResponseWriter(responseWriter);
+      }
+
+   }
+
    @Override
    public void setRequest(Object myrequest)
    {

Modified: branches/community/Seam_2_3/jboss-seam/src/main/resources/META-INF/faces-config.xml
===================================================================
--- branches/community/Seam_2_3/jboss-seam/src/main/resources/META-INF/faces-config.xml	2012-11-28 11:12:35 UTC (rev 15372)
+++ branches/community/Seam_2_3/jboss-seam/src/main/resources/META-INF/faces-config.xml	2012-11-28 11:59:36 UTC (rev 15373)
@@ -6,6 +6,7 @@
 
    <factory>
       <application-factory>org.jboss.seam.jsf.SeamApplicationFactory</application-factory>
+      <exception-handler-factory>org.jboss.seam.jsf.SeamExceptionHandlerFactory</exception-handler-factory>
    </factory>
 
    <application>



More information about the seam-commits mailing list