📄 contextfilter.java
字号:
StringBuffer contextUriBuffer = new StringBuffer();
if (httpRequest.getContextPath() != null) {
contextUriBuffer.append(httpRequest.getContextPath());
}
if (httpRequest.getServletPath() != null) {
contextUriBuffer.append(httpRequest.getServletPath());
}
if (httpRequest.getPathInfo() != null) {
contextUriBuffer.append(httpRequest.getPathInfo());
}
String contextUri = contextUriBuffer.toString();
// Verbose Debugging
if (Debug.verboseOn()) {
for (int i = 0; i < allowList.size(); i++) {
Debug.logVerbose("[Allow]: " + ((String) allowList.get(i)), module);
}
Debug.logVerbose("[Request path]: " + requestPath, module);
Debug.logVerbose("[Request info]: " + requestInfo, module);
Debug.logVerbose("[Servlet path]: " + httpRequest.getServletPath(), module);
}
// check to make sure the requested url is allowed
if (!allowList.contains(requestPath) && !allowList.contains(requestInfo) && !allowList.contains(httpRequest.getServletPath())) {
String filterMessage = "[Filtered request]: " + contextUri;
if (redirectPath == null) {
int error = 404;
try {
error = Integer.parseInt(errorCode);
} catch (NumberFormatException nfe) {
Debug.logWarning(nfe, "Error code specified would not parse to Integer : " + errorCode, module);
error = 404;
}
filterMessage = filterMessage + " (" + error + ")";
wrapper.sendError(error, contextUri);
} else {
filterMessage = filterMessage + " (" + redirectPath + ")";
if (!redirectPath.toLowerCase().startsWith("http")) {
redirectPath = httpRequest.getContextPath() + redirectPath;
}
wrapper.sendRedirect(redirectPath);
}
Debug.logWarning(filterMessage, module);
return;
}
}
// we're done checking; continue on
chain.doFilter(request, response);
}
/**
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
getDispatcher().deregister();
config = null;
}
protected LocalDispatcher getDispatcher() {
LocalDispatcher dispatcher = (LocalDispatcher) config.getServletContext().getAttribute("dispatcher");
if (dispatcher == null) {
GenericDelegator delegator = getDelegator();
if (delegator == null) {
Debug.logError("[ContextFilter.init] ERROR: delegator not defined.", module);
return null;
}
Collection readers = null;
String readerFiles = config.getServletContext().getInitParameter("serviceReaderUrls");
if (readerFiles != null) {
readers = new ArrayList();
List readerList = StringUtil.split(readerFiles, ";");
Iterator i = readerList.iterator();
while (i.hasNext()) {
try {
String name = (String) i.next();
URL readerURL = config.getServletContext().getResource(name);
if (readerURL != null)
readers.add(readerURL);
} catch (NullPointerException npe) {
Debug.logInfo(npe, "[ContextFilter.init] ERROR: Null pointer exception thrown.", module);
} catch (MalformedURLException e) {
Debug.logError(e, "[ContextFilter.init] ERROR: cannot get URL from String.", module);
}
}
}
// get the unique name of this dispatcher
String dispatcherName = config.getServletContext().getInitParameter("localDispatcherName");
if (dispatcherName == null)
Debug.logError("No localDispatcherName specified in the web.xml file", module);
dispatcher = new WebAppDispatcher(dispatcherName, delegator, readers);
config.getServletContext().setAttribute("dispatcher", dispatcher);
if (dispatcher == null)
Debug.logError("[ContextFilter.init] ERROR: dispatcher could not be initialized.", module);
}
return dispatcher;
}
protected GenericDelegator getDelegator() {
GenericDelegator delegator = (GenericDelegator) config.getServletContext().getAttribute("delegator");
if (delegator == null) {
String delegatorName = config.getServletContext().getInitParameter("entityDelegatorName");
if (delegatorName == null || delegatorName.length() <= 0) {
delegatorName = "default";
}
if (Debug.infoOn()) Debug.logInfo("[ContextFilter.init] Getting Entity Engine Delegator with delegator name " + delegatorName, module);
delegator = GenericDelegator.getGenericDelegator(delegatorName);
config.getServletContext().setAttribute("delegator", delegator);
if (delegator == null) {
Debug.logError("[ContextFilter.init] ERROR: delegator factory returned null for delegatorName \"" + delegatorName + "\"", module);
}
}
return delegator;
}
protected Security getSecurity() {
Security security = (Security) config.getServletContext().getAttribute("security");
if (security == null) {
GenericDelegator delegator = (GenericDelegator) config.getServletContext().getAttribute("delegator");
if (delegator != null) {
try {
security = SecurityFactory.getInstance(delegator);
} catch (SecurityConfigurationException e) {
Debug.logError(e, "[ServiceDispatcher.init] : No instance of security imeplemtation found.", module);
}
}
config.getServletContext().setAttribute("security", security);
if (security == null) {
Debug.logError("[ContextFilter.init] ERROR: security create failed.", module);
}
}
return security;
}
protected String getWebSiteId() {
String webSiteId = (String) config.getServletContext().getAttribute("webSiteId");
if (webSiteId == null) {
webSiteId = config.getServletContext().getInitParameter("webSiteId");
config.getServletContext().setAttribute("webSiteId", webSiteId);
if (webSiteId == null) {
//Debug.logError("[ContextFilter.init] ERROR: website not defined for context.", module);
}
}
return webSiteId;
}
protected String getServerId() {
String serverId = (String) config.getServletContext().getAttribute("_serverId");
if (serverId == null) {
serverId = config.getServletContext().getInitParameter("ofbizServerName");
config.getServletContext().setAttribute("_serverId", serverId);
}
return serverId;
}
protected boolean getComponents() throws ServletException {
try {
ComponentContainer.loadComponents(false);
} catch (AlreadyLoadedException e) {
Debug.logVerbose("Components already loaded; not loading from ContextFilter", module);
return false;
} catch (ComponentException e) {
Debug.logError(e, module);
throw new ServletException("Unable to load components; cannot start ContextFilter");
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -