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 Warren Bell <wa...@gmail.com> on 2012/02/09 04:10:15 UTC

Proxy org.apache.http.HttpResponse into javax.servlet.http.HttpServletResponse

I am writing a small proxy servlet and want to know what is the best way
to convert the entity from an org.apache.http.HttpResponse, I am getting
from a destination server through HttpClient, into the
javax.servlet.http.HttpServletResponse outputStream I need in my servlet
to send back to the client's browser. I am currently doing something
like this below, but I am running into issues with chunked data.

I have already transferred all response headers from
orgApacheHttpHttpResponse to javaxServletHttpHttpServletResponse before
the code below.

...

HttpEntity entity = orgApacheHttpHttpResponse.getEntity();

InputStream inputStream = null;
try
{
  inputStream = entity.getContent();
  BufferedInputStream bufferedInputStream = new
BufferedInputStream(inputStream);
  OutputStream outputStream =
javaxServletHttpHttpServletResponse.getOutputStream();
  int intNextByte;
  while((intNextByte = bufferedInputStream.read()) != -1)
  {
    outputStream.write(intNextByte);
  }
}
catch(IOException ioex)
{
  ...
}
catch(RuntimeException rex)
{
  ...
}
finally
{
  Close all streams
}


The code above does work when I manually exclude the "Transfer-Encoding:
chunked" header from the javax.servlet.http.HttpServletResponse, but am
I suppose to do that ? Basically the response that comes back to the
client's browser should be the same as the response that comes back to
HTTPClient in my servlet.

Also, are there any response headers that I should not transfer over to
javax.servlet.http.HttpServletResponse like maybe Transfer-Encoding ?
Are there headers that get added by the web server after my response has
left the servlet ?


-- 
Thanks,

Warren Bell

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


Re: Proxy org.apache.http.HttpResponse into javax.servlet.http.HttpServletResponse

Posted by Dan Checkoway <dc...@gmail.com>.
And on an almost completely unrelated note...if you need to copy data from
an InputStream to OutputStream like that, you can do it with a one-liner:
(ok 2 lines if you include the import)  :-)

import org.apache.commons.io.IOUtils;

...  IOUtils.copy(entity.getContent(),
javaxServletHttpHttpServletResponse.getOutputStream());

On Thu, Feb 9, 2012 at 12:06 PM, Warren Bell <wa...@gmail.com> wrote:

> Oleg,
>
> That would explain my chunked data problem. I will exclude all
> hop-by-hop headers from being transfered.
>
> Thanks,
>
> Warren Bell
>
> On 2/9/12 6:11 AM, Oleg Kalnichevski wrote:
> > On Wed, 2012-02-08 at 19:10 -0800, Warren Bell wrote:
> >> I am writing a small proxy servlet and want to know what is the best way
> >> to convert the entity from an org.apache.http.HttpResponse, I am getting
> >> from a destination server through HttpClient, into the
> >> javax.servlet.http.HttpServletResponse outputStream I need in my servlet
> >> to send back to the client's browser. I am currently doing something
> >> like this below, but I am running into issues with chunked data.
> >>
> >> I have already transferred all response headers from
> >> orgApacheHttpHttpResponse to javaxServletHttpHttpServletResponse before
> >> the code below.
> >>
> >> ...
> >>
> >> HttpEntity entity = orgApacheHttpHttpResponse.getEntity();
> >>
> >> InputStream inputStream = null;
> >> try
> >> {
> >>   inputStream = entity.getContent();
> >>   BufferedInputStream bufferedInputStream = new
> >> BufferedInputStream(inputStream);
> >>   OutputStream outputStream =
> >> javaxServletHttpHttpServletResponse.getOutputStream();
> >>   int intNextByte;
> >>   while((intNextByte = bufferedInputStream.read()) != -1)
> >>   {
> >>     outputStream.write(intNextByte);
> >>   }
> >> }
> >> catch(IOException ioex)
> >> {
> >>   ...
> >> }
> >> catch(RuntimeException rex)
> >> {
> >>   ...
> >> }
> >> finally
> >> {
> >>   Close all streams
> >> }
> >>
> >>
> >> The code above does work when I manually exclude the "Transfer-Encoding:
> >> chunked" header from the javax.servlet.http.HttpServletResponse, but am
> >> I suppose to do that ?
> >
> > Yes, you are. There are so called 'hop-by-hop' headers that MAY NOT be
> > transmitted by HTTP proxies to the next hop. See RFC 2616, section
> > 13.5.1: 'End-to-end and Hop-by-hop Headers'.
> >
> > Oleg
> >
> >
> >>  Basically the response that comes back to the
> >> client's browser should be the same as the response that comes back to
> >> HTTPClient in my servlet.
> >>
> >> Also, are there any response headers that I should not transfer over to
> >> javax.servlet.http.HttpServletResponse like maybe Transfer-Encoding ?
> >> Are there headers that get added by the web server after my response has
> >> left the servlet ?
> >>
> >>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Proxy org.apache.http.HttpResponse into javax.servlet.http.HttpServletResponse

Posted by Warren Bell <wa...@gmail.com>.
Oleg,

That would explain my chunked data problem. I will exclude all
hop-by-hop headers from being transfered.

Thanks,

Warren Bell

On 2/9/12 6:11 AM, Oleg Kalnichevski wrote:
> On Wed, 2012-02-08 at 19:10 -0800, Warren Bell wrote:
>> I am writing a small proxy servlet and want to know what is the best way
>> to convert the entity from an org.apache.http.HttpResponse, I am getting
>> from a destination server through HttpClient, into the
>> javax.servlet.http.HttpServletResponse outputStream I need in my servlet
>> to send back to the client's browser. I am currently doing something
>> like this below, but I am running into issues with chunked data.
>>
>> I have already transferred all response headers from
>> orgApacheHttpHttpResponse to javaxServletHttpHttpServletResponse before
>> the code below.
>>
>> ...
>>
>> HttpEntity entity = orgApacheHttpHttpResponse.getEntity();
>>
>> InputStream inputStream = null;
>> try
>> {
>>   inputStream = entity.getContent();
>>   BufferedInputStream bufferedInputStream = new
>> BufferedInputStream(inputStream);
>>   OutputStream outputStream =
>> javaxServletHttpHttpServletResponse.getOutputStream();
>>   int intNextByte;
>>   while((intNextByte = bufferedInputStream.read()) != -1)
>>   {
>>     outputStream.write(intNextByte);
>>   }
>> }
>> catch(IOException ioex)
>> {
>>   ...
>> }
>> catch(RuntimeException rex)
>> {
>>   ...
>> }
>> finally
>> {
>>   Close all streams
>> }
>>
>>
>> The code above does work when I manually exclude the "Transfer-Encoding:
>> chunked" header from the javax.servlet.http.HttpServletResponse, but am
>> I suppose to do that ?
> 
> Yes, you are. There are so called 'hop-by-hop' headers that MAY NOT be
> transmitted by HTTP proxies to the next hop. See RFC 2616, section
> 13.5.1: 'End-to-end and Hop-by-hop Headers'.
> 
> Oleg
> 
> 
>>  Basically the response that comes back to the
>> client's browser should be the same as the response that comes back to
>> HTTPClient in my servlet.
>>
>> Also, are there any response headers that I should not transfer over to
>> javax.servlet.http.HttpServletResponse like maybe Transfer-Encoding ?
>> Are there headers that get added by the web server after my response has
>> left the servlet ?
>>
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 

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


Re: Proxy org.apache.http.HttpResponse into javax.servlet.http.HttpServletResponse

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2012-02-08 at 19:10 -0800, Warren Bell wrote:
> I am writing a small proxy servlet and want to know what is the best way
> to convert the entity from an org.apache.http.HttpResponse, I am getting
> from a destination server through HttpClient, into the
> javax.servlet.http.HttpServletResponse outputStream I need in my servlet
> to send back to the client's browser. I am currently doing something
> like this below, but I am running into issues with chunked data.
> 
> I have already transferred all response headers from
> orgApacheHttpHttpResponse to javaxServletHttpHttpServletResponse before
> the code below.
> 
> ...
> 
> HttpEntity entity = orgApacheHttpHttpResponse.getEntity();
> 
> InputStream inputStream = null;
> try
> {
>   inputStream = entity.getContent();
>   BufferedInputStream bufferedInputStream = new
> BufferedInputStream(inputStream);
>   OutputStream outputStream =
> javaxServletHttpHttpServletResponse.getOutputStream();
>   int intNextByte;
>   while((intNextByte = bufferedInputStream.read()) != -1)
>   {
>     outputStream.write(intNextByte);
>   }
> }
> catch(IOException ioex)
> {
>   ...
> }
> catch(RuntimeException rex)
> {
>   ...
> }
> finally
> {
>   Close all streams
> }
> 
> 
> The code above does work when I manually exclude the "Transfer-Encoding:
> chunked" header from the javax.servlet.http.HttpServletResponse, but am
> I suppose to do that ?

Yes, you are. There are so called 'hop-by-hop' headers that MAY NOT be
transmitted by HTTP proxies to the next hop. See RFC 2616, section
13.5.1: 'End-to-end and Hop-by-hop Headers'.

Oleg


>  Basically the response that comes back to the
> client's browser should be the same as the response that comes back to
> HTTPClient in my servlet.
> 
> Also, are there any response headers that I should not transfer over to
> javax.servlet.http.HttpServletResponse like maybe Transfer-Encoding ?
> Are there headers that get added by the web server after my response has
> left the servlet ?
> 
> 



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