1 7 package org.infoglue.deliver.portal.dispatcher; 8 9 import java.io.IOException ; 10 11 import javax.servlet.RequestDispatcher ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 import org.infoglue.cms.security.InfoGluePrincipal; 17 import org.infoglue.cms.util.CmsPropertyHandler; 18 19 import webwork.action.Action; 20 import webwork.action.ActionContext; 21 import webwork.action.ServletActionContext; 22 import webwork.dispatcher.ActionResult; 23 import webwork.dispatcher.GenericDispatcher; 24 import webwork.dispatcher.ServletDispatcher; 25 import webwork.util.ServletValueStack; 26 27 44 public class DeliveryServletDispatcher extends ServletDispatcher 45 { 46 private String actionExtension = ".action"; 47 48 61 public void service(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException 62 { 63 if(CmsPropertyHandler.getApplicationName().equalsIgnoreCase("cms")) 65 { 66 super.service(aRequest, aResponse); 67 return; 68 } 69 70 String servletPath = (String ) aRequest.getAttribute("javax.servlet.include.servlet_path"); 72 if (servletPath == null) 73 servletPath = aRequest.getServletPath(); 74 75 76 String actionName = getActionName(servletPath); 77 GenericDispatcher gd = new GenericDispatcher(actionName, false); 78 ActionContext context = gd.prepareContext(); 79 80 InfoGluePrincipal principal = (InfoGluePrincipal)aRequest.getSession().getAttribute("infogluePrincipal"); 81 if(principal != null) 82 aRequest.setAttribute("infoglueRemoteUser", principal.getName()); 83 84 aRequest.setAttribute("webwork.request_url", aRequest.getRequestURL()); 85 86 ServletActionContext.setContext(aRequest, aResponse, getServletContext(), actionName); 87 88 gd.prepareValueStack(); 89 ActionResult ar = null; 90 try 91 { 92 gd.executeAction(); 93 ar = gd.finish(); 94 } 95 catch (Throwable e) 96 { 97 log.error("Could not execute action", e); 98 try 99 { 100 aResponse.sendError(404, "Could not execute action [" + actionName + "]:" + e.getMessage() + getHTMLErrorMessage(e)); 101 } 102 catch (IOException e1) 103 { 104 } 105 } 106 107 if (ar != null && ar.getActionException() != null) 108 { 109 log.error("Could not execute action", ar.getActionException()); 110 try 111 { 112 aResponse.sendError(500, ar.getActionException().getMessage() + getHTMLErrorMessage(ar.getActionException())); 113 } 114 catch (IOException e1) 115 { 116 } 117 } 118 119 if (ar != null && ar.getResult() != null && ar.getView() == null && !ar.getResult().equals(Action.NONE)) { 121 try 122 { 123 aResponse.sendError(404, "No view for result [" + ar.getResult() + "] exists for action [" + actionName + "]"); 124 } 125 catch (IOException e) 126 { 127 } 128 } 129 130 if (ar != null && ar.getView() != null && ar.getActionException() == null) 131 { 132 String view = ar.getView().toString(); 133 log.debug("Result:" + view); 134 135 RequestDispatcher dispatcher = null; 136 try 137 { 138 dispatcher = aRequest.getRequestDispatcher(view); 139 } 140 catch (Throwable e) 141 { 142 } 144 145 if (dispatcher == null) 146 throw new ServletException ("No presentation file with name '" + view + "' found!"); 147 148 try 149 { 150 if (aRequest.getAttribute("javax.servlet.include.servlet_path") == null) 154 { 155 aRequest.setAttribute("webwork.view_uri", view); 156 aRequest.setAttribute("webwork.request_uri", aRequest.getRequestURI()); 157 aRequest.setAttribute("webwork.request_url", aRequest.getRequestURL()); 158 160 dispatcher.forward(aRequest, aResponse); 161 } 162 else 163 { 164 dispatcher.include(aRequest, aResponse); 167 } 168 } 169 catch (IOException e) 170 { 171 throw new ServletException (e); 172 } 173 finally 174 { 175 aRequest.setAttribute(STACK_HEAD, ServletValueStack.getStack(aRequest).popValue()); 178 } 179 } 180 181 gd.finalizeContext(); 182 } 183 184 188 private String getActionName(String name) 189 { 190 int beginIdx = name.lastIndexOf("/"); 192 int endIdx = name.lastIndexOf(actionExtension); 193 return name.substring((beginIdx == -1 ? 0 : beginIdx + 1), 194 endIdx == -1 ? name.length() : endIdx); 195 } 196 197 } 198 | Popular Tags |