You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Anthony Schexnaildre <ap...@gmail.com> on 2008/06/27 21:51:31 UTC

Modify Response XML

Hello group,

I very much want to diddle the response xml on it's way out to add  
attributes and other bits. Unfortunately I seem to be at a loss of how  
to accomplish this even though it seems like it should be very easy. I  
am using CXF 2.1.1, JAXRS and JAXB if that is of interest.

I have written an Interceptor to test with modeled after what I think  
is happening in the LoggingOutInterceptor, placed it in all different  
locations on the interceptor chain to see what happens in the message  
and I am not finding too much.

If I iterate through message.getInterceptorChain() I get:

org.apache.cxf.interceptor.MessageSenderInterceptor
org.apache.cxf.interceptor.LoggingOutInterceptor
org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor
com.mypackage.cxf.ContentsDumpInterceptor
org.apache.cxf.interceptor.MessageSenderInterceptor 
$MessageSenderEndingInterceptor

If I iterate through message.getContentFormats() I get:

OutputStream
List

The list is filled with a ResponseImpl as that is what I am returning  
from the method invocation.

If I try to print the OutputStream like the LoggingOutInterceptor does  
nothing happens:

public void handleMessage(Message message) throws Fault { 	
        	StringBuilder s = new StringBuilder();
        	try {
        		CachedOutputStream cos = new  
CacheAndWriteOutputStream(message.getContent(OutputStream.class));
		cos.writeCacheTo(s);
		LOG.info("~~~~~~~~My Message: " + s.toString() );
	} catch (IOException e) {
		e.printStackTrace();
	}	
}

Where does the XML live? How to I get at it? Please point me in the  
right direction. This must be very simple.

-Anthony



Re: Modify Response XML

Posted by Anthony Schexnaildre <ap...@gmail.com>.
Never mind, got it. I just write the bytes to the OutputStream that is  
passed into the method. duh.

Thank you,

Anthony

On Jun 27, 2008, at 5:34 PM, Gopal Patwa wrote:

>
> You might want to see JAXBElementProvider class you can find marshal  
> and
> unmarshal jaxb methods
>
> And you can easily override the default JAXB provider by copying it  
> make the
> changes and to register user  defined provider.
>
> Here is the example from my app spring context file
>
>    <jaxrs:server id="service" address="/">
>        <jaxrs:serviceBeans>
>            <bean  
> class="com.liquid.fulfiller.sample.server.CustomerService"
> />
>        </jaxrs:serviceBeans>
>        <jaxrs:entityProviders>
>            <bean  
> class="com.liquid.fulfiller.jaxrs.JAXBElementProvider"/>
>        </jaxrs:entityProviders>
>    </jaxrs:server>
>
>
> Hope this help
>
> -Gopal
>
>
> Anthony Schexnaildre wrote:
>>
>> Hello group,
>>
>> I very much want to diddle the response xml on it's way out to add
>> attributes and other bits. Unfortunately I seem to be at a loss of  
>> how
>> to accomplish this even though it seems like it should be very  
>> easy. I
>> am using CXF 2.1.1, JAXRS and JAXB if that is of interest.
>>
>> I have written an Interceptor to test with modeled after what I think
>> is happening in the LoggingOutInterceptor, placed it in all different
>> locations on the interceptor chain to see what happens in the message
>> and I am not finding too much.
>>
>> If I iterate through message.getInterceptorChain() I get:
>>
>> org.apache.cxf.interceptor.MessageSenderInterceptor
>> org.apache.cxf.interceptor.LoggingOutInterceptor
>> org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor
>> com.mypackage.cxf.ContentsDumpInterceptor
>> org.apache.cxf.interceptor.MessageSenderInterceptor
>> $MessageSenderEndingInterceptor
>>
>> If I iterate through message.getContentFormats() I get:
>>
>> OutputStream
>> List
>>
>> The list is filled with a ResponseImpl as that is what I am returning
>> from the method invocation.
>>
>> If I try to print the OutputStream like the LoggingOutInterceptor  
>> does
>> nothing happens:
>>
>> public void handleMessage(Message message) throws Fault { 	
>>        	StringBuilder s = new StringBuilder();
>>        	try {
>>        		CachedOutputStream cos = new
>> CacheAndWriteOutputStream(message.getContent(OutputStream.class));
>> 		cos.writeCacheTo(s);
>> 		LOG.info("~~~~~~~~My Message: " + s.toString() );
>> 	} catch (IOException e) {
>> 		e.printStackTrace();
>> 	}	
>> }
>>
>> Where does the XML live? How to I get at it? Please point me in the
>> right direction. This must be very simple.
>>
>> -Anthony
>>
>>
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/Modify-Response-XML-tp18162638p18164257.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


Re: Modify Response XML

Posted by Anthony Schexnaildre <ap...@gmail.com>.
Gopal,

Thanks for the pointer. It seems I can marshall the xml into a  
friendly format like in the code below but I am kind of lost as how I  
am supposed to work with it now. I have been trying to trace through  
the source code to see what happens not getting very far. I was  
assuming that marshaller.marshal(entity, doc); would put a Node in the  
message so I could get at it with message.getContent(...) from within  
an Interceptor but that does not seem the case as I still only have an  
OutputStream and a List<Response> when I call  
message.getContentFormats() (I have tried moving my Interceptor to  
different phases also to see if the Node appears in a different phase  
than expected). If I am supposed to play with the XML in the message  
body writer where do I put the Node when I am done so the execution  
chain can continue as normal? Some more hints would be greatly  
appreciated.

public void writeTo(Template entity, MediaType media,
		MultivaluedMap<String, Object> map, OutputStream os)
		throws IOException {
     try {
     	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setNamespaceAware(true);
         DocumentBuilder db = dbf.newDocumentBuilder();
         Document doc = db.newDocument();

         JAXBContext context = getJAXBContext(entity.getClass());
         Marshaller marshaller = context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
         marshaller.marshal(entity, doc);

     } catch (JAXBException e) {
         //TODO: better exception handling
         e.printStackTrace();
     } catch (ParserConfigurationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

Best Regards,

- Anthony


On Jun 27, 2008, at 5:34 PM, Gopal Patwa wrote:

>
> You might want to see JAXBElementProvider class you can find marshal  
> and
> unmarshal jaxb methods
>
> And you can easily override the default JAXB provider by copying it  
> make the
> changes and to register user  defined provider.
>
> Here is the example from my app spring context file
>
>    <jaxrs:server id="service" address="/">
>        <jaxrs:serviceBeans>
>            <bean  
> class="com.liquid.fulfiller.sample.server.CustomerService"
> />
>        </jaxrs:serviceBeans>
>        <jaxrs:entityProviders>
>            <bean  
> class="com.liquid.fulfiller.jaxrs.JAXBElementProvider"/>
>        </jaxrs:entityProviders>
>    </jaxrs:server>
>
>
> Hope this help
>
> -Gopal
>
>
> Anthony Schexnaildre wrote:
>>
>> Hello group,
>>
>> I very much want to diddle the response xml on it's way out to add
>> attributes and other bits. Unfortunately I seem to be at a loss of  
>> how
>> to accomplish this even though it seems like it should be very  
>> easy. I
>> am using CXF 2.1.1, JAXRS and JAXB if that is of interest.
>>
>> I have written an Interceptor to test with modeled after what I think
>> is happening in the LoggingOutInterceptor, placed it in all different
>> locations on the interceptor chain to see what happens in the message
>> and I am not finding too much.
>>
>> If I iterate through message.getInterceptorChain() I get:
>>
>> org.apache.cxf.interceptor.MessageSenderInterceptor
>> org.apache.cxf.interceptor.LoggingOutInterceptor
>> org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor
>> com.mypackage.cxf.ContentsDumpInterceptor
>> org.apache.cxf.interceptor.MessageSenderInterceptor
>> $MessageSenderEndingInterceptor
>>
>> If I iterate through message.getContentFormats() I get:
>>
>> OutputStream
>> List
>>
>> The list is filled with a ResponseImpl as that is what I am returning
>> from the method invocation.
>>
>> If I try to print the OutputStream like the LoggingOutInterceptor  
>> does
>> nothing happens:
>>
>> public void handleMessage(Message message) throws Fault { 	
>>        	StringBuilder s = new StringBuilder();
>>        	try {
>>        		CachedOutputStream cos = new
>> CacheAndWriteOutputStream(message.getContent(OutputStream.class));
>> 		cos.writeCacheTo(s);
>> 		LOG.info("~~~~~~~~My Message: " + s.toString() );
>> 	} catch (IOException e) {
>> 		e.printStackTrace();
>> 	}	
>> }
>>
>> Where does the XML live? How to I get at it? Please point me in the
>> right direction. This must be very simple.
>>
>> -Anthony
>>
>>
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/Modify-Response-XML-tp18162638p18164257.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


Re: Modify Response XML

Posted by Gopal Patwa <go...@gmail.com>.
You might want to see JAXBElementProvider class you can find marshal and
unmarshal jaxb methods

And you can easily override the default JAXB provider by copying it make the
changes and to register user  defined provider.

Here is the example from my app spring context file

    <jaxrs:server id="service" address="/">
        <jaxrs:serviceBeans>
            <bean class="com.liquid.fulfiller.sample.server.CustomerService"
/>
        </jaxrs:serviceBeans>
        <jaxrs:entityProviders>
            <bean class="com.liquid.fulfiller.jaxrs.JAXBElementProvider"/>
        </jaxrs:entityProviders>        
    </jaxrs:server>
 

Hope this help

-Gopal


Anthony Schexnaildre wrote:
> 
> Hello group,
> 
> I very much want to diddle the response xml on it's way out to add  
> attributes and other bits. Unfortunately I seem to be at a loss of how  
> to accomplish this even though it seems like it should be very easy. I  
> am using CXF 2.1.1, JAXRS and JAXB if that is of interest.
> 
> I have written an Interceptor to test with modeled after what I think  
> is happening in the LoggingOutInterceptor, placed it in all different  
> locations on the interceptor chain to see what happens in the message  
> and I am not finding too much.
> 
> If I iterate through message.getInterceptorChain() I get:
> 
> org.apache.cxf.interceptor.MessageSenderInterceptor
> org.apache.cxf.interceptor.LoggingOutInterceptor
> org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor
> com.mypackage.cxf.ContentsDumpInterceptor
> org.apache.cxf.interceptor.MessageSenderInterceptor 
> $MessageSenderEndingInterceptor
> 
> If I iterate through message.getContentFormats() I get:
> 
> OutputStream
> List
> 
> The list is filled with a ResponseImpl as that is what I am returning  
> from the method invocation.
> 
> If I try to print the OutputStream like the LoggingOutInterceptor does  
> nothing happens:
> 
> public void handleMessage(Message message) throws Fault { 	
>         	StringBuilder s = new StringBuilder();
>         	try {
>         		CachedOutputStream cos = new  
> CacheAndWriteOutputStream(message.getContent(OutputStream.class));
> 		cos.writeCacheTo(s);
> 		LOG.info("~~~~~~~~My Message: " + s.toString() );
> 	} catch (IOException e) {
> 		e.printStackTrace();
> 	}	
> }
> 
> Where does the XML live? How to I get at it? Please point me in the  
> right direction. This must be very simple.
> 
> -Anthony
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Modify-Response-XML-tp18162638p18164257.html
Sent from the cxf-user mailing list archive at Nabble.com.