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.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 | |
|
57 | |
|
58 | |
|
59 | |
|
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 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
protected WebApplicationContext initWebApplicationContext() throws BeansException { |
81 | 0 | return null; |
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 | |
|
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 | |
|
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 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
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 | |
|
151 | |
|
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 | |
} |