3

In my application, I have 2 bundles. Both are using cxf to create Restful server. In those bundles I load cxf via blueprint. I define cxf:bus on those bundles with the same id to expect that two bundle will share the same bus instance then I can configure a Authentication interceptor on one bundle and It will apply for the other bundle also. They looks like below.

Bundle 1:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
       xmlns:cxf="http://cxf.apache.org/blueprint/core"
       xsi:schemaLocation="
         http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
         http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
         http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
         ">
<bean id="authInterceptor" class="com.dasannetworks.vn.rest.impl.AuthInterceptorImpl"></bean>
<cxf:bus id="my-app-bus" name="tutorial">
   <cxf:inInterceptors>
        <ref component-id="authInterceptor"/>
    </cxf:inInterceptors>
</cxf:bus>


<bean id="Rest1ServiceImpl"class="com.dasannetworks.vn.rest.impl.Rest1ServiceImpl"></bean>

<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
    <property name="serializeAsArray" value="true"/>
    <property name="dropRootElement" value="true"/>
    <property name="supportUnwrapped" value="true"/>
</bean>

<jaxrs:server id="custom1Service" address="/rest1">
    <jaxrs:serviceBeans>
        <ref component-id="rest1ServiceImpl"/>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <ref component-id="jsonProvider"/>
    </jaxrs:providers>
</jaxrs:server>

Bundle 2:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
       xmlns:cxf="http://cxf.apache.org/blueprint/core"
       xsi:schemaLocation="
         http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
         http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
         http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
         ">

<cxf:bus id="my-app-bus" bus="tutorial"></cxf:bus>
<bean id="rest2Service" class="com.dasannetworks.vn.rest2.impl.Rest2ServiceImpl" />
<jaxrs:server id="custom2Service" address="/rest2">
    <jaxrs:serviceBeans>
        <ref component-id="rest2Service"/>
    </jaxrs:serviceBeans>

After installing and run: The outcome: all rests request to "/cxf/rest1/" will run into authInterceptor whereas all rests request to "cxf/rest2" are not.

Could anyone give me some advice about how to share the same cxf:bus on both bundles ? Thank you in advance.

user3444693
  • 464
  • 4
  • 7

1 Answers1

2

Since I cannot solve this problem with blueprint, I changed the way to fix it. Instead of blueprint, I initiated CXF Bus Rest Server with Activator.

Bundle 1 Activator: In bundle 1, I get the default bus and add authentication interceptor on it then register a Rest Server endpoint on it.

public void start(BundleContext bundleContext) throws Exception {
    Bus defaultBus = BusFactory.getDefaultBus();
    defaultBus.setId("my-app-bus");
    BusFactory.clearDefaultBusForAnyThread(defaultBus);
    System.out.println(this.getClass().getName());
    System.out.println(defaultBus.getId());
    defaultBus.getInInterceptors().clear();
    defaultBus.getInInterceptors().add(new AuthInterceptorImpl());

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(defaultBus);
    sf.setAddress("/tutorial");
    sf.setServiceClass(HelloRestServiceImpl.class);
    sf.create();
}

In bundle 2, Bundle 2 Activator I get the default bus, register and set that bus for Rest Server.

@Override
public void start(BundleContext bundleContext) throws Exception {
    Bus defaultBus = BusFactory.getDefaultBus();
    List<Interceptor<? extends Message>> inInterceptors = defaultBus.getInInterceptors();
    for(Interceptor interceptor: inInterceptors) {
        System.out.println( interceptor.getClass().getName());
    }

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(defaultBus);
    sf.setAddress("/Rest2");
    sf.setServiceClass(Rest2ServiceImpl.class);
    sf.create();
}

==> The result : both bundles are now use the same bus, and bundle 2 can run into authentication interceptor which I registered on bundle 1 also. !!!

user3444693
  • 464
  • 4
  • 7