public ControllerRequest(Class<? extends Controller> controllerClass, Matcher matchedUrl) { this.controllerClass = controllerClass; if (matchedUrl.groupCount() > 0) { List<String> argsList = new ArrayList<String>(matchedUrl.groupCount()); for (int i = 1; i <= matchedUrl.groupCount(); i++) { argsList.add(matchedUrl.group(i)); } this.args = argsList; } else { this.args = new ArrayList<String>(0); } }
/** * Replace occurrences of "%ab" with the character represented by the hex value. Strings of * escaped characters are treated as UTF-8 byte sequences and decoded appropriately. */ private static String decode(String s) { int length = s.length(); StringBuilder str = new StringBuilder(length); Matcher matcher = PATTERN.matcher(s); int offset = 0; byte[] bb = null; while (matcher.find(offset)) { int count = matcher.groupCount(); for (int i = 0; i < count; i++) { String match = matcher.group(0); int num = match.length() / 3; if (bb == null || bb.length < num) { bb = new byte[num]; } for (int j = 0; j < num; j++) { int head = j * 3 + 1; int tail = head + 2; bb[j] = (byte) Integer.parseInt(match.substring(head, tail), 16); } try { String text = new String(bb, "UTF-8"); str.append(s.substring(offset, matcher.start())); str.append(text); } catch (UnsupportedEncodingException e) { // NOTE: This should *never* be thrown because all // JVMs are required to support UTF-8. I mean, // the strings in the .class file are all in // a modified UTF-8, for pete's sake! :) } } offset = matcher.end(); } if (offset < length) { str.append(s.substring(offset)); } return str.toString(); }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; // create input/output dir patterns String contextPath = httpRequest.getContextPath(); if (this.inDirPattern == null) { // NOTE: Have to do this here because the context path is not // available in init(). this.inDirPattern = Pattern.compile("^" + escape(contextPath) + escape(this.inDirName) + "/(.*)"); this.outDirPattern = Pattern.compile("^" + escape(contextPath) + "/help/[a-z]{2}(?:_[A-Z]{2})?/.*"); if (ZimbraLog.webclient.isDebugEnabled()) { ZimbraLog.webclient.debug("### indir pattern: " + this.inDirPattern.pattern()); ZimbraLog.webclient.debug("### outdir pattern: " + this.outDirPattern.pattern()); } } // check to see if we need to redirect this request String requestUri = httpRequest.getRequestURI(); if (this.outDirPattern.matcher(requestUri).matches()) { // allow it to go through chain.doFilter(request, response); return; } // make list of potential locales to check Locale preferredLocale = getLocale(httpRequest); String language = preferredLocale.getLanguage(); String country = preferredLocale.getCountry(); Locale[] locales = {preferredLocale, country != null ? new Locale(language) : null, Locale.US}; if (ZimbraLog.webclient.isDebugEnabled()) { for (Locale locale : locales) { ZimbraLog.webclient.debug("locale: " + locale); } } // find out which version of the requested file exists Locale actualLocale = preferredLocale; Matcher matcher = this.inDirPattern.matcher(requestUri); if (!matcher.matches()) { httpResponse.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Help URL doesn't match input pattern."); return; } if (ZimbraLog.webclient.isDebugEnabled()) { ZimbraLog.webclient.debug("### filename: " + matcher.group(1)); } String filename = decode(matcher.group(1)).replace('/', File.separatorChar); if (ZimbraLog.webclient.isDebugEnabled()) { ZimbraLog.webclient.debug("### filename: " + filename); } File baseDir = new File(this.context.getRealPath("/")); if (ZimbraLog.webclient.isDebugEnabled()) { ZimbraLog.webclient.debug("### basedir: " + baseDir); } for (Locale locale : locales) { if (locale == null) continue; File file = new File( baseDir, this.outDirName.replaceAll("\\{locale\\}", locale.toString()) + File.separatorChar + filename); if (file.exists()) { actualLocale = locale; break; } } // redirect String redirectUrl = contextPath + this.outDirName.replaceAll("\\{locale\\}", actualLocale.toString()) + "/" + filename; if (ZimbraLog.webclient.isDebugEnabled()) { ZimbraLog.webclient.debug("redirecting to: " + redirectUrl); } httpResponse.sendRedirect(redirectUrl); }