1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging.serviceexporters;
17
18 import java.util.List;
19
20 import org.apache.cxf.Bus;
21 import org.apache.cxf.binding.BindingFactoryManager;
22 import org.apache.cxf.interceptor.LoggingInInterceptor;
23 import org.apache.cxf.interceptor.LoggingOutInterceptor;
24 import org.apache.cxf.jaxrs.JAXRSBindingFactory;
25 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
26 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
27 import org.apache.log4j.Logger;
28 import org.kuali.rice.core.api.exception.RiceRuntimeException;
29 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
30 import org.kuali.rice.ksb.api.bus.support.RestServiceDefinition;
31
32
33
34
35
36
37 public class RESTServiceExporter extends AbstractWebServiceExporter implements ServiceExporter {
38
39 private static final Logger LOG = Logger.getLogger(RESTServiceExporter.class);
40
41 public RESTServiceExporter(RestServiceDefinition serviceDefinition, Bus cxfBus) {
42 super(serviceDefinition, cxfBus);
43 }
44
45
46
47
48
49
50
51 @Override
52 public void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) {
53 RestServiceDefinition restServiceDef = (RestServiceDefinition)serviceDefinition;
54
55 LOG.info("Creating JAXRSService " + restServiceDef.getServiceName());
56 JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
57 svrFactory.setBus(getCXFBus());
58
59 List<Object> resources = restServiceDef.getResources();
60 if (resources != null && !resources.isEmpty()) {
61 svrFactory.setServiceBeans(resources);
62 } else {
63 try {
64 Class<?> resourceClass = this.getClass().getClassLoader().loadClass(restServiceDef.getResourceClass());
65 svrFactory.setResourceClasses(resourceClass);
66 svrFactory.setResourceProvider(resourceClass, new SingletonResourceProvider(serviceImpl));
67 } catch (ClassNotFoundException e) {
68 throw new RiceRuntimeException("Failed to publish the service because resource class could not be loaded: " + restServiceDef.getResourceClass(), e);
69 }
70 }
71
72 svrFactory.setServiceName(restServiceDef.getServiceName());
73 svrFactory.setAddress(address);
74 svrFactory.setExtensionMappings(restServiceDef.getExtensionMappings());
75 svrFactory.setLanguageMappings(restServiceDef.getLanguageMappings());
76
77 List<Object> providers = restServiceDef.getProviders();
78 if (providers != null) {
79 svrFactory.setProviders(providers);
80 }
81
82 BindingFactoryManager bindingFactoryManager = getCXFBus().getExtension(BindingFactoryManager.class);
83 JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory();
84 bindingFactory.setBus(getCXFBus());
85 bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory);
86
87
88 if (LOG.isDebugEnabled()) {
89 svrFactory.getInInterceptors().add(new LoggingInInterceptor());
90 }
91
92 if (LOG.isDebugEnabled()) {
93 svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
94 }
95
96
97 svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm());
98 svrFactory.create();
99 }
100
101 }