View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * ServiceExporter for RESTful services.  This class handles the service binding and exposure via CXF.
35   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * This publishes the cxf service onto the cxf bus.
47  	 *
48  	 * @param serviceImpl
49  	 * @throws Exception
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  		//Set logging interceptors
88          if (LOG.isDebugEnabled()) {
89          	svrFactory.getInInterceptors().add(new LoggingInInterceptor());
90          }
91  //        svrFactory.getInInterceptors().add(new RESTConnector.VerifyingInInterceptor());
92          if (LOG.isDebugEnabled()) {
93          	svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
94          }
95  //		svrFactory.getOutInterceptors().add(new RESTConnector.SigningOutInterceptor());
96  
97          svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm());
98  		svrFactory.create();
99  	}
100 
101 }