package nl.nn.adapterframework.testtool;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Locale;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public class HttpServletResponseMock implements HttpServletResponse {
	String outputFile;

	public String getOutputFile() {
		return outputFile;
	}

	public void setOutputFile(String outputFile) {
		this.outputFile = outputFile;
	}

	public void flushBuffer() throws IOException {
		// TODO Auto-generated method stub
		
	}

	public int getBufferSize() {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getCharacterEncoding() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getContentType() {
		// TODO Auto-generated method stub
		return null;
	}

	public Locale getLocale() {
		// TODO Auto-generated method stub
		return null;
	}

	public ServletOutputStream getOutputStream() throws IOException {
		return new ServletOutputStreamMock(getOutputFile());
	}

	public PrintWriter getWriter() throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isCommitted() {
		// TODO Auto-generated method stub
		return false;
	}

	public void reset() {
		// TODO Auto-generated method stub
		
	}

	public void resetBuffer() {
		// TODO Auto-generated method stub
		
	}

	public void setBufferSize(int arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setCharacterEncoding(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setContentLength(int arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setContentType(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setLocale(Locale arg0) {
		// TODO Auto-generated method stub
		
	}

	public void addCookie(Cookie arg0) {
		// TODO Auto-generated method stub
		
	}

	public void addDateHeader(String arg0, long arg1) {
		// TODO Auto-generated method stub
		
	}

	public void addHeader(String arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	public void addIntHeader(String arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public boolean containsHeader(String arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public String encodeRedirectURL(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public String encodeRedirectUrl(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public String encodeURL(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public String encodeUrl(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getHeader(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<String> getHeaderNames() {
		// TODO Auto-generated method stub
		return null;
	}

	public Collection<String> getHeaders(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getStatus() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void sendError(int arg0) throws IOException {
		// TODO Auto-generated method stub
		
	}

	public void sendError(int arg0, String arg1) throws IOException {
		// TODO Auto-generated method stub
		
	}

	public void sendRedirect(String arg0) throws IOException {
		// TODO Auto-generated method stub
		
	}

	public void setDateHeader(String arg0, long arg1) {
		// TODO Auto-generated method stub
		
	}

	public void setHeader(String arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	public void setIntHeader(String arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void setStatus(int arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setStatus(int arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}
}

class ServletOutputStreamMock extends ServletOutputStream {
	FileOutputStream fileOutputStream;

	ServletOutputStreamMock(String outputFile) throws FileNotFoundException {
		fileOutputStream = new FileOutputStream(outputFile);
	}

	@Override
	public void write(int arg0) throws IOException {
		fileOutputStream.write(arg0);
	}

	@Override
	public void close() throws IOException {
		fileOutputStream.close();
		super.close();
	}
}
@@ -430,16 +430,51 @@
 putFile;
+%0A%09ServletOutputStream outputStream;
 %0A%0A%09publi
@@ -1124,22 +1124,61 @@
 ion %7B%0A%09%09
-return
+if (outputStream == null) %7B%0A%09%09%09outputStream =
  new Ser
@@ -1216,16 +1216,43 @@
 ile());%0A
+%09%09%7D%0A%09%09return outputStream;%0A
 %09%7D%0A%0A%09pub
@@ -3706,36 +3706,36 @@
 etHeader(String 
-arg0
+name
 , String arg1) %7B
@@ -3731,55 +3731,222 @@
 ing 
-arg1
+value
 ) %7B%0A%09%09
-// TODO Auto-generated method stub
+String line = name + %22: %22 + value + %22%5Cn%22;%0A%09%09try %7B%0A%09%09%09getOutputStream().write(line.getBytes());%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new RuntimeException(%22Could not write header to output stream%22, e);
 %0A%09%09
+%7D
 %0A%09%7D%0A

Write headers to file also