package org.archive.wayback.webapp;

import java.io.IOException;
import java.io.PrintWriter;

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

public class PerfWritingHttpServletResponse extends HttpServletResponseWrapper {
	
	protected Enum<?> perfStat;
	protected String perfStatsHeader;
	protected boolean hasWritten;
	protected HttpServletResponse httpResponse;
	
	protected int expireTimeout = 60;
	
	protected String requestURI;
	protected boolean perfCookie = false;
	
	public PerfWritingHttpServletResponse(HttpServletRequest request, HttpServletResponse response, Enum<?> stat, String perfStatsHeader)
	{
		super(response);
		
		this.httpResponse = response;
		this.requestURI = request.getRequestURI();
		this.perfStat = stat;
		this.perfStatsHeader = perfStatsHeader;
	}
	
	public void writePerfStats()
	{
		if (hasWritten) {
			return;
		}
		
		long elapsed = PerfStats.timeEnd(perfStat);
		
		if (perfStatsHeader != null) {
			httpResponse.setHeader(perfStatsHeader, PerfStats.getAllStats());
		}
		
		if (requestURI != null) {
			Cookie cookie = new Cookie("wb_total_perf", String.valueOf(elapsed));
			cookie.setMaxAge(expireTimeout);
			//cookie.setDomain(domainName);
			cookie.setPath(requestURI);
			httpResponse.addCookie(cookie);
		}
		
		hasWritten = true;
	}

	@Override
	public void sendError(int sc, String msg) throws IOException {
		writePerfStats();
		super.sendError(sc, msg);
	}

	@Override
	public void sendError(int sc) throws IOException {
		writePerfStats();
		super.sendError(sc);
	}

	@Override
	public void sendRedirect(String location) throws IOException {
		writePerfStats();
		super.sendRedirect(location);
	}

	@Override
	public ServletOutputStream getOutputStream() throws IOException {
		writePerfStats();
		return super.getOutputStream();
	}

	@Override
	public PrintWriter getWriter() throws IOException {
		writePerfStats();
		return super.getWriter();
	}

	/**
	 * @deprecated 1.8.1, no replacement. this method has no effect.
	 */
	public void enablePerfCookie() {
		this.perfCookie  = true;
    }
}

@@ -392,18 +392,17 @@
 apper %7B%0A
-%09
 %0A
+
 %09protect
@@ -533,18 +533,17 @@
 sponse;%0A
-%09
 %0A
+
 %09protect
@@ -569,18 +569,17 @@
 t = 60;%0A
-%09
 %0A
+
 %09protect
@@ -639,17 +639,16 @@
  false;%0A
-%09
 %0A%09public
@@ -706,17 +706,20 @@
 request,
- 
+%0A%09%09%09
 HttpServ
@@ -777,18 +777,17 @@
 sHeader)
-%0A%09
+ 
 %7B%0A%09%09supe
@@ -799,19 +799,17 @@
 ponse);%0A
-%09%09
 %0A
+
 %09%09this.h
@@ -946,17 +946,16 @@
 der;%0A%09%7D%0A
-%09
 %0A%09public
@@ -976,18 +976,17 @@
 fStats()
-%0A%09
+ 
 %7B%0A%09%09if (
@@ -1014,19 +1014,17 @@
 rn;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09long e
@@ -1061,19 +1061,17 @@
 fStat);%0A
-%09%09
 %0A
+
 %09%09if (pe
@@ -1168,19 +1168,17 @@
 ));%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09if (re
@@ -1415,11 +1415,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%09ha
@@ -2185,25 +2185,24 @@
 fCookie 
- 
 = true;%0A
     %7D%0A%7D%0A
@@ -2197,12 +2197,9 @@
 ue;%0A
-    
+%09
 %7D%0A%7D%0A

clean up code format style.