Coverage Report - org.kuali.rice.ksb.messaging.servlet.KSBDispatcherServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
KSBDispatcherServlet
0%
0/49
0%
0/20
3.286
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.ksb.messaging.servlet;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.servlet.ServletException;
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 import javax.xml.namespace.QName;
 26  
 
 27  
 import org.apache.cxf.Bus;
 28  
 import org.apache.cxf.interceptor.Interceptor;
 29  
 import org.apache.cxf.transport.servlet.ServletController;
 30  
 import org.apache.cxf.transport.servlet.ServletTransportFactory;
 31  
 import org.apache.log4j.Logger;
 32  
 import org.kuali.rice.core.config.ConfigContext;
 33  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 34  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 35  
 import org.kuali.rice.ksb.messaging.SOAPServiceDefinition;
 36  
 import org.kuali.rice.ksb.messaging.ServerSideRemotedServiceHolder;
 37  
 import org.kuali.rice.ksb.messaging.ServiceInfo;
 38  
 import org.kuali.rice.ksb.security.SignatureSigningResponseWrapper;
 39  
 import org.kuali.rice.ksb.security.SignatureVerifyingRequestWrapper;
 40  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 41  
 import org.springframework.beans.BeansException;
 42  
 import org.springframework.web.HttpRequestHandler;
 43  
 import org.springframework.web.context.WebApplicationContext;
 44  
 import org.springframework.web.servlet.DispatcherServlet;
 45  
 import org.springframework.web.servlet.HandlerAdapter;
 46  
 import org.springframework.web.servlet.HandlerExecutionChain;
 47  
 import org.springframework.web.servlet.ModelAndView;
 48  
 import org.springframework.web.servlet.mvc.Controller;
 49  
 import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
 50  
 import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
 51  
 
 52  
 
 53  
 /**
 54  
  * A {@link DispatcherServlet} which dispatches incoming requests to the appropriate
 55  
  * service endpoint.
 56  
  *
 57  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 58  
  */
 59  0
 public class KSBDispatcherServlet extends DispatcherServlet {
 60  
 
 61  0
         private static final Logger LOG = Logger.getLogger(KSBDispatcherServlet.class);
 62  
 
 63  
         private static final long serialVersionUID = 6790121225857950019L;
 64  
         private KSBHttpInvokerHandler httpInvokerHandler;
 65  
         private ServletController cxfServletController;
 66  
 
 67  
         /**
 68  
          * Instantiate the WebApplicationContext for this servlet, either a default
 69  
          * XmlWebApplicationContext or a custom context class if set. This implementation
 70  
          * expects custom contexts to implement ConfigurableWebApplicationContext.
 71  
          * Can be overridden in subclasses.
 72  
          * @param parent the parent ApplicationContext to use, or <code>null</code> if none
 73  
          * @return the WebApplicationContext for this servlet
 74  
          * @throws BeansException if the context couldn't be initialized
 75  
          * @see #setContextClass
 76  
          * @see org.springframework.web.context.support.XmlWebApplicationContext
 77  
          */
 78  
         protected WebApplicationContext initWebApplicationContext() throws BeansException {
 79  0
                 return null;//we want to start spring all by ourselves
 80  
         }
 81  
 
 82  
         protected void initFrameworkServlet() throws ServletException, BeansException {
 83  0
                 this.httpInvokerHandler = new KSBHttpInvokerHandler();
 84  
                 
 85  0
         Bus bus = KSBServiceLocator.getCXFBus();
 86  
 
 87  0
         List<Interceptor> inInterceptors = KSBServiceLocator.getInInterceptors();
 88  0
         if(inInterceptors != null) {
 89  0
                 List<Interceptor> busInInterceptors = bus.getInInterceptors();
 90  0
                 busInInterceptors.addAll(inInterceptors);
 91  
         }
 92  
        
 93  0
         List<Interceptor> outInterceptors = KSBServiceLocator.getOutInterceptors();
 94  0
         if(outInterceptors != null) {
 95  0
                 List<Interceptor> busOutInterceptors = bus.getOutInterceptors();
 96  0
                 busOutInterceptors.addAll(outInterceptors);
 97  
         }
 98  
         
 99  0
                 ServletTransportFactory servletTransportFactory = KSBServiceLocator.getCXFServletTransportFactory();
 100  
                                 
 101  0
                 this.cxfServletController = new ServletController(servletTransportFactory, this.getServletConfig(), this.getServletContext(), bus);
 102  
                 
 103  0
                 if (!ConfigContext.getCurrentContextConfig().getDevMode()) {
 104  
                     // disable handling of URLs ending in /services which display CXF generated service lists
 105  0
                     this.cxfServletController.setHideServiceList(true);
 106  
                 }
 107  
                 
 108  0
                 this.setPublishEvents(false);
 109  0
         }
 110  
 
 111  
         protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
 112  0
                 if (handler instanceof HttpRequestHandler) {
 113  0
                         return new HttpRequestHandlerAdapter();
 114  0
                 } else if (handler instanceof Controller) {
 115  0
                         Object unwrappedHandler = ClassLoaderUtils.unwrapFromProxy(handler);
 116  0
                         if (unwrappedHandler instanceof CXFServletControllerAdapter) {
 117  0
                                 ((CXFServletControllerAdapter)unwrappedHandler).setController(cxfServletController);
 118  
                         }                        
 119  0
                         return new SimpleControllerHandlerAdapter();
 120  
                 }
 121  0
                 throw new RiceRuntimeException("handler of type " + handler.getClass().getName() + " is not known and can't be used by " + KSBDispatcherServlet.class.getName());
 122  
         }
 123  
 
 124  
         /**
 125  
          * Return the HandlerExecutionChain for this request.
 126  
          * Try all handler mappings in order.
 127  
          * @param request current HTTP request
 128  
          * @param cache whether to cache the HandlerExecutionChain in a request attribute
 129  
          * @return the HandlerExceutionChain, or <code>null</code> if no handler could be found
 130  
          */
 131  
         protected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache) throws Exception {
 132  0
                 return this.httpInvokerHandler.getHandler(request);
 133  
         }
 134  
 
 135  
         @Override
 136  
         protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
 137  
                 try {
 138  0
                         QName serviceName = this.httpInvokerHandler.getServiceNameFromRequest(request);
 139  0
                         LOG.info("Caught Exception from service " + serviceName, ex);
 140  0
                 } catch (Throwable throwable) {
 141  0
                         LOG.warn("Caught exception attempting to log exception thrown from remotely accessed service", throwable);
 142  0
                 }
 143  0
                 return null;
 144  
         }
 145  
 
 146  
         /**
 147  
          * Overrides the service method to replace the request and responses with one which will provide input and output streams for
 148  
          * verifying and signing the data.
 149  
          */
 150  
         @Override
 151  
         protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 152  0
                 if (isSecure(request)) {
 153  0
                         super.service(new SignatureVerifyingRequestWrapper(request), new SignatureSigningResponseWrapper(response));
 154  
                 } else {
 155  0
                         super.service(request, response);
 156  
                 }
 157  0
         }
 158  
 
 159  
         protected boolean isSecure(HttpServletRequest request) {
 160  0
                 QName serviceName = this.httpInvokerHandler.getServiceNameFromRequest(request);
 161  0
                 if (LOG.isDebugEnabled()) {
 162  0
                     LOG.debug("Checking service " + serviceName + " for security enabled");
 163  
                 }
 164  0
                 ServerSideRemotedServiceHolder serviceHolder = KSBServiceLocator.getServiceDeployer().getRemotedServiceHolder(serviceName);
 165  0
                 if (serviceHolder == null) {
 166  0
                         LOG.error("Attempting to acquire non-existent service " + request.getRequestURI());
 167  0
                     throw new RiceRuntimeException("Attempting to acquire non-existent service.");
 168  
                 }
 169  0
                 ServiceInfo serviceInfo = serviceHolder.getServiceInfo();
 170  0
                 if (serviceInfo.getServiceDefinition() instanceof SOAPServiceDefinition) {
 171  0
                     return false;
 172  
                 }
 173  0
                 return serviceInfo.getServiceDefinition().getBusSecurity();
 174  
         }
 175  
 }