Another way (that my company currently use), would be to make an InputStream
that wraps around the HttpMethod and releases the connection when close() is
called. This wrapper is nice to reuse other places where you don't want to
handle anything more than an InputStream.
public class HttpMethodInputStream extends InputStream {
private InputStream wrappedInputStream;
private HttpMethod httpMethod;
public HttpMethodInputStream(HttpMethod httpMethod) throws IOException {
this.httpMethod = httpMethod;
this.wrappedInputStream = httpMethod.getResponseBodyAsStream();
}
public int available() throws IOException {
return wrappedInputStream.available();
}
public void close() throws IOException {
wrappedInputStream.close();
httpMethod.releaseConnection();
}
public boolean equals(Object obj) {
return wrappedInputStream.equals(obj);
}
public int hashCode() {
return wrappedInputStream.hashCode();
}
public void mark(int readlimit) {
wrappedInputStream.mark(readlimit);
}
public boolean markSupported() {
return wrappedInputStream.markSupported();
}
public int read() throws IOException {
return wrappedInputStream.read();
}
public int read(byte[] b, int off, int len) throws IOException {
return wrappedInputStream.read(b, off, len);
}
public int read(byte[] b) throws IOException {
return wrappedInputStream.read(b);
}
public void reset() throws IOException {
wrappedInputStream.reset();
}
public long skip(long n) throws IOException {
return wrappedInputStream.skip(n);
}
public String toString() {
return wrappedInputStream.toString();
}
}
//Just create the bufferedinputstream like this instead:
bufferedInputstream = new BufferedInputStream(new
HttpMethodInputStream(getMethod));
Eirik
2009/1/20 arnab_ghosh <[email protected]>
>
> Hi Marcus,
>
> I have tested the code. It is working fine.
> Thanks a lot.
>
> Regards
> Arnab
>
> M.C.S. wrote:
> >
> > Hi Arnab,
> >
> > now I guess I realize your problem. I even just read the JavaDoc of
> > HttpClient version 4, which says "Closing the input stream will trigger
> > connection release". So with this up2date version you won't have any
> > problems.
> >
> > If you really need to close the connection yourself and/or you don't
> > want to upgrade, subclassing StreamingResolution should help. Design
> > your subclass to hold the HttpClient (or whatever you need to release
> > the connection). A very simple approach (untested code):
> >
> > ************************************************************************
> >
> > public class HttpClientStreamingResolution extends StreamingResolution {
> >
> > private GetMethod getMethod;
> >
> > public HttpClientStreamingResolution(
> > String contentType,
> > GetMethod getMethod) {
> > // calling super constructor
> > super(contentType, getMethod.getResponseBodyAsStream());
> > this.getMethod = getMethod;
> > }
> >
> > @Override
> > protected void stream(HttpServletResponse response) throws Exception
> {
> > super.stream(response);
> > getMethod.releaseConnection();
> > }
> > }
> >
> > ************************************************************************
> >
> > Regards
> > Marcus
> >
> >
> > arnab_ghosh schrieb:
> >> Hi Marcus,
> >>
> >> Thanks once again for such prompt reply.
> >> I understood the the stream will be closed. But this HttpClient creates
> a
> >> connection for fetching the response. It says that the connection should
> >> be
> >> released, once response is read.
> >>
> >> I will provide you the code snippet for your help:
> >> try {
> >> GetMethod getMethod = new GetMethod(assetURL);
> >> int statusCode = httpClient.executeMethod(getMethod);
> >>
> >> log.info("statusCode : {}", statusCode);
> >>
> >> // Wrap the InputStream in a BufferedInputStream
> >> bufferedInputstream = new
> >> BufferedInputStream(getMethod.getResponseBodyAsStream());
> >>
> >> // Get the content type of the file
> >> contentType =
> >> getMethod.getResponseHeader("content-type").getValue();
> >> log.info("The contentType : {}", contentType);
> >>
> >> // Create a StreamingResolution with inputStream
> >> streamingResolution = new StreamingResolution(contentType,
> >> bufferedInputstream);
> >>
> >> } catch (IOException ioEx) {
> >> log.error("Error occurred: " + ioEx.getMessage(), ioEx);
> >> } finally {
> >>
> >> getMethod.releaseConnection(); // This line if presently closes
> >> the
> >> Stream abruptly
> >> }
> >>
> >> // set the fileName in the StreamingResolution.This in turn sets the
> >> appropriate response header.
> >> streamingResolution.setFilename(fileName + "." + extension);
> >>
> >> // return the FileInputStream wrapped in the resolution
> >> return streamingResolution;
> >>
> >> execute method will close the Stream but how will the connection be
> >> released?
> >>
> >>
> >>
> >> Regards
> >> Arnab
> >>
> >> M.C.S. wrote:
> >>
> >>> Hi Arnab,
> >>>
> >>> arnab_ghosh wrote:
> >>>
> >>>> I have used HttpClient for getting the InputStream from a web service.
> >>>> Will
> >>>> the StreamingResolution also release the connection?
> >>>>
> >>>>
> >>> after streaming the StreamingResolution will close any InputStream you
> >>> pass on to it. It does not matter where you obtained the stream.
> >>>
> >>> Regards
> >>> Marcus
> >>>
> >>>
> ------------------------------------------------------------------------------
> >>> This SF.net email is sponsored by:
> >>> SourcForge Community
> >>> SourceForge wants to tell your story.
> >>> http://p.sf.net/sfu/sf-spreadtheword
> >>> _______________________________________________
> >>> Stripes-users mailing list
> >>> [email protected]
> >>> https://lists.sourceforge.net/lists/listinfo/stripes-users
> >>>
> >>>
> >>>
> >>
> >>
> >
> >
> >
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by:
> > SourcForge Community
> > SourceForge wants to tell your story.
> > http://p.sf.net/sfu/sf-spreadtheword
> > _______________________________________________
> > Stripes-users mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/stripes-users
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/File-Download-in-Stripes-using-HttpClient-tp21558239p21563132.html
> Sent from the stripes-users mailing list archive at Nabble.com.
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> SourcForge Community
> SourceForge wants to tell your story.
> http://p.sf.net/sfu/sf-spreadtheword
> _______________________________________________
> Stripes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users