jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alvin Antony" <Alvin.Ant...@consol.de>
Subject AW: Problem with ObjectInputStream and WebRequest.setUserData & .getUserData()
Date Fri, 06 May 2005 10:32:27 GMT
Hello Vincent,

Well I also tried the same test of yours with request.getInputstream and
it just works!! So the problem is when I use an ObjectInputStream. You
could find a sample testcase here which fail on test.


package org.apache.cactus.sample.servlet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;


/**
 * @author antony
 *
 * Test case for testing WebRequest.setUserData with an
ObjectInputStream
 */
public class TestObjectInputStreamServlet extends ServletTestCase {

    
    public TestObjectInputStreamServlet ( String name )
    {
        super(name);
    }
    
    /**
     * Prepare the request object. Add an ObjectInputStream into request
object.
     * 
     * @param theRequest
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void beginSendUserData(WebRequest theRequest) throws
IOException, ClassNotFoundException
    {
        Map projectInfo = new HashMap();
        String projectName = "Cactus";
        String organisation = "Apache.org";
        projectInfo.put("projectname", projectName);
        projectInfo.put("organisation", organisation);
        
        ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();        
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
        objectOutputStream.writeObject( projectInfo );
        objectOutputStream.flush();
        byteArrayOutputStream.close();
        
        ObjectInputStream objectInputStream = new ObjectInputStream( new
ByteArrayInputStream( byteArrayOutputStream.toByteArray() ) ) ;

        
        theRequest.setUserData( objectInputStream );
        theRequest.setContentType( "application/binary" );
    }

    /**
     * Verify that we can send arbitrary binary data (ObjectStream) in
the request body.
     * 
     * @exception Exception on test failure
     */
    public void testSendUserData() throws Exception
    {
        InputStream in = request.getInputStream();
        ObjectInputStream  objectInputStream = new ObjectInputStream (
in );        
        Map projectInfo = (Map) objectInputStream.readObject();
        assertNotNull( projectInfo );
        assertEquals( projectInfo.get( "projectname"), "Cactus" );
        assertEquals( projectInfo.get( "organisation"), "Apache.org" );

    }
    
}

Thanks,

Alvin


-----Urspr√ľngliche Nachricht-----
Von: Vincent Massol [mailto:vmassol@pivolis.com] 
Gesendet: Freitag, 6. Mai 2005 11:39
An: 'Cactus Users List'
Betreff: RE: Problem with ObjectInputStream and WebRequest.setUserData &
.getUserData() 

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
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-user-help@jakarta.apache.org




Mime
View raw message