You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ba...@apache.org on 2006/02/09 20:06:25 UTC
svn commit: r376389 - in /myfaces:
commons/trunk/src/main/java/org/apache/myfaces/config/
commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/
tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/util/
tomahawk/trunk/core/...
Author: baranda
Date: Thu Feb 9 11:06:23 2006
New Revision: 376389
URL: http://svn.apache.org/viewcvs?rev=376389&view=rev
Log:
Created ExtensionsPhaseListener in tomahawk, which puts the javascript code for the dummyForm and the autoscroll to be rendered by the extensionsFilter. Should fix MYFACES-1102
Added:
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java (with props)
Modified:
myfaces/commons/trunk/src/main/java/org/apache/myfaces/config/MyfacesConfig.java
myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/AddResourceFactory.java
myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/JavascriptUtils.java
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/util/ExtensionsFilter.java
myfaces/tomahawk/trunk/core/src/main/resources-facesconfig/META-INF/faces-config.xml
Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/config/MyfacesConfig.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/config/MyfacesConfig.java?rev=376389&r1=376388&r2=376389&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/config/MyfacesConfig.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/config/MyfacesConfig.java Thu Feb 9 11:06:23 2006
@@ -51,7 +51,7 @@
private static final String INIT_PARAM_AUTO_SCROLL = "org.apache.myfaces.AUTO_SCROLL";
private static final boolean INIT_PARAM_AUTO_SCROLL_DEFAULT = false;
- private static final String INIT_PARAM_ADD_RESOURCE_CLASS = "org.apache.myfaces.ADD_RESOURCE_CLASS";
+ public static final String INIT_PARAM_ADD_RESOURCE_CLASS = "org.apache.myfaces.ADD_RESOURCE_CLASS";
private boolean _prettyHtml;
private boolean _detectJavascript;
Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/AddResourceFactory.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/AddResourceFactory.java?rev=376389&r1=376388&r2=376389&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/AddResourceFactory.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/AddResourceFactory.java Thu Feb 9 11:06:23 2006
@@ -16,18 +16,19 @@
package org.apache.myfaces.renderkit.html.util;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.faces.context.FacesContext;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.myfaces.config.MyfacesConfig;
import org.apache.myfaces.util.ClassUtils;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.faces.context.FacesContext;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* This class provides the ability to instantiate AddResource objects.
* By default, this class will instantiate instances of
@@ -156,9 +157,15 @@
}
public static AddResource getInstance(FacesContext context)
- {
+ {
return getInstance(context.getExternalContext().getRequestContextPath(),
MyfacesConfig.getCurrentInstance(context.getExternalContext()).getAddResourceClass());
+ }
+
+ public static AddResource getInstance(HttpServletRequest request)
+ {
+ ServletContext servletContext = ((HttpServletRequest)request).getSession().getServletContext();
+ return getInstance(request.getContextPath(), servletContext.getInitParameter(MyfacesConfig.INIT_PARAM_ADD_RESOURCE_CLASS));
}
}
Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/JavascriptUtils.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/JavascriptUtils.java?rev=376389&r1=376388&r2=376389&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/JavascriptUtils.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/JavascriptUtils.java Thu Feb 9 11:06:23 2006
@@ -319,8 +319,8 @@
"}\n");
ExternalContext externalContext = facesContext.getExternalContext();
String oldViewId = getOldViewId(externalContext);
- if (oldViewId != null && oldViewId.equals(facesContext.getViewRoot().getViewId()))
- {
+ //if (oldViewId != null && oldViewId.equals(facesContext.getViewRoot().getViewId()))
+ //{
//ok, we stayed on the same page, so let's scroll it to the former place
String scrolling = (String)externalContext.getRequestParameterMap().get(AUTO_SCROLL_PARAM);
if (scrolling != null && scrolling.length() > 0)
@@ -341,7 +341,7 @@
}
script.append("window.scrollTo(").append(x).append(",").append(y).append(");\n");
}
- }
+ //}
writer.writeText(script.toString(),null);
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/util/ExtensionsFilter.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/util/ExtensionsFilter.java?rev=376389&r1=376388&r2=376389&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/util/ExtensionsFilter.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/util/ExtensionsFilter.java Thu Feb 9 11:06:23 2006
@@ -18,27 +18,8 @@
import org.apache.commons.fileupload.FileUpload;
import org.apache.myfaces.renderkit.html.util.AddResource;
import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
-import org.apache.myfaces.renderkit.html.util.HtmlBufferResponseWriterWrapper;
-import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
-import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
-import org.apache.myfaces.renderkit.html.HTML;
-import org.apache.myfaces.config.MyfacesConfig;
-
-import javax.faces.FactoryFinder;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.ExternalContext;
-import javax.faces.context.FacesContext;
-import javax.faces.context.FacesContextFactory;
-import javax.faces.context.ResponseWriter;
-import javax.faces.lifecycle.Lifecycle;
-import javax.faces.lifecycle.LifecycleFactory;
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
+
+import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@@ -136,24 +117,19 @@
ExtensionsResponseWrapper extendedResponse = new ExtensionsResponseWrapper((HttpServletResponse) response);
- FacesContext facesContext = getFacesContext(extendedRequest, extendedResponse);
-
// Serve resources
- AddResource addResource = AddResourceFactory.getInstance(facesContext);
+ AddResource addResource = AddResourceFactory.getInstance(httpRequest);
if( addResource.isResourceUri( httpRequest ) ){
addResource.serveResource(_servletContext, httpRequest, httpResponse);
return;
}
-
// Standard request
chain.doFilter(extendedRequest, extendedResponse);
extendedResponse.finishResponse();
// write the javascript stuff for myfaces and headerInfo, if needed
- renderCodeBeforeBodyEnd(facesContext);
-
HttpServletResponse servletResponse = (HttpServletResponse)response;
addResource.parseResponse(extendedRequest, extendedResponse.toString(),
@@ -165,18 +141,14 @@
if( ! addResource.hasHeaderBeginInfos(extendedRequest) ){
// writes the response if no header info is needed
addResource.writeResponse(extendedRequest, servletResponse);
- facesContext.release();
return;
}
-
// Some headerInfo has to be added
addResource.writeWithFullHeader(extendedRequest, servletResponse);
// writes the response
addResource.writeResponse(extendedRequest, servletResponse);
-
- facesContext.release();
}
/**
@@ -186,70 +158,5 @@
// NoOp
}
- private FacesContext getFacesContext(ServletRequest request, ServletResponse response) {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- if (facesContext != null) return facesContext;
-
- FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
- LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
- Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
-
- ServletContext servletContext = ((HttpServletRequest)request).getSession().getServletContext();
-
- facesContext = contextFactory.getFacesContext(servletContext, request, response, lifecycle);
- UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, JavascriptUtils.getOldViewId(facesContext.getExternalContext()));
- facesContext.setViewRoot(view);
-
- return facesContext;
- }
-
- /**
- * Renders stuff such as the dummy form and the autoscroll javascript, which goes before the closing </body>
- * @throws IOException
- */
- public static void renderCodeBeforeBodyEnd(FacesContext facesContext) throws IOException
- {
- Object myFacesJavascript = facesContext.getExternalContext().getRequestMap().get(ORG_APACHE_MYFACES_MY_FACES_JAVASCRIPT);
-
- if (myFacesJavascript != null)
- {
- return;
- }
-
- ResponseWriter responseWriter = facesContext.getResponseWriter();
- HtmlBufferResponseWriterWrapper writerWrapper = HtmlBufferResponseWriterWrapper
- .getInstance(responseWriter);
- facesContext.setResponseWriter(writerWrapper);
-
- if (DummyFormUtils.isWriteDummyForm(facesContext))
- {
- DummyFormUtils.writeDummyForm(writerWrapper, DummyFormUtils.getDummyFormParameters(facesContext));
- }
-
- MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
- if (myfacesConfig.isDetectJavascript())
- {
- if (! JavascriptUtils.isJavascriptDetected(facesContext.getExternalContext()))
- {
-
- writerWrapper.startElement("script",null);
- writerWrapper.writeAttribute("attr", HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
- StringBuffer script = new StringBuffer();
- script.append("document.location.replace('").
- append(facesContext.getApplication().getViewHandler().getResourceURL(facesContext, "/_javascriptDetector_")).append("?goto=").append(facesContext.getApplication().getViewHandler().getActionURL(facesContext, facesContext.getViewRoot().getViewId())).append("');");
- writerWrapper.writeText(script.toString(),null);
- writerWrapper.endElement(HTML.SCRIPT_ELEM);
- }
- }
-
- if (myfacesConfig.isAutoScroll())
- {
- JavascriptUtils.renderAutoScrollFunction(facesContext, writerWrapper);
- }
-
- //facesContext.setResponseWriter(responseWriter);
-
- facesContext.getExternalContext().getRequestMap().put(ORG_APACHE_MYFACES_MY_FACES_JAVASCRIPT, "<!-- MYFACES JAVASCRIPT -->\n"+writerWrapper.toString()+"\n");
- }
}
Added: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java?rev=376389&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java (added)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java Thu Feb 9 11:06:23 2006
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.renderkit.html.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.config.MyfacesConfig;
+import org.apache.myfaces.renderkit.html.HTML;
+
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PhaseListener;
+import java.io.IOException;
+
+/**
+ * This phase listener puts in the request the javascript code needed to render the dummyForm
+ * and the autoscroll feature.
+ *
+ * The ExtensionsFilter will put this code before the closing &tt;/body> tag.
+ *
+ * @author Bruno Aranda (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ExtensionsPhaseListener implements PhaseListener {
+
+ private static final Log log = LogFactory.getLog(ExtensionsPhaseListener.class);
+
+
+ private static final String ORG_APACHE_MYFACES_MY_FACES_JAVASCRIPT = "org.apache.myfaces.myFacesJavascript";
+
+ public PhaseId getPhaseId()
+ {
+ return PhaseId.RENDER_RESPONSE;
+ }
+
+ public void beforePhase(PhaseEvent event)
+ {
+ }
+
+ public void afterPhase(PhaseEvent event)
+ {
+ FacesContext facesContext = event.getFacesContext();
+
+ try
+ {
+ renderCodeBeforeBodyEnd(facesContext);
+ } catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Renders stuff such as the dummy form and the autoscroll javascript, which goes before the closing </body>
+ * @throws IOException
+ */
+ public static void renderCodeBeforeBodyEnd(FacesContext facesContext) throws IOException
+ {
+ Object myFacesJavascript = facesContext.getExternalContext().getRequestMap().get(ORG_APACHE_MYFACES_MY_FACES_JAVASCRIPT);
+
+ if (myFacesJavascript != null)
+ {
+ return;
+ }
+
+ ResponseWriter responseWriter = facesContext.getResponseWriter();
+ HtmlBufferResponseWriterWrapper writerWrapper = HtmlBufferResponseWriterWrapper
+ .getInstance(responseWriter);
+ facesContext.setResponseWriter(writerWrapper);
+
+ if (DummyFormUtils.isWriteDummyForm(facesContext))
+ {
+ DummyFormUtils.writeDummyForm(writerWrapper, DummyFormUtils.getDummyFormParameters(facesContext));
+ }
+
+ MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
+ if (myfacesConfig.isDetectJavascript())
+ {
+ if (! JavascriptUtils.isJavascriptDetected(facesContext.getExternalContext()))
+ {
+
+ writerWrapper.startElement("script",null);
+ writerWrapper.writeAttribute("attr", HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
+ StringBuffer script = new StringBuffer();
+ script.append("document.location.replace('").
+ append(facesContext.getApplication().getViewHandler().getResourceURL(facesContext, "/_javascriptDetector_")).append("?goto=").append(facesContext.getApplication().getViewHandler().getActionURL(facesContext, facesContext.getViewRoot().getViewId())).append("');");
+ writerWrapper.writeText(script.toString(),null);
+ writerWrapper.endElement(HTML.SCRIPT_ELEM);
+ }
+ }
+
+ if (myfacesConfig.isAutoScroll())
+ {
+ JavascriptUtils.renderAutoScrollFunction(facesContext, writerWrapper);
+ }
+
+ facesContext.setResponseWriter(responseWriter);
+
+ facesContext.getExternalContext().getRequestMap().put(ORG_APACHE_MYFACES_MY_FACES_JAVASCRIPT, "<!-- MYFACES JAVASCRIPT -->\n"+writerWrapper.toString()+"\n");
+ }
+
+
+}
Propchange: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/util/ExtensionsPhaseListener.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision
Modified: myfaces/tomahawk/trunk/core/src/main/resources-facesconfig/META-INF/faces-config.xml
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/resources-facesconfig/META-INF/faces-config.xml?rev=376389&r1=376388&r2=376389&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/resources-facesconfig/META-INF/faces-config.xml (original)
+++ myfaces/tomahawk/trunk/core/src/main/resources-facesconfig/META-INF/faces-config.xml Thu Feb 9 11:06:23 2006
@@ -22,6 +22,9 @@
<faces-config xmlns="http://java.sun.com/JSF/Configuration">
+ <lifecycle>
+ <phase-listener>org.apache.myfaces.renderkit.html.util.ExtensionsPhaseListener</phase-listener>
+ </lifecycle>
<!-- MyFaces extended standard components (in alphabetical order) -->