1 24 package org.ofbiz.webapp.control; 25 26 import javax.servlet.RequestDispatcher ; 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletContext ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 import java.io.IOException ; 35 import java.util.Enumeration ; 36 37 import com.ibm.bsf.BSFManager; 38 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.UtilHttp; 41 import org.ofbiz.base.util.UtilJ2eeCompat; 42 import org.ofbiz.base.util.UtilTimer; 43 import org.ofbiz.base.util.UtilValidate; 44 import org.ofbiz.entity.GenericDelegator; 45 import org.ofbiz.entity.GenericValue; 46 import org.ofbiz.entity.transaction.GenericTransactionException; 47 import org.ofbiz.entity.transaction.TransactionUtil; 48 import org.ofbiz.security.Security; 49 import org.ofbiz.service.LocalDispatcher; 50 import org.ofbiz.webapp.stats.ServerHitBin; 51 52 60 public class ControlServlet extends HttpServlet { 61 62 public static final String module = ControlServlet.class.getName(); 63 64 public ControlServlet() { 65 super(); 66 } 67 68 71 public void init(ServletConfig config) throws ServletException { 72 super.init(config); 73 if (Debug.infoOn()) { 74 Debug.logInfo("[ControlServlet.init] Loading Control Servlet mounted on path " + config.getServletContext().getRealPath("/"), module); 75 } 76 77 configureBsf(); 79 getRequestHandler(); 81 } 82 83 86 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 87 doGet(request, response); 88 } 89 90 93 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 94 String charset = getServletContext().getInitParameter("charset"); 96 if (charset == null || charset.length() == 0) charset = request.getCharacterEncoding(); 97 if (charset == null || charset.length() == 0) charset = "UTF-8"; 98 Debug.logInfo("The character encoding of the request is: [" + request.getCharacterEncoding() + "]. The character encoding we will use for the request and response is: [" + charset + "]", module); 99 100 if (!"none".equals(charset)) { 101 request.setCharacterEncoding(charset); 102 } 103 104 String contentType = "text/html"; 106 if (charset.length() > 0 && !"none".equals(charset)) { 107 response.setContentType(contentType + "; charset=" + charset); 108 response.setCharacterEncoding(charset); 109 } else { 110 response.setContentType(contentType); 111 } 112 113 long requestStartTime = System.currentTimeMillis(); 114 HttpSession session = request.getSession(); 115 116 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 117 119 String webappName = UtilHttp.getApplicationName(request); 121 122 String rname = ""; 123 if (request.getPathInfo() != null) { 124 rname = request.getPathInfo().substring(1); 125 } 126 if (rname.indexOf('/') > 0) { 127 rname = rname.substring(0, rname.indexOf('/')); 128 } 129 130 UtilTimer timer = null; 131 if (Debug.timingOn()) { 132 timer = new UtilTimer(); 133 timer.setLog(true); 134 timer.timerString("[" + rname + "] Servlet Starting, doing setup", module); 135 } 136 137 request.setAttribute("_CONTROL_PATH_", request.getContextPath() + request.getServletPath()); 139 if (Debug.verboseOn()) 140 Debug.logVerbose("Control Path: " + request.getAttribute("_CONTROL_PATH_"), module); 141 142 GenericDelegator delegator = null; 145 String delegatorName = (String ) session.getAttribute("delegatorName"); 146 if (UtilValidate.isNotEmpty(delegatorName)) { 147 delegator = GenericDelegator.getGenericDelegator(delegatorName); 148 } 149 if (delegator == null) { 150 delegator = (GenericDelegator) getServletContext().getAttribute("delegator"); 151 } 152 if (delegator == null) { 153 Debug.logError("[ControlServlet] ERROR: delegator not found in ServletContext", module); 154 } else { 155 request.setAttribute("delegator", delegator); 156 session.setAttribute("delegatorName", delegator.getDelegatorName()); 158 } 159 160 LocalDispatcher dispatcher = (LocalDispatcher) session.getAttribute("dispatcher"); 161 if (dispatcher == null) { 162 dispatcher = (LocalDispatcher) getServletContext().getAttribute("dispatcher"); 163 } 164 if (dispatcher == null) { 165 Debug.logError("[ControlServlet] ERROR: dispatcher not found in ServletContext", module); 166 } 167 request.setAttribute("dispatcher", dispatcher); 168 169 Security security = (Security) session.getAttribute("security"); 170 if (security == null) { 171 security = (Security) getServletContext().getAttribute("security"); 172 } 173 if (security == null) { 174 Debug.logError("[ControlServlet] ERROR: security not found in ServletContext", module); 175 } 176 request.setAttribute("security", security); 177 178 if (Debug.verboseOn()) { 180 logRequestInfo(request); 181 } 182 183 if (Debug.timingOn()) timer.timerString("[" + rname + "] Setup done, doing Event(s) and View(s)", module); 184 185 request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean (true)); 187 188 String errorPage = null; 189 try { 190 getRequestHandler().doRequest(request, response, null, userLogin, delegator); 192 } catch (RequestHandlerException e) { 193 Throwable throwable = e.getNested() != null ? e.getNested() : e; 194 Debug.logError(throwable, "Error in request handler: ", module); 195 request.setAttribute("_ERROR_MESSAGE_", throwable.toString()); 196 errorPage = getRequestHandler().getDefaultErrorPage(request); 197 } catch (Exception e) { 198 Debug.logError(e, "Error in request handler: ", module); 199 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 200 errorPage = getRequestHandler().getDefaultErrorPage(request); 201 } 202 203 207 if (errorPage != null) { 208 Debug.logError("An error occurred, going to the errorPage: " + errorPage, module); 209 210 RequestDispatcher rd = request.getRequestDispatcher(errorPage); 211 212 if (request.getAttribute("_ERROR_OCCURRED_") == null && rd != null) { 214 request.setAttribute("_ERROR_OCCURRED_", new Boolean (true)); 215 Debug.logError("Including errorPage: " + errorPage, module); 216 rd.include(request, response); 217 218 226 } else { 227 if (rd == null) { 228 Debug.logError("Could not get RequestDispatcher for errorPage: " + errorPage, module); 229 } 230 231 String errorMessage = "<html><body>ERROR in error page, (infinite loop or error page not found with name [" + errorPage + "]), but here is the text just in case it helps you: " + request.getAttribute("ERROR_MESSAGE_") + "</body></html>"; 232 if (UtilJ2eeCompat.useOutputStreamNotWriter(getServletContext())) { 233 response.getOutputStream().print(errorMessage); 234 } else { 235 response.getWriter().print(errorMessage); 236 } 237 } 238 } 239 240 try { 242 if (TransactionUtil.isTransactionInPlace()) { 244 Debug.logWarning("*** NOTICE: ControlServlet finished w/ a transaction in place! Rolling back.", module); 245 TransactionUtil.rollback(); 246 } 247 248 if (TransactionUtil.suspendedTransactionsHeld()) { 250 int suspended = TransactionUtil.cleanSuspendedTransactions(); 251 Debug.logWarning("Resumed/Rolled Back [" + suspended + "] transactions.", module); 252 } 253 } catch (GenericTransactionException e) { 254 Debug.logWarning(e, module); 255 } 256 257 ServerHitBin.countRequest(webappName + "." + rname, request, requestStartTime, System.currentTimeMillis() - requestStartTime, userLogin, delegator); 258 if (Debug.timingOn()) timer.timerString("[" + rname + "] Done rendering page, Servlet Finished", module); 259 } 260 261 264 public void destroy() { 265 super.destroy(); 266 } 267 268 protected RequestHandler getRequestHandler() { 269 return RequestHandler.getRequestHandler(getServletContext()); 270 } 271 272 protected void configureBsf() { 273 String [] bshExtensions = {"bsh"}; 274 BSFManager.registerScriptingEngine("beanshell", "org.ofbiz.base.util.OfbizBshBsfEngine", bshExtensions); 275 276 String [] jsExtensions = {"js"}; 277 BSFManager.registerScriptingEngine("javascript", "org.ofbiz.base.util.OfbizJsBsfEngine", jsExtensions); 278 279 String [] smExtensions = {"sm"}; 280 BSFManager.registerScriptingEngine("simplemethod", "org.ofbiz.minilang.SimpleMethodBsfEngine", smExtensions); 281 } 282 283 protected void logRequestInfo(HttpServletRequest request) { 284 ServletContext servletContext = this.getServletContext(); 285 HttpSession session = request.getSession(); 286 287 Debug.logVerbose("--- Start Request Headers: ---", module); 288 Enumeration headerNames = request.getHeaderNames(); 289 while (headerNames.hasMoreElements()) { 290 String headerName = (String ) headerNames.nextElement(); 291 Debug.logVerbose(headerName + ":" + request.getHeader(headerName), module); 292 } 293 Debug.logVerbose("--- End Request Headers: ---", module); 294 295 Debug.logVerbose("--- Start Request Parameters: ---", module); 296 Enumeration paramNames = request.getParameterNames(); 297 while (paramNames.hasMoreElements()) { 298 String paramName = (String ) paramNames.nextElement(); 299 Debug.logVerbose(paramName + ":" + request.getParameter(paramName), module); 300 } 301 Debug.logVerbose("--- End Request Parameters: ---", module); 302 303 Debug.logVerbose("--- Start Request Attributes: ---", module); 304 Enumeration reqNames = request.getAttributeNames(); 305 while (reqNames != null && reqNames.hasMoreElements()) { 306 String attName = (String ) reqNames.nextElement(); 307 Debug.logVerbose(attName + ":" + request.getAttribute(attName), module); 308 } 309 Debug.logVerbose("--- End Request Attributes ---", module); 310 311 Debug.logVerbose("--- Start Session Attributes: ---", module); 312 Enumeration sesNames = null; 313 try { 314 sesNames = session.getAttributeNames(); 315 } catch (IllegalStateException e) { 316 Debug.logVerbose("Cannot get session attributes : " + e.getMessage(), module); 317 } 318 while (sesNames != null && sesNames.hasMoreElements()) { 319 String attName = (String ) sesNames.nextElement(); 320 Debug.logVerbose(attName + ":" + session.getAttribute(attName), module); 321 } 322 Debug.logVerbose("--- End Session Attributes ---", module); 323 324 Enumeration appNames = servletContext.getAttributeNames(); 325 Debug.logVerbose("--- Start ServletContext Attributes: ---", module); 326 while (appNames != null && appNames.hasMoreElements()) { 327 String attName = (String ) appNames.nextElement(); 328 Debug.logVerbose(attName + ":" + servletContext.getAttribute(attName), module); 329 } 330 Debug.logVerbose("--- End ServletContext Attributes ---", module); 331 } 332 } 333 | Popular Tags |