Author: alexsmirnov
Date: 2008-04-18 18:16:41 -0400 (Fri, 18 Apr 2008)
New Revision: 7952
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java
trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js
Log:
Fix
http://jira.jboss.com/jira/browse/RF-3013
partially fix for
http://jira.jboss.com/jira/browse/RF-3102
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java 2008-04-18
19:56:31 UTC (rev 7951)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxViewHandler.java 2008-04-18
22:16:41 UTC (rev 7952)
@@ -127,21 +127,21 @@
* @see
org.ajax4jsf.framework.ViewHandlerWrapper#restoreView(javax.faces.context.FacesContext,
* java.lang.String)
*/
- public UIViewRoot restoreView(FacesContext context, String viewId) {
- UIViewRoot viewRoot = super.restoreView(context, viewId);
- ExternalContext externalContext = context.getExternalContext();
- Map<String, Object> requestMap = externalContext.getRequestMap();
- if (null == viewRoot) {
- requestMap.put(VIEW_EXPIRED, Messages
- .getMessage(Messages.AJAX_VIEW_EXPIRED));
- if (_log.isDebugEnabled()) {
- _log
- .debug("Detect session expiration in AJAX request - view don't restored
for a viewId "
- + viewId);
- }
- }
- return viewRoot;
- }
+// public UIViewRoot restoreView(FacesContext context, String viewId) {
+// UIViewRoot viewRoot = super.restoreView(context, viewId);
+// ExternalContext externalContext = context.getExternalContext();
+// Map<String, Object> requestMap = externalContext.getRequestMap();
+// if (null == viewRoot) {
+// requestMap.put(VIEW_EXPIRED, Messages
+// .getMessage(Messages.AJAX_VIEW_EXPIRED));
+// if (_log.isDebugEnabled()) {
+// _log
+// .debug("Detect session expiration in AJAX request - view don't restored
for a viewId "
+// + viewId);
+// }
+// }
+// return viewRoot;
+// }
/*
* (non-Javadoc)
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java 2008-04-18
19:56:31 UTC (rev 7951)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java 2008-04-18
22:16:41 UTC (rev 7952)
@@ -60,6 +60,10 @@
*/
public abstract class BaseFilter implements Filter {
+ public static final String AJAX_PUSH_READY = "READY";
+
+ public static final String AJAX_PUSH_STATUS_HEADER = "Ajax-Push-Status";
+
public static final String AJAX_PUSH_KEY_HEADER = "Ajax-Push-Key";
private static final Log log = LogFactory.getLog(BaseFilter.class);
@@ -460,10 +464,10 @@
.getListener(ajaxPushHeader);
// To avoid XmlHttpRequest parsing exceptions.
httpServletResponse.setContentType("text/plain");
- httpServletResponse.setContentLength(0);
if (listener.isPerformed()) {
listener.processed();
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
+ httpServletResponse.setHeader(AJAX_PUSH_STATUS_HEADER, AJAX_PUSH_READY);
if (log.isDebugEnabled()) {
log
.debug("Occurs event for a id "
@@ -476,6 +480,7 @@
log.debug("No event for a id " + ajaxPushHeader);
}
}
+ httpServletResponse.setContentLength(0);
} else
// check for resource request
if (!getResourceService().serviceResource(httpServletRequest,
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java 2008-04-18
19:56:31 UTC (rev 7951)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java 2008-04-18
22:16:41 UTC (rev 7952)
@@ -29,6 +29,7 @@
import java.util.Map;
import java.util.Set;
+import javax.faces.application.ViewExpiredException;
import javax.faces.context.FacesContext;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -47,6 +48,8 @@
import org.apache.commons.logging.LogFactory;
public abstract class BaseXMLFilter {
+ public static final String AJAX_EXPIRED = "Ajax-Expired";
+
private static final Log log = LogFactory.getLog(BaseXMLFilter.class);
public static final String RESPONSE_WRAPPER_ATTRIBUTE =
"com.exade.vcp.Filter.ResponseWrapper";
@@ -146,6 +149,27 @@
servletResponseWrapper);
chain.doFilter(request, servletResponseWrapper);
+ } catch (ServletException e) {
+ log.error("Exception in the filter chain", e);
+ if(isViewExpired(e)){
+ Writer output = resetResponse(response, servletResponseWrapper, "true");
+ String message = Messages
+ .getMessage(Messages.AJAX_VIEW_EXPIRED);
+ response.setHeader(AJAX_EXPIRED, message);
+ output.write("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"
+ + "<html
xmlns=\"http://www.w3.org/1999/xhtml\"><head>"
+ + "<meta name=\""
+ + AjaxContainerRenderer.AJAX_FLAG_HEADER
+ + "\" content=\"true\" />" + "<meta
name=\""
+ + AJAX_EXPIRED
+ + "\" content=\"" + message + "\" />"
+ + "</head></html>");
+ output.flush();
+ response.flushBuffer();
+ return;
+ } else {
+ throw e;
+ }
} finally {
request.removeAttribute(RESPONSE_WRAPPER_ATTRIBUTE);
}
@@ -158,10 +182,6 @@
String redirectLocation = servletResponseWrapper.getRedirectLocation();
String characterEncoding = servletResponseWrapper
.getCharacterEncoding();
- Object view_expired = request.getAttribute(AjaxViewHandler.VIEW_EXPIRED);
- if(null != view_expired){
- servletResponseWrapper.setHeader("Ajax-Expired", view_expired.toString());
- }
Writer output;
if (null != redirectLocation) {
if (isAjaxRequest(request)) {
@@ -171,32 +191,10 @@
log.debug("Create AJAX redirect response to url: "
+ redirectLocation);
}
- response.reset();
- // Keep cookies.
- for (Iterator<Cookie> iter = servletResponseWrapper.getCookies()
- .iterator(); iter.hasNext();) {
- Cookie cookie = (Cookie) iter.next();
- response.addCookie(cookie);
- }
- // Copy response headers
- Map<String, Object> headers = servletResponseWrapper.getHeaders();
- for (Iterator<Map.Entry<String, Object>> iter =
headers.entrySet().iterator(); iter
- .hasNext();) {
- Map.Entry<String, Object> header = iter.next();
- response.setHeader((String) header.getKey(),
- (String) header.getValue());
- }
- response.setHeader(AjaxContainerRenderer.AJAX_FLAG_HEADER,
+ output = resetResponse(response, servletResponseWrapper,
"redirect");
- // Not caching AJAX request
- response.setHeader("Cache-Control",
- "no-cache, must-revalidate, max_age=0, no-store");
- response.setHeader("Expires", "0");
- response.setHeader("Pragma", "no-cache");
- response.setContentType(getMimetype() + ";charset=UTF-8");
response.setHeader(AjaxContainerRenderer.AJAX_LOCATION_HEADER,
redirectLocation);
- output = createResponseWriter(response, "UTF-8");
// For buggy XmlHttpRequest realisations repeat headers in
// <meta>
output.write("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"
@@ -330,7 +328,63 @@
/**
* @param response
+ * @param servletResponseWrapper
+ * @param ajaxResponseType
* @return
+ * @throws IOException
+ * @throws UnsupportedEncodingException
+ */
+ private Writer resetResponse(final HttpServletResponse response,
+ FilterServletResponseWrapper servletResponseWrapper,
+ String ajaxResponseType) throws IOException,
+ UnsupportedEncodingException {
+ Writer output;
+ response.reset();
+ // Keep cookies.
+ for (Iterator<Cookie> iter = servletResponseWrapper.getCookies()
+ .iterator(); iter.hasNext();) {
+ Cookie cookie = (Cookie) iter.next();
+ response.addCookie(cookie);
+ }
+ // Copy response headers
+ Map<String, Object> headers = servletResponseWrapper.getHeaders();
+ for (Iterator<Map.Entry<String, Object>> iter =
headers.entrySet().iterator(); iter
+ .hasNext();) {
+ Map.Entry<String, Object> header = iter.next();
+ response.setHeader((String) header.getKey(),
+ (String) header.getValue());
+ }
+ response.setHeader(AjaxContainerRenderer.AJAX_FLAG_HEADER,
+ ajaxResponseType);
+ // Not caching AJAX request
+ response.setHeader("Cache-Control",
+ "no-cache, must-revalidate, max_age=0, no-store");
+ response.setHeader("Expires", "0");
+ response.setHeader("Pragma", "no-cache");
+ response.setContentType(getMimetype() + ";charset=UTF-8");
+ output = createResponseWriter(response, "UTF-8");
+ return output;
+ }
+
+ /**
+ * Check for a {@link ViewExpiredException} in the exception chain.
+ * @param e exception from filter chain
+ * @return true if any exception in the chain instance of the {@link
ViewExpiredException}
+ */
+ private boolean isViewExpired(Throwable e) {
+ while (null != e) {
+ if (e instanceof ViewExpiredException) {
+ return true;
+ } else {
+ e = e.getCause();
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @param response
+ * @return
* @throws ServletException
*/
protected FilterServletResponseWrapper getWrapper(
Modified: trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js
===================================================================
--- trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js 2008-04-18 19:56:31 UTC
(rev 7951)
+++ trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js 2008-04-18 22:16:41 UTC
(rev 7952)
@@ -233,12 +233,10 @@
{
elements = element.selectNodes(".//*[local-name()=\""+
elementname +"\"]");
- LOG.debug("selectNodes found "+elements.length);
}
catch (ex) {
try {
elements = element.getElementsByTagName(elementname);
- LOG.debug("getElementsByTagName found "+elements.length);
} catch(nf){
LOG.debug("getElementsByTagName found no elements, "+nf.Message);
}
@@ -573,9 +571,11 @@
request.onreadystatechange = function(){
if (request.readyState == 4 ) {
try {
- if(request.status == 200){
- A4J.AJAX.SubmitRequest(containerId,form||options.dummyForm,null,options);
- }
+ if(request.status == 200){
+
if(request.getResponseHeader("Ajax-Push-Status")=="READY"){
+ A4J.AJAX.SubmitRequest(containerId,form||options.dummyForm,null,options);
+ }
+ }
} catch(e){
// Network error.
}
@@ -822,7 +822,7 @@
// .........
// </div>
//
- } else if( idsFromResponse != "" ) {
+ } else if( idsFromResponse && idsFromResponse != "" ) {
LOG.debug("Update page by list of rendered areas from response " +
idsFromResponse );
// Append scripts and styles to head, if not presented in page before.
req.appendNewHeadElements();