myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bom...@apache.org
Subject svn commit: r1068593 - in /myfaces/tobago/trunk: tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/ tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/aj...
Date Tue, 08 Feb 2011 21:37:30 GMT
Author: bommel
Date: Tue Feb  8 21:37:29 2011
New Revision: 1068593

URL: http://svn.apache.org/viewvc?rev=1068593&view=rev
Log:
(TOBAGO-969) Support for redirect in the ajax case

Modified:
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java
    myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java
    myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java
    myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java?rev=1068593&r1=1068592&r2=1068593&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java
(original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/ajax/AjaxUtils.java
Tue Feb  8 21:37:29 2011
@@ -18,24 +18,18 @@ package org.apache.myfaces.tobago.ajax;
  */
 
 
+import org.apache.myfaces.tobago.internal.ajax.AjaxInternalUtils;
 import org.apache.myfaces.tobago.internal.util.ResponseUtils;
 import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.apache.myfaces.tobago.internal.ajax.AjaxInternalUtils;
 
-import javax.faces.FactoryFinder;
-import javax.faces.application.Application;
-import javax.faces.application.ViewHandler;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
-import javax.faces.context.ResponseWriter;
-import javax.faces.render.RenderKit;
-import javax.faces.render.RenderKitFactory;
-import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.io.Writer;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -104,32 +98,32 @@ public class AjaxUtils {
     if (!isAjaxRequest(facesContext)) {
       return false;
     }
-    ResponseWriter writer = facesContext.getResponseWriter();
-    if (writer == null) {
-      RenderKit renderKit = facesContext.getRenderKit();
-      if (renderKit == null) {
-        RenderKitFactory renderFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
-        Application application = facesContext.getApplication();
-        ViewHandler applicationViewHandler = application.getViewHandler();
-        String renderKitId = applicationViewHandler.calculateRenderKitId(facesContext);
-        renderKit = renderFactory.getRenderKit(facesContext, renderKitId);
-      }
-      writer = renderKit.createResponseWriter(((HttpServletResponse)
-              facesContext.getExternalContext().getResponse()).getWriter(), null, null);
-    }
+    HttpServletResponse httpServletResponse
+          = (HttpServletResponse) facesContext.getExternalContext().getResponse();
+    Writer writer = httpServletResponse.getWriter();
+    String contentType = "application/json; charset=UTF-8";
+    ResponseUtils.ensureContentTypeHeader(facesContext, contentType);
     ResponseUtils.ensureNoCacheHeader(facesContext);
-    writer.startElement("redirect", null);
-    writer.writeAttribute("url", url, null);
-    writer.endElement("redirect");
-    writer.flush();
+    redirectInternal(writer, url);
+    writer.close();
     facesContext.responseComplete();
     return true;
   }
 
-  public static void redirect(ServletResponse response, String url) throws IOException {
-    PrintWriter out = response.getWriter();
-    out.print("<redirect url=");
-    out.print(url);
-    out.println("</redirect>");
+  private static void redirectInternal(Writer writer, String url) throws IOException {
+    writer.write("{\n  \"tobagoAjaxResponse\": true,\n");
+    writer.write("  \"responseCode\": 302,\n");
+    writer.write("  \"location\": \"");
+    writer.write(url);
+    writer.write("\"\n}\n");
+    writer.flush();
+  }
+
+  public static void redirect(HttpServletResponse response, String url) throws IOException
{
+    PrintWriter writer = response.getWriter();
+    String contentType = "application/json; charset=UTF-8";
+    ResponseUtils.ensureContentTypeHeader(response, contentType);
+    ResponseUtils.ensureNoCacheHeader(response);
+    redirectInternal(writer, url);
   }
 }

Modified: myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java?rev=1068593&r1=1068592&r2=1068593&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java
(original)
+++ myfaces/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/ResponseUtils.java
Tue Feb  8 21:37:29 2011
@@ -37,32 +37,40 @@ public class ResponseUtils {
     ExternalContext externalContext = facesContext.getExternalContext();
     if (externalContext.getResponse() instanceof HttpServletResponse) {
       HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
-      response.setHeader("Cache-Control", "no-cache,no-store,max-age=0,must-revalidate");
-      response.setHeader("Pragma", "no-cache");
-      response.setDateHeader("Expires", 0);
-      response.setDateHeader("max-age", 0);
+      ensureNoCacheHeader(response);
     }
   }
 
+  public static void ensureNoCacheHeader(HttpServletResponse response) {
+    response.setHeader("Cache-Control", "no-cache,no-store,max-age=0,must-revalidate");
+    response.setHeader("Pragma", "no-cache");
+    response.setDateHeader("Expires", 0);
+    response.setDateHeader("max-age", 0);
+  }
+
   public static void ensureContentTypeHeader(FacesContext facesContext, String contentType)
{
     // TODO PortletRequest
     if (facesContext.getExternalContext().getResponse() instanceof HttpServletResponse) {
       HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
-      if (!response.containsHeader("Content-Type")) {
-        response.setContentType(contentType);
-      } else {
-        try {
-          String responseContentType = response.getContentType();
-          if (!responseContentType.equalsIgnoreCase(contentType)) {
-            response.setContentType(contentType);
-            if (LOG.isInfoEnabled()) {
-              LOG.info("Reponse already contains Header Content-Type '" + responseContentType
-                  + "'. Setting Content-Type to '" + contentType + "'");
-            }
+      ensureContentTypeHeader(response, contentType);
+    }
+  }
+
+  public static void ensureContentTypeHeader(HttpServletResponse response, String contentType)
{
+    if (!response.containsHeader("Content-Type")) {
+      response.setContentType(contentType);
+    } else {
+      try {
+        String responseContentType = response.getContentType();
+        if (!responseContentType.equalsIgnoreCase(contentType)) {
+          response.setContentType(contentType);
+          if (LOG.isInfoEnabled()) {
+            LOG.info("Reponse already contains Header Content-Type '" + responseContentType
+                + "'. Setting Content-Type to '" + contentType + "'");
           }
-        } catch (Error e) {
-          LOG.warn("The method ServletResponse.getContentType() is not available before Servlet
2.4");
         }
+      } catch (Error e) {
+        LOG.warn("The method ServletResponse.getContentType() is not available before Servlet
2.4");
       }
     }
   }

Modified: myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java?rev=1068593&r1=1068592&r2=1068593&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java
(original)
+++ myfaces/tobago/trunk/tobago-extension/tobago-deprecation/src/main/java/org/apache/myfaces/tobago/ajax/api/AjaxUtils.java
Tue Feb  8 21:37:29 2011
@@ -22,7 +22,7 @@ import org.apache.myfaces.tobago.interna
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
-import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.Map;
 
@@ -138,7 +138,7 @@ public class AjaxUtils {
    * @deprecated Please use org.apache.myfaces.tobago.ajax.AjaxUtils.redirect
    */
   @Deprecated
-  public static void redirect(ServletResponse response, String url) throws IOException {
+  public static void redirect(HttpServletResponse response, String url) throws IOException
{
     org.apache.myfaces.tobago.ajax.AjaxUtils.redirect(response, url);
   }
 }

Modified: myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js?rev=1068593&r1=1068592&r2=1068593&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
(original)
+++ myfaces/tobago/trunk/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
Tue Feb  8 21:37:29 2011
@@ -2243,6 +2243,8 @@ var LOG = {
 Tobago.Updater = {
   CODE_SUCCESS: 200,
 
+  CODE_REDIRECT: 302,
+
   CODE_NOT_MODIFIED: 304,
 
   CODE_RELOAD_REQUIRED: 309,
@@ -2362,6 +2364,9 @@ Tobago.Updater = {
         alert('wait: full reload requeste: responseCode = ' + requestOptions.resultData.responseCode);
       }
       Tobago.submitAction(null, Tobago.page.id);
+    } else if (requestOptions.resultData.responseCode == Tobago.Updater.CODE_REDIRECT) {
+      window.location = requestOptions.resultData.location;
+      return;
     }
 
     Tobago.replaceJsfState(requestOptions.resultData.jsfState);



Mime
View raw message