Hi , I have written a filter that strips the response of some html tags. The filter is working and executing around 30milliseconds.
The problem is that the request takes around 30 seconds to load this in a browser. The filter gets executed fast but the status bar in the browser does not complete and the page gets struck to about 25 seconds before the response comes and gets rendered. Apache 6.0.26, windows 7 OS, jdk 1.5 here is the filter code public class InterimFilter implements Filter { public final static String NAV_START = "<!--START Nav-->"; public final static String NAV_END = "<!--END Nav-->"; public final static String NAV_START_HIDE = "<!--hide"; public final static String NAV_END_HIDE = "hide-->"; public final static double millsToSecConvertFactor = 0.001; private FilterConfig filterConfig; public void init( FilterConfig config ) throws ServletException { System.out.println( "((((((((((- Initialised Interim Filter -))))))))))" ); } public void destroy() { filterConfig = null; } public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException { HttpServletResponse httpRes = (HttpServletResponse)response; //PrintWriter out = httpRes.getWriter(); GenericResponseWrapper wrapp = new GenericResponseWrapper( httpRes); long startTime = System.currentTimeMillis(); chain.doFilter(request, wrapp); long endTime = System.currentTimeMillis(); byte[] c = wrapp.getResponseBytes(); String content = new String(c); //actual filter work //from start to end of contents. int i = content.indexOf( NAV_START ); int j = content.indexOf( NAV_END ); if( ( i >=0 ) && ( j > i ) ) { String nav = content.substring( i, j + NAV_END.length() ); StringBuffer buffer = new StringBuffer(); for( int k = nav.indexOf( NAV_START_HIDE ), l = nav.indexOf( NAV_END_HIDE ) + NAV_END_HIDE.length(), m = 0; ( k >=0 ) && ( l > k ); ) { buffer.append( nav.substring( m, k ) ); m = l; k = nav.indexOf( NAV_START_HIDE, l ); //new if (k == -1) { buffer.append( nav.substring(l, nav.indexOf(NAV_END))); break; } //end new l = nav.indexOf( NAV_END_HIDE, k ) + NAV_END_HIDE.length(); } nav= buffer.toString(); buffer = new StringBuffer(); buffer.append( content.substring( 0, i ) ); buffer.append( nav ); buffer.append( content.substring( j ) ); content = buffer.toString(); buffer = null; nav = null; } //PrintWriter out = httpRes.getWriter(); PrintWriter out = wrapp.getWriter(); httpRes.setContentLength(content.toString().length()); httpRes.setContentType( "text/html; charset=UTF-8" ) ; out.write(content); out.flush(); out.close(); } public FilterConfig getFilterConfig() { return this.filterConfig; } public void setFilterConfig (FilterConfig filterConfig) { this.filterConfig = filterConfig; } public class GenericResponseWrapper extends HttpServletResponseWrapper { private PrintWriter writer; private ServletOutputStream outputStream; private ByteArrayOutputStream buffer; public GenericResponseWrapper(HttpServletResponse response) { super(response); buffer = new ByteArrayOutputStream(); } public byte[] getResponseBytes() { flushBuffer(); return buffer.toByteArray(); } public void flushBuffer() { if (outputStream != null) { try { outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } } if (writer != null) { writer.flush(); } } public ServletOutputStream getOutputStream() { if (outputStream == null) { outputStream = new FilterServletOutputStream(buffer); } return outputStream; } public byte[] getData() { return buffer.toByteArray(); } public PrintWriter getWriter() { if (writer == null) { try { writer = new PrintWriter(new OutputStreamWriter(outputStream, this.getCharacterEncoding())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return writer; /*return new PrintWriter(getOutputStream(), true);*/ } } } -----end code----- I have to use the ByteArrayOutputStream to wrap the response. pls let me know if u want to know anything else. Any ideas / suggestions will be highly appreciated. Regards, Manny