/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. ======= WrapItUp.java  ======== 

2. import java.io.*; 

3. import javax.servlet.*; 

4. import javax.servlet.http.*; 

5.  

6. public final class WrapItUp 

7.    implements Filter 

8.

9. private FilterConfig cfg = null

10.  

11. public void doFilter 

12. ( ServletRequest req, ServletResponse resp, 

13.        FilterChain chain ) 

14.           throws IOException, ServletException 

15.

16.   PrintWriter out = resp.getWriter(); 

17.  

18.   if ( req.getParameter("paruno")==null

19.      req.setAttribute("paruno","splendid"); 

20.  

21.   ResponseImpersonator fakeresp = new ResponseImpersonator 

22.    ( (HttpServletResponse) resp  ); 

23.  

24.   chain.doFilter( req, fakeresp ); 

25.  

26.   String doc = fakeresp.toString(); 

27.  

28.   BufferedReader br = new BufferedReader 

29.        ( new StringReader(doc) ); 

30.  

31.   String ln = br.readLine(); 

32.  

33.   while ( ln != null

34.   { 

35.     if ( ln.indexOf("<body")!= -1 

36.         ||  ln.indexOf("<BODY")!= -1  ) 

37.     { 

38.        out.println( ln ); 

39.  

40.        cfg.getServletContext().log 

41.         ( cfg.getFilterName() +  " : Doing my thing" ); 

42.  

43.        out.println( "<center><TABLE border=5 " ); 

44.        out.println( "cellpadding=20  width=80% height=80%" ); 

45.        out.println( " bgcolor=#"  + kolor + " >"  ); 

46.        out.println( 

47.        "<tr align=center valign=center><td bgcolor=#FFCC99>" ); 

48.  

49.        out.println( "<h3>Filtered by : <b><i>"  ); 

50.        out.println( cfg.getFilterName() ); 

51.        out.println( "</i></b></h3></td></tr>" ); 

52.        out.println("<tr align=center valign=center><td>"); 

53.        out.println("<h2>"); 

54.     } 

55.     else 

56.     if ( ln.indexOf("</body") != -1 

57.         ||  ln.indexOf("</BODY") != -1  ) 

58.     { 

59.        out.println( "</h2>" ); 

60.        out.println( "</td></tr>" ); 

61.        out.println( "<tr align=center valign=center>" ); 

62.        out.println( "<td bgcolor=#FFCC99>" ); 

63.        out.println( "Sent to : " + req.getRemoteAddr() + "<br>" ); 

64.        out.println( 

65.         java.text.DateFormat.getDateTimeInstance().format 

66.          ( new java.util.Date() ) ); 

67.        out.println( "<br>©SEMM 2004<br>" ); 

68.        out.println( "</td></tr></table></center>" ); 

69.        out.println( ln ); 

70.     } 

71.     else 

72.        out.println( ln ); 

73.  

74.     ln = br.readLine(); 

75.   }//end while 

76.

77.  

78. private String kolor; 

79.  

80. public void init( FilterConfig f ) 

81.

82.  cfg = f ; 

83.  kolor = cfg.getInitParameter("pkolor"); 

84.  if ( kolor==null ) kolor = "FFFFFF"

85.

86.  

87. public void destroy() { cfg=null; } 

88.

89. //==================== 

90.  

91. class ResponseImpersonator 

92.   extends HttpServletResponseWrapper 

93.

94. private StringWriter sw = null

95.  

96. public PrintWriter getWriter() 

97.     throws java.io.IOException 

98.

99.   if ( sw != null

100.   { 

101.     throw new IllegalStateException 

102.       ("repeated getWriter() call"); 

103.   } 

104.   if ( bos != null

105.   { 

106.     throw new IllegalStateException 

107.       ("getOutputStream() was called first"); 

108.   } 

109.   sw = new StringWriter(2048); 

110.   return new PrintWriter(sw); 

111.

112.  

113. private ByteArrayOutputStream bos ; 

114.  

115. public ServletOutputStream getOutputStream() 

116.     throws java.io.IOException 

117.

118.   if ( sw != null

119.   { 

120.     throw new IllegalStateException 

121.       ("getWriter() was called first"); 

122.   } 

123.   if ( bos != null

124.   { 

125.     throw new IllegalStateException 

126.       ("repeated getOutputStream() call"); 

127.   } 

128.   bos = new ByteArrayOutputStream(16*1024); 

129.  

130.   return new ServletOutputStream() 

131.   { 

132.     public void write( int i ) 

133.               throws java.io.IOException 

134.     {   bos.write(i);  } 

135.   }; 

136.

137.  

138. ResponseImpersonator( HttpServletResponse r ) 

139. {  super(r);  } 

140.  

141. public String toString() 

142.

143.   if ( sw != null)  return sw.toString(); 

144.   if ( bos != null)  return bos.toString(); 

145.   return null

146.

147.

148.  

149. ======= ServUno.java  ======== 

150. import javax.servlet.http.*; 

151. import javax.servlet.*; 

152. import java.io.*; 

153.  

154. public class ServUno 

155.   extends HttpServlet 

156.

157. public void 

158. doGet ( HttpServletRequest req, HttpServletResponse resp ) 

159.   throws ServletException, IOException 

160.

161.   PrintWriter out = resp.getWriter(); 

162.  

163.   String par = req.getParameter("paruno"); 

164.   if  (par==null)  par =(String)req.getAttribute("paruno"); 

165.   if  (par==null)  par = "ordinary"

166.  

167.   out.println ( "<HTML><TITLE>Smurflut</TITLE></HEAD>" ) ; 

168.   out.println ( "<BODY>" ) ; 

169.   out.println ( "This HTML has been generated " ) ; 

170.   out.println ( "by this " + par + " servlet " ) ; 

171.  

172.   out.println ( "</BODY></HTML>" ) ; 

173.   out.flush (); 

174.   out.close (); 

175.

176.

177.  

178. ======= sim.jsp  ======== 

179. <HTML><TITLE>JahvahSurfer Peige</TITLE></HEAD

180. <body

181. <h3> 

182. <%= new java.util.Date() %> 

183. </h3> 

184. </body

185.  

186. ======= web.xml  ======== 

187. <?xml version = "1.0" encoding = "ISO-8859-1"?> 

188. <!DOCTYPE web-app PUBLIC 

189.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 

190.   "http://java.sun.com/dtd/web-app_2_3.dtd"

191.  

192. <web-app> 

193.  

194. <filter> 

195.   <filter-name>wrap</filter-name> 

196.   <filter-class>WrapItUp</filter-class

197.   <init-param> 

198.     <param-name>pkolor</param-name> 

199.     <param-value>66FFFF</param-value> 

200.   </init-param> 

201. </filter> 

202.  

203. <filter-mapping> 

204.     <filter-name>wrap</filter-name> 

205.     <url-pattern>*.sx</url-pattern> 

206.   </filter-mapping> 

207.  

208. <servlet> 

209.   <servlet-name>surf</servlet-name> 

210.   <servlet-class>ServUno</servlet-class

211. </servlet> 

212.  

213. <servlet> 

214.   <servlet-name>jsim</servlet-name> 

215.   <jsp-file>/sim.jsp</jsp-file> 

216. </servlet> 

217.  

218. <servlet-mapping> 

219.   <servlet-name>surf</servlet-name> 

220.   <url-pattern>/filt.sx</url-pattern> 

221. </servlet-mapping> 

222.  

223. <servlet-mapping> 

224.   <servlet-name>jsim</servlet-name> 

225.   <url-pattern>/fjsp.sx</url-pattern> 

226. </servlet-mapping> 

227.  

228. </web-app> 

229. ============ 

230.   <url-pattern>/somezurflutz/*</url-pattern> 

231.  

232.   <servlet-name>smurfalot</servlet-name> 

233.  

234.