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