1 17 package org.apache.geronimo.tomcat.listener; 18 19 import java.util.Stack ; 20 21 import javax.servlet.ServletRequest ; 22 import javax.servlet.ServletResponse ; 23 24 import org.apache.catalina.Container; 25 import org.apache.catalina.Globals; 26 import org.apache.catalina.InstanceEvent; 27 import org.apache.catalina.InstanceListener; 28 import org.apache.catalina.core.StandardWrapper; 29 import org.apache.geronimo.tomcat.GeronimoStandardContext; 30 import org.apache.geronimo.tomcat.interceptor.BeforeAfter; 31 import org.apache.geronimo.tomcat.realm.TomcatGeronimoRealm; 32 import org.apache.tomcat.util.buf.MessageBytes; 33 import org.apache.tomcat.util.http.mapper.Mapper; 34 import org.apache.tomcat.util.http.mapper.MappingData; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 public class DispatchListener implements InstanceListener { 39 40 private static final Log log = LogFactory.getLog(DispatchListener.class); 41 42 private static ThreadLocal currentContext = new ThreadLocal () { 43 protected Object initialValue() { 44 return new Stack (); 45 } 46 }; 47 48 public void instanceEvent(InstanceEvent event) { 49 50 if (event.getType().equals(InstanceEvent.BEFORE_DISPATCH_EVENT)) { 51 52 Container parent = event.getWrapper().getParent(); 53 if (parent instanceof GeronimoStandardContext) { 54 beforeDispatch((GeronimoStandardContext) parent, event 55 .getRequest(), event.getResponse()); 56 } 57 } 58 59 if (event.getType().equals(InstanceEvent.AFTER_DISPATCH_EVENT)) { 60 Container parent = event.getWrapper().getParent(); 61 if (parent instanceof GeronimoStandardContext) { 62 afterDispatch((GeronimoStandardContext) parent, event 63 .getRequest(), event.getResponse()); 64 } 65 } 66 } 67 68 private void beforeDispatch(GeronimoStandardContext webContext, 69 ServletRequest request, ServletResponse response) { 70 71 BeforeAfter beforeAfter = webContext.getBeforeAfter(); 72 if (beforeAfter != null) { 73 Stack stack = (Stack ) currentContext.get(); 74 Object context[] = new Object [webContext.getContextCount() + 1]; 75 String wrapperName = getWrapperName(request, webContext); 76 context[webContext.getContextCount()] = TomcatGeronimoRealm 77 .setRequestWrapperName(wrapperName); 78 79 beforeAfter.before(context, request, response); 80 stack.push(context); 81 } 82 } 83 84 private void afterDispatch(GeronimoStandardContext webContext, 85 ServletRequest request, ServletResponse response) { 86 BeforeAfter beforeAfter = webContext.getBeforeAfter(); 87 if (beforeAfter != null) { 88 Stack stack = (Stack ) currentContext.get(); 89 Object context[] = (Object []) stack.pop(); 90 beforeAfter.after(context, request, response); 91 TomcatGeronimoRealm 92 .setRequestWrapperName((String ) context[webContext 93 .getContextCount()]); 94 95 } 96 } 97 98 private String getWrapperName(ServletRequest request, 99 GeronimoStandardContext webContext) { 100 101 MappingData mappingData = new MappingData(); 102 Mapper mapper = webContext.getMapper(); 103 MessageBytes mb = MessageBytes.newInstance(); 104 105 String dispatchPath = 106 (String ) request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR); 107 mb.setString(webContext.getName() + dispatchPath); 108 109 try { 110 mapper.map(mb, mappingData); 111 StandardWrapper wrapper = (StandardWrapper) mappingData.wrapper; 112 return wrapper.getName(); 113 } catch (Exception e) { 114 log.error(e.getMessage(), e); 115 } 116 117 return null; 118 } 119 120 } 121 | Popular Tags |