users@glassfish.java.net

Problem when using Servlet filter in front of a web service

From: Legolas Woodland <legolas.w_at_gmail.com>
Date: Wed, 5 Dec 2007 15:23:21 +0430

Hi
Thank you for reading my post
Can some one pelase let me know what is wrong with this filter?

[code]
 public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        boolean state = false;
        try {
            System.out.println(request.getInputStream());
            byte[] b = new byte[req.getInputStream().available()];
            req.getInputStream().read(b, 0, req.getInputStream().available()
- 1);
            System.out.println(req.getInputStream().available());
           String str = new String(b, request.getCharacterEncoding());
            System.out.println(str);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {

        }
        chain.doFilter(request, response);
[/code]

the above code return an exception like following one in the server:

[code]
StandardWrapperValve[echo]: PWC1406: Servlet.service() for servlet echo
threw exception
java.lang.ArrayIndexOutOfBoundsException
        at java.lang.System.arraycopy(Native Method)
        at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java
:404)
        at org.apache.coyote.tomcat5.InputBuffer.read(InputBuffer.java:341)
        at org.apache.coyote.tomcat5.CoyoteInputStream.read(
CoyoteInputStream.java:247)
        at myFilter.doFilter(myFilter.java:120)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
ApplicationFilterChain.java:230)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
ApplicationFilterChain.java:198)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(
MonitorFilter.java:390)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
ApplicationFilterChain.java:230)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(
ApplicationFilterChain.java:198)
        at org.apache.catalina.core.StandardWrapperValve.invoke(
StandardWrapperValve.java:288)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(
StandardContextValve.java:271)
        at org.apache.catalina.core.StandardContextValve.invoke(
StandardContextValve.java:202)
        at org.apache.catalina.core.StandardPipeline.doInvoke(
StandardPipeline.java:632)
        at org.apache.catalina.core.StandardPipeline.doInvoke(
StandardPipeline.java:577)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
        at org.apache.catalina.core.StandardHostValve.invoke(
StandardHostValve.java:206)
        at org.apache.catalina.core.StandardPipeline.doInvoke(
StandardPipeline.java:632)
        at org.apache.catalina.core.StandardPipeline.doInvoke(
StandardPipeline.java:577)
        at org.apache.catalina.core.StandardPipeline.invoke(
StandardPipeline.java:571)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java
:1080)
        at org.apache.catalina.core.StandardEngineValve.invoke(
StandardEngineValve.java:150)
        at org.apache.catalina.core.StandardPipeline.doInvoke(
StandardPipeline.java:632)
        at org.apache.catalina.core.StandardPipeline.doInvoke(
StandardPipeline.java:577)
        at org.apache.catalina.core.StandardPipeline.invoke(
StandardPipeline.java:571)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java
:1080)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(
CoyoteAdapter.java:270)
        at
com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(
DefaultProcessorTask.java:637)
        at
com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(
DefaultProcessorTask.java:568)
        at
com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(
DefaultProcessorTask.java:813)
        at
com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask
(DefaultReadTask.java:339)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(
DefaultReadTask.java:261)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(
DefaultReadTask.java:212)
        at com.sun.enterprise.web.connector.grizzly.TaskBase.run(
TaskBase.java:265)
        at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(
SSLWorkerThread.java:106)


[code]



my client code is like:

in the client side which is a web service client (filter works on any
request come to my web applicaton)
[code]

        at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(
StreamSOAPCodec.java:130)
        at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(
SOAPBindingCodec.java:294)
        at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(
HttpTransportPipe.java:173)
        at com.sun.xml.xwss.XWSSClientPipe.process(XWSSClientPipe.java:140)
        at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(
PipeAdapter.java:115)
        at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:595)
        at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:554)
        at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:539)
        at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:436)
        at com.sun.xml.ws.client.Stub.process(Stub.java:248)
        at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:134)
        at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(
SyncMethodHandler.java:244)
        at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(
SyncMethodHandler.java:224)
        at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:117)
        at $Proxy29.echo(Unknown Source)
        at caller.caller.main(caller.java:36)

[code]