You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Parimal Dhinoja <pd...@gmail.com> on 2009/11/30 18:27:12 UTC

how to close inputStream before returning response

Hi All,

Following is my service method where I am reading file and feed it into
Response object.  Even if I try to close my file streams in finally block, I
didn't receive any file back in response. if I remove stream closing
statements from finally block, I am getting my file in response body when
checked in browser. Can someone please let me know how I can close my
streams or does it will close automatically after Response object is
returned?


@GET
    @Path("/message1")
    public Response getMessage1(@QueryParam("propertyName")
    String propertyName)
    {
        String param = propertyName;
        String filePath = "I://abc.xml";

        FileInputStream fis = null;
        BufferedInputStream bis = null;
        ResponseBuilder rs = null;
        Response response = null;
        try
        {
            fis = new FileInputStream(new File(filePath));
            bis = new BufferedInputStream(fis);
            rs = Response.status(200).entity(bis);
            response = rs.build();
            // bis.close();
            // fis.close();
        }
        catch (Exception e)
        {
            LOGGER.info(e.getMessage());
        }
        finally
        {
            try
            {
                if (bis != null)
                {
                    bis.close();
                    LOGGER.info("===>bis is closed");
                }
                if (fis != null)
                {
                    fis.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.info("Exception occured while closing streams");
            }
        }
        // return Response.status(200).entity(bis).build();
        return response;

    }

-- 
Regards,
Parimal
"Nothing is stationary,Change is a part of Life"

Re: how to close inputStream before returning response

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi,


> Thanks guys,
>
> Sergey, can you provide any link or document for the last option(prototype
> one) you suggested?

See

http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Resourcelifecycles

and

http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Lifecyclemanagement

>
> Also if I want to send multiple XML files in response, what will be the good
> solution? at present, I am getting HttpServletResponse object stream in my
> service implementation and feed all files streams one by one.

One option is to use multiparts :
http://cwiki.apache.org/CXF20DOC/jax-rs.html#JAX-RS-Writingattachments

the other one which might be simpler to deal with at the client side is to return say an html page with links pointing to individual 
files which, perhaps, can be served by CXF Servelt or the default servlet container servlet

hope it helps, Sergey

>
> I also tried returning File object from service which works well if there is
> only single file to send in response.
>
> Regards,
> Parimal
>
> On Mon, Nov 30, 2009 at 3:43 PM, Sergey Beryozkin <sb...@progress.com>wrote:
>
>> Hi
>>
>> Thanks, this is one option, so registering an out interceptor at one of
>> the stages which follows MARSHAL (POST_MARSHAL or MARSHAL_ENDING) should
>> work.
>>
>> However, just returning an InputStream should result in it being closed
>> after it's been copied to the output stream. BinaryDataProvider closes
>> InputStream after the copy has been completed.
>>
>> Another option is to register a prototype-scope resource class and have
>> a @PreDestroy method on this class...
>>
>> Cheers, Sergey
>>
>>
>>
>> -----Original Message-----
>> From: vickatvuuch [mailto:vlisovsky@gmail.com]
>> Sent: 30 November 2009 18:22
>> To: users@cxf.apache.org
>> Subject: Re: how to close inputStream before returning response
>>
>>
>> You can take a look at one of the out interceptors that handle the
>> response.
>> I would start somewhere around JAXRSOutInterceptor and go from there.
>>
>>
>> Parimal Dhinoja wrote:
>> >
>> > Hi All,
>> >
>> > Following is my service method where I am reading file and feed it
>> into
>> > Response object.  Even if I try to close my file streams in finally
>> block,
>> > I
>> > didn't receive any file back in response. if I remove stream closing
>> > statements from finally block, I am getting my file in response body
>> when
>> > checked in browser. Can someone please let me know how I can close my
>> > streams or does it will close automatically after Response object is
>> > returned?
>> >
>> >
>> > @GET
>> >     @Path("/message1")
>> >     public Response getMessage1(@QueryParam("propertyName")
>> >     String propertyName)
>> >     {
>> >         String param = propertyName;
>> >         String filePath = "I://abc.xml";
>> >
>> >         FileInputStream fis = null;
>> >         BufferedInputStream bis = null;
>> >         ResponseBuilder rs = null;
>> >         Response response = null;
>> >         try
>> >         {
>> >             fis = new FileInputStream(new File(filePath));
>> >             bis = new BufferedInputStream(fis);
>> >             rs = Response.status(200).entity(bis);
>> >             response = rs.build();
>> >             // bis.close();
>> >             // fis.close();
>> >         }
>> >         catch (Exception e)
>> >         {
>> >             LOGGER.info(e.getMessage());
>> >         }
>> >         finally
>> >         {
>> >             try
>> >             {
>> >                 if (bis != null)
>> >                 {
>> >                     bis.close();
>> >                     LOGGER.info("===>bis is closed");
>> >                 }
>> >                 if (fis != null)
>> >                 {
>> >                     fis.close();
>> >                 }
>> >             }
>> >             catch (Exception e)
>> >             {
>> >                 LOGGER.info("Exception occured while closing
>> streams");
>> >             }
>> >         }
>> >         // return Response.status(200).entity(bis).build();
>> >         return response;
>> >
>> >     }
>> >
>> > --
>> > Regards,
>> > Parimal
>> > "Nothing is stationary,Change is a part of Life"
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/how-to-close-inputStream-before-returning-response
>> -tp26578495p26579444.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
>>
>
>
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
> 


Re: how to close inputStream before returning response

Posted by Parimal Dhinoja <pd...@gmail.com>.
Thanks guys,

Sergey, can you provide any link or document for the last option(prototype
one) you suggested?

Also if I want to send multiple XML files in response, what will be the good
solution? at present, I am getting HttpServletResponse object stream in my
service implementation and feed all files streams one by one.

I also tried returning File object from service which works well if there is
only single file to send in response.

Regards,
Parimal

On Mon, Nov 30, 2009 at 3:43 PM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
> Thanks, this is one option, so registering an out interceptor at one of
> the stages which follows MARSHAL (POST_MARSHAL or MARSHAL_ENDING) should
> work.
>
> However, just returning an InputStream should result in it being closed
> after it's been copied to the output stream. BinaryDataProvider closes
> InputStream after the copy has been completed.
>
> Another option is to register a prototype-scope resource class and have
> a @PreDestroy method on this class...
>
> Cheers, Sergey
>
>
>
> -----Original Message-----
> From: vickatvuuch [mailto:vlisovsky@gmail.com]
> Sent: 30 November 2009 18:22
> To: users@cxf.apache.org
> Subject: Re: how to close inputStream before returning response
>
>
> You can take a look at one of the out interceptors that handle the
> response.
> I would start somewhere around JAXRSOutInterceptor and go from there.
>
>
> Parimal Dhinoja wrote:
> >
> > Hi All,
> >
> > Following is my service method where I am reading file and feed it
> into
> > Response object.  Even if I try to close my file streams in finally
> block,
> > I
> > didn't receive any file back in response. if I remove stream closing
> > statements from finally block, I am getting my file in response body
> when
> > checked in browser. Can someone please let me know how I can close my
> > streams or does it will close automatically after Response object is
> > returned?
> >
> >
> > @GET
> >     @Path("/message1")
> >     public Response getMessage1(@QueryParam("propertyName")
> >     String propertyName)
> >     {
> >         String param = propertyName;
> >         String filePath = "I://abc.xml";
> >
> >         FileInputStream fis = null;
> >         BufferedInputStream bis = null;
> >         ResponseBuilder rs = null;
> >         Response response = null;
> >         try
> >         {
> >             fis = new FileInputStream(new File(filePath));
> >             bis = new BufferedInputStream(fis);
> >             rs = Response.status(200).entity(bis);
> >             response = rs.build();
> >             // bis.close();
> >             // fis.close();
> >         }
> >         catch (Exception e)
> >         {
> >             LOGGER.info(e.getMessage());
> >         }
> >         finally
> >         {
> >             try
> >             {
> >                 if (bis != null)
> >                 {
> >                     bis.close();
> >                     LOGGER.info("===>bis is closed");
> >                 }
> >                 if (fis != null)
> >                 {
> >                     fis.close();
> >                 }
> >             }
> >             catch (Exception e)
> >             {
> >                 LOGGER.info("Exception occured while closing
> streams");
> >             }
> >         }
> >         // return Response.status(200).entity(bis).build();
> >         return response;
> >
> >     }
> >
> > --
> > Regards,
> > Parimal
> > "Nothing is stationary,Change is a part of Life"
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/how-to-close-inputStream-before-returning-response
> -tp26578495p26579444.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>


-- 
Regards,
Parimal
"Nothing is stationary,Change is a part of Life"

RE: how to close inputStream before returning response

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

Thanks, this is one option, so registering an out interceptor at one of
the stages which follows MARSHAL (POST_MARSHAL or MARSHAL_ENDING) should
work. 

However, just returning an InputStream should result in it being closed
after it's been copied to the output stream. BinaryDataProvider closes
InputStream after the copy has been completed.

Another option is to register a prototype-scope resource class and have
a @PreDestroy method on this class...

Cheers, Sergey 

 

-----Original Message-----
From: vickatvuuch [mailto:vlisovsky@gmail.com] 
Sent: 30 November 2009 18:22
To: users@cxf.apache.org
Subject: Re: how to close inputStream before returning response


You can take a look at one of the out interceptors that handle the
response.
I would start somewhere around JAXRSOutInterceptor and go from there.


Parimal Dhinoja wrote:
> 
> Hi All,
> 
> Following is my service method where I am reading file and feed it
into
> Response object.  Even if I try to close my file streams in finally
block,
> I
> didn't receive any file back in response. if I remove stream closing
> statements from finally block, I am getting my file in response body
when
> checked in browser. Can someone please let me know how I can close my
> streams or does it will close automatically after Response object is
> returned?
> 
> 
> @GET
>     @Path("/message1")
>     public Response getMessage1(@QueryParam("propertyName")
>     String propertyName)
>     {
>         String param = propertyName;
>         String filePath = "I://abc.xml";
> 
>         FileInputStream fis = null;
>         BufferedInputStream bis = null;
>         ResponseBuilder rs = null;
>         Response response = null;
>         try
>         {
>             fis = new FileInputStream(new File(filePath));
>             bis = new BufferedInputStream(fis);
>             rs = Response.status(200).entity(bis);
>             response = rs.build();
>             // bis.close();
>             // fis.close();
>         }
>         catch (Exception e)
>         {
>             LOGGER.info(e.getMessage());
>         }
>         finally
>         {
>             try
>             {
>                 if (bis != null)
>                 {
>                     bis.close();
>                     LOGGER.info("===>bis is closed");
>                 }
>                 if (fis != null)
>                 {
>                     fis.close();
>                 }
>             }
>             catch (Exception e)
>             {
>                 LOGGER.info("Exception occured while closing
streams");
>             }
>         }
>         // return Response.status(200).entity(bis).build();
>         return response;
> 
>     }
> 
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
> 
> 

-- 
View this message in context:
http://old.nabble.com/how-to-close-inputStream-before-returning-response
-tp26578495p26579444.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: how to close inputStream before returning response

Posted by vickatvuuch <vl...@gmail.com>.
You can take a look at one of the out interceptors that handle the response.
I would start somewhere around JAXRSOutInterceptor and go from there.


Parimal Dhinoja wrote:
> 
> Hi All,
> 
> Following is my service method where I am reading file and feed it into
> Response object.  Even if I try to close my file streams in finally block,
> I
> didn't receive any file back in response. if I remove stream closing
> statements from finally block, I am getting my file in response body when
> checked in browser. Can someone please let me know how I can close my
> streams or does it will close automatically after Response object is
> returned?
> 
> 
> @GET
>     @Path("/message1")
>     public Response getMessage1(@QueryParam("propertyName")
>     String propertyName)
>     {
>         String param = propertyName;
>         String filePath = "I://abc.xml";
> 
>         FileInputStream fis = null;
>         BufferedInputStream bis = null;
>         ResponseBuilder rs = null;
>         Response response = null;
>         try
>         {
>             fis = new FileInputStream(new File(filePath));
>             bis = new BufferedInputStream(fis);
>             rs = Response.status(200).entity(bis);
>             response = rs.build();
>             // bis.close();
>             // fis.close();
>         }
>         catch (Exception e)
>         {
>             LOGGER.info(e.getMessage());
>         }
>         finally
>         {
>             try
>             {
>                 if (bis != null)
>                 {
>                     bis.close();
>                     LOGGER.info("===>bis is closed");
>                 }
>                 if (fis != null)
>                 {
>                     fis.close();
>                 }
>             }
>             catch (Exception e)
>             {
>                 LOGGER.info("Exception occured while closing streams");
>             }
>         }
>         // return Response.status(200).entity(bis).build();
>         return response;
> 
>     }
> 
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
> 
> 

-- 
View this message in context: http://old.nabble.com/how-to-close-inputStream-before-returning-response-tp26578495p26579444.html
Sent from the cxf-user mailing list archive at Nabble.com.