There isn't any generic way of notifying that the stream is closed.
However, you can return your own implementation of an InputStream.
Delegate your calls to the real InputStream, and in the InputStream
wrapper, also call the close to the sequence.
So something like this in your resource method:
final ResultSequence seq = ...;
final InputStream realInputStream = ...;
return new InputStream() {
@Override
public int read(byte b[]) throws IOException {
return realInputStream.read(b);
}
@Override
public int read(byte b[], int off, int len)throws IOException {
return realInputStream.read(b, off, len);
}
@Override
public void close() throws IOException {
realInputStream.close();
}
@Override
public int read() throws IOException {
return realInputStream.read();
}
/* override the rest of the InputStream methods and delegate to
the real input stream */
};
I would use a real class instead of an anonymous class.
On Mon, Aug 23, 2010 at 7:41 AM, srinivasan venkat <[email protected]> wrote:
>
> Thanks for your response.
>
> Is there any way to get notification from the InputStreamProvider class that
> the current input stream has been closed, So that in our resource class we
> will invoke ResultSequence session to get closed.
>
> Since ,ResultSequence is associated with session, If we perform session.
> Close(), it is making stream to get closed.
>
> So, We plan to close the session after stream get closed by provider class.
>
> Is there any such indicator without subclasssing from the Provider to
> resource class to inform that the current stream is closed?.
>
>
>
>
>
> Thanks,
>
> Srinivasan. V
>
> --- On Fri, 20/8/10, Bryant Luk <[email protected]> wrote:
>
> From: Bryant Luk <[email protected]>
> Subject: Re: Stream Close Problem.
> To: [email protected]
> Date: Friday, 20 August, 2010, 7:57 PM
>
> Hi,
>
> When you return an InputStream as your entity (either directly or via
> a Response object), by default,
> org.apache.wink.common.internal.providers.entity.InputStreamProvider
> is used to read the InputStream and then write it back out to the
> client. After InputStreamProvider is done writing the entity, it
> calls InputStream.close().
>
> You can check the source for the last release 1.1.1 here:
>
> http://svn.apache.org/repos/asf/incubator/wink/tags/wink-1.1.1-incubating/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/InputStreamProvider.java
>
> The InputStreamProvider should be the last object that touches the
> returned InputStream. Was this what you wanted? Are you seeing
> different behavior?
>
> If you want to customize this behavior some more, you can write your
> own customized InputStream MessageBodyWriter and add it to your
> Application sub-class getClasses(). You can use Wink's code as an
> example.
>
> On Fri, Aug 20, 2010 at 8:11 AM, srinivasan venkat <[email protected]>
> wrote:
> >
> > Hi,
> >
> > I am currently using ResultSequence class in com.marklogic.xcc,
> > InputStream obj = resultSeq.next().asInputStream();
> >
> > My aim is direcly hook this input stream object to apache wink layer in
> > Restful webservice implementation as,
> > Response.status(200).entity(obj).type("multipart/mixed").build();
> >
> > According to xcc documentation,
> > When using a non-cached ResultSequence you should call close() on it when
> > you're finished so
> > that the connection can be re-used. This can be tricky if you're handing
> > XCC's InputStream to some other object.If you allow the owning
> > ResultSequence to fall out of scope (be garbage collected) it will close
> > all open InputSequences when it's finalized.
> >
> > So,We need to close the stream in any of rest layer.Otherwise there is risk
> > of stream getting closed any time during garbage collection.xcc will close
> > the stream in server connection anytime when we are not closing.
> >
> > We can't close this in resource class as we are getting stream got closed
> > exception.
> > So,We need to close the ResultSequence stream and its associated session in
> > any of Rest layer.We need the stream to be closed in server side not in
> > apache wink client side.Can we achieve this in any of the handler chains
> > ,providers etc.
> >
> > Need help in this regard.
> >
> > Thanks,
> > Srinivasan.V
>