// Copyright (c) 2003-2012, Jodd Team (jodd.org). All Rights Reserved.

package jodd.lagarto.filter;

import jodd.io.FileNameUtil;
import jodd.log.Log;
import jodd.servlet.DispatcherUtil;
import jodd.servlet.filter.CharArrayResponseWrapper;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

/**
 * Lagarto servlet filter takes HTML content and invokes user defined parser on it.
 * This filter is a generic one and does not use Lagarto parser explicitly.
 * It just gives a placeholder where user can add it's own parsing mechanism.
 */
public abstract class LagartoServletFilter implements Filter {

	private static final Log log = Log.getLogger(LagartoServletFilter.class);

	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void destroy() {
	}

	/**
	 * Wraps the response and parse it using Lagarto parser.
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String actionPath = DispatcherUtil.getServletPath(request);

		if (acceptActionPath(request, actionPath) == false) {
			filterChain.doFilter(servletRequest, servletResponse);
			return;
		}

		CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response);
		filterChain.doFilter(servletRequest, wrapper);
		char[] content = wrapper.toCharArray();

		if ((content != null) && (content.length != 0)) {
			if (log.isDebugEnabled()) {
				log.debug("Lagarto is about to parse: " + actionPath);
			}
			try {
				content = parse(content, request);
			} catch (Exception ex) {
				log.error("Error parsing", ex);
				throw new ServletException(ex);
			}
			Writer out = servletResponse.getWriter();
			out.write(content);
		}
	}

	/**
	 * Accepts action path for further parsing. By default, only <code>*.htm(l)</code>
	 * requests are passed through and those without any extension.
	 */
	protected boolean acceptActionPath(HttpServletRequest request, String actionPath) {
		String extension = FileNameUtil.getExtension(actionPath);
		if (extension.length() == 0) {
			return true;
		}
		if (extension.equals("html") || extension.equals("htm")) {
			return true;
		}
		return false;
	}

	/**
	 * Main method that parses content. It can use Lagarto parser
	 * or any other custom parsing technology.
	 */
	protected abstract char[] parse(char[] content, HttpServletRequest request);

}

@@ -212,24 +212,22 @@
 let.
-filter.CharArray
+wrapper.Buffer
 Resp
@@ -587,32 +587,8 @@
 on;%0D
-%0Aimport java.io.Writer;%0D
 %0A%0D%0A/
@@ -1657,25 +1657,22 @@
 %09%7D%0D%0A%0D%0A%09%09
-CharArray
+Buffer
 Response
@@ -1693,25 +1693,22 @@
 r = new 
-CharArray
+Buffer
 Response
@@ -1777,16 +1777,18 @@
 pper);%0D%0A
+%0D%0A
 %09%09char%5B%5D
@@ -1810,19 +1810,31 @@
 per.
-toCharArray
+getBufferContentAsChars
 ();%0D
@@ -2150,65 +2150,42 @@
 %09%7D%0D%0A
+%0D%0A
 %09%09%09
-Writer out = servletResponse.getWriter();%0D%0A%09%09%09out.writ
+wrapper.writeContentToRespons
 e(co
@@ -2880,10 +2880,8 @@
 t);%0D%0A%0D%0A%7D
-%0D%0A

Use generic BufferResponseWrapper (Fixes issue 9).