You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Alvin Antony <Al...@consol.de> on 2005/05/06 11:04:46 UTC

Problem with ObjectInputStream and WebRequest.setUserData & .getUserData()

Dear Friends,
 
I am new to the mailing list and so to Cactus!! Please pardon me if it
is a silly Q.
 
I am writing a test suit for an Applet& Servlet client server
Application. 
The Applet sends the information to the Servlet through an Object
stream, but my test case for this throws an EOFException.
 
 
public void beginService ( WebRequest webRequest ) throws
ClassNotFoundException, IOException 
    {          
        ProdFinderIntegratonTestRepository prodFinderIntegrationTestRep
= new ProdFinderIntegratonTestRepository ();
        Map input;
        try {
            input =
prodFinderIntegrationTestRep.getRequestToCreateMainCategory();
                    assertNotNull( input );
                    
                    ByteArrayOutputStream bos = new
ByteArrayOutputStream();        
                    ObjectOutputStream oos = new ObjectOutputStream( bos
);
                    oos.writeObject( input );
                    oos.flush();     
                    bos.close();
                    ByteArrayInputStream byteArrayInputStream  =  new
ByteArrayInputStream( bos.toByteArray() );        
                    objectInputStream = new ObjectInputStream(
byteArrayInputStream );        
                    webRequest.setContentType("application/binary");
                    webRequest.setUserData( objectInputStream ) ;       
        } catch (ObjectNotFoundException e) {            
            e.printStackTrace();
        } catch (PFInternalError e) {           
            e.printStackTrace();
        
        } 
    }
 
    /**
     * 
     * Class under test for void service(HttpServletRequest,
HttpServletResponse)
     */
    public void testService() throws ServletException, IOException {

        InputStream is =  request.getInputStream();        
        ObjectInputStream oInput = new ObjectInputStream(is);        
        MessageReceiver messageReceiver = new MessageReceiver();
        messageReceiver.init( config );
        messageReceiver.service(request, response );
        assertNotNull( response);        
    }
 
   [cactus] Testcase:
testService(com.siemens.productfinder.communication.TestMe
ssageReceiver): Caused an ERROR
   [cactus] null
   [cactus] java.io.EOFException
   [cactus]     at
java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInp
utStream.java:2165)
   [cactus]     at
java.io.ObjectInputStream$BlockDataInputStream.readShort(Obje
ctInputStream.java:2631)
   [cactus]     at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.
java:734)
   [cactus]     at
java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
   [cactus]     at
com.siemens.productfinder.communication.TestMessageReceiver.t
estService(Unknown Source)...
 
 
Any help would be great
 
Thanks in advance,
 
 
Alvin
 

RE: Problem with ObjectInputStream and WebRequest.setUserData & .getUserData()

Posted by Vincent Massol <vm...@pivolis.com>.
Hi Alvin,

Personally I've never tried reading data with an ObjectInputStream inside
Cactus. The test we have for the setuserData() feature is the following:

    public void beginSendUserData(WebRequest theRequest)
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(
            "<data>some data to send in the body</data>".getBytes());

        theRequest.setUserData(bais);
        theRequest.setContentType("text/xml");
    }

    /**
     * Verify that we can send arbitrary data in the request body.
     * 
     * @exception Exception on test failure
     */
    public void testSendUserData() throws Exception
    {
        String buffer;
        StringBuffer body = new StringBuffer();

        BufferedReader reader = request.getReader();

        while ((buffer = reader.readLine()) != null)
        {
            body.append(buffer);
        }

        assertEquals("<data>some data to send in the body</data>", 
            body.toString());
        assertEquals("text/xml", request.getContentType());
    }

Hmm... Maybe the problem is with the "request.getInputStream();". In the
test above it's doing a "request.getReader()".

It would be nice to have another test with an input stream to try to
reproduce your problem. If you could come up with a simple test as above
that would reproduce the problem, I could add it to the Cactus test suite
and we could tackle the problem (if any) and fix it.

Thanks
-Vincent

> -----Original Message-----
> From: Alvin Antony [mailto:Alvin.Antony@consol.de]
> Sent: vendredi 6 mai 2005 11:05
> To: cactus-user@jakarta.apache.org
> Subject: Problem with ObjectInputStream and WebRequest.setUserData &
> .getUserData()
> 
> Dear Friends,
> 
> I am new to the mailing list and so to Cactus!! Please pardon me if it
> is a silly Q.
> 
> I am writing a test suit for an Applet& Servlet client server
> Application.
> The Applet sends the information to the Servlet through an Object
> stream, but my test case for this throws an EOFException.
> 
> 
> public void beginService ( WebRequest webRequest ) throws
> ClassNotFoundException, IOException
>     {
>         ProdFinderIntegratonTestRepository prodFinderIntegrationTestRep
> = new ProdFinderIntegratonTestRepository ();
>         Map input;
>         try {
>             input =
> prodFinderIntegrationTestRep.getRequestToCreateMainCategory();
>                     assertNotNull( input );
> 
>                     ByteArrayOutputStream bos = new
> ByteArrayOutputStream();
>                     ObjectOutputStream oos = new ObjectOutputStream( bos
> );
>                     oos.writeObject( input );
>                     oos.flush();
>                     bos.close();
>                     ByteArrayInputStream byteArrayInputStream  =  new
> ByteArrayInputStream( bos.toByteArray() );
>                     objectInputStream = new ObjectInputStream(
> byteArrayInputStream );
>                     webRequest.setContentType("application/binary");
>                     webRequest.setUserData( objectInputStream ) ;
>         } catch (ObjectNotFoundException e) {
>             e.printStackTrace();
>         } catch (PFInternalError e) {
>             e.printStackTrace();
> 
>         }
>     }
> 
>     /**
>      *
>      * Class under test for void service(HttpServletRequest,
> HttpServletResponse)
>      */
>     public void testService() throws ServletException, IOException {
> 
>         InputStream is =  request.getInputStream();
>         ObjectInputStream oInput = new ObjectInputStream(is);
>         MessageReceiver messageReceiver = new MessageReceiver();
>         messageReceiver.init( config );
>         messageReceiver.service(request, response );
>         assertNotNull( response);
>     }
> 
>    [cactus] Testcase:
> testService(com.siemens.productfinder.communication.TestMe
> ssageReceiver): Caused an ERROR
>    [cactus] null
>    [cactus] java.io.EOFException
>    [cactus]     at
> java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInp
> utStream.java:2165)
>    [cactus]     at
> java.io.ObjectInputStream$BlockDataInputStream.readShort(Obje
> ctInputStream.java:2631)
>    [cactus]     at
> java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.
> java:734)
>    [cactus]     at
> java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
>    [cactus]     at
> com.siemens.productfinder.communication.TestMessageReceiver.t
> estService(Unknown Source)...
> 
> 
> Any help would be great
> 
> Thanks in advance,
> 
> 
> Alvin
>