You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/04/28 19:38:26 UTC
svn commit: r939021 - in /myfaces:
core/trunk/impl/src/main/java/org/apache/myfaces/application/
core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/
core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/
core/trunk/impl/src/main/java...
Author: jakobk
Date: Wed Apr 28 17:38:25 2010
New Revision: 939021
URL: http://svn.apache.org/viewvc?rev=939021&view=rev
Log:
MYFACES-2666 Getting a full-page-refresh when running JSF's Ajax on the Trinidad 2 (trunk) (moved all ResponseSwitch-related stuff to shared and implemented the response switching on JspViewDeclarationLanguageBase, because it only makes sence for JSPs; merged ExternalContextUtils from impl and shared)
Added:
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java (with props)
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java (with props)
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java (with props)
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java (with props)
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java (with props)
Removed:
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/HttpServletResponseSwitch.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ResponseSwitch.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletResponseSwitch.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SwitchableOutputStream.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/SwitchableWriter.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/util/ExternalContextUtils.java
Modified:
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RenderResponseExecutor.java
myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java (contents, props changed)
myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/JspViewDeclarationLanguageBase.java
Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java?rev=939021&r1=939020&r2=939021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java Wed Apr 28 17:38:25 2010
@@ -39,8 +39,8 @@ import org.apache.myfaces.resource.Resou
import org.apache.myfaces.resource.ResourceLoader;
import org.apache.myfaces.resource.ResourceMeta;
import org.apache.myfaces.shared_impl.util.ClassUtils;
+import org.apache.myfaces.shared_impl.util.ExternalContextUtils;
import org.apache.myfaces.shared_impl.util.StringUtils;
-import org.apache.myfaces.util.ExternalContextUtils;
/**
* DOCUMENT ME!
Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java?rev=939021&r1=939020&r2=939021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/PartialViewContextImpl.java Wed Apr 28 17:38:25 2010
@@ -44,7 +44,6 @@ import javax.faces.view.ViewMetadata;
import org.apache.myfaces.context.PartialResponseWriterImpl;
import org.apache.myfaces.shared_impl.util.StringUtils;
-import org.apache.myfaces.util.ExternalContextUtils;
public class PartialViewContextImpl extends PartialViewContext {
@@ -324,14 +323,6 @@ public class PartialViewContextImpl exte
private void processPartialRendering(UIViewRoot viewRoot, PhaseId phaseId)
{
- // try to enable the ResponseSwitch again (disabled in RenderResponseExecutor)
- Object response = _facesContext.getExternalContext().getResponse();
- ResponseSwitch responseSwitch = ExternalContextUtils.getResponseSwitch(response);
- if (responseSwitch != null)
- {
- responseSwitch.setEnabled(_facesContext, true);
- }
-
//TODO process partial rendering
//https://issues.apache.org/jira/browse/MYFACES-2118
Collection<String> renderIds = getRenderIds();
Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RenderResponseExecutor.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RenderResponseExecutor.java?rev=939021&r1=939020&r2=939021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RenderResponseExecutor.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RenderResponseExecutor.java Wed Apr 28 17:38:25 2010
@@ -32,9 +32,6 @@ import javax.faces.event.PhaseId;
import javax.faces.event.PreRenderViewEvent;
import javax.faces.view.ViewDeclarationLanguage;
-import org.apache.myfaces.context.servlet.ResponseSwitch;
-import org.apache.myfaces.util.ExternalContextUtils;
-
/**
* Implements the lifecycle as described in Spec. 1.0 PFD Chapter 2
*
@@ -55,39 +52,6 @@ class RenderResponseExecutor implements
String viewId;
String newViewId;
- // JSF 2.0 section 2.2.6: if the current response
- // is a partial response, then there must be
- // no content written outside of the f:view
- // -= Leonardo Uribe =- This section was commented because it
- // causes problems when a library try to override the default
- // PartialViewContext implementation like trinidad. Since ajax
- // stuff is only for facelets, there is no reason to keep this
- // code here. Instead, this code was moved to
- // JspViewDeclarationLanguage.buildView, because it has more sense
- // in that place.
- /*
- if (facesContext.getPartialViewContext().isPartialRequest())
- {
- // try to get (or create) a ResponseSwitch and turn off the output
- Object response = facesContext.getExternalContext().getResponse();
- ResponseSwitch responseSwitch = ExternalContextUtils.getResponseSwitch(response);
- if (responseSwitch == null)
- {
- // no ResponseSwitch installed yet - create one
- responseSwitch = ExternalContextUtils.createResponseSwitch(response);
- if (responseSwitch != null)
- {
- // install the ResponseSwitch
- facesContext.getExternalContext().setResponse(responseSwitch);
- }
- }
- if (responseSwitch != null)
- {
- responseSwitch.setEnabled(false);
- }
- }
- */
-
try
{
// do-while, because the view might change in PreRenderViewEvent-listeners
Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java?rev=939021&r1=939020&r2=939021&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java Wed Apr 28 17:38:25 2010
@@ -33,9 +33,7 @@ import javax.servlet.http.HttpServletRes
import javax.servlet.jsp.jstl.core.Config;
import org.apache.myfaces.application.jsp.ServletViewResponseWrapper;
-import org.apache.myfaces.context.servlet.ResponseSwitch;
import org.apache.myfaces.shared_impl.view.JspViewDeclarationLanguageBase;
-import org.apache.myfaces.util.ExternalContextUtils;
import org.apache.myfaces.view.facelets.tag.composite.CompositeLibrary;
import org.apache.myfaces.view.facelets.tag.jsf.core.CoreLibrary;
import org.apache.myfaces.view.facelets.tag.jsf.html.HtmlLibrary;
@@ -80,28 +78,6 @@ public class JspViewDeclarationLanguage
super.buildView(context, view);
ExternalContext externalContext = context.getExternalContext();
-
- if (context.getPartialViewContext().isPartialRequest())
- {
- // try to get (or create) a ResponseSwitch and turn off the output
- Object origResponse = context.getExternalContext().getResponse();
- ResponseSwitch responseSwitch = ExternalContextUtils.getResponseSwitch(origResponse);
- if (responseSwitch == null)
- {
- // no ResponseSwitch installed yet - create one
- responseSwitch = ExternalContextUtils.createResponseSwitch(origResponse);
- if (responseSwitch != null)
- {
- // install the ResponseSwitch
- context.getExternalContext().setResponse(responseSwitch);
- }
- }
- if (responseSwitch != null)
- {
- responseSwitch.setEnabled(context, false);
- }
- }
-
ServletResponse response = (ServletResponse) externalContext.getResponse();
ServletRequest request = (ServletRequest) externalContext.getRequest();
@@ -198,8 +174,10 @@ public class JspViewDeclarationLanguage
return;
}
- //Skip this step if we are rendering an ajax request, because no content outside
- //f:view tag should be output.
+ // Skip this step if we are rendering an ajax request, because no content outside
+ // f:view tag should be output.
+ // Note that the ResponseSwitch would prevent this output from beeing written
+ // in renderView(), but not providing the information at all makes it faster!
if (!context.getPartialViewContext().isPartialRequest())
{
// store the wrapped response in the request, so it is thread-safe
Modified: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java?rev=939021&r1=939020&r2=939021&view=diff
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java (original)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java Wed Apr 28 17:38:25 2010
@@ -20,18 +20,22 @@ package org.apache.myfaces.shared.util;
import java.io.IOException;
import java.io.InputStream;
-
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.ExternalContext;
-
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.myfaces.shared.view.HttpServletResponseSwitch;
+import org.apache.myfaces.shared.view.ResponseSwitch;
+import org.apache.myfaces.shared.view.ServletResponseSwitch;
+
/**
* This provides some functionality for determining some things about the
* native request object that is not provided by the base utils.
@@ -623,4 +627,78 @@ public final class ExternalContextUtils
_PORTLET_10_SUPPORTED = portlet10Supported;
_PORTLET_20_SUPPORTED = portlet20Supported;
}
-}
\ No newline at end of file
+
+ /**
+ * Trys to obtain a HttpServletResponse from the Response.
+ * Note that this method also trys to unwrap any ServletResponseWrapper
+ * in order to retrieve a valid HttpServletResponse.
+ * @param response
+ * @return if found, the HttpServletResponse, null otherwise
+ */
+ public static HttpServletResponse getHttpServletResponse(Object response)
+ {
+ // unwrap the response until we find a HttpServletResponse
+ while (response != null)
+ {
+ if (response instanceof HttpServletResponse)
+ {
+ // found
+ return (HttpServletResponse) response;
+ }
+ if (response instanceof ServletResponseWrapper)
+ {
+ // unwrap
+ response = ((ServletResponseWrapper) response).getResponse();
+ }
+ // no more possibilities to find a HttpServletResponse
+ break;
+ }
+ return null; // not found
+ }
+
+ /**
+ * Trys to obtain a ResponseSwitch from the Response.
+ * @param response
+ * @return if found, the ResponseSwitch, null otherwise
+ */
+ public static ResponseSwitch getResponseSwitch(Object response)
+ {
+ // unwrap the response until we find a ResponseSwitch
+ while (response != null)
+ {
+ if (response instanceof ResponseSwitch)
+ {
+ // found
+ return (ResponseSwitch) response;
+ }
+ if (response instanceof ServletResponseWrapper)
+ {
+ // unwrap
+ response = ((ServletResponseWrapper) response).getResponse();
+ }
+ // no more possibilities to find a ResponseSwitch
+ break;
+ }
+ return null; // not found
+ }
+
+ /**
+ * Try to create a ResponseSwitch for this response.
+ * @param response
+ * @return the created ResponseSwitch, if there is a ResponseSwitch
+ * implementation for the given response, null otherwise
+ */
+ public static ResponseSwitch createResponseSwitch(Object response)
+ {
+ if (response instanceof HttpServletResponse)
+ {
+ return new HttpServletResponseSwitch((HttpServletResponse) response);
+ }
+ else if (response instanceof ServletResponse)
+ {
+ return new ServletResponseSwitch((ServletResponse) response);
+ }
+ return null;
+ }
+
+}
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/util/ExternalContextUtils.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java?rev=939021&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java Wed Apr 28 17:38:25 2010
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shared.view;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+/**
+ * Implementation of a switching response wrapper to turn
+ * output on and off according to the JSF spec 2.0.
+ * <p/>
+ * Implemented as HttpServletResponseWrapper,
+ * so that the switching does not interfere with methods that
+ * expect a HttpServletResponse when invoking ExternalContext.getResponse().
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @author Jakob Korherr
+ * @version $Revision$ $Date$
+ */
+public class HttpServletResponseSwitch extends HttpServletResponseWrapper implements ResponseSwitch
+{
+ private PrintWriter _switchableWriter;
+ private SwitchableOutputStream _switchableOutputStream;
+ private boolean _enabled = true;
+
+ public HttpServletResponseSwitch(HttpServletResponse response)
+ {
+ super(response);
+ }
+
+ /**
+ * Enables or disables the Response's Writer and OutputStream.
+ * @param enabled
+ */
+ public void setEnabled(boolean enabled)
+ {
+ _enabled = enabled;
+ }
+
+ /**
+ * Are the Response's Writer and OutputStream currently enabled?
+ * @return
+ */
+ public boolean isEnabled()
+ {
+ return _enabled;
+ }
+
+
+ @Override
+ public int getBufferSize()
+ {
+ if (isEnabled())
+ {
+ return super.getBufferSize();
+ }
+ return 0;
+ }
+
+ @Override
+ public boolean isCommitted()
+ {
+ if (isEnabled())
+ {
+ return super.isCommitted();
+ }
+ return false;
+ }
+
+ @Override
+ public void reset()
+ {
+ if (isEnabled())
+ {
+ super.reset();
+ }
+ }
+
+ @Override
+ public void resetBuffer()
+ {
+ if (isEnabled())
+ {
+ super.resetBuffer();
+ }
+ }
+
+ @Override
+ public void flushBuffer() throws IOException
+ {
+ if (isEnabled())
+ {
+ super.flushBuffer();
+ }
+ }
+
+ @Override
+ public void setResponse(ServletResponse response)
+ {
+ // only change the response if it is not the same object
+ if (response instanceof HttpServletResponse && response != getResponse())
+ {
+ // our OutputStream and our Writer are not valid for the new response
+ _switchableOutputStream = null;
+ _switchableWriter = null;
+
+ // set the new response
+ super.setResponse(response);
+ }
+ }
+
+ @Override
+ public ServletOutputStream getOutputStream() throws IOException
+ {
+ if (_switchableOutputStream == null)
+ {
+ _switchableOutputStream = new SwitchableOutputStream(super.getOutputStream(), this);
+ }
+ return _switchableOutputStream;
+ }
+
+ @Override
+ public PrintWriter getWriter() throws IOException
+ {
+ if (_switchableWriter == null)
+ {
+ _switchableWriter = new PrintWriter(new SwitchableWriter(super.getWriter(), this));
+ }
+ return _switchableWriter;
+ }
+
+}
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/HttpServletResponseSwitch.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/JspViewDeclarationLanguageBase.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/JspViewDeclarationLanguageBase.java?rev=939021&r1=939020&r2=939021&view=diff
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/JspViewDeclarationLanguageBase.java (original)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/JspViewDeclarationLanguageBase.java Wed Apr 28 17:38:25 2010
@@ -42,6 +42,7 @@ import org.apache.myfaces.shared.applica
import org.apache.myfaces.shared.application.ViewHandlerSupport;
import org.apache.myfaces.shared.config.MyfacesConfig;
import org.apache.myfaces.shared.renderkit.html.util.JavascriptUtils;
+import org.apache.myfaces.shared.util.ExternalContextUtils;
public abstract class JspViewDeclarationLanguageBase extends ViewDeclarationLanguageBase
@@ -60,6 +61,28 @@ public abstract class JspViewDeclaration
{
// memorize that buildView() has been called for this view
setViewBuilt(context, view);
+
+ if (context.getPartialViewContext().isPartialRequest())
+ {
+ // try to get (or create) a ResponseSwitch and turn off the output
+ Object origResponse = context.getExternalContext().getResponse();
+ ResponseSwitch responseSwitch = ExternalContextUtils.getResponseSwitch(origResponse);
+ if (responseSwitch == null)
+ {
+ // no ResponseSwitch installed yet - create one
+ responseSwitch = ExternalContextUtils.createResponseSwitch(origResponse);
+ if (responseSwitch != null)
+ {
+ // install the ResponseSwitch
+ context.getExternalContext().setResponse(responseSwitch);
+ }
+ }
+ if (responseSwitch != null)
+ {
+ // turn the output off
+ responseSwitch.setEnabled(false);
+ }
+ }
}
/**
@@ -131,10 +154,18 @@ public abstract class JspViewDeclaration
responseWriter = renderKit.createResponseWriter(externalContext.getResponseOutputWriter(), null, externalContext.getRequestCharacterEncoding());
context.setResponseWriter(responseWriter);
}
+
+ // try to enable the ResponseSwitch again (disabled in buildView())
+ Object response = context.getExternalContext().getResponse();
+ ResponseSwitch responseSwitch = ExternalContextUtils.getResponseSwitch(response);
+ if (responseSwitch != null)
+ {
+ responseSwitch.setEnabled(true);
+ }
ResponseWriter oldResponseWriter = responseWriter;
StringWriter stateAwareWriter = null;
-
+
StateManager stateManager = context.getApplication().getStateManager();
if (stateManager.isSavingStateInClient(context))
{
@@ -153,7 +184,6 @@ public abstract class JspViewDeclaration
{
context.setResponseWriter(oldResponseWriter);
}
-
// We're done with the document - now we can write all content
// to the response, properly replacing the state-markers on the way out
@@ -166,13 +196,20 @@ public abstract class JspViewDeclaration
{
stateManager.saveView(context);
}
+
+ // now disable the ResponseSwitch again
+ if (responseSwitch != null)
+ {
+ responseSwitch.setEnabled(false);
+ }
// Final step - we output any content in the wrappedResponse response from above to the response,
// removing the wrappedResponse response from the request, we don't need it anymore
ViewResponseWrapper afterViewTagResponse = (ViewResponseWrapper) externalContext.getRequestMap()
.get(AFTER_VIEW_TAG_CONTENT_PARAM);
externalContext.getRequestMap().remove(AFTER_VIEW_TAG_CONTENT_PARAM);
-
+
+ // afterViewTagResponse is null if the current request is a partial request
if (afterViewTagResponse != null)
{
afterViewTagResponse.flushToWriter(externalContext.getResponseOutputWriter(), externalContext.getResponseCharacterEncoding());
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java?rev=939021&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java Wed Apr 28 17:38:25 2010
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shared.view;
+
+/**
+ * Responses which can be enabled or disabled implement this interface.
+ *
+ * @author Jakob Korherr (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public interface ResponseSwitch
+{
+
+ /**
+ * Enables or disables the Response's Writer and OutputStream.
+ * @param enabled
+ */
+ public void setEnabled(boolean enabled);
+
+ /**
+ * Are the Response's Writer and OutputStream currently enabled?
+ * @return
+ */
+ public boolean isEnabled();
+
+}
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ResponseSwitch.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java?rev=939021&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java Wed Apr 28 17:38:25 2010
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shared.view;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletResponse;
+import javax.servlet.ServletResponseWrapper;
+
+/**
+ * Implementation of a switching response wrapper to turn
+ * output on and off according to the JSF spec 2.0.
+ * <p/>
+ * Fall-back implementation of HttpServletResponseSwitch
+ * for non HttpServletResponses.
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @author Jakob Korherr
+ * @version $Revision$ $Date$
+ */
+public class ServletResponseSwitch extends ServletResponseWrapper implements ResponseSwitch
+{
+
+ private PrintWriter _switchableWriter;
+ private SwitchableOutputStream _switchableOutputStream;
+ private boolean _enabled = true;
+
+ public ServletResponseSwitch(ServletResponse response)
+ {
+ super(response);
+ }
+
+ /**
+ * Enables or disables the Response's Writer and OutputStream.
+ * @param enabled
+ */
+ public void setEnabled(boolean enabled)
+ {
+ _enabled = enabled;
+ }
+
+ /**
+ * Are the Response's Writer and OutputStream currently enabled?
+ * @return
+ */
+ public boolean isEnabled()
+ {
+ return _enabled;
+ }
+
+ @Override
+ public int getBufferSize()
+ {
+ if (isEnabled())
+ {
+ return super.getBufferSize();
+ }
+ return 0;
+ }
+
+ @Override
+ public boolean isCommitted()
+ {
+ if (isEnabled())
+ {
+ return super.isCommitted();
+ }
+ return false;
+ }
+
+ @Override
+ public void reset()
+ {
+ if (isEnabled())
+ {
+ super.reset();
+ }
+ }
+
+ @Override
+ public void resetBuffer()
+ {
+ if (isEnabled())
+ {
+ super.resetBuffer();
+ }
+ }
+
+ @Override
+ public void flushBuffer() throws IOException
+ {
+ if (isEnabled())
+ {
+ super.flushBuffer();
+ }
+ }
+
+ @Override
+ public void setResponse(ServletResponse response)
+ {
+ // only change the response if it is not the same object
+ if (response != getResponse())
+ {
+ // our OutputStream and our Writer are not valid for the new response
+ _switchableOutputStream = null;
+ _switchableWriter = null;
+
+ // set the new response
+ super.setResponse(response);
+ }
+ }
+
+ @Override
+ public ServletOutputStream getOutputStream() throws IOException
+ {
+ if (_switchableOutputStream == null)
+ {
+ _switchableOutputStream = new SwitchableOutputStream(super.getOutputStream(), this);
+ }
+ return _switchableOutputStream;
+ }
+
+ @Override
+ public PrintWriter getWriter() throws IOException
+ {
+ if (_switchableWriter == null)
+ {
+ _switchableWriter = new PrintWriter(new SwitchableWriter(super.getWriter(), this));
+ }
+ return _switchableWriter;
+ }
+
+}
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/ServletResponseSwitch.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java?rev=939021&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java Wed Apr 28 17:38:25 2010
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shared.view;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.ServletOutputStream;
+
+/**
+ * Delegates the standard OutputStream-methods (close, flush, write)
+ * to the given ServletOutputStream, if the ResponseSwitch is enabled
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @author Jakob Korherr
+ * @version $Revision$ $Date$
+ */
+class SwitchableOutputStream extends ServletOutputStream
+{
+
+ OutputStream _delegate = null;
+ ResponseSwitch _responseSwitch = null;
+
+ public SwitchableOutputStream(ServletOutputStream delegate, ResponseSwitch responseSwitch)
+ {
+ _delegate = delegate;
+ _responseSwitch = responseSwitch;
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.close();
+ }
+ }
+
+ @Override
+ public void flush() throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.flush();
+ }
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(b, off, len);
+ }
+ }
+
+ @Override
+ public void write(byte[] b) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(b);
+ }
+ }
+
+ @Override
+ public void write(int b) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(b);
+ }
+ }
+
+}
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java?rev=939021&view=auto
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java (added)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java Wed Apr 28 17:38:25 2010
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shared.view;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Delegates the standard Writer-methods (append, close, flush, write)
+ * to the given Writer, if the ResponseSwitch is enabled
+ *
+ * @author Werner Punz (latest modification by $Author$)
+ * @author Jakob Korherr
+ * @version $Revision$ $Date$
+ */
+class SwitchableWriter extends Writer
+{
+
+ Writer _delegate = null;
+ ResponseSwitch _responseSwitch = null;
+
+ public SwitchableWriter(Writer delegate, ResponseSwitch responseSwitch)
+ {
+ _delegate = delegate;
+ _responseSwitch = responseSwitch;
+ }
+
+ public void write(String s, int start, int end) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(s, start, end);
+ }
+ }
+
+ public void write(String s) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(s);
+ }
+ }
+
+ public void write(char[] c, int start, int end) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(c, start, end);
+ }
+ }
+
+ public void write(char[] c) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(c);
+
+ }
+ }
+
+ public void write(int i) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.write(i);
+ }
+ }
+
+ public void flush() throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.flush();
+ }
+ }
+
+ public void close() throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ _delegate.close();
+ }
+ }
+
+ public Writer append(char c) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ return _delegate.append(c);
+ }
+ return this;
+ }
+
+ public Writer append(CharSequence csq, int start, int end)
+ throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ return _delegate.append(csq, start, end);
+ }
+ return this;
+ }
+
+ public Writer append(CharSequence csq) throws IOException
+ {
+ if (_responseSwitch.isEnabled())
+ {
+ return _delegate.append(csq);
+ }
+ return this;
+ }
+}
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/view/SwitchableWriter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain