You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Marcos Oliva <mo...@tradedeck.com> on 2001/01/03 04:10:15 UTC

Xerces and XML Serialization..

Hello guys,

I am a newbie with Xerces and I have been trying to make this to work,,,

I have an Applet that is sending a XML dom to a servlet but for the love of
G...!!
I have not been able to make the program to work..

[ snip of applet side ]
 HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
 conn.setDoInput(true);
 conn.setDoOutput(true);
 // Don't used a cached version of URL connection.
 conn.setUseCaches (false);
 // Specify the content type that we will send binary data
 conn.setRequestProperty("Content-Type", "application/octet-stream");
 conn.setRequestProperty("method","POST");
 ObjectOutputStream out =  new ObjectOutputStream(conn.getOutputStream());
 XMLSerializer ser = 	new XMLSerializer(pwt , new OutputFormat("xml",
"UTF-8", false));
ser.asDOMSerializer();
ser.serialize(inxmlcommand);
out.close();
[ -- end of applet snipte ]

[Servlet snip it]
	    DOMParser parser =  new DOMParser();

	    //InputSource inSrc = new InputSource(req.getInputStream());

	    parser.parse(new InputSource(req.getInputStream()));
	    Document docOut = parser.getDocument();

it never passed the parser method..

If you have another sample of XML serialized methods, please send them this
way.

marcos oliva


RE: Reading from open sockets [Was: Xerces and XML Serialization..]

Posted by Marcos Oliva <mo...@tradedeck.com>.
to Klaus and Andy,, thanks a lot for your comments


marcos oliva
TradeDeck.com 

-----Original Message-----
From: Andy Clark [mailto:andyc@apache.org]
Sent: Thursday, January 04, 2001 11:06 PM
To: xerces-j-dev@xml.apache.org
Subject: Re: Reading from open sockets [Was: Xerces and XML
Serialization..]


Klaus Kiehne wrote:
> The request to a servlet is send via a socket and the same socket is
> used, to send back the response, so the socket can't be closed after
> receiving the request. The method ServeltRequest.getInputStream()

As a reminder, I wrote a general purpose solution to this problem
as a sample for Xerces2. Since the socket stream has to remain
open, I use the "wrapper" technique on both ends of the pipe in
order to "put a stream inside a stream", if that makes sense.

By wrapping the output stream (and having the appropriate input
stream wrapper on the other side), you can make an XML file appear 
to end. So the parser sees an end to the document and the socket 
stream remains open. This technique actually works with any data
of arbitrary length.

If you'd like to see the source code for the sample, check out
the samples/socket directory of the Xerces 2.0.0 (alpha) release.
Or you can extract it directly from CVS with the following
command:

  cvs checkout -d socket -r xerces_j_2 xml-xerces/java/samples/socket

-- 
Andy Clark * IBM, TRL - Japan * andyc@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org

Re: Reading from open sockets [Was: Xerces and XML Serialization..]

Posted by Andy Clark <an...@apache.org>.
Klaus Kiehne wrote:
> The request to a servlet is send via a socket and the same socket is
> used, to send back the response, so the socket can't be closed after
> receiving the request. The method ServeltRequest.getInputStream()

As a reminder, I wrote a general purpose solution to this problem
as a sample for Xerces2. Since the socket stream has to remain
open, I use the "wrapper" technique on both ends of the pipe in
order to "put a stream inside a stream", if that makes sense.

By wrapping the output stream (and having the appropriate input
stream wrapper on the other side), you can make an XML file appear 
to end. So the parser sees an end to the document and the socket 
stream remains open. This technique actually works with any data
of arbitrary length.

If you'd like to see the source code for the sample, check out
the samples/socket directory of the Xerces 2.0.0 (alpha) release.
Or you can extract it directly from CVS with the following
command:

  cvs checkout -d socket -r xerces_j_2 xml-xerces/java/samples/socket

-- 
Andy Clark * IBM, TRL - Japan * andyc@apache.org

Reading from open sockets [Was: Xerces and XML Serialization..]

Posted by Klaus Kiehne <ki...@struktur.de>.
Hello Marcos,

this seems to be a problem with the SocketInputStream you get from
  req.getInputStream()
in your servlet. 

The request to a servlet is send via a socket and the same socket is 
used, to send back the response, so the socket can't be closed after 
receiving the request. The method ServeltRequest.getInputStream() 
answers with a SocketInputStream or some wrapping InputStream around a
SocketInputStream. Unfortunately the method SocketInputStream.read(...)
will always answer a value of zero or above until the socket is closed
(since the stream doesn't know, whether the sender will send some more
data later). 

The method below tries to read data from the InputStream until reaching the
end. The end is detected by a result of -1 in the line 
  result = fInputStream.read(data, offset, capacity);
but this reult will never be given. :-(

To fix your problem you have to use the contentLength given by the request
(Method ServletRequest.getContentLength() ). The easiest way will be to 
read all data from the inputStream into an byte array and use an internal 
ByteArrayInputStream for the InputSource. Of course this consumes some
additional memory for the byteArray. 

Code would look like: 

	java.io.InputStream requestIn = aRequest.getInputStream ();
	int contentLength = aRequest.getContentLength();
	byte[] buffer     = new byte[contentLength];
	int    offset        = 0;
	while (offset < contentLength) {
		offset += requestIn.read (buffer, offset, contentLength - offset);
	}
	requestIn = new java.io.ByteArrayInputStream(buffer);

If you want to avoid the allocation of the byte array you can create your own 
Subclass of InputStream which is initialized with the requests input stream and
content length and answers only the contentLength number of bytes. (If you do 
this, please post the code to the mailing list ;-)

HTH
Klaus


Marcos Oliva wrote:
> 
> Some more of my findings on these issue,,
> 
> on this method   "parser.parse(inSrc) "
> 
> I traced it to the package,, org.apache.xerces.utils.ChunkyByteArray.java
> on a private mehod called fill():
> 
> [Code of fill()]
>  private void fill() throws IOException {
>         int bufnum = fLength >> CHUNK_SHIFT;
>         byte[] data = new byte[CHUNK_SIZE];
>         fData[bufnum] = data;
>         int offset = 0;
>         int capacity = CHUNK_SIZE;
>         int result = 0;
>         do {
>             result = fInputStream.read(data, offset, capacity);
>             if (result == -1) {
>                 data[offset] = (byte)0xff;
>                 fInputStream.close();
>                 fInputStream = null;
>                 break;
>             }
>             if (result > 0) {
>                 fLength += result;
>                 offset += result;
>                 capacity -= result;
>             }
>         } while (capacity > 0);
>     }
> 
> [ends here]
> 
> It dies once it tries to do for the second time the following line
> result = fInputStream.read(data, offset, capacity);

-- 
___   ________________________________________________________________
                               _____  
        Klaus Kiehne          /     /|  Chief Software Architect        
___     Dipl.-Informatiker   /____ / |  mailto:kiehne@struktur.de       
        struktur AG         |     |  |  tel:   +49. 711. 89 66 56-61    
___     Junghansstrasse 5   |     | /   fax:   +49. 711. 89 66 56-10    
___     D-70469 Stuttgart   |_____|/    http:  www.struktur.de          
___   ________________________________________________________________


RE: Xerces and XML Serialization..

Posted by Dream Catcher <is...@leonis.nus.edu.sg>.
Hello, I found out I encoutered the same problem with Marcos...I read the
mails and remember there is one person who pointed out how to overcome the
problem. It is about setting getInputstream features.. but I coudldn't
find the specified mail, can somebody mail me if you still keep it? Or
Marcos, have you solvoed your problem somehow? Could you somehow
enlighten me? Thanks...

Best Regards
Wang Yue

    *------------------------------*
      Attitude makes the difference
    *------------------------------*

On Wed, 3 Jan 2001, Marcos Oliva wrote:

> 
> Some more of my findings on these issue,, 
> 
> on this method   "parser.parse(inSrc) "
> 
> I traced it to the package,, org.apache.xerces.utils.ChunkyByteArray.java
> on a private mehod called fill():
> 
> [Code of fill()]
>  private void fill() throws IOException {
>         int bufnum = fLength >> CHUNK_SHIFT;
>         byte[] data = new byte[CHUNK_SIZE];
>         fData[bufnum] = data;
>         int offset = 0;
>         int capacity = CHUNK_SIZE;
>         int result = 0;
>         do {
>             result = fInputStream.read(data, offset, capacity);
>             if (result == -1) {
>                 data[offset] = (byte)0xff;
>                 fInputStream.close();
>                 fInputStream = null;
>                 break;
>             }
>             if (result > 0) {
>                 fLength += result;
>                 offset += result;
>                 capacity -= result;
>             }
>         } while (capacity > 0);
>     }
> 
> [ends here]
> 
> It dies once it tries to do for the second time the following line 
> result = fInputStream.read(data, offset, capacity);
> 
> 
> 
> 
> -----Original Message-----
> From: Marcos Oliva [mailto:moliva@tradedeck.com]
> Sent: Wednesday, January 03, 2001 12:54 PM
> To: xerces-j-dev@xml.apache.org
> Subject: RE: Xerces and XML Serialization..
> 
> 
> 
> HI Scott, 
> I took that line
> conn.setRequestProperty("method","POST");
> from the code but still ,, when it gets to the servlet
> it just dies, :
> 
> <Servlet snipet>
> 
>   InputSource inSrc = new InputSource(req.getInputStream());
>   parser.parse(inSrc); 
>   Document docOut = parser.getDocument();
> 
> <Servlet snipet>
> 
> athe inSrc var holds the InputStream of the req method,, but 
> then when I passed it to the parser.parser method then it dies,, noo error
> an nothing, I am using Visual Cafe for debugging this thing.
> 
> Any other suggestions
> 
> 
> marcos oliva
> 
> 
> -----Original Message-----
> From: Scott Carter [mailto:scarter@dotsconnect.com]
> Sent: Wednesday, January 03, 2001 5:54 AM
> To: xerces-j-dev@xml.apache.org
> Subject: RE: Xerces and XML Serialization..
> 
> 
> Hi Marcos,
> 
> I'm doing the same thing, but not with an Applet.  Try removing the
> following line and see what you get:
> 
> conn.setRequestProperty("method","POST");
> 
> 
> 
> -----Original Message-----
> From: Marcos Oliva [mailto:moliva@tradedeck.com]
> Sent: Tuesday, January 02, 2001 10:10 PM
> To: xerces-j-dev@xml.apache.org
> Subject: Xerces and XML Serialization..
> 
> 
> Hello guys,
> 
> I am a newbie with Xerces and I have been trying to make this to work,,,
> 
> I have an Applet that is sending a XML dom to a servlet but for the love of
> G...!!
> I have not been able to make the program to work..
> 
> [ snip of applet side ]
>  HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
>  conn.setDoInput(true);
>  conn.setDoOutput(true);
>  // Don't used a cached version of URL connection.
>  conn.setUseCaches (false);
>  // Specify the content type that we will send binary data
>  conn.setRequestProperty("Content-Type", "application/octet-stream");
>  conn.setRequestProperty("method","POST");
>  ObjectOutputStream out =  new ObjectOutputStream(conn.getOutputStream());
>  XMLSerializer ser = 	new XMLSerializer(pwt , new OutputFormat("xml",
> "UTF-8", false));
> ser.asDOMSerializer();
> ser.serialize(inxmlcommand);
> out.close();
> [ -- end of applet snipte ]
> 
> [Servlet snip it]
> 	    DOMParser parser =  new DOMParser();
> 
> 	    //InputSource inSrc = new InputSource(req.getInputStream());
> 
> 	    parser.parse(new InputSource(req.getInputStream()));
> 	    Document docOut = parser.getDocument();
> 
> it never passed the parser method..
> 
> If you have another sample of XML serialized methods, please send them this
> way.
> 
> marcos oliva
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
> 


RE: Xerces and XML Serialization..

Posted by Marcos Oliva <mo...@tradedeck.com>.
Some more of my findings on these issue,, 

on this method   "parser.parse(inSrc) "

I traced it to the package,, org.apache.xerces.utils.ChunkyByteArray.java
on a private mehod called fill():

[Code of fill()]
 private void fill() throws IOException {
        int bufnum = fLength >> CHUNK_SHIFT;
        byte[] data = new byte[CHUNK_SIZE];
        fData[bufnum] = data;
        int offset = 0;
        int capacity = CHUNK_SIZE;
        int result = 0;
        do {
            result = fInputStream.read(data, offset, capacity);
            if (result == -1) {
                data[offset] = (byte)0xff;
                fInputStream.close();
                fInputStream = null;
                break;
            }
            if (result > 0) {
                fLength += result;
                offset += result;
                capacity -= result;
            }
        } while (capacity > 0);
    }

[ends here]

It dies once it tries to do for the second time the following line 
result = fInputStream.read(data, offset, capacity);




-----Original Message-----
From: Marcos Oliva [mailto:moliva@tradedeck.com]
Sent: Wednesday, January 03, 2001 12:54 PM
To: xerces-j-dev@xml.apache.org
Subject: RE: Xerces and XML Serialization..



HI Scott, 
I took that line
conn.setRequestProperty("method","POST");
from the code but still ,, when it gets to the servlet
it just dies, :

<Servlet snipet>

  InputSource inSrc = new InputSource(req.getInputStream());
  parser.parse(inSrc); 
  Document docOut = parser.getDocument();

<Servlet snipet>

athe inSrc var holds the InputStream of the req method,, but 
then when I passed it to the parser.parser method then it dies,, noo error
an nothing, I am using Visual Cafe for debugging this thing.

Any other suggestions


marcos oliva


-----Original Message-----
From: Scott Carter [mailto:scarter@dotsconnect.com]
Sent: Wednesday, January 03, 2001 5:54 AM
To: xerces-j-dev@xml.apache.org
Subject: RE: Xerces and XML Serialization..


Hi Marcos,

I'm doing the same thing, but not with an Applet.  Try removing the
following line and see what you get:

conn.setRequestProperty("method","POST");



-----Original Message-----
From: Marcos Oliva [mailto:moliva@tradedeck.com]
Sent: Tuesday, January 02, 2001 10:10 PM
To: xerces-j-dev@xml.apache.org
Subject: Xerces and XML Serialization..


Hello guys,

I am a newbie with Xerces and I have been trying to make this to work,,,

I have an Applet that is sending a XML dom to a servlet but for the love of
G...!!
I have not been able to make the program to work..

[ snip of applet side ]
 HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
 conn.setDoInput(true);
 conn.setDoOutput(true);
 // Don't used a cached version of URL connection.
 conn.setUseCaches (false);
 // Specify the content type that we will send binary data
 conn.setRequestProperty("Content-Type", "application/octet-stream");
 conn.setRequestProperty("method","POST");
 ObjectOutputStream out =  new ObjectOutputStream(conn.getOutputStream());
 XMLSerializer ser = 	new XMLSerializer(pwt , new OutputFormat("xml",
"UTF-8", false));
ser.asDOMSerializer();
ser.serialize(inxmlcommand);
out.close();
[ -- end of applet snipte ]

[Servlet snip it]
	    DOMParser parser =  new DOMParser();

	    //InputSource inSrc = new InputSource(req.getInputStream());

	    parser.parse(new InputSource(req.getInputStream()));
	    Document docOut = parser.getDocument();

it never passed the parser method..

If you have another sample of XML serialized methods, please send them this
way.

marcos oliva


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org

RE: Xerces and XML Serialization..

Posted by Marcos Oliva <mo...@tradedeck.com>.
HI Scott, 
I took that line
conn.setRequestProperty("method","POST");
from the code but still ,, when it gets to the servlet
it just dies, :

<Servlet snipet>

  InputSource inSrc = new InputSource(req.getInputStream());
  parser.parse(inSrc); 
  Document docOut = parser.getDocument();

<Servlet snipet>

athe inSrc var holds the InputStream of the req method,, but 
then when I passed it to the parser.parser method then it dies,, noo error
an nothing, I am using Visual Cafe for debugging this thing.

Any other suggestions


marcos oliva


-----Original Message-----
From: Scott Carter [mailto:scarter@dotsconnect.com]
Sent: Wednesday, January 03, 2001 5:54 AM
To: xerces-j-dev@xml.apache.org
Subject: RE: Xerces and XML Serialization..


Hi Marcos,

I'm doing the same thing, but not with an Applet.  Try removing the
following line and see what you get:

conn.setRequestProperty("method","POST");



-----Original Message-----
From: Marcos Oliva [mailto:moliva@tradedeck.com]
Sent: Tuesday, January 02, 2001 10:10 PM
To: xerces-j-dev@xml.apache.org
Subject: Xerces and XML Serialization..


Hello guys,

I am a newbie with Xerces and I have been trying to make this to work,,,

I have an Applet that is sending a XML dom to a servlet but for the love of
G...!!
I have not been able to make the program to work..

[ snip of applet side ]
 HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
 conn.setDoInput(true);
 conn.setDoOutput(true);
 // Don't used a cached version of URL connection.
 conn.setUseCaches (false);
 // Specify the content type that we will send binary data
 conn.setRequestProperty("Content-Type", "application/octet-stream");
 conn.setRequestProperty("method","POST");
 ObjectOutputStream out =  new ObjectOutputStream(conn.getOutputStream());
 XMLSerializer ser = 	new XMLSerializer(pwt , new OutputFormat("xml",
"UTF-8", false));
ser.asDOMSerializer();
ser.serialize(inxmlcommand);
out.close();
[ -- end of applet snipte ]

[Servlet snip it]
	    DOMParser parser =  new DOMParser();

	    //InputSource inSrc = new InputSource(req.getInputStream());

	    parser.parse(new InputSource(req.getInputStream()));
	    Document docOut = parser.getDocument();

it never passed the parser method..

If you have another sample of XML serialized methods, please send them this
way.

marcos oliva


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org

RE: Xerces and XML Serialization..

Posted by Scott Carter <sc...@dotsconnect.com>.
Hi Marcos,

I'm doing the same thing, but not with an Applet.  Try removing the
following line and see what you get:

conn.setRequestProperty("method","POST");



-----Original Message-----
From: Marcos Oliva [mailto:moliva@tradedeck.com]
Sent: Tuesday, January 02, 2001 10:10 PM
To: xerces-j-dev@xml.apache.org
Subject: Xerces and XML Serialization..


Hello guys,

I am a newbie with Xerces and I have been trying to make this to work,,,

I have an Applet that is sending a XML dom to a servlet but for the love of
G...!!
I have not been able to make the program to work..

[ snip of applet side ]
 HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
 conn.setDoInput(true);
 conn.setDoOutput(true);
 // Don't used a cached version of URL connection.
 conn.setUseCaches (false);
 // Specify the content type that we will send binary data
 conn.setRequestProperty("Content-Type", "application/octet-stream");
 conn.setRequestProperty("method","POST");
 ObjectOutputStream out =  new ObjectOutputStream(conn.getOutputStream());
 XMLSerializer ser = 	new XMLSerializer(pwt , new OutputFormat("xml",
"UTF-8", false));
ser.asDOMSerializer();
ser.serialize(inxmlcommand);
out.close();
[ -- end of applet snipte ]

[Servlet snip it]
	    DOMParser parser =  new DOMParser();

	    //InputSource inSrc = new InputSource(req.getInputStream());

	    parser.parse(new InputSource(req.getInputStream()));
	    Document docOut = parser.getDocument();

it never passed the parser method..

If you have another sample of XML serialized methods, please send them this
way.

marcos oliva


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org