Coverage Report - org.kuali.rice.ksb.messaging.serviceexporters.RESTServiceExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
RESTServiceExporter
0%
0/34
0%
0/10
4.5
 
 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  0
         private static final Logger LOG = Logger.getLogger(RESTServiceExporter.class);
 41  
 
 42  
         public RESTServiceExporter(RestServiceDefinition serviceDefinition, Bus cxfBus, ServerRegistry cxfServerRegistry) {
 43  0
                 super(serviceDefinition, cxfBus, cxfServerRegistry);
 44  0
         }
 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  0
                 RestServiceDefinition restServiceDef = (RestServiceDefinition)serviceDefinition;
 55  
 
 56  0
                 LOG.info("Creating JAXRSService " + restServiceDef.getServiceName());
 57  0
                 JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
 58  0
         svrFactory.setBus(getCXFBus());
 59  
 
 60  0
         List<Object> resources = restServiceDef.getResources();
 61  0
         if (resources != null && !resources.isEmpty()) {
 62  0
                 svrFactory.setServiceBeans(resources);
 63  
         } else {
 64  
                 try {
 65  0
                         Class<?> resourceClass = this.getClass().getClassLoader().loadClass(restServiceDef.getResourceClass());
 66  0
                         svrFactory.setResourceClasses(resourceClass);
 67  0
                         svrFactory.setResourceProvider(resourceClass, new SingletonResourceProvider(serviceImpl));
 68  0
                 } catch (ClassNotFoundException e) {
 69  0
                         throw new RiceRuntimeException("Failed to publish the service because resource class could not be loaded: " + restServiceDef.getResourceClass(), e);
 70  0
                 }
 71  
         }
 72  
 
 73  0
         svrFactory.setServiceName(restServiceDef.getServiceName());
 74  0
         svrFactory.setAddress(address);
 75  0
         svrFactory.setExtensionMappings(restServiceDef.getExtensionMappings());
 76  0
         svrFactory.setLanguageMappings(restServiceDef.getLanguageMappings());
 77  
 
 78  0
         List<Object> providers = restServiceDef.getProviders();
 79  0
         if (providers != null) {
 80  0
                 svrFactory.setProviders(providers);
 81  
         }
 82  
 
 83  0
         BindingFactoryManager bindingFactoryManager = getCXFBus().getExtension(BindingFactoryManager.class);
 84  0
         JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory();
 85  0
         bindingFactory.setBus(getCXFBus());
 86  0
         bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory);
 87  
 
 88  
                 //Set logging interceptors
 89  0
         if (LOG.isDebugEnabled()) {
 90  0
                 svrFactory.getInInterceptors().add(new LoggingInInterceptor());
 91  
         }
 92  
 //        svrFactory.getInInterceptors().add(new RESTConnector.VerifyingInInterceptor());
 93  0
         if (LOG.isDebugEnabled()) {
 94  0
                 svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
 95  
         }
 96  
 //                svrFactory.getOutInterceptors().add(new RESTConnector.SigningOutInterceptor());
 97  
 
 98  0
         svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm());
 99  0
                 svrFactory.create();
 100  0
         }
 101  
 
 102  
 }