You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Safwan Kamarrudin <sh...@alumni.cmu.edu> on 2006/08/20 02:33:42 UTC

Buffered HttpResponse

Greetings,

I have a question on how to buffer a HttpResponse and perform some
operations in each iteration. In short, I would like to buffer a
FileEntity response and update a JProgressBar with the transfer
progress of the FileEntity. Below is my code snippet:

                    response.setStatusCode(HttpStatus.SC_OK);
                    File fSharedFile = new File(getSharedFiles().get(path));
                    String sFileName = fSharedFile.getName();

                    if (fSharedFile.exists()) {
                        //display progress bar and set the size
                        showUlProgressBar(fSharedFile.getName());
                        setUlProgressBarSize(sFileName,
Integer.parseInt(String.valueOf(fSharedFile.length())));

                        FileEntity body = new FileEntity(fSharedFile,
"application/octet-stream");
                        body.setChunked(true);
                        BufferedHttpEntity bufBody = new
BufferedHttpEntity(body);
                        response.setEntity(bufBody);
                        InputStream in = bufBody.getContent();

                        OutputStream out = new
HttpDataOutputStream(new
SocketHttpDataTransmitter(htRequests.get(this.thisConn.toString()), 8
* 1024));

                        System.out.println("Serving file " +
fSharedFile.getPath());

                        // Transfer bytes from in to out
                        byte[] buf = new byte[8 * 1024];
                        int len;
                        int iTransferredSize = 0;

                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                            iTransferredSize += len;
                            //update file transfer progress
                            updateUlProgressBar(sFileName, iTransferredSize);
                        }

                        System.out.println("Done serving file " +
fSharedFile.getPath());

                        //Close streams
                        in.close();
                        out.close();

                        //hide progress bar
                        hideUlProgressBar(fSharedFile.getName());
                    }


The response will be sent to the client but it will be malformed. For
some reason the client receives the HTTP stream but is unable to act
on it.

I would be really glad if someone could point me in the right direction.

Thanks in advance.

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


Re: Re: Buffered HttpResponse

Posted by Safwan Kamarrudin <sh...@alumni.cmu.edu>.
On 8/20/06, Roland Weber <ht...@dubioso.net> wrote:
> Are you talking about buffering in the client or in the server?
> Are you talking about a progress bar at the client or at the server?

The answer to both questions is the server side.

> This looks like server side. A user interface on the server side?

Yes, I'm developing a peer-to-peer file sharing utility (where every
peer is both a client and a server), hence the need to show all active
uploads and their transfer progress.

> > Integer.parseInt(String.valueOf(fSharedFile.length())));
>
> That's an awfully complex way to turn a long into an int.

Yeah, I agree. I'm sure there's a much better way of achieving it :).

> I don't see the point in buffering a file entity. File entities use
> their own buffer in writeTo().

This is exactly what I was trying to figure out. I wasn't sure whether
the buffering should be done while constructing the response or in the
Entity itself. I scoured the list of documentation to no avail. I also
did go through the source in order to understand the code structure,
also to no avail.

> You do NOT use a data transmitter directly! At least not if you
> want to talk HTTP. You use a HttpServerConnection that knows how
> to send the status line and response headers before sending the
> response body.

I see. I was trying out so many different things and they simply
didn't work. Out of frustration I decided to try the most fundamental
thing that the API provides, hence the aforementioned code.

> Here is my advice, assuming you want a progress bar on the server side:
>
> 1. throw away the code snippet above

Will do.

> 2. study class ElementalHttpServer from the examples to understand
>    how files are served:
> http://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/examples/org/apache/http/examples/ElementalHttpServer.java

I did use the above example as a starting point, and keep building on
top of it.

> 3. implement file serving without progress bar, based on what you've
>    seen in ElementalHttpServer

Will do.

> 4. derive your own FileEntityWithProgressBar from the FileEntity,
>    overriding the writeTo() method to update the progress bar

This is exactly what I've been looking for.

> hope that helps,
>   Roland

Thanks a lot Roland, I'm sure it does.

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


Re: Buffered HttpResponse

Posted by Roland Weber <ht...@dubioso.net>.
Hello Safwan,

> I have a question on how to buffer a HttpResponse and perform some
> operations in each iteration. In short, I would like to buffer a
> FileEntity response and update a JProgressBar with the transfer
> progress of the FileEntity.

Are you talking about buffering in the client or in the server?
Are you talking about a progress bar at the client or at the server?

> Below is my code snippet:
> 
>                    response.setStatusCode(HttpStatus.SC_OK);

This looks like server side. A user interface on the server side?

> Integer.parseInt(String.valueOf(fSharedFile.length())));

That's an awfully complex way to turn a long into an int.

>                        FileEntity body = new FileEntity(fSharedFile,
> "application/octet-stream");
>                        body.setChunked(true);
>                        BufferedHttpEntity bufBody = new
> BufferedHttpEntity(body);

I don't see the point in buffering a file entity. File entities use
their own buffer in writeTo().

>                        response.setEntity(bufBody);
>                        InputStream in = bufBody.getContent();
> 
>                        OutputStream out = new
> HttpDataOutputStream(new
> SocketHttpDataTransmitter(htRequests.get(this.thisConn.toString()), 8
> * 1024));

You do NOT use a data transmitter directly! At least not if you
want to talk HTTP. You use a HttpServerConnection that knows how
to send the status line and response headers before sending the
response body.
> 
>                        System.out.println("Serving file " +
> fSharedFile.getPath());
> 
>                        // Transfer bytes from in to out
>                        byte[] buf = new byte[8 * 1024];
>                        int len;
>                        int iTransferredSize = 0;
> 
>                        while ((len = in.read(buf)) > 0) {
>                            out.write(buf, 0, len);
>                            iTransferredSize += len;
>                            //update file transfer progress
>                            updateUlProgressBar(sFileName,
> iTransferredSize);
>                        }
> 
>                        System.out.println("Done serving file " +
> fSharedFile.getPath());
> 
>                        //Close streams
>                        in.close();
>                        out.close();
> 
>                        //hide progress bar
>                        hideUlProgressBar(fSharedFile.getName());
>                    }
> 
> 
> The response will be sent to the client but it will be malformed. For
> some reason the client receives the HTTP stream but is unable to act
> on it.
> 
> I would be really glad if someone could point me in the right direction.

Here is my advice, assuming you want a progress bar on the server side:

1. throw away the code snippet above

2. study class ElementalHttpServer from the examples to understand
   how files are served:
http://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/examples/org/apache/http/examples/ElementalHttpServer.java

3. implement file serving without progress bar, based on what you've
   seen in ElementalHttpServer

4. derive your own FileEntityWithProgressBar from the FileEntity,
   overriding the writeTo() method to update the progress bar

hope that helps,
  Roland

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