| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.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 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 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 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
protected WebApplicationContext initWebApplicationContext() throws BeansException { |
| 79 | 0 | return null; |
| 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 | |
|
| 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 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 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 | |
|
| 148 | |
|
| 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 | |
} |