package com.rockson.servletplus;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.alibaba.fastjson.JSON;

public class Response extends HttpServletResponseWrapper {
	protected ServletContext servletContext;
	protected Request req;
	public Response(HttpServletResponse response,Request req,ServletContext servletContext) {
		super(response);
		this.req = req;
		this.servletContext = servletContext;
	}

	public void json(Object object) throws IOException {
		getWriter().write(JSON.toJSONString(object));
	}

	protected void copy(InputStream in, OutputStream out, int bufferSize) throws IOException {
		byte[] buff = new byte[bufferSize <= 0 ? 4 * 1024 : bufferSize];
		int len;
		while (-1 != (len = in.read(buff))) {
			out.write(buff, 0, len);
		}
	}
	
	public void render(String jsp) throws IOException, ServletException {
		req.getRequestDispatcher(jsp).forward(req, this);
	}

	public void send(String str) throws IOException {
		if (null == str) {
			return;
		}
		getWriter().write(str);
	}
	protected String makeEtag(File file) {
		return "W/\""+file.lastModified()+"\"";
	}

	public void sendFile(String str) throws IOException {
		String path = servletContext.getRealPath(str);
		File file = new File(path);
		String etag = req.getHeader("If-None-Match");
		if(null!= etag && makeEtag(file).equals(etag)) {
			setStatus(304);
			return;
		}
		addHeader("ETag", makeEtag(file));
		String modifiedSince = req.getHeader("If-Modified-Since");
		try {
			if(null!=modifiedSince && Math.floor(TimeUtils.parseGMT(modifiedSince).getTime()/1000)>=Math.floor(file.lastModified()/1000)){
				setStatus(304);
				return;
			}
		} catch (ParseException e) {
			//go on
		}
		addHeader("Last-Modified", TimeUtils.formatGMT(new Date(file.lastModified())));
		addHeader("Content-Length", "" + file.length());
		addHeader("Content-Type", Files.probeContentType(file.toPath()));
		InputStream inputStream= new FileInputStream(file);
		sendFile(inputStream);
		inputStream.close();
	}

	public void sendFile(InputStream in) throws IOException {
		copy(in, getOutputStream(), 0);
	}

	public void sendFile(Path path) throws IOException {
		reset();
		File file = path.toFile();
		addHeader("Content-Length", "" + file.length());
		addHeader("Content-Type", Files.probeContentType(path));
		Files.copy(path, getOutputStream());
	}

	public void download(Path path) throws IOException {
		download(path, null);
	}

	public void download(Path path, String name) throws IOException {
		reset();
		File file = path.toFile();
		addHeader("Content-Disposition", "attachment; filename=" + (null == name ? file.getName() : name));
		addHeader("Content-Length", "" + file.length());
		addHeader("Content-Type", Files.probeContentType(path));
		
		Files.copy(path, getOutputStream());
	}
	
}

@@ -1669,16 +1669,118 @@
 tion %7B%0D%0A
+%09%09sendFile(str, false);%0D%0A%09%7D%0D%0A%09public void sendFile(String str,boolean useCache) throws IOException %7B%0D%0A
 %09%09String
@@ -1848,24 +1848,43 @@
 ile(path);%0D%0A
+%09%09if(useCache) %7B%0D%0A%09
 %09%09String eta
@@ -1916,24 +1916,25 @@
 e-Match%22);%0D%0A
+%09
 %09%09if(null!= 
@@ -1976,16 +1976,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 setStatu
@@ -1994,16 +1994,17 @@
 (304);%0D%0A
+%09
 %09%09%09retur
@@ -2005,27 +2005,29 @@
 %09return;%0D%0A%09%09
+%09
 %7D%0D%0A
+%09
 %09%09addHeader(
@@ -2052,16 +2052,17 @@
 ile));%0D%0A
+%09
 %09%09String
@@ -2121,15 +2121,17 @@
 %0D%0A%09%09
+%09
 try %7B%0D%0A
+%09
 %09%09%09i
@@ -2257,24 +2257,25 @@
 000))%7B%0D%0A%09%09%09%09
+%09
 setStatus(30
@@ -2275,32 +2275,33 @@
 atus(304);%0D%0A%09%09%09%09
+%09
 return;%0D%0A%09%09%09%7D%0D%0A%09
@@ -2296,19 +2296,21 @@
 rn;%0D%0A%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%7D catc
@@ -2336,16 +2336,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 //go on%0D
@@ -2348,19 +2348,21 @@
 o on%0D%0A%09%09
+%09
 %7D%0D%0A
+%09
 %09%09addHea
@@ -2432,24 +2432,29 @@
 fied())));%0D%0A
+%09%09%7D%0D%0A
 %09%09addHeader(

add sendFile method