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