Skip to main content

How to set Woodstox output properties ?

1 reply [Last post]
phans33
Offline
Joined: 2013-05-07
Points: 0

Have a plain vanilla JAX-WS RI web service.
Everything text book and everything created via annotations.

I need to have my output (my response) filter out characters that are invalid. As it turns out Woodstox has a way of doing this because you can set at runtime which InvalidCharHandler you wish to use.

There's a posting on the topic here: http://www.cowtowncoder.com/blog/archives/2008/12/entry_106.html

However I have no idea how to do this in the context of a plain vanilla web service created through annotation. For example I don't have an XMLOutputFactory anywhere. In fact the whole marshalling / unmarshalling is all behind-the-scenes from my point of view. Can't even catch if there's an exception in that process.

So how can I set such a Woodstox property ? Seems like a pretty simple thing but I just cannot figure out how to do it.

Hope you can help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
phans33
Offline
Joined: 2013-05-07
Points: 0

I have found the answer myself. Perhaps there are other ways of doing this but this one works for me.

First you need create your own version of an XMLOutputFactory.

Here's what I do:

package package org.fantastic.ws;

public class MyXMLOutputFactory extends WstxOutputFactory {

    public MyXMLOutputFactory() {
        super();
        SetProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
                new InvalidCharHandler.ReplacingHandler(' '));
    }
}

The above XMLOutputFactory is derived from JAX-WS's default XMLOutputFactory (WstxOutputFactory) but it changes the P_OUTPUT_INVALID_CHAR_HANDLER property. Apart from this they are completely similar.

Then comes the next problem. Had to look into JDK code for this one: What determines which XMLOutputFactory that JAX-WS uses? Well, it turns out this can be overridden by the use of a System Property, namely javax.xml.stream.XMLOutputFactory. As you may have quessed there's an equivalent one for XMLInputFactory but in this posting we're only concerned with the XML output part. (note: I don't know if this trick is true for any JAX-WS implementation but it is at least true for the JAX-WS RI from Sun/Oracle).

I like best to set the System Property programmatically:

System.setProperty("javax.xml.stream.XMLOutputFactory", "org.fantastic.ws.MyXMLOutputFactory");  

but you can of course also do it from command line. I set this during web service startup, i.e. I have a ServletContextListener and inside the contextInitialized callback I set the property as above.

That is all. Works as expected for my part. You can use the same method to change other configuration properties for Woodstox and thereby change how JAX-WS's default XML encoder/decoder works.