001/** 002 * Copyright 2005-2017 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 */ 016package org.kuali.rice.ksb.messaging.serviceexporters; 017 018import java.util.List; 019 020import org.apache.cxf.Bus; 021import org.apache.cxf.binding.BindingFactoryManager; 022import org.apache.cxf.interceptor.LoggingInInterceptor; 023import org.apache.cxf.interceptor.LoggingOutInterceptor; 024import org.apache.cxf.jaxrs.JAXRSBindingFactory; 025import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; 026import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; 027import org.apache.log4j.Logger; 028import org.kuali.rice.core.api.exception.RiceRuntimeException; 029import org.kuali.rice.ksb.api.bus.ServiceDefinition; 030import org.kuali.rice.ksb.api.bus.support.RestServiceDefinition; 031 032 033/** 034 * ServiceExporter for RESTful services. This class handles the service binding and exposure via CXF. 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 */ 037public class RESTServiceExporter extends AbstractWebServiceExporter implements ServiceExporter { 038 039 private static final Logger LOG = Logger.getLogger(RESTServiceExporter.class); 040 041 public RESTServiceExporter(RestServiceDefinition serviceDefinition, Bus cxfBus) { 042 super(serviceDefinition, cxfBus); 043 } 044 045 /** 046 * This publishes the cxf service onto the cxf bus. 047 * 048 * @param serviceImpl 049 * @throws Exception 050 */ 051 @Override 052 public void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) { 053 RestServiceDefinition restServiceDef = (RestServiceDefinition)serviceDefinition; 054 055 LOG.info("Creating JAXRSService " + restServiceDef.getServiceName()); 056 JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean(); 057 svrFactory.setBus(getCXFBus()); 058 059 List<Object> resources = restServiceDef.getResources(); 060 if (resources != null && !resources.isEmpty()) { 061 svrFactory.setServiceBeans(resources); 062 } else { 063 try { 064 Class<?> resourceClass = this.getClass().getClassLoader().loadClass(restServiceDef.getResourceClass()); 065 svrFactory.setResourceClasses(resourceClass); 066 svrFactory.setResourceProvider(resourceClass, new SingletonResourceProvider(serviceImpl)); 067 } catch (ClassNotFoundException e) { 068 throw new RiceRuntimeException("Failed to publish the service because resource class could not be loaded: " + restServiceDef.getResourceClass(), e); 069 } 070 } 071 072 svrFactory.setServiceName(restServiceDef.getServiceName()); 073 svrFactory.setAddress(address); 074 svrFactory.setExtensionMappings(restServiceDef.getExtensionMappings()); 075 svrFactory.setLanguageMappings(restServiceDef.getLanguageMappings()); 076 077 List<Object> providers = restServiceDef.getProviders(); 078 if (providers != null) { 079 svrFactory.setProviders(providers); 080 } 081 082 BindingFactoryManager bindingFactoryManager = getCXFBus().getExtension(BindingFactoryManager.class); 083 JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory(); 084 bindingFactory.setBus(getCXFBus()); 085 bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory); 086 087 //Set logging interceptors 088 if (LOG.isDebugEnabled()) { 089 svrFactory.getInInterceptors().add(new LoggingInInterceptor()); 090 } 091// svrFactory.getInInterceptors().add(new RESTConnector.VerifyingInInterceptor()); 092 if (LOG.isDebugEnabled()) { 093 svrFactory.getOutInterceptors().add(new LoggingOutInterceptor()); 094 } 095// svrFactory.getOutInterceptors().add(new RESTConnector.SigningOutInterceptor()); 096 097 svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm()); 098 svrFactory.create(); 099 } 100 101}