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/16 20:47:25 UTC

Exception : getWriter() has already been called for this response

Hi,

I have implemented RESTful CXF service with my spring project. in impl
class, I have used MessageContext to retrieve HttpResponse and I am using
response.getWriter() to set HttpResponse with my content.

when I call this service from browser, I get the response what I have set in
impl, but on tomcat console, I am getting following exception. Please help.
this is the last piece of work I have left to finish my task.

16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain doIntercept
WARNING: Interceptor has thrown exception, unwinding now
java.lang.IllegalStateException: getWriter() has already been called for
this response
 at
org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
at
org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
 at
javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
at
org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
 at
org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
at
org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
 at
com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)

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

Re: Exception : getWriter() has already been called for this response

Posted by Parimal Dhinoja <pd...@gmail.com>.
Hi Sergey,

I am facing another problem while calling my REST service. I have already
sent my impl class code earlier in this mail thread. It seems that my partly
impl code executes 2 times. following is my tomcat log. Please look into the
log marked bold. any idea please?

Regards,
parimal

17-Nov-2009 12:34:51
org.apache.cxf.binding.http.interceptor.DispatchInterceptor handleMessage
INFO: Invoking GET on /hi
17-Nov-2009 12:34:51
org.apache.cxf.binding.http.interceptor.URIParameterInInterceptor
handleMessage
INFO: URIParameterInterceptor handle message on path [/hi] with content-type
[null]
12:34:51,843 [http-8080-1] INFO  HelloWorldImpl - Recieved request ==> hello
12:34:51,843 [http-8080-1] INFO  HelloWorldImpl - Recieved request ==> hello
*12:34:51,843 [http-8080-1] INFO  HelloWorldImpl - propertyCode=10810145*
*12:34:51,843 [http-8080-1] INFO  HelloWorldImpl - propertyCode=10810145*
*12:34:51,843 [http-8080-1] INFO  HelloWorldImpl -
FilePath=I://home//opera//PMS/10810145/DEL10965261change.xml*
*12:34:51,843 [http-8080-1] INFO  HelloWorldImpl -
FilePath=I://home//opera//PMS/10810145/DEL10965261change.xml*
*12:34:51,844 [http-8080-1] INFO  HelloWorldImpl - Content-length=8773*
*12:34:51,844 [http-8080-1] INFO  HelloWorldImpl - Content-length=8773*

On Tue, Nov 17, 2009 at 11:41 AM, Parimal Dhinoja <pd...@gmail.com>wrote:

> Thank you so much Sergey for prompt reply and the links.
>
> I agree that JAX-RS might be more appropriate solution to my problem.
>
> But I still has a query about Response object. As you see, I need to read
> the phisical XML files that are stored on file system and based on user
> query parameter, I need to read one of those files and feed into Response
> stream.
>
> As per my understanding, jax-rs return xml response of the pojo class you
> have defined.
>
> so Please let me know, what I have implemented to return my physical xml
> file, is correct and whether it will work. I have to get HttpResponse from
> MessageContext and feed my xml file into response's body.
>
> As I said, I am new to CXF and may be there are better solutions to the
> problem I have which I am not aware of. It would be great if you suggest
> specific to my problem on hand.
>
> Again, I really appreciate you help.
>
> Regards,
> Parimal
>
>
>
> On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin <sb...@progress.com>wrote:
>
>> Hi
>>
>>
>> Thank you Sergey,
>>>
>>> Yes it seems working. now I haven't got any exception. but I am not
>>> verifying it. Actually following is my code in impl. I am sending xml
>>> file
>>> in Httpresponse stream. and I do not know if anything will add to
>>> response
>>> or overwrite by further interceptor process. I am using this service from
>>> browser. Please let me know what I am doing is right way of doing it. I
>>> am
>>> beginner to CXF REST.
>>>
>>
>> this is a JAXWS code but you can do REST with it if you'd like to. You'd
>> probably want to use Provider<Source> though, see
>>
>>
>> http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/restful_dispatch/
>>
>> alternatively, please consider using JAXRS
>>
>> http://cxf.apache.org/docs/jax-rs.html
>>
>> hope it helps, Sergey
>>
>>
>>
>>> @WebService(endpointInterface =
>>> "com.traveltripper.stargazer.service.impl.HelloWorld")
>>> public class HelloWorldImpl implements HelloWorld
>>> {
>>>
>>>   private static final Log log = LogFactory.getLog(HelloWorldImpl.class);
>>>
>>>   @Resource
>>>   private WebServiceContext context;
>>>
>>>   public void getHi()
>>>   {
>>>      try
>>>       {
>>>           MessageContext ctx = context.getMessageContext();
>>>           HttpServletRequest request =   (HttpServletRequest)
>>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
>>>           HttpServletResponse response =  (HttpServletResponse)
>>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>>>           response.setContentType("text/xml");
>>>           String reqParameter = request.getParameter("propertyCode");
>>>           String filePath = "c://"+ "DEL10965261change.xml";
>>>
>>>           FileInputStream fis = new FileInputStream(new File(filePath));
>>>
>>>           BufferedInputStream bis = new BufferedInputStream(fis);
>>>           ServletOutputStream sos = response.getOutputStream();
>>>           byte[] buffer = new byte[5000];
>>>           response.setHeader("Content-Length:",
>>> String.valueOf(bis.available()));
>>>           log.info("Content-length=" + bis.available());
>>>           while (true)
>>>           {
>>>               int bytesRead = bis.read(buffer, 0, buffer.length);
>>>               if (bytesRead < 0) break;
>>>               sos.write(buffer, 0, bytesRead);
>>>           }
>>>           fis.close();
>>>           sos.flush();
>>>           sos.close();
>>>
>>>       }
>>>       catch (Exception e)
>>>       {
>>>           e.getMessage();
>>>       }
>>>
>>>   }
>>> }
>>>
>>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
>>> sergey.beryozkin@iona.com
>>>
>>>> wrote:
>>>>
>>>
>>>
>>>> Hi
>>>>
>>>> Please try writing to response.getOutputStream() and it should work. I
>>>> missed overriding
>>>> response.getWriter()  in the HttpResponse context implementation.
>>>>
>>>> let me know please if it works
>>>> Sergey
>>>>
>>>>
>>>> Parimal Dhinoja wrote:
>>>> >
>>>> > Hi,
>>>> >
>>>> > I have implemented RESTful CXF service with my spring project. in impl
>>>> > class, I have used MessageContext to retrieve HttpResponse and I am
>>>> using
>>>> > response.getWriter() to set HttpResponse with my content.
>>>> >
>>>> > when I call this service from browser, I get the response what I have
>>>> set
>>>> > in
>>>> > impl, but on tomcat console, I am getting following exception. Please
>>>> > help.
>>>> > this is the last piece of work I have left to finish my task.
>>>> >
>>>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
>>>> > doIntercept
>>>> > WARNING: Interceptor has thrown exception, unwinding now
>>>> > java.lang.IllegalStateException: getWriter() has already been called
>>>> for
>>>> > this response
>>>> >  at
>>>> >
>>>> org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
>>>> > at
>>>> >
>>>>
>>>> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
>>>> >  at
>>>> >
>>>>
>>>> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
>>>> > at
>>>> >
>>>>
>>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
>>>> >  at
>>>> >
>>>>
>>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
>>>> > at
>>>> >
>>>>
>>>> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
>>>> >  at
>>>> >
>>>>
>>>> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
>>>> >
>>>> > --
>>>> > Regards,
>>>> > Parimal
>>>> > "Nothing is stationary,Change is a part of Life"
>>>> >
>>>> >
>>>>
>>>> --
>>>> View this message in context:
>>>>
>>>> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
>>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>>
>>>>
>>>>
>>>
>>> --
>>> Regards,
>>> Parimal
>>> "Nothing is stationary,Change is a part of Life"
>>>
>>>
>
>
> --
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
>



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

RE: Exception : getWriter() has already been called for this response

Posted by Sergey Beryozkin <sb...@progress.com>.
Please Set "application/xml" on the Response.type(...), might help

-----Original Message-----
From: Parimal Dhinoja [mailto:pdhinoja@gmail.com] 
Sent: 18 November 2009 21:21
To: users@cxf.apache.org
Subject: Re: Exception : getWriter() has already been called for this
response

Thank you Sergey,

another option also worked perfectly. only difference between the 2
options
are :

with my approach(using HttpResponse), browser shows complete XML file
(with
nodes also). where as by using Response object as you mentioned below,
it
only shows content(values) of XML file I am feeding. but both the
options
 sent entire XML file in response body perfectly if you check response
content in browser using a tool like firebug.

Thanks again.

Regards,
Parimal

On Wed, Nov 18, 2009 at 3:27 PM, Sergey Beryozkin
<sb...@progress.com>wrote:

> Hi
>
> This is one possible option, another one is to return a Response :
>
> return Response.status(200).entity(theInputStream).build()
>
> cheers, Sergey
>
> -----Original Message-----
> From: Parimal Dhinoja [mailto:pdhinoja@gmail.com]
> Sent: 18 November 2009 19:46
> To: users@cxf.apache.org
> Subject: Re: Exception : getWriter() has already been called for this
> response
>
> Thanks Sergey,
>
> I have given a try with JAX-RS and I am able to return my XML file. I
> have
> created a resource class where I defined a method. This method get
> HttpResponse object from MessageContext, read XML file from file
system
> and
> then feeding  it to HttpResponse outputstream. Please let me know if
it
> is
> ok.
>
> Also How can I use JAX-RS response object to accomplish the same task?
>
> Regards,
> Parimal
>
> On Wed, Nov 18, 2009 at 7:50 AM, Sergey Beryozkin
> <sb...@progress.com>wrote:
>
> > Hi
> >
> >
> >  Thank you so much Sergey for prompt reply and the links.
> >>
> >> I agree that JAX-RS might be more appropriate solution to my
problem.
> >>
> >> But I still has a query about Response object. As you see, I need
to
> read
> >> the phisical XML files that are stored on file system and based on
> user
> >> query parameter, I need to read one of those files and feed into
> Response
> >> stream.
> >>
> >> As per my understanding, jax-rs return xml response of the pojo
class
> you
> >> have defined.
> >>
> >
> > Not necessarily - you can return an input stream pointing to your
xml
> file.
> > If you return a jaxrs Response from your method then you can also
set
> the
> > required content type to be associated with a given input stream..
> >
> > Also, I'm not sure about the other question you posted in the
> follow-up
> > message. Apparently you;re using a deprected CXF HTTP binding - I
can
> not
> > help there, I was not even involved in that project. Please consider
> using
> > either JAXWS support for doing HTTP services or migrate to JAXRS
> >
> > thanks, Sergey
> >
> >
> >
> >> so Please let me know, what I have implemented to return my
physical
> xml
> >> file, is correct and whether it will work. I have to get
HttpResponse
> from
> >> MessageContext and feed my xml file into response's body.
> >>
> >> As I said, I am new to CXF and may be there are better solutions to
> the
> >> problem I have which I am not aware of. It would be great if you
> suggest
> >> specific to my problem on hand.
> >>
> >> Again, I really appreciate you help.
> >>
> >> Regards,
> >> Parimal
> >>
> >>
> >>
> >> On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin
> <sberyozk@progress.com
> >> >wrote:
> >>
> >>  Hi
> >>>
> >>>
> >>> Thank you Sergey,
> >>>
> >>>>
> >>>> Yes it seems working. now I haven't got any exception. but I am
not
> >>>> verifying it. Actually following is my code in impl. I am sending
> xml
> >>>> file
> >>>> in Httpresponse stream. and I do not know if anything will add to
> >>>> response
> >>>> or overwrite by further interceptor process. I am using this
> service
> >>>> from
> >>>> browser. Please let me know what I am doing is right way of doing
> it. I
> >>>> am
> >>>> beginner to CXF REST.
> >>>>
> >>>>
> >>> this is a JAXWS code but you can do REST with it if you'd like to.
> You'd
> >>> probably want to use Provider<Source> though, see
> >>>
> >>>
> >>>
> >>>
>
http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/
> samples/restful_dispatch/
> >>>
> >>> alternatively, please consider using JAXRS
> >>>
> >>> http://cxf.apache.org/docs/jax-rs.html
> >>>
> >>> hope it helps, Sergey
> >>>
> >>>
> >>>
> >>>  @WebService(endpointInterface =
> >>>> "com.traveltripper.stargazer.service.impl.HelloWorld")
> >>>> public class HelloWorldImpl implements HelloWorld
> >>>> {
> >>>>
> >>>>  private static final Log log =
> LogFactory.getLog(HelloWorldImpl.class);
> >>>>
> >>>>  @Resource
> >>>>  private WebServiceContext context;
> >>>>
> >>>>  public void getHi()
> >>>>  {
> >>>>     try
> >>>>      {
> >>>>          MessageContext ctx = context.getMessageContext();
> >>>>          HttpServletRequest request =   (HttpServletRequest)
> >>>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
> >>>>          HttpServletResponse response =  (HttpServletResponse)
> >>>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
> >>>>          response.setContentType("text/xml");
> >>>>          String reqParameter =
> request.getParameter("propertyCode");
> >>>>          String filePath = "c://"+ "DEL10965261change.xml";
> >>>>
> >>>>          FileInputStream fis = new FileInputStream(new
> File(filePath));
> >>>>
> >>>>          BufferedInputStream bis = new BufferedInputStream(fis);
> >>>>          ServletOutputStream sos = response.getOutputStream();
> >>>>          byte[] buffer = new byte[5000];
> >>>>          response.setHeader("Content-Length:",
> >>>> String.valueOf(bis.available()));
> >>>>          log.info("Content-length=" + bis.available());
> >>>>          while (true)
> >>>>          {
> >>>>              int bytesRead = bis.read(buffer, 0, buffer.length);
> >>>>              if (bytesRead < 0) break;
> >>>>              sos.write(buffer, 0, bytesRead);
> >>>>          }
> >>>>          fis.close();
> >>>>          sos.flush();
> >>>>          sos.close();
> >>>>
> >>>>      }
> >>>>      catch (Exception e)
> >>>>      {
> >>>>          e.getMessage();
> >>>>      }
> >>>>
> >>>>  }
> >>>> }
> >>>>
> >>>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
> >>>> sergey.beryozkin@iona.com
> >>>>
> >>>>  wrote:
> >>>>>
> >>>>>
> >>>>
> >>>>  Hi
> >>>>>
> >>>>> Please try writing to response.getOutputStream() and it should
> work. I
> >>>>> missed overriding
> >>>>> response.getWriter()  in the HttpResponse context
implementation.
> >>>>>
> >>>>> let me know please if it works
> >>>>> Sergey
> >>>>>
> >>>>>
> >>>>> Parimal Dhinoja wrote:
> >>>>> >
> >>>>> > Hi,
> >>>>> >
> >>>>> > I have implemented RESTful CXF service with my spring project.
> in
> >>>>> impl
> >>>>> > class, I have used MessageContext to retrieve HttpResponse and
I
> am
> >>>>> using
> >>>>> > response.getWriter() to set HttpResponse with my content.
> >>>>> >
> >>>>> > when I call this service from browser, I get the response what
I
> have
> >>>>> set
> >>>>> > in
> >>>>> > impl, but on tomcat console, I am getting following exception.
> Please
> >>>>> > help.
> >>>>> > this is the last piece of work I have left to finish my task.
> >>>>> >
> >>>>> > 16-Nov-2009 13:47:31
org.apache.cxf.phase.PhaseInterceptorChain
> >>>>> > doIntercept
> >>>>> > WARNING: Interceptor has thrown exception, unwinding now
> >>>>> > java.lang.IllegalStateException: getWriter() has already been
> called
> >>>>> for
> >>>>> > this response
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
>
org.apache.catalina.connector.Response.getOutputStream(Response.java:579
> )
> >>>>> > at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
>
org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFac
> ade.java:183)
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
>
javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrap
> per.java:102)
> >>>>> > at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
>
org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstr
> actHTTPDestination.java:482)
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
>
org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStrea
> m.onFirstWrite(AbstractHTTPDestination.java:546)
> >>>>> > at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
>
org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutpu
> tStream.java:61)
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
>
com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8Out
> putStreamWriter.java:94)
> >>>>> >
> >>>>> > --
> >>>>> > Regards,
> >>>>> > Parimal
> >>>>> > "Nothing is stationary,Change is a part of Life"
> >>>>> >
> >>>>> >
> >>>>>
> >>>>> --
> >>>>> View this message in context:
> >>>>>
> >>>>>
> >>>>>
>
http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-cal
> led-for-this-response-tp26378624p26379082.html
> >>>>> Sent from the cxf-user mailing list archive at Nabble.com.
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>> --
> >>>> Regards,
> >>>> Parimal
> >>>> "Nothing is stationary,Change is a part of Life"
> >>>>
> >>>>
> >>>>
> >>
> >> --
> >> Regards,
> >> Parimal
> >> "Nothing is stationary,Change is a part of Life"
> >>
> >>
> >
>
>
> --
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
>



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

Re: Exception : getWriter() has already been called for this response

Posted by Parimal Dhinoja <pd...@gmail.com>.
Thank you Sergey,

another option also worked perfectly. only difference between the 2 options
are :

with my approach(using HttpResponse), browser shows complete XML file (with
nodes also). where as by using Response object as you mentioned below, it
only shows content(values) of XML file I am feeding. but both the options
 sent entire XML file in response body perfectly if you check response
content in browser using a tool like firebug.

Thanks again.

Regards,
Parimal

On Wed, Nov 18, 2009 at 3:27 PM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
> This is one possible option, another one is to return a Response :
>
> return Response.status(200).entity(theInputStream).build()
>
> cheers, Sergey
>
> -----Original Message-----
> From: Parimal Dhinoja [mailto:pdhinoja@gmail.com]
> Sent: 18 November 2009 19:46
> To: users@cxf.apache.org
> Subject: Re: Exception : getWriter() has already been called for this
> response
>
> Thanks Sergey,
>
> I have given a try with JAX-RS and I am able to return my XML file. I
> have
> created a resource class where I defined a method. This method get
> HttpResponse object from MessageContext, read XML file from file system
> and
> then feeding  it to HttpResponse outputstream. Please let me know if it
> is
> ok.
>
> Also How can I use JAX-RS response object to accomplish the same task?
>
> Regards,
> Parimal
>
> On Wed, Nov 18, 2009 at 7:50 AM, Sergey Beryozkin
> <sb...@progress.com>wrote:
>
> > Hi
> >
> >
> >  Thank you so much Sergey for prompt reply and the links.
> >>
> >> I agree that JAX-RS might be more appropriate solution to my problem.
> >>
> >> But I still has a query about Response object. As you see, I need to
> read
> >> the phisical XML files that are stored on file system and based on
> user
> >> query parameter, I need to read one of those files and feed into
> Response
> >> stream.
> >>
> >> As per my understanding, jax-rs return xml response of the pojo class
> you
> >> have defined.
> >>
> >
> > Not necessarily - you can return an input stream pointing to your xml
> file.
> > If you return a jaxrs Response from your method then you can also set
> the
> > required content type to be associated with a given input stream..
> >
> > Also, I'm not sure about the other question you posted in the
> follow-up
> > message. Apparently you;re using a deprected CXF HTTP binding - I can
> not
> > help there, I was not even involved in that project. Please consider
> using
> > either JAXWS support for doing HTTP services or migrate to JAXRS
> >
> > thanks, Sergey
> >
> >
> >
> >> so Please let me know, what I have implemented to return my physical
> xml
> >> file, is correct and whether it will work. I have to get HttpResponse
> from
> >> MessageContext and feed my xml file into response's body.
> >>
> >> As I said, I am new to CXF and may be there are better solutions to
> the
> >> problem I have which I am not aware of. It would be great if you
> suggest
> >> specific to my problem on hand.
> >>
> >> Again, I really appreciate you help.
> >>
> >> Regards,
> >> Parimal
> >>
> >>
> >>
> >> On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin
> <sberyozk@progress.com
> >> >wrote:
> >>
> >>  Hi
> >>>
> >>>
> >>> Thank you Sergey,
> >>>
> >>>>
> >>>> Yes it seems working. now I haven't got any exception. but I am not
> >>>> verifying it. Actually following is my code in impl. I am sending
> xml
> >>>> file
> >>>> in Httpresponse stream. and I do not know if anything will add to
> >>>> response
> >>>> or overwrite by further interceptor process. I am using this
> service
> >>>> from
> >>>> browser. Please let me know what I am doing is right way of doing
> it. I
> >>>> am
> >>>> beginner to CXF REST.
> >>>>
> >>>>
> >>> this is a JAXWS code but you can do REST with it if you'd like to.
> You'd
> >>> probably want to use Provider<Source> though, see
> >>>
> >>>
> >>>
> >>>
> http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/
> samples/restful_dispatch/
> >>>
> >>> alternatively, please consider using JAXRS
> >>>
> >>> http://cxf.apache.org/docs/jax-rs.html
> >>>
> >>> hope it helps, Sergey
> >>>
> >>>
> >>>
> >>>  @WebService(endpointInterface =
> >>>> "com.traveltripper.stargazer.service.impl.HelloWorld")
> >>>> public class HelloWorldImpl implements HelloWorld
> >>>> {
> >>>>
> >>>>  private static final Log log =
> LogFactory.getLog(HelloWorldImpl.class);
> >>>>
> >>>>  @Resource
> >>>>  private WebServiceContext context;
> >>>>
> >>>>  public void getHi()
> >>>>  {
> >>>>     try
> >>>>      {
> >>>>          MessageContext ctx = context.getMessageContext();
> >>>>          HttpServletRequest request =   (HttpServletRequest)
> >>>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
> >>>>          HttpServletResponse response =  (HttpServletResponse)
> >>>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
> >>>>          response.setContentType("text/xml");
> >>>>          String reqParameter =
> request.getParameter("propertyCode");
> >>>>          String filePath = "c://"+ "DEL10965261change.xml";
> >>>>
> >>>>          FileInputStream fis = new FileInputStream(new
> File(filePath));
> >>>>
> >>>>          BufferedInputStream bis = new BufferedInputStream(fis);
> >>>>          ServletOutputStream sos = response.getOutputStream();
> >>>>          byte[] buffer = new byte[5000];
> >>>>          response.setHeader("Content-Length:",
> >>>> String.valueOf(bis.available()));
> >>>>          log.info("Content-length=" + bis.available());
> >>>>          while (true)
> >>>>          {
> >>>>              int bytesRead = bis.read(buffer, 0, buffer.length);
> >>>>              if (bytesRead < 0) break;
> >>>>              sos.write(buffer, 0, bytesRead);
> >>>>          }
> >>>>          fis.close();
> >>>>          sos.flush();
> >>>>          sos.close();
> >>>>
> >>>>      }
> >>>>      catch (Exception e)
> >>>>      {
> >>>>          e.getMessage();
> >>>>      }
> >>>>
> >>>>  }
> >>>> }
> >>>>
> >>>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
> >>>> sergey.beryozkin@iona.com
> >>>>
> >>>>  wrote:
> >>>>>
> >>>>>
> >>>>
> >>>>  Hi
> >>>>>
> >>>>> Please try writing to response.getOutputStream() and it should
> work. I
> >>>>> missed overriding
> >>>>> response.getWriter()  in the HttpResponse context implementation.
> >>>>>
> >>>>> let me know please if it works
> >>>>> Sergey
> >>>>>
> >>>>>
> >>>>> Parimal Dhinoja wrote:
> >>>>> >
> >>>>> > Hi,
> >>>>> >
> >>>>> > I have implemented RESTful CXF service with my spring project.
> in
> >>>>> impl
> >>>>> > class, I have used MessageContext to retrieve HttpResponse and I
> am
> >>>>> using
> >>>>> > response.getWriter() to set HttpResponse with my content.
> >>>>> >
> >>>>> > when I call this service from browser, I get the response what I
> have
> >>>>> set
> >>>>> > in
> >>>>> > impl, but on tomcat console, I am getting following exception.
> Please
> >>>>> > help.
> >>>>> > this is the last piece of work I have left to finish my task.
> >>>>> >
> >>>>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
> >>>>> > doIntercept
> >>>>> > WARNING: Interceptor has thrown exception, unwinding now
> >>>>> > java.lang.IllegalStateException: getWriter() has already been
> called
> >>>>> for
> >>>>> > this response
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> org.apache.catalina.connector.Response.getOutputStream(Response.java:579
> )
> >>>>> > at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFac
> ade.java:183)
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrap
> per.java:102)
> >>>>> > at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstr
> actHTTPDestination.java:482)
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStrea
> m.onFirstWrite(AbstractHTTPDestination.java:546)
> >>>>> > at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutpu
> tStream.java:61)
> >>>>> >  at
> >>>>> >
> >>>>>
> >>>>>
> >>>>>
> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8Out
> putStreamWriter.java:94)
> >>>>> >
> >>>>> > --
> >>>>> > Regards,
> >>>>> > Parimal
> >>>>> > "Nothing is stationary,Change is a part of Life"
> >>>>> >
> >>>>> >
> >>>>>
> >>>>> --
> >>>>> View this message in context:
> >>>>>
> >>>>>
> >>>>>
> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-cal
> led-for-this-response-tp26378624p26379082.html
> >>>>> Sent from the cxf-user mailing list archive at Nabble.com.
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>> --
> >>>> Regards,
> >>>> Parimal
> >>>> "Nothing is stationary,Change is a part of Life"
> >>>>
> >>>>
> >>>>
> >>
> >> --
> >> Regards,
> >> Parimal
> >> "Nothing is stationary,Change is a part of Life"
> >>
> >>
> >
>
>
> --
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
>



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

RE: Exception : getWriter() has already been called for this response

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

This is one possible option, another one is to return a Response :

return Response.status(200).entity(theInputStream).build()

cheers, Sergey

-----Original Message-----
From: Parimal Dhinoja [mailto:pdhinoja@gmail.com] 
Sent: 18 November 2009 19:46
To: users@cxf.apache.org
Subject: Re: Exception : getWriter() has already been called for this
response

Thanks Sergey,

I have given a try with JAX-RS and I am able to return my XML file. I
have
created a resource class where I defined a method. This method get
HttpResponse object from MessageContext, read XML file from file system
and
then feeding  it to HttpResponse outputstream. Please let me know if it
is
ok.

Also How can I use JAX-RS response object to accomplish the same task?

Regards,
Parimal

On Wed, Nov 18, 2009 at 7:50 AM, Sergey Beryozkin
<sb...@progress.com>wrote:

> Hi
>
>
>  Thank you so much Sergey for prompt reply and the links.
>>
>> I agree that JAX-RS might be more appropriate solution to my problem.
>>
>> But I still has a query about Response object. As you see, I need to
read
>> the phisical XML files that are stored on file system and based on
user
>> query parameter, I need to read one of those files and feed into
Response
>> stream.
>>
>> As per my understanding, jax-rs return xml response of the pojo class
you
>> have defined.
>>
>
> Not necessarily - you can return an input stream pointing to your xml
file.
> If you return a jaxrs Response from your method then you can also set
the
> required content type to be associated with a given input stream..
>
> Also, I'm not sure about the other question you posted in the
follow-up
> message. Apparently you;re using a deprected CXF HTTP binding - I can
not
> help there, I was not even involved in that project. Please consider
using
> either JAXWS support for doing HTTP services or migrate to JAXRS
>
> thanks, Sergey
>
>
>
>> so Please let me know, what I have implemented to return my physical
xml
>> file, is correct and whether it will work. I have to get HttpResponse
from
>> MessageContext and feed my xml file into response's body.
>>
>> As I said, I am new to CXF and may be there are better solutions to
the
>> problem I have which I am not aware of. It would be great if you
suggest
>> specific to my problem on hand.
>>
>> Again, I really appreciate you help.
>>
>> Regards,
>> Parimal
>>
>>
>>
>> On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin
<sberyozk@progress.com
>> >wrote:
>>
>>  Hi
>>>
>>>
>>> Thank you Sergey,
>>>
>>>>
>>>> Yes it seems working. now I haven't got any exception. but I am not
>>>> verifying it. Actually following is my code in impl. I am sending
xml
>>>> file
>>>> in Httpresponse stream. and I do not know if anything will add to
>>>> response
>>>> or overwrite by further interceptor process. I am using this
service
>>>> from
>>>> browser. Please let me know what I am doing is right way of doing
it. I
>>>> am
>>>> beginner to CXF REST.
>>>>
>>>>
>>> this is a JAXWS code but you can do REST with it if you'd like to.
You'd
>>> probably want to use Provider<Source> though, see
>>>
>>>
>>>
>>>
http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/
samples/restful_dispatch/
>>>
>>> alternatively, please consider using JAXRS
>>>
>>> http://cxf.apache.org/docs/jax-rs.html
>>>
>>> hope it helps, Sergey
>>>
>>>
>>>
>>>  @WebService(endpointInterface =
>>>> "com.traveltripper.stargazer.service.impl.HelloWorld")
>>>> public class HelloWorldImpl implements HelloWorld
>>>> {
>>>>
>>>>  private static final Log log =
LogFactory.getLog(HelloWorldImpl.class);
>>>>
>>>>  @Resource
>>>>  private WebServiceContext context;
>>>>
>>>>  public void getHi()
>>>>  {
>>>>     try
>>>>      {
>>>>          MessageContext ctx = context.getMessageContext();
>>>>          HttpServletRequest request =   (HttpServletRequest)
>>>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
>>>>          HttpServletResponse response =  (HttpServletResponse)
>>>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>>>>          response.setContentType("text/xml");
>>>>          String reqParameter =
request.getParameter("propertyCode");
>>>>          String filePath = "c://"+ "DEL10965261change.xml";
>>>>
>>>>          FileInputStream fis = new FileInputStream(new
File(filePath));
>>>>
>>>>          BufferedInputStream bis = new BufferedInputStream(fis);
>>>>          ServletOutputStream sos = response.getOutputStream();
>>>>          byte[] buffer = new byte[5000];
>>>>          response.setHeader("Content-Length:",
>>>> String.valueOf(bis.available()));
>>>>          log.info("Content-length=" + bis.available());
>>>>          while (true)
>>>>          {
>>>>              int bytesRead = bis.read(buffer, 0, buffer.length);
>>>>              if (bytesRead < 0) break;
>>>>              sos.write(buffer, 0, bytesRead);
>>>>          }
>>>>          fis.close();
>>>>          sos.flush();
>>>>          sos.close();
>>>>
>>>>      }
>>>>      catch (Exception e)
>>>>      {
>>>>          e.getMessage();
>>>>      }
>>>>
>>>>  }
>>>> }
>>>>
>>>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
>>>> sergey.beryozkin@iona.com
>>>>
>>>>  wrote:
>>>>>
>>>>>
>>>>
>>>>  Hi
>>>>>
>>>>> Please try writing to response.getOutputStream() and it should
work. I
>>>>> missed overriding
>>>>> response.getWriter()  in the HttpResponse context implementation.
>>>>>
>>>>> let me know please if it works
>>>>> Sergey
>>>>>
>>>>>
>>>>> Parimal Dhinoja wrote:
>>>>> >
>>>>> > Hi,
>>>>> >
>>>>> > I have implemented RESTful CXF service with my spring project.
in
>>>>> impl
>>>>> > class, I have used MessageContext to retrieve HttpResponse and I
am
>>>>> using
>>>>> > response.getWriter() to set HttpResponse with my content.
>>>>> >
>>>>> > when I call this service from browser, I get the response what I
have
>>>>> set
>>>>> > in
>>>>> > impl, but on tomcat console, I am getting following exception.
Please
>>>>> > help.
>>>>> > this is the last piece of work I have left to finish my task.
>>>>> >
>>>>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
>>>>> > doIntercept
>>>>> > WARNING: Interceptor has thrown exception, unwinding now
>>>>> > java.lang.IllegalStateException: getWriter() has already been
called
>>>>> for
>>>>> > this response
>>>>> >  at
>>>>> >
>>>>>
>>>>>
org.apache.catalina.connector.Response.getOutputStream(Response.java:579
)
>>>>> > at
>>>>> >
>>>>>
>>>>>
>>>>>
org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFac
ade.java:183)
>>>>> >  at
>>>>> >
>>>>>
>>>>>
>>>>>
javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrap
per.java:102)
>>>>> > at
>>>>> >
>>>>>
>>>>>
>>>>>
org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(Abstr
actHTTPDestination.java:482)
>>>>> >  at
>>>>> >
>>>>>
>>>>>
>>>>>
org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStrea
m.onFirstWrite(AbstractHTTPDestination.java:546)
>>>>> > at
>>>>> >
>>>>>
>>>>>
>>>>>
org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutpu
tStream.java:61)
>>>>> >  at
>>>>> >
>>>>>
>>>>>
>>>>>
com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8Out
putStreamWriter.java:94)
>>>>> >
>>>>> > --
>>>>> > Regards,
>>>>> > Parimal
>>>>> > "Nothing is stationary,Change is a part of Life"
>>>>> >
>>>>> >
>>>>>
>>>>> --
>>>>> View this message in context:
>>>>>
>>>>>
>>>>>
http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-cal
led-for-this-response-tp26378624p26379082.html
>>>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>>
>>>>>
>>>> --
>>>> Regards,
>>>> Parimal
>>>> "Nothing is stationary,Change is a part of Life"
>>>>
>>>>
>>>>
>>
>> --
>> Regards,
>> Parimal
>> "Nothing is stationary,Change is a part of Life"
>>
>>
>


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

Re: Exception : getWriter() has already been called for this response

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

I have given a try with JAX-RS and I am able to return my XML file. I have
created a resource class where I defined a method. This method get
HttpResponse object from MessageContext, read XML file from file system and
then feeding  it to HttpResponse outputstream. Please let me know if it is
ok.

Also How can I use JAX-RS response object to accomplish the same task?

Regards,
Parimal

On Wed, Nov 18, 2009 at 7:50 AM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
>
>  Thank you so much Sergey for prompt reply and the links.
>>
>> I agree that JAX-RS might be more appropriate solution to my problem.
>>
>> But I still has a query about Response object. As you see, I need to read
>> the phisical XML files that are stored on file system and based on user
>> query parameter, I need to read one of those files and feed into Response
>> stream.
>>
>> As per my understanding, jax-rs return xml response of the pojo class you
>> have defined.
>>
>
> Not necessarily - you can return an input stream pointing to your xml file.
> If you return a jaxrs Response from your method then you can also set the
> required content type to be associated with a given input stream..
>
> Also, I'm not sure about the other question you posted in the follow-up
> message. Apparently you;re using a deprected CXF HTTP binding - I can not
> help there, I was not even involved in that project. Please consider using
> either JAXWS support for doing HTTP services or migrate to JAXRS
>
> thanks, Sergey
>
>
>
>> so Please let me know, what I have implemented to return my physical xml
>> file, is correct and whether it will work. I have to get HttpResponse from
>> MessageContext and feed my xml file into response's body.
>>
>> As I said, I am new to CXF and may be there are better solutions to the
>> problem I have which I am not aware of. It would be great if you suggest
>> specific to my problem on hand.
>>
>> Again, I really appreciate you help.
>>
>> Regards,
>> Parimal
>>
>>
>>
>> On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin <sberyozk@progress.com
>> >wrote:
>>
>>  Hi
>>>
>>>
>>> Thank you Sergey,
>>>
>>>>
>>>> Yes it seems working. now I haven't got any exception. but I am not
>>>> verifying it. Actually following is my code in impl. I am sending xml
>>>> file
>>>> in Httpresponse stream. and I do not know if anything will add to
>>>> response
>>>> or overwrite by further interceptor process. I am using this service
>>>> from
>>>> browser. Please let me know what I am doing is right way of doing it. I
>>>> am
>>>> beginner to CXF REST.
>>>>
>>>>
>>> this is a JAXWS code but you can do REST with it if you'd like to. You'd
>>> probably want to use Provider<Source> though, see
>>>
>>>
>>>
>>> http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/restful_dispatch/
>>>
>>> alternatively, please consider using JAXRS
>>>
>>> http://cxf.apache.org/docs/jax-rs.html
>>>
>>> hope it helps, Sergey
>>>
>>>
>>>
>>>  @WebService(endpointInterface =
>>>> "com.traveltripper.stargazer.service.impl.HelloWorld")
>>>> public class HelloWorldImpl implements HelloWorld
>>>> {
>>>>
>>>>  private static final Log log = LogFactory.getLog(HelloWorldImpl.class);
>>>>
>>>>  @Resource
>>>>  private WebServiceContext context;
>>>>
>>>>  public void getHi()
>>>>  {
>>>>     try
>>>>      {
>>>>          MessageContext ctx = context.getMessageContext();
>>>>          HttpServletRequest request =   (HttpServletRequest)
>>>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
>>>>          HttpServletResponse response =  (HttpServletResponse)
>>>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>>>>          response.setContentType("text/xml");
>>>>          String reqParameter = request.getParameter("propertyCode");
>>>>          String filePath = "c://"+ "DEL10965261change.xml";
>>>>
>>>>          FileInputStream fis = new FileInputStream(new File(filePath));
>>>>
>>>>          BufferedInputStream bis = new BufferedInputStream(fis);
>>>>          ServletOutputStream sos = response.getOutputStream();
>>>>          byte[] buffer = new byte[5000];
>>>>          response.setHeader("Content-Length:",
>>>> String.valueOf(bis.available()));
>>>>          log.info("Content-length=" + bis.available());
>>>>          while (true)
>>>>          {
>>>>              int bytesRead = bis.read(buffer, 0, buffer.length);
>>>>              if (bytesRead < 0) break;
>>>>              sos.write(buffer, 0, bytesRead);
>>>>          }
>>>>          fis.close();
>>>>          sos.flush();
>>>>          sos.close();
>>>>
>>>>      }
>>>>      catch (Exception e)
>>>>      {
>>>>          e.getMessage();
>>>>      }
>>>>
>>>>  }
>>>> }
>>>>
>>>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
>>>> sergey.beryozkin@iona.com
>>>>
>>>>  wrote:
>>>>>
>>>>>
>>>>
>>>>  Hi
>>>>>
>>>>> Please try writing to response.getOutputStream() and it should work. I
>>>>> missed overriding
>>>>> response.getWriter()  in the HttpResponse context implementation.
>>>>>
>>>>> let me know please if it works
>>>>> Sergey
>>>>>
>>>>>
>>>>> Parimal Dhinoja wrote:
>>>>> >
>>>>> > Hi,
>>>>> >
>>>>> > I have implemented RESTful CXF service with my spring project. in
>>>>> impl
>>>>> > class, I have used MessageContext to retrieve HttpResponse and I am
>>>>> using
>>>>> > response.getWriter() to set HttpResponse with my content.
>>>>> >
>>>>> > when I call this service from browser, I get the response what I have
>>>>> set
>>>>> > in
>>>>> > impl, but on tomcat console, I am getting following exception. Please
>>>>> > help.
>>>>> > this is the last piece of work I have left to finish my task.
>>>>> >
>>>>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
>>>>> > doIntercept
>>>>> > WARNING: Interceptor has thrown exception, unwinding now
>>>>> > java.lang.IllegalStateException: getWriter() has already been called
>>>>> for
>>>>> > this response
>>>>> >  at
>>>>> >
>>>>>
>>>>> org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
>>>>> > at
>>>>> >
>>>>>
>>>>>
>>>>> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
>>>>> >  at
>>>>> >
>>>>>
>>>>>
>>>>> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
>>>>> > at
>>>>> >
>>>>>
>>>>>
>>>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
>>>>> >  at
>>>>> >
>>>>>
>>>>>
>>>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
>>>>> > at
>>>>> >
>>>>>
>>>>>
>>>>> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
>>>>> >  at
>>>>> >
>>>>>
>>>>>
>>>>> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
>>>>> >
>>>>> > --
>>>>> > Regards,
>>>>> > Parimal
>>>>> > "Nothing is stationary,Change is a part of Life"
>>>>> >
>>>>> >
>>>>>
>>>>> --
>>>>> View this message in context:
>>>>>
>>>>>
>>>>> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
>>>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>>
>>>>>
>>>> --
>>>> Regards,
>>>> Parimal
>>>> "Nothing is stationary,Change is a part of Life"
>>>>
>>>>
>>>>
>>
>> --
>> Regards,
>> Parimal
>> "Nothing is stationary,Change is a part of Life"
>>
>>
>


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

Re: Exception : getWriter() has already been called for this response

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

> Thank you so much Sergey for prompt reply and the links.
>
> I agree that JAX-RS might be more appropriate solution to my problem.
>
> But I still has a query about Response object. As you see, I need to read
> the phisical XML files that are stored on file system and based on user
> query parameter, I need to read one of those files and feed into Response
> stream.
>
> As per my understanding, jax-rs return xml response of the pojo class you
> have defined.

Not necessarily - you can return an input stream pointing to your xml file.
If you return a jaxrs Response from your method then you can also set the required content type to be associated with a given input 
stream..

Also, I'm not sure about the other question you posted in the follow-up message. Apparently you;re using a deprected CXF HTTP 
binding - I can not help there, I was not even involved in that project. Please consider using either JAXWS support for doing HTTP 
services or migrate to JAXRS

thanks, Sergey

>
> so Please let me know, what I have implemented to return my physical xml
> file, is correct and whether it will work. I have to get HttpResponse from
> MessageContext and feed my xml file into response's body.
>
> As I said, I am new to CXF and may be there are better solutions to the
> problem I have which I am not aware of. It would be great if you suggest
> specific to my problem on hand.
>
> Again, I really appreciate you help.
>
> Regards,
> Parimal
>
>
>
> On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin <sb...@progress.com>wrote:
>
>> Hi
>>
>>
>> Thank you Sergey,
>>>
>>> Yes it seems working. now I haven't got any exception. but I am not
>>> verifying it. Actually following is my code in impl. I am sending xml file
>>> in Httpresponse stream. and I do not know if anything will add to response
>>> or overwrite by further interceptor process. I am using this service from
>>> browser. Please let me know what I am doing is right way of doing it. I am
>>> beginner to CXF REST.
>>>
>>
>> this is a JAXWS code but you can do REST with it if you'd like to. You'd
>> probably want to use Provider<Source> though, see
>>
>>
>> http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/restful_dispatch/
>>
>> alternatively, please consider using JAXRS
>>
>> http://cxf.apache.org/docs/jax-rs.html
>>
>> hope it helps, Sergey
>>
>>
>>
>>> @WebService(endpointInterface =
>>> "com.traveltripper.stargazer.service.impl.HelloWorld")
>>> public class HelloWorldImpl implements HelloWorld
>>> {
>>>
>>>   private static final Log log = LogFactory.getLog(HelloWorldImpl.class);
>>>
>>>   @Resource
>>>   private WebServiceContext context;
>>>
>>>   public void getHi()
>>>   {
>>>      try
>>>       {
>>>           MessageContext ctx = context.getMessageContext();
>>>           HttpServletRequest request =   (HttpServletRequest)
>>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
>>>           HttpServletResponse response =  (HttpServletResponse)
>>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>>>           response.setContentType("text/xml");
>>>           String reqParameter = request.getParameter("propertyCode");
>>>           String filePath = "c://"+ "DEL10965261change.xml";
>>>
>>>           FileInputStream fis = new FileInputStream(new File(filePath));
>>>
>>>           BufferedInputStream bis = new BufferedInputStream(fis);
>>>           ServletOutputStream sos = response.getOutputStream();
>>>           byte[] buffer = new byte[5000];
>>>           response.setHeader("Content-Length:",
>>> String.valueOf(bis.available()));
>>>           log.info("Content-length=" + bis.available());
>>>           while (true)
>>>           {
>>>               int bytesRead = bis.read(buffer, 0, buffer.length);
>>>               if (bytesRead < 0) break;
>>>               sos.write(buffer, 0, bytesRead);
>>>           }
>>>           fis.close();
>>>           sos.flush();
>>>           sos.close();
>>>
>>>       }
>>>       catch (Exception e)
>>>       {
>>>           e.getMessage();
>>>       }
>>>
>>>   }
>>> }
>>>
>>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
>>> sergey.beryozkin@iona.com
>>>
>>>> wrote:
>>>>
>>>
>>>
>>>> Hi
>>>>
>>>> Please try writing to response.getOutputStream() and it should work. I
>>>> missed overriding
>>>> response.getWriter()  in the HttpResponse context implementation.
>>>>
>>>> let me know please if it works
>>>> Sergey
>>>>
>>>>
>>>> Parimal Dhinoja wrote:
>>>> >
>>>> > Hi,
>>>> >
>>>> > I have implemented RESTful CXF service with my spring project. in impl
>>>> > class, I have used MessageContext to retrieve HttpResponse and I am
>>>> using
>>>> > response.getWriter() to set HttpResponse with my content.
>>>> >
>>>> > when I call this service from browser, I get the response what I have
>>>> set
>>>> > in
>>>> > impl, but on tomcat console, I am getting following exception. Please
>>>> > help.
>>>> > this is the last piece of work I have left to finish my task.
>>>> >
>>>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
>>>> > doIntercept
>>>> > WARNING: Interceptor has thrown exception, unwinding now
>>>> > java.lang.IllegalStateException: getWriter() has already been called
>>>> for
>>>> > this response
>>>> >  at
>>>> >
>>>> org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
>>>> > at
>>>> >
>>>>
>>>> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
>>>> >  at
>>>> >
>>>>
>>>> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
>>>> > at
>>>> >
>>>>
>>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
>>>> >  at
>>>> >
>>>>
>>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
>>>> > at
>>>> >
>>>>
>>>> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
>>>> >  at
>>>> >
>>>>
>>>> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
>>>> >
>>>> > --
>>>> > Regards,
>>>> > Parimal
>>>> > "Nothing is stationary,Change is a part of Life"
>>>> >
>>>> >
>>>>
>>>> --
>>>> View this message in context:
>>>>
>>>> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
>>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>>
>>>>
>>>>
>>>
>>> --
>>> Regards,
>>> Parimal
>>> "Nothing is stationary,Change is a part of Life"
>>>
>>>
>
>
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
> 


Re: Exception : getWriter() has already been called for this response

Posted by Parimal Dhinoja <pd...@gmail.com>.
Thank you so much Sergey for prompt reply and the links.

I agree that JAX-RS might be more appropriate solution to my problem.

But I still has a query about Response object. As you see, I need to read
the phisical XML files that are stored on file system and based on user
query parameter, I need to read one of those files and feed into Response
stream.

As per my understanding, jax-rs return xml response of the pojo class you
have defined.

so Please let me know, what I have implemented to return my physical xml
file, is correct and whether it will work. I have to get HttpResponse from
MessageContext and feed my xml file into response's body.

As I said, I am new to CXF and may be there are better solutions to the
problem I have which I am not aware of. It would be great if you suggest
specific to my problem on hand.

Again, I really appreciate you help.

Regards,
Parimal



On Tue, Nov 17, 2009 at 4:16 PM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
>
> Thank you Sergey,
>>
>> Yes it seems working. now I haven't got any exception. but I am not
>> verifying it. Actually following is my code in impl. I am sending xml file
>> in Httpresponse stream. and I do not know if anything will add to response
>> or overwrite by further interceptor process. I am using this service from
>> browser. Please let me know what I am doing is right way of doing it. I am
>> beginner to CXF REST.
>>
>
> this is a JAXWS code but you can do REST with it if you'd like to. You'd
> probably want to use Provider<Source> though, see
>
>
> http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/restful_dispatch/
>
> alternatively, please consider using JAXRS
>
> http://cxf.apache.org/docs/jax-rs.html
>
> hope it helps, Sergey
>
>
>
>> @WebService(endpointInterface =
>> "com.traveltripper.stargazer.service.impl.HelloWorld")
>> public class HelloWorldImpl implements HelloWorld
>> {
>>
>>   private static final Log log = LogFactory.getLog(HelloWorldImpl.class);
>>
>>   @Resource
>>   private WebServiceContext context;
>>
>>   public void getHi()
>>   {
>>      try
>>       {
>>           MessageContext ctx = context.getMessageContext();
>>           HttpServletRequest request =   (HttpServletRequest)
>> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
>>           HttpServletResponse response =  (HttpServletResponse)
>> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>>           response.setContentType("text/xml");
>>           String reqParameter = request.getParameter("propertyCode");
>>           String filePath = "c://"+ "DEL10965261change.xml";
>>
>>           FileInputStream fis = new FileInputStream(new File(filePath));
>>
>>           BufferedInputStream bis = new BufferedInputStream(fis);
>>           ServletOutputStream sos = response.getOutputStream();
>>           byte[] buffer = new byte[5000];
>>           response.setHeader("Content-Length:",
>> String.valueOf(bis.available()));
>>           log.info("Content-length=" + bis.available());
>>           while (true)
>>           {
>>               int bytesRead = bis.read(buffer, 0, buffer.length);
>>               if (bytesRead < 0) break;
>>               sos.write(buffer, 0, bytesRead);
>>           }
>>           fis.close();
>>           sos.flush();
>>           sos.close();
>>
>>       }
>>       catch (Exception e)
>>       {
>>           e.getMessage();
>>       }
>>
>>   }
>> }
>>
>> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <
>> sergey.beryozkin@iona.com
>>
>>> wrote:
>>>
>>
>>
>>> Hi
>>>
>>> Please try writing to response.getOutputStream() and it should work. I
>>> missed overriding
>>> response.getWriter()  in the HttpResponse context implementation.
>>>
>>> let me know please if it works
>>> Sergey
>>>
>>>
>>> Parimal Dhinoja wrote:
>>> >
>>> > Hi,
>>> >
>>> > I have implemented RESTful CXF service with my spring project. in impl
>>> > class, I have used MessageContext to retrieve HttpResponse and I am
>>> using
>>> > response.getWriter() to set HttpResponse with my content.
>>> >
>>> > when I call this service from browser, I get the response what I have
>>> set
>>> > in
>>> > impl, but on tomcat console, I am getting following exception. Please
>>> > help.
>>> > this is the last piece of work I have left to finish my task.
>>> >
>>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
>>> > doIntercept
>>> > WARNING: Interceptor has thrown exception, unwinding now
>>> > java.lang.IllegalStateException: getWriter() has already been called
>>> for
>>> > this response
>>> >  at
>>> >
>>> org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
>>> > at
>>> >
>>>
>>> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
>>> >  at
>>> >
>>>
>>> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
>>> > at
>>> >
>>>
>>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
>>> >  at
>>> >
>>>
>>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
>>> > at
>>> >
>>>
>>> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
>>> >  at
>>> >
>>>
>>> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
>>> >
>>> > --
>>> > Regards,
>>> > Parimal
>>> > "Nothing is stationary,Change is a part of Life"
>>> >
>>> >
>>>
>>> --
>>> View this message in context:
>>>
>>> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>
>>>
>>>
>>
>> --
>> Regards,
>> Parimal
>> "Nothing is stationary,Change is a part of Life"
>>
>>


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

Re: Exception : getWriter() has already been called for this response

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

> Thank you Sergey,
> 
> Yes it seems working. now I haven't got any exception. but I am not
> verifying it. Actually following is my code in impl. I am sending xml file
> in Httpresponse stream. and I do not know if anything will add to response
> or overwrite by further interceptor process. I am using this service from
> browser. Please let me know what I am doing is right way of doing it. I am
> beginner to CXF REST.

this is a JAXWS code but you can do REST with it if you'd like to. You'd probably want to use Provider<Source> though, see

http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/restful_dispatch/

alternatively, please consider using JAXRS

http://cxf.apache.org/docs/jax-rs.html

hope it helps, Sergey

> 
> @WebService(endpointInterface =
> "com.traveltripper.stargazer.service.impl.HelloWorld")
> public class HelloWorldImpl implements HelloWorld
> {
> 
>    private static final Log log = LogFactory.getLog(HelloWorldImpl.class);
> 
>    @Resource
>    private WebServiceContext context;
> 
>    public void getHi()
>    {
>       try
>        {
>            MessageContext ctx = context.getMessageContext();
>            HttpServletRequest request =   (HttpServletRequest)
> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
>            HttpServletResponse response =  (HttpServletResponse)
> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>            response.setContentType("text/xml");
>            String reqParameter = request.getParameter("propertyCode");
>            String filePath = "c://"+ "DEL10965261change.xml";
> 
>            FileInputStream fis = new FileInputStream(new File(filePath));
> 
>            BufferedInputStream bis = new BufferedInputStream(fis);
>            ServletOutputStream sos = response.getOutputStream();
>            byte[] buffer = new byte[5000];
>            response.setHeader("Content-Length:",
> String.valueOf(bis.available()));
>            log.info("Content-length=" + bis.available());
>            while (true)
>            {
>                int bytesRead = bis.read(buffer, 0, buffer.length);
>                if (bytesRead < 0) break;
>                sos.write(buffer, 0, bytesRead);
>            }
>            fis.close();
>            sos.flush();
>            sos.close();
> 
>        }
>        catch (Exception e)
>        {
>            e.getMessage();
>        }
> 
>    }
> }
> 
> On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <sergey.beryozkin@iona.com
>> wrote:
> 
>>
>> Hi
>>
>> Please try writing to response.getOutputStream() and it should work. I
>> missed overriding
>> response.getWriter()  in the HttpResponse context implementation.
>>
>> let me know please if it works
>> Sergey
>>
>>
>> Parimal Dhinoja wrote:
>> >
>> > Hi,
>> >
>> > I have implemented RESTful CXF service with my spring project. in impl
>> > class, I have used MessageContext to retrieve HttpResponse and I am using
>> > response.getWriter() to set HttpResponse with my content.
>> >
>> > when I call this service from browser, I get the response what I have set
>> > in
>> > impl, but on tomcat console, I am getting following exception. Please
>> > help.
>> > this is the last piece of work I have left to finish my task.
>> >
>> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
>> > doIntercept
>> > WARNING: Interceptor has thrown exception, unwinding now
>> > java.lang.IllegalStateException: getWriter() has already been called for
>> > this response
>> >  at
>> > org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
>> > at
>> >
>> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
>> >  at
>> >
>> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
>> > at
>> >
>> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
>> >  at
>> >
>> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
>> > at
>> >
>> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
>> >  at
>> >
>> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
>> >
>> > --
>> > Regards,
>> > Parimal
>> > "Nothing is stationary,Change is a part of Life"
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
>>
> 
> 
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
>

Re: Exception : getWriter() has already been called for this response

Posted by Parimal Dhinoja <pd...@gmail.com>.
Thank you Sergey,

Yes it seems working. now I haven't got any exception. but I am not
verifying it. Actually following is my code in impl. I am sending xml file
in Httpresponse stream. and I do not know if anything will add to response
or overwrite by further interceptor process. I am using this service from
browser. Please let me know what I am doing is right way of doing it. I am
beginner to CXF REST.

@WebService(endpointInterface =
"com.traveltripper.stargazer.service.impl.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{

    private static final Log log = LogFactory.getLog(HelloWorldImpl.class);

    @Resource
    private WebServiceContext context;

    public void getHi()
    {
       try
        {
            MessageContext ctx = context.getMessageContext();
            HttpServletRequest request =   (HttpServletRequest)
ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
            HttpServletResponse response =  (HttpServletResponse)
ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
            response.setContentType("text/xml");
            String reqParameter = request.getParameter("propertyCode");
            String filePath = "c://"+ "DEL10965261change.xml";

            FileInputStream fis = new FileInputStream(new File(filePath));

            BufferedInputStream bis = new BufferedInputStream(fis);
            ServletOutputStream sos = response.getOutputStream();
            byte[] buffer = new byte[5000];
            response.setHeader("Content-Length:",
String.valueOf(bis.available()));
            log.info("Content-length=" + bis.available());
            while (true)
            {
                int bytesRead = bis.read(buffer, 0, buffer.length);
                if (bytesRead < 0) break;
                sos.write(buffer, 0, bytesRead);
            }
            fis.close();
            sos.flush();
            sos.close();

        }
        catch (Exception e)
        {
            e.getMessage();
        }

    }
}

On Mon, Nov 16, 2009 at 3:40 PM, Sergey Beryozkin <sergey.beryozkin@iona.com
> wrote:

>
> Hi
>
> Please try writing to response.getOutputStream() and it should work. I
> missed overriding
> response.getWriter()  in the HttpResponse context implementation.
>
> let me know please if it works
> Sergey
>
>
> Parimal Dhinoja wrote:
> >
> > Hi,
> >
> > I have implemented RESTful CXF service with my spring project. in impl
> > class, I have used MessageContext to retrieve HttpResponse and I am using
> > response.getWriter() to set HttpResponse with my content.
> >
> > when I call this service from browser, I get the response what I have set
> > in
> > impl, but on tomcat console, I am getting following exception. Please
> > help.
> > this is the last piece of work I have left to finish my task.
> >
> > 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
> > doIntercept
> > WARNING: Interceptor has thrown exception, unwinding now
> > java.lang.IllegalStateException: getWriter() has already been called for
> > this response
> >  at
> > org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
> > at
> >
> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
> >  at
> >
> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
> > at
> >
> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
> >  at
> >
> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
> > at
> >
> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
> >  at
> >
> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
> >
> > --
> > Regards,
> > Parimal
> > "Nothing is stationary,Change is a part of Life"
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>


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

Re: Exception : getWriter() has already been called for this response

Posted by Sergey Beryozkin <se...@iona.com>.
Hi

Please try writing to response.getOutputStream() and it should work. I
missed overriding 
response.getWriter()  in the HttpResponse context implementation.

let me know please if it works
Sergey


Parimal Dhinoja wrote:
> 
> Hi,
> 
> I have implemented RESTful CXF service with my spring project. in impl
> class, I have used MessageContext to retrieve HttpResponse and I am using
> response.getWriter() to set HttpResponse with my content.
> 
> when I call this service from browser, I get the response what I have set
> in
> impl, but on tomcat console, I am getting following exception. Please
> help.
> this is the last piece of work I have left to finish my task.
> 
> 16-Nov-2009 13:47:31 org.apache.cxf.phase.PhaseInterceptorChain
> doIntercept
> WARNING: Interceptor has thrown exception, unwinding now
> java.lang.IllegalStateException: getWriter() has already been called for
> this response
>  at
> org.apache.catalina.connector.Response.getOutputStream(Response.java:579)
> at
> org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
>  at
> javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:102)
> at
> org.apache.cxf.transport.http.AbstractHTTPDestination.flushHeaders(AbstractHTTPDestination.java:482)
>  at
> org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream.onFirstWrite(AbstractHTTPDestination.java:546)
> at
> org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:61)
>  at
> com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter.write(UTF8OutputStreamWriter.java:94)
> 
> -- 
> Regards,
> Parimal
> "Nothing is stationary,Change is a part of Life"
> 
> 

-- 
View this message in context: http://old.nabble.com/Exception-%3A-getWriter%28%29-has-already-been-called-for-this-response-tp26378624p26379082.html
Sent from the cxf-user mailing list archive at Nabble.com.