📄 requesthandler.java
字号:
/* * $Id: RequestHandler.java 6954 2006-03-09 02:28:33Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.webapp.control;import java.io.IOException;import java.io.Serializable;import java.io.UnsupportedEncodingException;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javolution.util.FastMap;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.StringUtil;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilObject;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.webapp.event.EventFactory;import org.ofbiz.webapp.event.EventHandler;import org.ofbiz.webapp.event.EventHandlerException;import org.ofbiz.webapp.stats.ServerHitBin;import org.ofbiz.webapp.stats.VisitHandler;import org.ofbiz.webapp.view.ViewFactory;import org.ofbiz.webapp.view.ViewHandler;import org.ofbiz.webapp.view.ViewHandlerException;import org.ofbiz.webapp.website.WebSiteWorker;/** * RequestHandler - Request Processor Object * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author Dustin Caldwell * @version $Rev: 6954 $ * @since 2.0 */public class RequestHandler implements Serializable { public static final String module = RequestHandler.class.getName(); public static final String err_resource = "WebappUiLabels"; public static RequestHandler getRequestHandler(ServletContext servletContext) { RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_"); if (rh == null) { rh = new RequestHandler(); rh.init(servletContext); servletContext.setAttribute("_REQUEST_HANDLER_", rh); } return rh; } private ServletContext context = null; private RequestManager requestManager = null; private ViewFactory viewFactory = null; private EventFactory eventFactory = null; public void init(ServletContext context) { Debug.logInfo("[RequestHandler Loading...]", module); this.context = context; this.requestManager = new RequestManager(context); this.viewFactory = new ViewFactory(this); this.eventFactory = new EventFactory(this); } public void doRequest(HttpServletRequest request, HttpServletResponse response, String chain, GenericValue userLogin, GenericDelegator delegator) throws RequestHandlerException { String eventType = null; String eventPath = null; String eventMethod = null; // workaraound if we are in the root webapp String cname = UtilHttp.getApplicationName(request); // Grab data from request object to process String requestUri = RequestHandler.getRequestUri(request.getPathInfo()); String nextView = RequestHandler.getNextPageUri(request.getPathInfo()); if (request.getAttribute("targetRequestUri") == null) { if (request.getSession().getAttribute("_PREVIOUS_REQUEST_") != null) { request.setAttribute("targetRequestUri", request.getSession().getAttribute("_PREVIOUS_REQUEST_")); } else { request.setAttribute("targetRequestUri", "/" + requestUri); } } // Check for chained request. if (chain != null) { requestUri = RequestHandler.getRequestUri(chain); if (request.getAttribute("_POST_CHAIN_VIEW_") != null) { nextView = (String) request.getAttribute("_POST_CHAIN_VIEW_"); } else { nextView = RequestHandler.getNextPageUri(chain); } if (Debug.infoOn()) Debug.logInfo("[RequestHandler]: Chain in place: requestUri=" + requestUri + " nextView=" + nextView, module); } else { // Check to make sure we are allowed to access this request directly. (Also checks if this request is defined.) if (!requestManager.allowDirectRequest(requestUri)) { throw new RequestHandlerException("Unknown request [" + requestUri + "]; this request does not exist or cannot be called directly."); } // Check if we SHOULD be secure and are not. If we are posting let it pass to not lose data. (too late now anyway) if (!request.isSecure() && requestManager.requiresHttps(requestUri) && !request.getMethod().equalsIgnoreCase("POST")) { StringBuffer urlBuf = new StringBuffer(); urlBuf.append(request.getPathInfo()); if (request.getQueryString() != null) { urlBuf.append("?").append(request.getQueryString()); } String newUrl = RequestHandler.makeUrl(request, response, urlBuf.toString()); if (newUrl.toUpperCase().startsWith("HTTPS")) { // if we are supposed to be secure, redirect secure. callRedirect(newUrl.toString(), response, request); } } // If its the first visit run the first visit events. HttpSession session = request.getSession(); if (session.getAttribute("visit") == null) { Debug.logInfo("This is the first request in this visit.", module); // This isn't an event because it is required to run. We do not want to make it optional. VisitHandler.setInitialVisit(request, response); Collection events = requestManager.getFirstVisitEvents(); if (events != null) { Iterator i = events.iterator(); while (i.hasNext()) { Map eventMap = (Map) i.next(); String eType = (String) eventMap.get(ConfigXMLReader.EVENT_TYPE); String ePath = (String) eventMap.get(ConfigXMLReader.EVENT_PATH); String eMeth = (String) eventMap.get(ConfigXMLReader.EVENT_METHOD); try { String returnString = this.runEvent(request, response, eType, ePath, eMeth); if (returnString != null && !returnString.equalsIgnoreCase("success")) { throw new EventHandlerException("First-Visit event did not return 'success'."); } else if (returnString == null) { nextView = "none:"; } } catch (EventHandlerException e) { Debug.logError(e, module); } } } } // Invoke the pre-processor (but NOT in a chain) Collection preProcEvents = requestManager.getPreProcessor(); if (preProcEvents != null) { Iterator i = preProcEvents.iterator(); while (i.hasNext()) { Map eventMap = (Map) i.next(); String eType = (String) eventMap.get(ConfigXMLReader.EVENT_TYPE); String ePath = (String) eventMap.get(ConfigXMLReader.EVENT_PATH); String eMeth = (String) eventMap.get(ConfigXMLReader.EVENT_METHOD); try { String returnString = this.runEvent(request, response, eType, ePath, eMeth); if (returnString != null && !returnString.equalsIgnoreCase("success")) { throw new EventHandlerException("Pre-Processor event did not return 'success'."); } else if (returnString == null) { nextView = "none:"; } } catch (EventHandlerException e) { Debug.logError(e, module); } } } } // Pre-Processor/First-Visit event(s) can interrupt the flow by returning null. // Warning: this could cause problems if more then one event attempts to return a response. if ("none:".equals(nextView)) { if (Debug.infoOn()) Debug.logInfo("[Pre-Processor Interrupted Request, not running: " + requestUri, module); return; } if (Debug.infoOn()) Debug.logInfo("[Processing Request]: " + requestUri, module); String eventReturnString = null; // Perform security check. if (requestManager.requiresAuth(requestUri)) { // Invoke the security handler // catch exceptions and throw RequestHandlerException if failed. Debug.logVerbose("[RequestHandler]: AuthRequired. Running security check.", module); String checkLoginType = requestManager.getEventType("checkLogin"); String checkLoginPath = requestManager.getEventPath("checkLogin"); String checkLoginMethod = requestManager.getEventMethod("checkLogin"); String checkLoginReturnString = null; try { checkLoginReturnString = this.runEvent(request, response, checkLoginType, checkLoginPath, checkLoginMethod); } catch (EventHandlerException e) { throw new RequestHandlerException(e.getMessage(), e); } if (!"success".equalsIgnoreCase(checkLoginReturnString)) { // previous URL already saved by event, so just do as the return says... eventReturnString = checkLoginReturnString; eventType = checkLoginType; eventPath = checkLoginPath; eventMethod = checkLoginMethod; requestUri = "checkLogin"; } } // Invoke the defined event (unless login failed) if (eventReturnString == null) { eventType = requestManager.getEventType(requestUri); eventPath = requestManager.getEventPath(requestUri); eventMethod = requestManager.getEventMethod(requestUri); if (eventType != null && eventPath != null && eventMethod != null) { try { long eventStartTime = System.currentTimeMillis(); // run the event eventReturnString = this.runEvent(request, response, eventType, eventPath, eventMethod); // save the server hit ServerHitBin.countEvent(cname + "." + eventMethod, request, eventStartTime, System.currentTimeMillis() - eventStartTime, userLogin, delegator); // set the default event return if (eventReturnString == null) { nextView = "none:"; } } catch (EventHandlerException e) { // check to see if there is an "error" response, if so go there and make an request error message String tryErrorMsg = requestManager.getRequestAttribute(requestUri, "error"); if (tryErrorMsg != null) { eventReturnString = "error"; Locale locale = UtilHttp.getLocale(request); String errMsg = UtilProperties.getMessage(RequestHandler.err_resource, "requestHandler.error_call_event", locale); request.setAttribute("_ERROR_MESSAGE_", errMsg + ": " + e.toString()); } else { throw new RequestHandlerException("Error calling event and no error repsonse was specified", e); } } } } // If error, then display more error messages: if ("error".equals(eventReturnString)) { if (Debug.errorOn()) { String errorMessageHeader = "Request " + requestUri + " caused an error with the following message: "; if (request.getAttribute("_ERROR_MESSAGE_") != null) { Debug.logError(errorMessageHeader + request.getAttribute("_ERROR_MESSAGE_"), module); } if (request.getAttribute("_ERROR_MESSAGE_LIST_") != null) { Debug.logError(errorMessageHeader + request.getAttribute("_ERROR_MESSAGE_LIST_"), module); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -