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

org.ajax4jsf.taglib.ajax.ServletResponseWrapperInclude Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/*
 * Copyright 1999,2004 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.ajax4jsf.taglib.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.jsp.JspWriter;

import com.sun.corba.se.impl.ior.ByteBuffer;

/**
 * ServletResponseWrapper used by the JSP 'include' action.
 * 
 * This wrapper response object is passed to RequestDispatcher.include(), so
 * that the output of the included resource is appended to that of the including
 * page.
 * 
 * @author Pierre Delisle
 */

public class ServletResponseWrapperInclude extends HttpServletResponseWrapper {

	/**
	 * PrintWriter which appends to the JspWriter of the including page.
	 */
	private PrintWriter _printWriter;
	
	private ByteBuffer _bytes ;
	
	private ServletOutputStream _servletStream;

	private JspWriter _jspWriter;
	
	private boolean useWriter = false;
	
	private boolean useStream = false;

	public ServletResponseWrapperInclude(ServletResponse response,
			JspWriter jspWriter) {
		super((HttpServletResponse) response);
		this._printWriter = new PrintWriter(jspWriter);
		this._jspWriter = jspWriter;
		this._bytes = new ByteBuffer(response.getBufferSize());
		this._servletStream = new ServletOutputStream(){
			// TODO - use properly encoding ???
			public void write(int b) throws IOException {
				_bytes.append(b);				
			}
		};
	}

	/**
	 * Returns a wrapper around the JspWriter of the including page.
	 */
	public PrintWriter getWriter() throws IOException {
		if (useStream) {
			throw new IllegalStateException();
		}
		useWriter = true;
		return _printWriter;
	}

	public ServletOutputStream getOutputStream() throws IOException {
		if(useWriter){
			throw new IllegalStateException();
		}
		useStream = true;
		return _servletStream;
	}

	/**
	 * Clears the output buffer of the JspWriter associated with the including
	 * page.
	 */
	public void resetBuffer() {
		try {
			_jspWriter.clearBuffer();
			useWriter = false;
			useStream = false;
		} catch (IOException ioe) {
		}
	}
	
	public void reset() {
		// TODO Auto-generated method stub
		resetBuffer();
	}
	
	public void flushBuffer() throws IOException {
		if (useStream) {
			// TODO - detect encoding ?
			_jspWriter.write(new String(_bytes.toArray()));
			_bytes = new ByteBuffer(getBufferSize());
		}
	}
	
	// Override ignored methods.
	public void setBufferSize(int arg0) {
	}
	
	public void setContentLength(int arg0) {
	}
	public void setContentType(String arg0) {
	}
	public void setStatus(int arg0) {
	}
	public void setLocale(Locale arg0) {
	}
	public void sendRedirect(String arg0) throws IOException {
		throw new IllegalStateException();
	}
//	public void setStatus(int arg0, String arg1) {
//	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy