You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by santhosh <sa...@gmail.com> on 2011/12/14 15:00:07 UTC

How to get the message content as a string from RequestHandler?

Hi,

I am new to Apache CXF and RESTFul web services,
I have written an Interceptor for transaction logging by implementing the
below interface and it works fine,

org.apache.cxf.jaxrs.ext.RequestHandler .

Issue is,
The request contains xml data and  I have to get that xml data as a string.

I have implemented the org.apache.cxf.jaxrs.ext.RequestHandler interface
like below, but I am not able get the content. Please help me if there is
any way to get the xml as string from message object. 

public Response handleRequest( Message message, ClassResourceInfo
classResouceInfo )
{

    Set<Class&lt;?>> contentFormats = message.getContentFormats();

    if( contentFormats != null )
    {
	    for( Class<?> contentFormat : contentFormats )
	    {
	       Object contentMessage = message.getContent(contentFormat);

               if( contentMessage.toString().startsWith("<?xml") )
	        {
		 String content = contentMessage.toString();
	        }
           }
    }
}


Thanks,
Santhosh




--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-get-the-message-content-as-a-string-from-RequestHandler-tp5074552p5074552.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to get the message content as a string from RequestHandler?

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 14/12/11 14:00, santhosh wrote:
> Hi,
>
> I am new to Apache CXF and RESTFul web services,
> I have written an Interceptor for transaction logging by implementing the
> below interface and it works fine,
>
> org.apache.cxf.jaxrs.ext.RequestHandler .
>
> Issue is,
> The request contains xml data and  I have to get that xml data as a string.
>
> I have implemented the org.apache.cxf.jaxrs.ext.RequestHandler interface
> like below, but I am not able get the content. Please help me if there is
> any way to get the xml as string from message object.
>
> public Response handleRequest( Message message, ClassResourceInfo
> classResouceInfo )
> {
>
>      Set<Class&lt;?>>  contentFormats = message.getContentFormats();
>
>      if( contentFormats != null )
>      {
> 	    for( Class<?>  contentFormat : contentFormats )
> 	    {
> 	       Object contentMessage = message.getContent(contentFormat);
>
>                 if( contentMessage.toString().startsWith("<?xml") )
> 	        {
> 		 String content = contentMessage.toString();
> 	        }
>             }
>      }
> }
>
>

Do you need it for the logging purposes ?
One option is to use LoggingInInterceptor;
Another one is to do message.getContent(InputStream.class), use CXF 
IOUtils to read into String and then set a new InputStream on the 
message for the application code to get the data as well

Sergey

> Thanks,
> Santhosh
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-get-the-message-content-as-a-string-from-RequestHandler-tp5074552p5074552.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com