tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Manny Mondeo <manny...@yahoo.com>
Subject slow servlet filter for ByteArrayOutputStream response Wrapper
Date Tue, 01 Jun 2010 02:53:26 GMT
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 b=
rowser. The filter gets executed fast but the status bar in the browser doe=
s not complete and the page gets struck to about 25 seconds before the resp=
onse comes and gets rendered.

Apache 6.0.26, windows 7 OS, jdk 1.5

here is the filter code

=A0
public class InterimFilter implements Filter=20
{
=A0=A0=A0 public final static String NAV_START =3D "<!--START Nav-->";
=A0=A0=A0 =A0=A0=A0 public final static String NAV_END =3D "<!--END Nav-->"=
;
=A0=A0=A0 public final static String NAV_START_HIDE =3D "<!--hide";
=A0=A0=A0 public final static String NAV_END_HIDE =3D "hide-->";
=A0=A0=A0 public final static double millsToSecConvertFactor =3D 0.001;
=A0=A0=A0 private FilterConfig filterConfig;
=A0=A0=A0=20
public void init( FilterConfig config ) throws ServletException
{
=A0=A0=A0 System.out.println( "((((((((((- Initialised Interim Filter -))))=
))))))" );
}
public void destroy()
{=A0=A0=A0=20
filterConfig =3D null;
}
=A0=A0=A0=20
=A0public void doFilter( ServletRequest request, ServletResponse response, =
FilterChain chain ) =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 throw=
s IOException, ServletException
{=20
=A0=A0=A0 =A0 HttpServletResponse=A0=A0=A0 httpRes=A0=A0 =3D (HttpServletRe=
sponse)response;
=A0=A0=A0 =A0//PrintWriter out =3D httpRes.getWriter();
=A0=A0=A0 =A0GenericResponseWrapper wrapp=A0=A0=A0 =3D new GenericResponseW=
rapper( httpRes);
=A0=A0=A0 =A0long startTime =3D System.currentTimeMillis();
=A0=A0=A0 =A0chain.doFilter(request, wrapp);
=A0=A0=A0 =A0long endTime =3D System.currentTimeMillis();
=A0=A0=A0 =A0byte[] c=A0 =3D wrapp.getResponseBytes();
=A0=A0=A0 =A0String content =3D new String(c);
=A0=A0=A0=20
=A0=A0=A0 //actual filter work
=A0=A0=A0=A0=A0=A0=A0=A0 //from start to end of contents.
=A0=A0=A0=A0=A0=A0=A0=A0 int i =3D content.indexOf( NAV_START );
=A0=A0=A0=A0=A0=A0=A0=A0 int j =3D content.indexOf( NAV_END );
=A0=A0=A0=A0=A0=A0=A0=A0 if( ( i >=3D0 ) && ( j > i ) )
=A0=A0=A0=A0=A0=A0=A0=A0 {
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 String nav =3D content.sub=
string( i, j + NAV_END.length() );
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 StringBuffer buffer =3D ne=
w StringBuffer();
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 for( int k =3D nav.indexOf=
( NAV_START_HIDE ), l =3D nav.indexOf( NAV_END_HIDE ) + NAV_END_HIDE.length=
(), m =3D 0; ( k >=3D0 ) && ( l > k ); )
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 bu=
ffer.append( nav.substring( m, k ) );
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 m =
=3D l;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 k =
=3D nav.indexOf(=A0 NAV_START_HIDE, l );
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 //=
new
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if=
 (k =3D=3D -1)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0 buffer.append( nav.substring(l, nav.indexOf(NAV_END))=
);
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0 break;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 //=
end new
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 l =
=3D nav.indexOf(=A0 NAV_END_HIDE, k ) + NAV_END_HIDE.length();
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 nav=3D buffer.toString();
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 buffer =3D new StringBuffer()=
;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 buffer.append( content.sub=
string( 0, i ) );
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 buffer.append( nav );
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 buffer.append( content.sub=
string( j ) );
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=20
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 content =3D buffer.toStrin=
g();
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 buffer =3D null;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 nav =3D null;
=A0=A0=A0=A0=A0=A0=A0=A0 }
=A0=A0=A0=A0=A0=A0=A0=A0 //PrintWriter out =3D httpRes.getWriter();
=A0=A0=A0=A0=A0=A0=A0=A0 PrintWriter out =3D wrapp.getWriter();
=A0=A0=A0=A0=A0=A0=A0=A0 httpRes.setContentLength(content.toString().length=
());
=A0=A0=A0=A0=A0=A0=A0=A0 httpRes.setContentType( "text/html; charset=3DUTF-=
8" ) ;
=A0=A0=A0=A0=A0=A0=A0=A0 out.write(content);
=A0=A0=A0=A0=A0=A0=A0=A0 out.flush();
=A0=A0=A0=A0=A0=A0=A0=A0 out.close();=A0=A0=A0 =A0=A0=A0 =A0
=A0=A0=A0 =A0=A0=A0 =A0
=A0=A0=A0=A0 }
=A0=A0=A0 =A0
=A0public FilterConfig getFilterConfig()
=A0 {
=A0=A0=A0 return this.filterConfig;
=A0 }
=A0 public void setFilterConfig (FilterConfig filterConfig)
=A0 {
=A0=A0=A0 this.filterConfig =3D filterConfig;
=A0 }
=A0=A0=A0 =A0=20
=A0 public class GenericResponseWrapper extends HttpServletResponseWrapper=
=A0=A0=A0=20
{
=A0=A0=A0 =A0=A0=A0=A0 private PrintWriter writer;=A0=20
=A0=A0=A0 =A0=A0=A0=A0 private ServletOutputStream outputStream;
=A0=A0=A0 =A0=A0=A0=A0 private ByteArrayOutputStream buffer;=A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0
=A0=A0=A0 =A0=A0=A0=A0 public GenericResponseWrapper(HttpServletResponse re=
sponse)=A0=A0=A0=20
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =A0=A0=A0 {=A0=A0=A0=A0=A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 super(response);=A0=A0=A0=A0=A0=A0=
=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 buffer =3D new ByteArrayOutputStrea=
m();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 public byte[] getResponseBytes()
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 flushBuffer();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return buffer.toByteArray();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 public void flushBuffer()
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if (outputStream !=3D null)=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 try
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 outputStream.fl=
ush();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 catch (IOException e)
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 e.printStackTra=
ce();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if (writer !=3D null)
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 writer.flush();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }

=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 public ServletOutputStream getOutputStream()=
=A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if (outputStream =3D=3D null)
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0outputStream =3D new F=
ilterServletOutputStream(buffer);
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0 =A0=A0=A0 =
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return outputStream;
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 public byte[] getData()=A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return buffer.toByteArray();=A0=A0=
=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 public PrintWriter getWriter()=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {=A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if (writer =3D=3D null)=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 try=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 writer =3D new =
PrintWriter(new OutputStreamWriter(outputStream, this.getCharacterEncoding(=
)));
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 catch (UnsupportedEncodin=
gException e)=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 {
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 e.printStackTra=
ce();
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }=A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 return writer;
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 /*return new PrintWriter(getOutputS=
tream(), true);*/=A0=20
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=20
=A0=A0=A0 =A0=A0=A0 }
=A0=A0=A0 =A0=20
}



-----end code-----

I have to use the ByteArrayOutputStream=A0 to wrap the response.

pls let me know if u want to know anything else.
Any ideas / suggestions will be=A0 highly appreciated.

Regards,
Manny



=0A=0A=0A      
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message