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 2006-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  
 
 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.message.Message;
 30  
 import org.apache.cxf.transport.servlet.ServletController;
 31  
 import org.apache.cxf.transport.servlet.ServletTransportFactory;
 32  
 import org.apache.log4j.Logger;
 33  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 34  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 35  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 36  
 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
 37  
 import org.kuali.rice.ksb.api.bus.Endpoint;
 38  
 import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
 39  
 import org.kuali.rice.ksb.api.bus.support.SoapServiceConfiguration;
 40  
 import org.kuali.rice.ksb.security.SignatureSigningResponseWrapper;
 41  
 import org.kuali.rice.ksb.security.SignatureVerifyingRequestWrapper;
 42  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 43  
 import org.springframework.beans.BeansException;
 44  
 import org.springframework.web.HttpRequestHandler;
 45  
 import org.springframework.web.context.WebApplicationContext;
 46  
 import org.springframework.web.servlet.DispatcherServlet;
 47  
 import org.springframework.web.servlet.HandlerAdapter;
 48  
 import org.springframework.web.servlet.HandlerExecutionChain;
 49  
 import org.springframework.web.servlet.ModelAndView;
 50  
 import org.springframework.web.servlet.mvc.Controller;
 51  
 import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
 52  
 import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
 53  
 
 54  
 
 55  
 /**
 56  
  * A {@link DispatcherServlet} which dispatches incoming requests to the appropriate
 57  
  * service endpoint.
 58  
  *
 59  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 60  
  */
 61  0
 public class KSBDispatcherServlet extends DispatcherServlet {
 62  
 
 63  0
         private static final Logger LOG = Logger.getLogger(KSBDispatcherServlet.class);
 64  
 
 65  
         private static final long serialVersionUID = 6790121225857950019L;
 66  
         private KSBHttpInvokerHandler httpInvokerHandler;
 67  
         private ServletController cxfServletController;
 68  
 
 69  
         /**
 70  
          * Instantiate the WebApplicationContext for this servlet, either a default
 71  
          * XmlWebApplicationContext or a custom context class if set. This implementation
 72  
          * expects custom contexts to implement ConfigurableWebApplicationContext.
 73  
          * Can be overridden in subclasses.
 74  
          * @param parent the parent ApplicationContext to use, or <code>null</code> if none
 75  
          * @return the WebApplicationContext for this servlet
 76  
          * @throws BeansException if the context couldn't be initialized
 77  
          * @see #setContextClass
 78  
          * @see org.springframework.web.context.support.XmlWebApplicationContext
 79  
          */
 80  
         protected WebApplicationContext initWebApplicationContext() throws BeansException {
 81  0
                 return null;//we want to start spring all by ourselves
 82  
         }
 83  
 
 84  
         protected void initFrameworkServlet() throws ServletException, BeansException {
 85  0
                 this.httpInvokerHandler = new KSBHttpInvokerHandler();
 86  
                 
 87  0
         Bus bus = KSBServiceLocator.getCXFBus();
 88  
 
 89  0
         List<Interceptor<? extends Message>> inInterceptors = KSBServiceLocator.getInInterceptors();
 90  0
         if(inInterceptors != null) {
 91  0
                 List<Interceptor<? extends Message>> busInInterceptors = bus.getInInterceptors();
 92  0
                 busInInterceptors.addAll(inInterceptors);
 93  
         }
 94  
        
 95  0
         List<Interceptor<? extends Message>> outInterceptors = KSBServiceLocator.getOutInterceptors();
 96  0
         if(outInterceptors != null) {
 97  0
                 List<Interceptor<? extends Message>> busOutInterceptors = bus.getOutInterceptors();
 98  0
                 busOutInterceptors.addAll(outInterceptors);
 99  
         }
 100  
         
 101  0
                 ServletTransportFactory servletTransportFactory = KSBServiceLocator.getCXFServletTransportFactory();
 102  
                                 
 103  0
                 this.cxfServletController = new ServletController(servletTransportFactory, this.getServletConfig(), this.getServletContext(), bus);
 104  
                 
 105  0
                 if (!ConfigContext.getCurrentContextConfig().getDevMode()) {
 106  
                     // disable handling of URLs ending in /services which display CXF generated service lists
 107  0
                     this.cxfServletController.setHideServiceList(true);
 108  
                 }
 109  
                 
 110  0
                 this.setPublishEvents(false);
 111  0
         }
 112  
 
 113  
         protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
 114  0
                 if (handler instanceof HttpRequestHandler) {
 115  0
                         return new HttpRequestHandlerAdapter();
 116  0
                 } else if (handler instanceof Controller) {
 117  0
                         Object unwrappedHandler = ClassLoaderUtils.unwrapFromProxy(handler);
 118  0
                         if (unwrappedHandler instanceof CXFServletControllerAdapter) {
 119  
                                 // TODO this just seems weird as this controller is initially null when it's created, does there need to be some synchronization here?
 120  0
                                 ((CXFServletControllerAdapter)unwrappedHandler).setController(cxfServletController);
 121  
                         }                        
 122  0
                         return new SimpleControllerHandlerAdapter();
 123  
                 }
 124  0
                 throw new RiceRuntimeException("handler of type " + handler.getClass().getName() + " is not known and can't be used by " + KSBDispatcherServlet.class.getName());
 125  
         }
 126  
 
 127  
         /**
 128  
          * Return the HandlerExecutionChain for this request.
 129  
          * Try all handler mappings in order.
 130  
          * @param request current HTTP request
 131  
          * @param cache whether to cache the HandlerExecutionChain in a request attribute
 132  
          * @return the HandlerExceutionChain, or <code>null</code> if no handler could be found
 133  
          */
 134  
         protected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache) throws Exception {
 135  0
                 return this.httpInvokerHandler.getHandler(request);
 136  
         }
 137  
 
 138  
         @Override
 139  
         protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
 140  
                 try {
 141  0
                         QName serviceName = this.httpInvokerHandler.getServiceNameFromRequest(request);
 142  0
                         LOG.info("Caught Exception from service " + serviceName, ex);
 143  0
                 } catch (Throwable throwable) {
 144  0
                         LOG.warn("Caught exception attempting to log exception thrown from remotely accessed service", throwable);
 145  0
                 }
 146  0
                 return null;
 147  
         }
 148  
 
 149  
         /**
 150  
          * Overrides the service method to replace the request and responses with one which will provide input and output streams for
 151  
          * verifying and signing the data.
 152  
          */
 153  
         @Override
 154  
         protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 155  0
                 if (isSecure(request)) {
 156  0
                         super.service(new SignatureVerifyingRequestWrapper(request), new SignatureSigningResponseWrapper(response));
 157  
                 } else {
 158  0
                         super.service(request, response);
 159  
                 }
 160  0
         }
 161  
 
 162  
         protected boolean isSecure(HttpServletRequest request) {
 163  0
                 QName serviceName = this.httpInvokerHandler.getServiceNameFromRequest(request);
 164  0
                 if (LOG.isDebugEnabled()) {
 165  0
                     LOG.debug("Checking service " + serviceName + " for security enabled");
 166  
                 }
 167  0
                 Endpoint endpoint = KsbApiServiceLocator.getServiceBus().getEndpoint(serviceName);
 168  0
                 if (endpoint == null) {
 169  0
                         LOG.error("Attempting to acquire non-existent service " + request.getRequestURI());
 170  0
                     throw new RiceRuntimeException("Attempting to acquire non-existent service.");
 171  
                 }
 172  0
                 ServiceConfiguration serviceConfiguration = endpoint.getServiceConfiguration();
 173  0
                 if (serviceConfiguration instanceof SoapServiceConfiguration) {
 174  0
                     return false;
 175  
                 }
 176  0
                 return serviceConfiguration.getBusSecurity();
 177  
         }
 178  
 }