All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.myfaces.shared_tomahawk.view.HttpServletResponseSwitch Maven / Gradle / Ivy

Go to download

JSF components and utilities that can be used with any JSF implementation. This library is based on the JSF1.1 version of Tomahawk, but with minor source code and build changes to take advantage of JSF2.1 features. A JSF2.1 implementation is required to use this version of the Tomahawk library.

The newest version!
/*
 * 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_tomahawk.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.
 * 

* 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: lu4242 $) * @author Jakob Korherr * @version $Revision: 1151676 $ $Date: 2011-07-27 19:01:24 -0500 (Wed, 27 Jul 2011) $ */ 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; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy