1 20 package org.apache.maven.cactus.sample; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import javax.servlet.Filter ; 26 import javax.servlet.FilterChain ; 27 import javax.servlet.FilterConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.ServletResponse ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.apache.maven.cactus.sample.util.GenericResponseWrapper; 34 35 42 public class SampleFilter implements Filter 43 { 44 48 private FilterConfig config; 49 50 58 public void init(FilterConfig theConfig) throws ServletException 59 { 60 this.config = theConfig; 61 } 62 63 75 public void doFilter(ServletRequest theRequest, 76 ServletResponse theResponse, FilterChain theChain) throws IOException , 77 ServletException 78 { 79 OutputStream out = theResponse.getOutputStream(); 80 81 addHeader(out); 82 83 GenericResponseWrapper wrapper = 88 new GenericResponseWrapper((HttpServletResponse ) theResponse); 89 90 theChain.doFilter(theRequest, wrapper); 91 92 out.write(wrapper.getData()); 93 addFooter(out); 94 out.close(); 95 } 96 97 106 protected void addHeader(OutputStream theOutputStream) throws IOException 107 { 108 String header = this.config.getInitParameter("header"); 109 110 if (header != null) 111 { 112 theOutputStream.write(header.getBytes()); 113 } 114 } 115 116 125 protected void addFooter(OutputStream theOutputStream) throws IOException 126 { 127 String footer = this.config.getInitParameter("footer"); 128 129 if (footer != null) 130 { 131 theOutputStream.write(footer.getBytes()); 132 } 133 } 134 135 139 public void destroy() 140 { 141 } 142 } 143 | Popular Tags |