001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.messaging.servlet;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024: import javax.xml.namespace.QName;
025:
026: import org.apache.log4j.Logger;
027: import org.kuali.bus.services.KSBServiceLocator;
028: import org.kuali.rice.exceptions.RiceRuntimeException;
029: import org.springframework.beans.BeansException;
030: import org.springframework.web.HttpRequestHandler;
031: import org.springframework.web.context.WebApplicationContext;
032: import org.springframework.web.servlet.DispatcherServlet;
033: import org.springframework.web.servlet.HandlerAdapter;
034: import org.springframework.web.servlet.HandlerExecutionChain;
035: import org.springframework.web.servlet.ModelAndView;
036: import org.springframework.web.servlet.mvc.Controller;
037: import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
038: import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
039:
040: import edu.iu.uis.eden.messaging.SOAPServiceDefinition;
041: import edu.iu.uis.eden.messaging.ServiceInfo;
042: import edu.iu.uis.eden.security.SignatureSigningResponseWrapper;
043: import edu.iu.uis.eden.security.SignatureVerifyingRequestWrapper;
044:
045: /**
046: * A {@link DispatcherServlet} which dispatches incoming requests to the appropriate
047: * service endpoint.
048: *
049: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
050: */
051: public class KSBDispatcherServlet extends DispatcherServlet {
052:
053: private static final Logger LOG = Logger
054: .getLogger(KSBDispatcherServlet.class);
055:
056: private static final long serialVersionUID = 6790121225857950019L;
057: private KSBHttpInvokerHandler httpInvokerHandler;
058:
059: /**
060: * Instantiate the WebApplicationContext for this servlet, either a default
061: * XmlWebApplicationContext or a custom context class if set. This implementation
062: * expects custom contexts to implement ConfigurableWebApplicationContext.
063: * Can be overridden in subclasses.
064: * @param parent the parent ApplicationContext to use, or <code>null</code> if none
065: * @return the WebApplicationContext for this servlet
066: * @throws BeansException if the context couldn't be initialized
067: * @see #setContextClass
068: * @see org.springframework.web.context.support.XmlWebApplicationContext
069: */
070: protected WebApplicationContext initWebApplicationContext()
071: throws BeansException {
072: return null;//we want to start spring all by ourselves
073: }
074:
075: protected void initFrameworkServlet() throws ServletException,
076: BeansException {
077: this .httpInvokerHandler = new KSBHttpInvokerHandler();
078: this .setPublishEvents(false);
079: }
080:
081: protected HandlerAdapter getHandlerAdapter(Object handler)
082: throws ServletException {
083: if (handler instanceof HttpRequestHandler) {
084: return new HttpRequestHandlerAdapter();
085: } else if (handler instanceof Controller) {
086: return new SimpleControllerHandlerAdapter();
087: }
088: throw new RiceRuntimeException("handler of type "
089: + handler.getClass().getName()
090: + " is not known and can't be used by "
091: + KSBDispatcherServlet.class.getName());
092: }
093:
094: /**
095: * Return the HandlerExecutionChain for this request.
096: * Try all handler mappings in order.
097: * @param request current HTTP request
098: * @param cache whether to cache the HandlerExecutionChain in a request attribute
099: * @return the HandlerExceutionChain, or <code>null</code> if no handler could be found
100: */
101: protected HandlerExecutionChain getHandler(
102: HttpServletRequest request, boolean cache) throws Exception {
103: return this .httpInvokerHandler.getHandler(request);
104: }
105:
106: @Override
107: protected ModelAndView processHandlerException(
108: HttpServletRequest request, HttpServletResponse response,
109: Object handler, Exception ex) throws Exception {
110: try {
111: QName serviceName = this .httpInvokerHandler
112: .getServiceNameFromRequest(request);
113: LOG
114: .info("Caught Exception from service "
115: + serviceName, ex);
116: } catch (Throwable throwable) {
117: LOG
118: .warn(
119: "Caught exception attempting to log exception thrown from remotely accessed service",
120: throwable);
121: }
122: return null;
123: }
124:
125: /**
126: * Overrides the service method to replace the request and responses with one which will provide input and output streams for
127: * verifying and signing the data.
128: */
129: @Override
130: protected void service(HttpServletRequest request,
131: HttpServletResponse response) throws ServletException,
132: IOException {
133: if (isSecure(request)) {
134: super .service(
135: new SignatureVerifyingRequestWrapper(request),
136: new SignatureSigningResponseWrapper(response));
137: } else {
138: super .service(request, response);
139: }
140: }
141:
142: protected boolean isSecure(HttpServletRequest request) {
143: QName serviceName = this .httpInvokerHandler
144: .getServiceNameFromRequest(request);
145: ServiceInfo serviceInfo = KSBServiceLocator
146: .getServiceDeployer().getRemotedServiceHolder(
147: serviceName).getServiceInfo();
148: if (serviceInfo.getServiceDefinition() instanceof SOAPServiceDefinition) {
149: return false;
150: }
151: return serviceInfo.getServiceDefinition().getBusSecurity();
152: }
153: }
|