| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.ksb.messaging.servlet; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
import java.util.List; |
| 20 | |
import java.util.Locale; |
| 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.api.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.context.i18n.LocaleContext; |
| 45 | |
import org.springframework.web.HttpRequestHandler; |
| 46 | |
import org.springframework.web.context.WebApplicationContext; |
| 47 | |
import org.springframework.web.servlet.DispatcherServlet; |
| 48 | |
import org.springframework.web.servlet.HandlerAdapter; |
| 49 | |
import org.springframework.web.servlet.HandlerExecutionChain; |
| 50 | |
import org.springframework.web.servlet.ModelAndView; |
| 51 | |
import org.springframework.web.servlet.mvc.Controller; |
| 52 | |
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter; |
| 53 | |
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 0 | public class KSBDispatcherServlet extends DispatcherServlet { |
| 63 | |
|
| 64 | 0 | private static final Logger LOG = Logger.getLogger(KSBDispatcherServlet.class); |
| 65 | |
|
| 66 | |
private static final long serialVersionUID = 6790121225857950019L; |
| 67 | |
private KSBHttpInvokerHandler httpInvokerHandler; |
| 68 | |
private ServletController cxfServletController; |
| 69 | |
|
| 70 | |
protected void initFrameworkServlet() throws ServletException, BeansException { |
| 71 | 0 | this.httpInvokerHandler = new KSBHttpInvokerHandler(); |
| 72 | |
|
| 73 | 0 | Bus bus = KSBServiceLocator.getCXFBus(); |
| 74 | |
|
| 75 | 0 | List<Interceptor<? extends Message>> inInterceptors = KSBServiceLocator.getInInterceptors(); |
| 76 | 0 | if(inInterceptors != null) { |
| 77 | 0 | List<Interceptor<? extends Message>> busInInterceptors = bus.getInInterceptors(); |
| 78 | 0 | busInInterceptors.addAll(inInterceptors); |
| 79 | |
} |
| 80 | |
|
| 81 | 0 | List<Interceptor<? extends Message>> outInterceptors = KSBServiceLocator.getOutInterceptors(); |
| 82 | 0 | if(outInterceptors != null) { |
| 83 | 0 | List<Interceptor<? extends Message>> busOutInterceptors = bus.getOutInterceptors(); |
| 84 | 0 | busOutInterceptors.addAll(outInterceptors); |
| 85 | |
} |
| 86 | |
|
| 87 | 0 | ServletTransportFactory servletTransportFactory = KSBServiceLocator.getCXFServletTransportFactory(); |
| 88 | |
|
| 89 | 0 | this.cxfServletController = new ServletController(servletTransportFactory, this.getServletConfig(), this.getServletContext(), bus); |
| 90 | |
|
| 91 | 0 | if (!ConfigContext.getCurrentContextConfig().getDevMode()) { |
| 92 | |
|
| 93 | 0 | this.cxfServletController.setHideServiceList(true); |
| 94 | |
} |
| 95 | |
|
| 96 | 0 | this.setPublishEvents(false); |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException { |
| 100 | 0 | if (handler instanceof HttpRequestHandler) { |
| 101 | 0 | return new HttpRequestHandlerAdapter(); |
| 102 | 0 | } else if (handler instanceof Controller) { |
| 103 | 0 | Object unwrappedHandler = ClassLoaderUtils.unwrapFromProxy(handler); |
| 104 | 0 | if (unwrappedHandler instanceof CXFServletControllerAdapter) { |
| 105 | |
|
| 106 | 0 | ((CXFServletControllerAdapter)unwrappedHandler).setController(cxfServletController); |
| 107 | |
} |
| 108 | 0 | return new SimpleControllerHandlerAdapter(); |
| 109 | |
} |
| 110 | 0 | throw new RiceRuntimeException("handler of type " + handler.getClass().getName() + " is not known and can't be used by " + KSBDispatcherServlet.class.getName()); |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
protected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache) throws Exception { |
| 121 | 0 | return this.httpInvokerHandler.getHandler(request); |
| 122 | |
} |
| 123 | |
|
| 124 | |
@Override |
| 125 | |
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
| 126 | |
try { |
| 127 | 0 | QName serviceName = this.httpInvokerHandler.getServiceNameFromRequest(request); |
| 128 | 0 | LOG.info("Caught Exception from service " + serviceName, ex); |
| 129 | 0 | } catch (Throwable throwable) { |
| 130 | 0 | LOG.warn("Caught exception attempting to log exception thrown from remotely accessed service", throwable); |
| 131 | 0 | } |
| 132 | 0 | return null; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
@Override |
| 140 | |
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 141 | 0 | if (isSecure(request)) { |
| 142 | 0 | super.service(new SignatureVerifyingRequestWrapper(request), new SignatureSigningResponseWrapper(response)); |
| 143 | |
} else { |
| 144 | 0 | super.service(request, response); |
| 145 | |
} |
| 146 | 0 | } |
| 147 | |
|
| 148 | |
protected boolean isSecure(HttpServletRequest request) { |
| 149 | 0 | QName serviceName = this.httpInvokerHandler.getServiceNameFromRequest(request); |
| 150 | 0 | if (LOG.isDebugEnabled()) { |
| 151 | 0 | LOG.debug("Checking service " + serviceName + " for security enabled"); |
| 152 | |
} |
| 153 | 0 | Endpoint endpoint = KsbApiServiceLocator.getServiceBus().getEndpoint(serviceName); |
| 154 | 0 | if (endpoint == null) { |
| 155 | 0 | LOG.error("Attempting to acquire non-existent service " + request.getRequestURI()); |
| 156 | 0 | throw new RiceRuntimeException("Attempting to acquire non-existent service."); |
| 157 | |
} |
| 158 | 0 | ServiceConfiguration serviceConfiguration = endpoint.getServiceConfiguration(); |
| 159 | 0 | if (serviceConfiguration instanceof SoapServiceConfiguration) { |
| 160 | 0 | return false; |
| 161 | |
} |
| 162 | 0 | return serviceConfiguration.getBusSecurity(); |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
@Override |
| 173 | |
protected LocaleContext buildLocaleContext(HttpServletRequest request) { |
| 174 | |
try { |
| 175 | |
|
| 176 | 0 | LocaleContext localeContext = super.buildLocaleContext(request); |
| 177 | |
|
| 178 | 0 | localeContext.getLocale(); |
| 179 | 0 | return localeContext; |
| 180 | 0 | } catch (NullPointerException npe) { |
| 181 | |
|
| 182 | |
|
| 183 | 0 | return new LocaleContext() { |
| 184 | |
public Locale getLocale() { |
| 185 | 0 | return Locale.ENGLISH; |
| 186 | |
} |
| 187 | |
}; |
| 188 | |
} |
| 189 | |
} |
| 190 | |
} |