Source:
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { String queryString = "?" + servletRequest.getQueryString(); int hash = queryString.indexOf('#'); if (hash >= 0) { queryString = queryString.substring(0, hash); } List<NameValuePair> pairs; try { pairs = URLEncodedUtils.parse(new URI(queryString), "UTF-8"); } catch (URISyntaxException e) { throw new ServletException("Unexpected URI parsing error on " + queryString, e); } LinkedHashMap<String, String> params = new LinkedHashMap<String, String>(); for (NameValuePair pair : pairs) { params.put(pair.getName(), pair.getValue()); } LinkedHashMap<String, String> specialHeaders = getVariablesFromRequestHeaders(servletRequest); StringBuffer urlBuf = new StringBuffer(); Matcher matcher = TEMPLATE_PATTERN.matcher(targetUriTemplate); while (matcher.find()) { String arg = matcher.group(1); String replacement = params.remove(arg); if (replacement == null) { replacement = specialHeaders.get(arg); if (replacement == null) { throw new ServletException("Missing HTTP parameter " + arg + " to fill the template"); } } matcher.appendReplacement(urlBuf, replacement); } matcher.appendTail(urlBuf); String newTargetUri = urlBuf.toString(); servletRequest.setAttribute(ATTR_TARGET_URI, newTargetUri); URI targetUriObj; try { targetUriObj = new URI(newTargetUri); } catch (Exception e) { throw new ServletException("Rewritten targetUri is invalid: " + newTargetUri,e); } servletRequest.setAttribute(ATTR_TARGET_HOST, URIUtils.extractHost(targetUriObj)); StringBuilder newQueryBuf = new StringBuilder(queryString.length()); for (Map.Entry<String, String> nameVal : params.entrySet()) { if (newQueryBuf.length() > 0) newQueryBuf.append('&'); newQueryBuf.append(nameVal.getKey()).append('='); if (nameVal.getValue() != null) newQueryBuf.append(nameVal.getValue()); } servletRequest.setAttribute(ATTR_QUERY_STRING, newQueryBuf.toString()); super.service(servletRequest, servletResponse); }

Target:
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { String queryString = "?" + servletRequest.getQueryString(); int hash = queryString.indexOf('#'); if (hash >= 0) { queryString = queryString.substring(0, hash); } List<NameValuePair> pairs; try { pairs = URLEncodedUtils.parse(new URI(queryString), "UTF-8"); } catch (URISyntaxException e) { throw new ServletException("Unexpected URI parsing error on " + queryString, e); } LinkedHashMap<String, String> params = new LinkedHashMap<String, String>(); for (NameValuePair pair : pairs) { params.put(pair.getName(), pair.getValue()); } StringBuffer urlBuf = new StringBuffer(); Matcher matcher = TEMPLATE_PATTERN.matcher(targetUriTemplate); while (matcher.find()) { String arg = matcher.group(1); String replacement = params.remove(arg); if (replacement == null) { replacement = servletRequest.getHeader(arg) ; if (replacement == null) { throw new ServletException("Missing HTTP parameter " + arg + " to fill the template"); } } matcher.appendReplacement(urlBuf, replacement); } matcher.appendTail(urlBuf); String newTargetUri = urlBuf.toString(); servletRequest.setAttribute(ATTR_TARGET_URI, newTargetUri); URI targetUriObj; try { targetUriObj = new URI(newTargetUri); } catch (Exception e) { throw new ServletException("Rewritten targetUri is invalid: " + newTargetUri,e); } servletRequest.setAttribute(ATTR_TARGET_HOST, URIUtils.extractHost(targetUriObj)); StringBuilder newQueryBuf = new StringBuilder(queryString.length()); for (Map.Entry<String, String> nameVal : params.entrySet()) { if (newQueryBuf.length() > 0) newQueryBuf.append('&'); newQueryBuf.append(nameVal.getKey()).append('='); if (nameVal.getValue() != null) newQueryBuf.append(nameVal.getValue()); } servletRequest.setAttribute(ATTR_QUERY_STRING, newQueryBuf.toString()); super.service(servletRequest, servletResponse); }