public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException { // Rewrite the supplied URL... URI uri = request.getURI(); String original = uri.toString(); UrlRules rules = UrlRules.getRules(mResolver); UrlRules.Rule rule = rules.matchRule(original); String rewritten = rule.apply(original); if (rewritten == null) { Log.w(TAG, "Blocked by " + rule.mName + ": " + original); throw new BlockedRequestException(rule); } else if (rewritten == original) { return executeWithoutRewriting(request, context); // Pass through } try { uri = new URI(rewritten); } catch (URISyntaxException e) { throw new RuntimeException("Bad URL from rule: " + rule.mName, e); } // Wrap request so we can replace the URI. RequestWrapper wrapper = wrapRequest(request); wrapper.setURI(uri); request = wrapper; if (Config.LOGV) { Log.v(TAG, "Rule " + rule.mName + ": " + original + " -> " + rewritten); } return executeWithoutRewriting(request, context); }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { log.info("Intercepted params: " + context); RequestWrapper wrapper = (RequestWrapper) request; URIBuilder uribuilder = new URIBuilder(wrapper.getURI()); uribuilder.addParameter("b", "\u4321"); try { wrapper.setURI(uribuilder.build()); } catch (URISyntaxException ex) { throw new HttpException("Invalid request URI", ex); } }
/** Wraps the request making it mutable. */ private static RequestWrapper wrapRequest(HttpUriRequest request) throws IOException { try { // We have to wrap it with the right type. Some code performs // instanceof checks. RequestWrapper wrapped; if (request instanceof HttpEntityEnclosingRequest) { wrapped = new EntityEnclosingRequestWrapper((HttpEntityEnclosingRequest) request); } else { wrapped = new RequestWrapper(request); } // Copy the headers from the original request into the wrapper. wrapped.resetHeaders(); return wrapped; } catch (ProtocolException e) { throw new ClientProtocolException(e); } }