View Javadoc

1   /**
2    * Copyright 2005-2011 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.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   * ServiceExporter for RESTful services.  This class handles the service binding and exposure via CXF.
36   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * This publishes the cxf service onto the cxf bus.
48  	 *
49  	 * @param serviceImpl
50  	 * @throws Exception
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  		//Set logging interceptors
89          if (LOG.isDebugEnabled()) {
90          	svrFactory.getInInterceptors().add(new LoggingInInterceptor());
91          }
92  //        svrFactory.getInInterceptors().add(new RESTConnector.VerifyingInInterceptor());
93          if (LOG.isDebugEnabled()) {
94          	svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
95          }
96  //		svrFactory.getOutInterceptors().add(new RESTConnector.SigningOutInterceptor());
97  
98          svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm());
99  		svrFactory.create();
100 	}
101 
102 }