package com.delidishes.validate.spring.filter;


import com.delidishes.validate.spring.ValidateHttpServletWrapper;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ValidateFilter implements Filter {

    private static final Logger LOG = Logger.getLogger(ValidateFilter.class);

    private String encoding;

    @Override
    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter("requestEncoding");
        if (encoding == null) {
            encoding = StandardCharsets.UTF_8.toString();
        }
        LOG.info(String.format("Init [%s] with encoding %s", this.getClass().getName(), encoding));
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(new ValidateHttpServletWrapper((HttpServletRequest) request, encoding), response);
    }

    @Override
    public void destroy() {
        LOG.info(String.format("Destroy [%s] with encoding %s", this.getClass().getName(), encoding));
    }
}

@@ -280,16 +280,145 @@
 harsets;
+%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0Aimport static java.util.stream.Collectors.toList;
 %0A%0Apublic
@@ -567,16 +567,55 @@
 ncoding;
+%0A    private List%3CString%3E contentTypes;
 %0A%0A    @O
@@ -948,24 +948,357 @@
  encoding));
+%0A%0A        String contentTypes = config.getInitParameter(%22contentTypes%22);%0A        this.contentTypes = contentTypes == null ? Collections.emptyList() : Arrays.stream(contentTypes.split(%22,%22)).map(String::trim).collect(toList());%0A        LOG.info(String.format(%22Init %5B%25s%5D with contentTypes %25s%22, this.getClass().getName(), contentTypes));
 %0A    %7D%0A%0A    
@@ -1432,32 +1432,104 @@
 vletException %7B%0A
+        if (getContentTypes().contains(request.getContentType())) %7B%0A    
         chain.do
@@ -1596,24 +1596,29 @@
 equest, 
-e
+getE
 ncoding
+()
 ), respo
@@ -1623,16 +1623,90 @@
 ponse);%0A
+        %7D else %7B%0A            chain.doFilter(request, response);%0A        %7D%0A
     %7D%0A%0A 
@@ -1854,11 +1854,157 @@
 );%0A    %7D
+%0A%0A    public String getEncoding() %7B%0A        return encoding;%0A    %7D%0A%0A    public List%3CString%3E getContentTypes() %7B%0A        return contentTypes;%0A    %7D
 %0A%7D%0A

add filter parameter for available content types