I'm using the Restlet HTTP client implementation to post a dynamic
Representation to my Restlet server implementation, however it doesn't look
like my data is getting written to the HTTP stream. I might be missing
something critical, but I've been able to understand the API so far. :) My
Representation class resembles the ObjectRepresentation that already exists,
except instead of serializing a Java object to an ObjectOutputStream, it
serializes a Java object to an XML stream. But, testing with the
ObjectRepresentation produces the same result.
Here's the basics of what I am trying to do:
Client client = new Client(Protocol.HTTP);
Response response = client.post("http://somehost.com/someurl", new
ObjectRepresentation("TestData"));
The response variable is not filled with any status or response.
Drilling down through the code I think I found the culprit:
in com.noelios.restlet.http.HttpClientCall.sendRequest() there's a check to see
if an entity exists:
Representation entity = request.isEntityAvailable() ? request.getEntity() :
null;
if(entity != null)
{
//The code to write the representation to the output stream.
}
The call to request.isEntityAvailable() goes back to the
org.restlet.data.Message class which checks that the entity is not null and
that the size of the entity is greater than 0. This is where I'm having
difficulty. In my Representation, I do not know the size of the resulting data
prior to it being written to the output stream, so my size is 0. However, if
you look at the com.noelios.restlet.ext.net.HttpUrlConnectionCall.sendRequest()
method, you see this:
// Adjust the streaming mode
if (entity.getSize() > 0)
{
// The size of the entity is known in advance
getConnection().setFixedLengthStreamingMode((int) entity.getSize());
}
else
{
// The size of the entity is not known in advance
if (this.clientHelper.getChunkLength() >= 0)
{
// Use chunked encoding
getConnection().setChunkedStreamingMode(
this.clientHelper.getChunkLength());
}
else
{
// Use entity buffering to determine the content length
}
}
This suggests that the data I am sending can have a size of 0, but when it does
it is never sent. And I can't "guess" a size, because if I guess wrong then the
setFixedLengthStreamingMode will cause the HTTP Post to fail when more bytes
are written than expected.
I guess my question is: When we subclass Representation, do we have to
calculate the getSize() value or can we allow it to be 0? In my case, I don't
really want to serialize my Java object until absolutely necessary, and I also
don't want to hold the serialized form of the object in memory prior to POSTing
it.
Am I missing something? Maybe it's not supposed to work this way.
Mitch