users@jersey.java.net

[Jersey] Can't override XML factory providers

From: Alejandro Pulver <alejandro.pulver_at_mulesoft.com>
Date: Fri, 30 Sep 2016 14:30:16 -0300

Hello,

I'm creating an application with a custom resource config, where I'm adding
a binder to setup XML factory providers. The binder is getting executed,
but when using jersey the default SaxParserFactoryInjectionProvider is
executed instead of the one I bound later. The version I'm using is 2.11.

The full existing initialization code is available at github:
https://github.com/mulesoft/mule/blob/mule-3.x/modules/jersey/src/main/java/org/mule/module/jersey/JerseyResourcesComponent.java

What I've added is here, and called "resourceConfig.register(new
JerseyXmlBinder())" (that is getting executed after the original
MessagingBinders's configure, where the factories are defined for the first
time).

public class JerseyXmlBinder extends AbstractBinder
{
    @Override
    protected void configure()
    {

bindFactory(DocumentBuilderFactoryProvider.class).to(DocumentBuilderFactory.class).in(PerThread.class);

bindFactory(SaxParserFactoryProvider.class).to(SAXParserFactory.class).in(PerThread.class);

bindFactory(XmlInputFactoryProvider.class).to(XMLInputFactory.class).in(PerThread.class);
    }
}

public class SaxParserFactoryProvider implements Factory<SAXParserFactory>
{

    @Inject
    public SaxParserFactoryProvider(final Configuration config)
    {
    }

    @Override
    @PerThread
    public SAXParserFactory provide()
    {
        SAXParserFactory factory = SAXParserFactory.newInstance();

        factory.setNamespaceAware(true);
        factory = new SecureSaxParserFactory(factory);

        return factory;
    }

    @Override
    public void dispose(SAXParserFactory instance)
    {
        //not used
    }
}

Am I doing something wrong?

Thanks,
Alejandro