1 9 package org.jboss.portal.server.invocation.component; 10 11 import java.io.IOException ; 12 13 import javax.servlet.RequestDispatcher ; 14 import javax.servlet.ServletContext ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 import org.jboss.portal.server.Application; 20 import org.jboss.portal.server.Component; 21 import org.jboss.portal.server.Instance; 22 import org.jboss.portal.server.PortalRequest; 23 import org.jboss.portal.server.PortalResponse; 24 import org.jboss.portal.server.Window; 25 import org.jboss.portal.server.WindowContext; 26 import org.jboss.portal.server.invocation.AttachmentKey; 27 import org.jboss.portal.server.invocation.Interceptor; 28 import org.jboss.portal.server.invocation.Invocation; 29 import org.jboss.portal.server.output.InternalErrorResult; 30 import org.jboss.portal.server.servlet.ServletCommand; 31 import org.jboss.portal.server.servlet.CommandServlet; 32 33 39 public class ContextDispatcherInterceptor implements Interceptor 40 { 41 42 public Object invoke(Invocation invocation) 43 { 44 PortalRequest preq = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST); 45 PortalResponse presp = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE); 46 47 Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW); 49 Instance instance = window.getInstance(); 50 Component component = instance.getComponent(); 51 Application application = component.getApplication(); 52 ServletContext servletContext = application.getServletContext(); 53 54 try 55 { 56 preq.setAttribute(ServletCommand.REQ_ATT_KEY, new InvokeNextCommand(invocation)); 57 RequestDispatcher switcher = servletContext.getNamedDispatcher("CommandServlet"); 58 switcher.include(preq, presp); 59 Object result = preq.getAttribute(ServletCommand.REQ_ATT_KEY); 60 return result; 61 } 62 catch (Exception e) 63 { 64 WindowContext ctx = (WindowContext)invocation.getAttachment(AttachmentKey.WINDOW_CONTEXT); 65 return new InternalErrorResult(ctx, e); 66 } 67 finally 68 { 69 preq.setAttribute(ServletCommand.REQ_ATT_KEY, null); 70 } 71 } 72 73 public static class InvokeNextCommand implements ServletCommand 74 { 75 76 private Invocation invocation; 77 78 public InvokeNextCommand(Invocation invocation) 79 { 80 this.invocation = invocation; 81 } 82 83 public Invocation getInvocation() 84 { 85 return invocation; 86 } 87 88 public Object execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException 89 { 90 try 91 { 92 invocation.setAttachment(AttachmentKey.DISPATCHED_REQUEST, req); 93 invocation.setAttachment(AttachmentKey.DISPATCHED_RESPONSE, resp); 94 return invocation.invokeNext(); 95 } 96 finally 97 { 98 invocation.removeAttachment(AttachmentKey.DISPATCHED_REQUEST); 99 invocation.removeAttachment(AttachmentKey.DISPATCHED_RESPONSE); 100 } 101 } 102 } 103 } 104 | Popular Tags |