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

cat.inspiracio.servlet.http.BufferedResponse Maven / Gradle / Ivy

Go to download

Dummy Servlet provides some implementations for the interfaces and classes of the Java Servlet spec that are useful for testing and simulation. Version 5.0.0 is for Servlet 5.0.0.

The newest version!
/*
Copyright 2016 Alexander Bunkenburg

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 cat.inspiracio.servlet.http;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;

/** It seems that in appengine, the response buffer cannot be reset.
 * 
 * See http://stackoverflow.com/questions/36691900/httpservletresponse-resetbuffer-doesnt-work.
 * 
 * Until I find out what's wrong or find a fix, this is a workaround. 
 * 
 * This implementation is not complete nor correct:
 * at the end of doGet(), must call response.flushBuffer(),
 * setting and getting buffer size is not accurate.
 * */
public class BufferedResponse extends HttpServletResponseWrapper{

	// state -----------------------------------------------------
	
	/** If null, getWriter() has not been called yet, or it has been reset. */
	private PrintWriter writer;
	
	private final int bufferSize=8192;
	
	// construction ---------------------------------------------

	/** Make a buffered response
	 * @param response buffer this response */
	public BufferedResponse(HttpServletResponse response){
		super(response);
	}

	// API methods ---------------------------------------------
	
	/** Puts a buffer around super.getWriter(). */
	@Override public PrintWriter getWriter() throws IOException{
		if(writer==null){
			Writer w=super.getWriter();
			w=new BufferedWriter(w, bufferSize);
			writer=new PrintWriter(w);
		}
		return writer;
	}

	@Override public void flushBuffer() throws IOException{
		if(writer!=null)writer.flush();
		super.flushBuffer();
	}

//	@Override public boolean isCommitted(){
//		/* Not so easy. I have to count the bytes that are written,
//		 * or make our own implementation of BufferedWriter. */
//	}
	
	@Override public void reset(){
		writer=null;
		super.reset();
	}

	@Override public void resetBuffer(){
		writer=null;	
		super.resetBuffer();
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy