Permalink
Browse files

Added a filter for caching

  • Loading branch information...
1 parent 98a545a commit 0ec22a5cffd859b9dc5013511fd2d7d2f80d694d @fsouza committed Dec 10, 2010
Showing with 100 additions and 0 deletions.
  1. +9 −0 WebContent/WEB-INF/web.xml
  2. +91 −0 src/com/franciscosouza/java/memcached/MemcachedFilter.java
View
9 WebContent/WEB-INF/web.xml
@@ -19,4 +19,13 @@
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
+ <filter>
+ <display-name>MemcachedFilter</display-name>
+ <filter-name>MemcachedFilter</filter-name>
+ <filter-class>com.franciscosouza.java.memcached.MemcachedFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>MemcachedFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
</web-app>
View
91 src/com/franciscosouza/java/memcached/MemcachedFilter.java
@@ -0,0 +1,91 @@
+package com.franciscosouza.java.memcached;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.InetSocketAddress;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+import net.spy.memcached.MemcachedClient;
+
+/**
+ * Servlet Filter implementation class MemcachedFilter
+ */
+public class MemcachedFilter implements Filter {
+
+ private MemcachedClient mmc;
+
+ static class MemcachedHttpServletResponseWrapper extends HttpServletResponseWrapper {
+
+ private StringWriter sw = new StringWriter();
+
+ public MemcachedHttpServletResponseWrapper(HttpServletResponse response) {
+ super(response);
+ }
+
+ public PrintWriter getWriter() throws IOException {
+ return new PrintWriter(sw);
+ }
+
+ public ServletOutputStream getOutputStream() throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ public String toString() {
+ return sw.toString();
+ }
+ }
+
+ /**
+ * Default constructor.
+ */
+ public MemcachedFilter() {
+ }
+
+ /**
+ * @see Filter#destroy()
+ */
+ public void destroy() {
+ }
+
+ /**
+ * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
+ */
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+ MemcachedHttpServletResponseWrapper wrapper = new MemcachedHttpServletResponseWrapper((HttpServletResponse) response);
+ chain.doFilter(request, wrapper);
+
+ HttpServletRequest inRequest = (HttpServletRequest) request;
+ HttpServletResponse inResponse = (HttpServletResponse) response;
+ PrintWriter out = inResponse.getWriter();
+
+ String content = wrapper.toString();
+ String key = inRequest.getRequestURI();
+
+ out.print(content);
+ mmc.set(key, 3600, content);
+ }
+
+ /**
+ * @see Filter#init(FilterConfig)
+ */
+ public void init(FilterConfig fConfig) throws ServletException {
+ try {
+ mmc = new MemcachedClient(new InetSocketAddress("localhost", 11211));
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new ServletException(e);
+ }
+ }
+
+}

0 comments on commit 0ec22a5

Please sign in to comment.