urlrewrite java_urlrewritefilter.java 源代码在线查看 - UrlRewriteFilter 是一个不错的URL转换工具 资源下载 虫虫电子下载站...

姬和豫
2023-12-01

} catch (MalformedURLException e) { log.debug(e); } String confUrlStr = null; if (confUrl != null) { confUrlStr = confUrl.toString(); } if (inputStream == null) { log.error("unable to find urlrewrite conf file at " + confPath); // set the writer back to null if (urlRewriter != null) { log.error("unloading existing conf"); urlRewriter = null; } } else { Conf conf = new Conf(context, inputStream, confPath, confUrlStr, modRewriteStyleConf); checkConf(conf); } } /** * Separate from checkConfLocal so that it can be overidden. */ protected void checkConf(Conf conf) { checkConfLocal(conf); } private void checkConfLocal(Conf conf) { if (log.isDebugEnabled()) { if (conf.getRules() != null) { log.debug("inited with " + conf.getRules().size() + " rules"); } log.debug("conf is " + (conf.isOk() ? "ok" : "NOT ok")); } confLastLoaded = conf; if (conf.isOk() && conf.isEngineEnabled()) { urlRewriter = new UrlRewriter(conf); log.info("loaded (conf ok)"); } else { if (!conf.isOk()) { log.error("Conf failed to load"); } if (!conf.isEngineEnabled()) { log.error("Engine explicitly disabled in conf"); // not really an error but we want ot to show in logs } if (urlRewriter != null) { log.error("unloading existing conf"); urlRewriter = null; } } } /** * Destroy is called by the application server when it unloads this filter. */ public void destroy() { log.info("destroy called"); destroyActual(); } public void destroyActual() { destroyUrlRewriter(); context = null; confLastLoad = 0; confPath = DEFAULT_WEB_CONF_PATH; confReloadCheckEnabled = false; confReloadCheckInterval = 0; confReloadInProgress = false; } protected void destroyUrlRewriter() { if (urlRewriter != null) { urlRewriter.destroy(); urlRewriter = null; } } /** * The main method called for each request that this filter is mapped for. * * @param request the request to filter * @param response the response to filter * @param chain the chain for the filtering * @throws IOException * @throws ServletException */ public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { UrlRewriter urlRewriter = getUrlRewriter(request, response, chain); final HttpServletRequest hsRequest = (HttpServletRequest) request; final HttpServletResponse hsResponse = (HttpServletResponse) response; UrlRewriteWrappedResponse urlRewriteWrappedResponse = new UrlRewriteWrappedResponse(hsResponse, hsRequest, urlRewriter); // check for status request if (statusEnabled && statusServerNameMatcher.isMatch(request.getServerName())) { String uri = hsRequest.getRequestURI(); if (log.isDebugEnabled()) { log.debug("checking for status path on " + uri); } String contextPath = hsRequest.getContextPath(); if (uri != null && uri.startsWith(contextPath + statusPath)) { showStatus(hsRequest, urlRewriteWrappedResponse); return; } } boolean requestRewritten = false; if (urlRewriter != null) { // process the request requestRewritten = urlRewriter.processRequest(hsRequest, urlRewriteWrappedResponse, chain); } else { if (log.isDebugEnabled()) { log.debug("urlRewriter engine not loaded ignoring request (could be a conf file problem)"); } } // if no rewrite has taken place continue as normal if (!requestRewritten) { chain.doFilter(hsRequest, urlRewriteWrappedResponse); } } /** * Called for every request. * * Split from doFilter so that it can be overriden. */ protected UrlRewriter getUrlRewriter(ServletRequest request, ServletResponse response, FilterChain chain) { // check to see if the conf needs reloading if (isTimeToReloadConf()) { reloadConf(); } return urlRewriter; } /** * Is it time to reload the configuration now. Depends on is conf reloading is enabled. */ public boolean isTimeToReloadConf() { if (!confLoadedFromFile) return false; long now = System.currentTimeMillis(); return confReloadCheckEnabled && !confReloadInProgress && (now - confReloadCheckInterval) > confReloadLastCheck; } /** * Forcibly reload the configuration now. */ public void reloadConf() { long now = System.currentTimeMillis(); confReloadInProgress = true; confReloadLastCheck = now; log.debug("starting conf reload check"); long confFileCurrentTime = getConfFileLastModified(); if (confLastLoad < confFileCurrentTime) { // reload conf confLastLoad = System.currentTimeMillis(); log.info("conf file modified since last load, reloading"); loadUrlRewriterLocal(); } else { log.debug("conf is not modified"); } confReloadInProgress = false; } /** * Gets the last modified date of the conf file. * * @return time as a long */ private long getConfFileLastModified() { File confFile = new File(context.getRealPath(confPath)); return confFile.lastModified(); } /** * Show the status of the conf and the filter to the user. * * @param request to get status info from * @param response response to show the status on. * @throws java.io.IOException if the output cannot be written */ private void showStatus(final HttpServletRequest request, final ServletResponse response) throws IOException { log.debug("showing status"); if ( allowConfSwapViaHttp ) { String newConfPath = request.getParameter("conf"); if ( !StringUtils.isBlank(newConfPath)) { confPath = newConfPath; loadUrlRewriterLocal(); } } Status status = new Status(confLastLoaded, this); status.displayStatusInContainer(request); response.setContentType("text/html; charset=UTF-8"); response.setContentLength(status.getBuffer().length()); final PrintWriter out = response.getWriter(); out.write(status.getBuffer().toString()); out.close(); } public boolean isConfReloadCheckEnabled() { return confReloadCheckEnabled; } /** * The amount of seconds between reload checks. * * @return int number of millis */ public int getConfReloadCheckInterval() { return confReloadCheckInterval / 1000; } public Date getConfReloadLastCheck() { return new Date(confReloadLastCheck); } public boolean isStatusEnabled() { return statusEnabled; } public String getStatusPath() { return statusPath; } public boolean isLoaded() { return urlRewriter != null; } public static String getFullVersionString() { Properties props = new Properties(); String buildNumberStr = ""; try { InputStream is = UrlRewriteFilter.class.getResourceAsStream("build.number.properties"); if ( is != null ) { props.load(is); String buildNumber = (String) props.get("build.number"); if (!StringUtils.isBlank(buildNumber)){ buildNumberStr = " build " + props.get("build.number"); } } } catch (IOException e) { log.error(e); } return VERSION + buildNumberStr; }}

 类似资料: