{"id":268,"date":"2016-05-28T19:06:48","date_gmt":"2016-05-28T18:06:48","guid":{"rendered":"http:\/\/tastaturkind.ch\/?p=268"},"modified":"2017-11-28T22:05:05","modified_gmt":"2017-11-28T21:05:05","slug":"minifyfilter","status":"publish","type":"post","link":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/2016\/05\/28\/minifyfilter\/","title":{"rendered":"MinifyFilter"},"content":{"rendered":"<pre>package com.ofrick.filters;\n\nimport javax.servlet.*;\nimport javax.servlet.http.*;\nimport java.io.*;\nimport java.util.regex.*;\n\npublic class MinifyFilter implements Filter {\n\nprivate Pattern regex = null;\n \n \/\/ This method is called once on server startup\n public void init(FilterConfig filterConfig) \n {\n regex = Pattern.compile(\"^\\\\s*$\\\\n|\\\\t\", Pattern.MULTILINE);\n }\n \n \/\/ This method is called for every request and needs to be thread safe.\n public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) \n throws java.io.IOException, javax.servlet.ServletException \n {\n HttpServletResponse resp = (HttpServletResponse) response;\n ResponseWrapper wrapper = new ResponseWrapper(resp);\n chain.doFilter(request, wrapper);\n \n String html = wrapper.toString();\n\nhtml = regex.matcher(html).replaceAll(\"\");\n \/\/html = multipleSpaces.matcher(html).replaceAll(\" \");\n resp.setContentLength(html.getBytes().length);\n PrintWriter out = resp.getWriter();\n out.write(html);\n out.flush();\n out.close();\n }\n \n \/\/ This method is called once on server shut down\n public void destroy() \n {\n \/\/\n }\n \n static class ResponseWrapper extends HttpServletResponseWrapper {\n private CharArrayWriter output;\n\npublic ResponseWrapper(HttpServletResponse response) {\n super(response);\n this.output = new CharArrayWriter();\n }\n\npublic String toString() {\n return output.toString();\n }\n\npublic PrintWriter getWriter() {\n return new PrintWriter(output);\n }\n }\n \n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package com.ofrick.filters; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.regex.*; public class MinifyFilter implements Filter { private Pattern regex = null; \/\/ This method is called once on server startup public void init(FilterConfig filterConfig) { regex = Pattern.compile(&#8222;^\\\\s*$\\\\n|\\\\t&#8220;, Pattern.MULTILINE); } &hellip; <a href=\"https:\/\/oliver-frick.ch\/wordpress\/index.php\/2016\/05\/28\/minifyfilter\/\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":1,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/268\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oliver-frick.ch\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}