Hi Sergey. Thanks again for the response. I stepped through the
JAXRSOutInterceptor in the debugger and found out that the content type
for the Response I had created was "octet/stream" instead of
"application/xml". Once I created the appropriate content type entry in
the MultivaluedMap returned by getMetadata() method, everything worked. 

While on that subject, is that the appropriate way of doing it? What I
ended up doing was something like the code snippet below. Apologies for
the bad formatting, I'm using Evolution to send the email...

// "error" is our error placeholder bean sent back as the entity
final Response newResponse = builder.entity(error.validate()).build();
newResponse.getMetadata().putAll(oldResponse.getMetadata());
final MultivaluedMap<String, Object> map = newResponse.getMetadata();

if (! map.containsKey(HttpHeaders.CONTENT_TYPE))
{
  final List<Object> contentTypes = new ArrayList<Object>();
  contentTypes.add("application/xml");
  map.put(HttpHeaders.CONTENT_TYPE, contentTypes);
}
else
{
  final List<Object> contentTypes = map.get(HttpHeaders.CONTENT_TYPE);

  if (! contentTypes.contains("application/xml"))
  {
    contentTypes.add("application/xml");
  }
}

A little clumsy maybe, but I was trying to preserve whatever content
types the original response had as well as making sure that the new
response declares "application/xml". Any suggestions on how to clean
this up and do it properly are appreciated. Anyway, after inserting this
code snippet, things work all the way back to the browser, and I see the
exception entity in XML form. Thanks,

/Henrik 

On Tue, 2009-11-03 at 18:25 -0500, Sergey Beryozkin wrote:
> Hi Henrik
> 
> I reckon looking at the wire representation would really help in
> figuring out what's going on. Also, can it be that say the XStream
> writer needs to have some kind of close() method being called ?  
> 
> Please send a wget/browser request through a tcp trace utility and let
> me know the details
> 
> Thanks, Sergey 
> 
> -----Original Message-----
> From: Henrik Martin [mailto:[email protected]] 
> Sent: 03 November 2009 22:03
> To: [email protected]
> Subject: Re: Would like to send custom XML back whenever a 400 or 500
> category error happens
> 
> Sergey, thanks for the response. I should have given you a little more
> detail. In my handleResponse() method, I do return a Response, which
> contains something similar to your suggested ExceptionInfo. It's just a
> Java bean containing some properties and some XStream annotations. When
> I run everything in the debugger, I can see that my entity (containing
> the exception information) gets handled by our MessageWriter, and proper
> XML gets generated (I'm printing it out inside the MessageWriter). This
> is what's puzzling me, I never see the XML after that. 
> 
> I don't get any exceptions being thrown anywhere, so I don't think it's
> a failure of any kind. Our MessageWriter is registered to handle any
> type thrown at it. It basically uses XStream to stream out the beans
> into XML. It seems to work fine. If I set the HTTP status code to 200
> and return the exact same exception data, it gets streamed out properly
> all the way back to the client. I'm curious to what the difference is in
> execution path when I'm setting the HTTP status to a 400 or 500? I've
> tried following along in the debugger, but there are so many levels of
> calls that it's a little overwhelming. Is there some sort of error
> handler in the CXF framework that takes different paths depending on the
> value of the HTTP status code? Thanks,
> 
> /Henrik
> 
> On Mon, 2009-11-02 at 17:11 -0500, Sergey Beryozkin wrote:
> > Hi Henrik
> > 
> >  
> > 
> > For some reasons I can only see your message in Archives, but not
> > Nabble.
> > 
> >  
> > 
> > >>The handleResponse() method in
> > >>my filter gets called, but I've found that if I return any kind of
> > >>error, i.e. a category 400 or 500 type error, the XML that I'm
> > returning
> > >>as the content doesn't get rendered in the browser
> >  
> > If one returns a custom Response from a filter, then its entity, if
> any,
> > will be 
> > serialized by the available writers.
> >  
> > So if you set a String instance as a custom entity and you happen to
> > have a custom message body writer which can
> > wrap Strings then the browser would show it as a plain text sequence.
> >  
> > Starting from CXF 2.2.4 it is possible to indicate that writers have
> to
> > be ignored. In meantime, the best way would be to return
> > an object like ExceptionInfo which will be serialized by the
> appropriate
> > XML writer.
> >  
> > Is it what might be happening in your case ?
> >  
> > If you're thinking of doing JAXRS only then filters should do well.  
> >  
> > Cheers, Sergey
> >  
> > 
> >    
> > 
> >  
> > 
> > 
> > 
> >   
> > 

Reply via email to