package org.zouzias.rediscachingfilter.filters;

import org.zouzias.rediscachingfilter.redis.RedisConnector;
import java.io.IOException;
import java.util.Map;
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.ws.rs.HttpMethod;
import org.apache.log4j.Logger;

public class RedisHTTPGetFilter implements Filter {

    private static final Logger logger = Logger.getLogger(RedisHTTPGetFilter.class);
    private FilterConfig filterConfig = null;

    public RedisHTTPGetFilter() {
    }

    private String requestParamsToString(HttpServletRequest req) {
        StringBuilder builder = new StringBuilder();

        Map<String, String[]> params = req.getParameterMap();
        for (String param : params.keySet()) {
            for (String value : params.get(param)) {
                if (builder.length() == 0) {
                    builder.append(param).append("=").append(value);
                } else {
                    builder.append("&").append(param).append("=").append(value);
                }
            }
        }
        return builder.toString();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        logger.debug("RedisHTTPGetFilter:doFilter()");

        HttpServletRequest req = (HttpServletRequest) request;
        String hashURL = null;
        String cachedContent = null;

        // Cache only HTTP GET method
        if (req.getMethod().compareTo(HttpMethod.GET) == 0) {
            hashURL = req.getRequestURL() + requestParamsToString(req);
            logger.debug("Request URL : " + hashURL);
            cachedContent = RedisConnector.getKey(hashURL);
        }

        if (cachedContent != null) {
            logger.debug("Cached element hit!");
            response.getOutputStream().write(cachedContent.getBytes("UTF-8"));
        } else {
            logger.debug("Cache miss! on hashURL " + hashURL);
            chain.doFilter(request, response);
            response.getOutputStream().print(response.toString());

            if (hashURL != null && response.toString() != null) {
                logger.debug("Hashing URL\n -->HASH: " + hashURL + "\n-->VALUE: " + response.toString());
                RedisConnector.addKey(hashURL, response.toString(), true);
            }
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
        if (filterConfig != null) {
            logger.debug("RedisHTTPGetFilter:Initializing filter");
        }
    }
}

@@ -441,16 +441,106 @@
 Method;%0A
+import org.apache.commons.codec.digest.DigestUtils;%0Aimport org.apache.commons.io.IOUtils;%0A
 import o
@@ -563,16 +563,16 @@
 Logger;%0A
-
 %0Apublic 
@@ -786,24 +786,80 @@
 () %7B%0A    %7D%0A%0A
+    /**%0A     *%0A     * @param req%0A     * @return%0A     */%0A
     private 
@@ -858,16 +858,23 @@
 private 
+static 
 String r
@@ -1432,32 +1432,660 @@
 tring();%0A    %7D%0A%0A
+    public static String stringifyHttpGetRequest(HttpServletRequest req) %7B%0A        return req.getRequestURL() + requestParamsToString(req);%0A    %7D%0A%0A    public static String stringifyHttpPostRequest(HttpServletRequest req) %7B%0A        try %7B%0A            String postTextContent = IOUtils.toString(req.getInputStream());%0A            logger.info(%22HTTP Post content is %22 + postTextContent);%0A            return req.getRequestURL() + %22:%22 +DigestUtils.md5Hex(postTextContent);%0A        %7D catch (IOException ex) %7B%0A            logger.error(%22Could not read POST input stream or stream is not UTF-8%22, ex);%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A
     @Override%0A  
@@ -2449,13 +2449,8 @@
 che 
-only 
 HTTP
@@ -2549,111 +2549,380 @@
 L = 
-req.getRequestURL() + requestParamsToString(req);%0A            logger.debug(%22Request URL : %22 + hashURL);
+stringifyHttpGetRequest(req);%0A            logger.debug(%22HTTP GET on %5B%22 + hashURL + %22%5D%22);%0A            cachedContent = RedisConnector.getKey(hashURL);%0A        %7D // Cache HTTP Post method %0A        else if (req.getMethod().compareTo(HttpMethod.POST) == 0) %7B%0A            String hashedPOSTContent = stringifyHttpPostRequest(req);%0A%0A            // Check if the content is in Redis
 %0A   
@@ -2964,35 +2964,45 @@
 ctor.getKey(hash
-URL
+edPOSTContent
 );%0A        %7D%0A%0A  

Add HTTP POST support: Work in progress