001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.ksb.messaging.serviceexporters; 017 018 import java.util.List; 019 020 import org.apache.cxf.Bus; 021 import org.apache.cxf.binding.BindingFactoryManager; 022 import org.apache.cxf.endpoint.ServerRegistry; 023 import org.apache.cxf.interceptor.LoggingInInterceptor; 024 import org.apache.cxf.interceptor.LoggingOutInterceptor; 025 import org.apache.cxf.jaxrs.JAXRSBindingFactory; 026 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; 027 import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; 028 import org.apache.log4j.Logger; 029 import org.kuali.rice.core.api.exception.RiceRuntimeException; 030 import org.kuali.rice.ksb.api.bus.ServiceDefinition; 031 import org.kuali.rice.ksb.api.bus.support.RestServiceDefinition; 032 033 034 /** 035 * ServiceExporter for RESTful services. This class handles the service binding and exposure via CXF. 036 * @author Kuali Rice Team (rice.collab@kuali.org) 037 */ 038 public class RESTServiceExporter extends AbstractWebServiceExporter implements ServiceExporter { 039 040 private static final Logger LOG = Logger.getLogger(RESTServiceExporter.class); 041 042 public RESTServiceExporter(RestServiceDefinition serviceDefinition, Bus cxfBus, ServerRegistry cxfServerRegistry) { 043 super(serviceDefinition, cxfBus, cxfServerRegistry); 044 } 045 046 /** 047 * This publishes the cxf service onto the cxf bus. 048 * 049 * @param serviceImpl 050 * @throws Exception 051 */ 052 @Override 053 public void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) { 054 RestServiceDefinition restServiceDef = (RestServiceDefinition)serviceDefinition; 055 056 LOG.info("Creating JAXRSService " + restServiceDef.getServiceName()); 057 JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean(); 058 svrFactory.setBus(getCXFBus()); 059 060 List<Object> resources = restServiceDef.getResources(); 061 if (resources != null && !resources.isEmpty()) { 062 svrFactory.setServiceBeans(resources); 063 } else { 064 try { 065 Class<?> resourceClass = this.getClass().getClassLoader().loadClass(restServiceDef.getResourceClass()); 066 svrFactory.setResourceClasses(resourceClass); 067 svrFactory.setResourceProvider(resourceClass, new SingletonResourceProvider(serviceImpl)); 068 } catch (ClassNotFoundException e) { 069 throw new RiceRuntimeException("Failed to publish the service because resource class could not be loaded: " + restServiceDef.getResourceClass(), e); 070 } 071 } 072 073 svrFactory.setServiceName(restServiceDef.getServiceName()); 074 svrFactory.setAddress(address); 075 svrFactory.setExtensionMappings(restServiceDef.getExtensionMappings()); 076 svrFactory.setLanguageMappings(restServiceDef.getLanguageMappings()); 077 078 List<Object> providers = restServiceDef.getProviders(); 079 if (providers != null) { 080 svrFactory.setProviders(providers); 081 } 082 083 BindingFactoryManager bindingFactoryManager = getCXFBus().getExtension(BindingFactoryManager.class); 084 JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory(); 085 bindingFactory.setBus(getCXFBus()); 086 bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory); 087 088 //Set logging interceptors 089 if (LOG.isDebugEnabled()) { 090 svrFactory.getInInterceptors().add(new LoggingInInterceptor()); 091 } 092 // svrFactory.getInInterceptors().add(new RESTConnector.VerifyingInInterceptor()); 093 if (LOG.isDebugEnabled()) { 094 svrFactory.getOutInterceptors().add(new LoggingOutInterceptor()); 095 } 096 // svrFactory.getOutInterceptors().add(new RESTConnector.SigningOutInterceptor()); 097 098 svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm()); 099 svrFactory.create(); 100 } 101 102 }