Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/org/adblockplus/brazil/RequestHandler.java

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Created April 11, 2014, 1:31 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/org/adblockplus/brazil/RequestHandler.java
diff --git a/src/org/adblockplus/brazil/RequestHandler.java b/src/org/adblockplus/brazil/RequestHandler.java
index 4c70a8adf75e0ea0105e0c3a1a0a27dca23e7669..9fa82f52312aaac17813fa15cca32a0b3f964854 100644
--- a/src/org/adblockplus/brazil/RequestHandler.java
+++ b/src/org/adblockplus/brazil/RequestHandler.java
@@ -48,9 +48,8 @@ import android.util.Log;
/**
* The <code>RequestHandler</code> implements a proxy service optionally
- * modifying output.
- * The following configuration parameters are used to initialize this
- * <code>Handler</code>:
+ * modifying output. The following configuration parameters are used to
+ * initialize this <code>Handler</code>:
* <dl class=props>
*
* <dt>prefix, suffix, glob, match
@@ -68,8 +67,8 @@ import android.util.Log;
*
* </dl>
*
- * A sample set of configuration parameters illustrating how to use this
- * handler follows:
+ * A sample set of configuration parameters illustrating how to use this handler
+ * follows:
*
* <pre>
* handler=adblock
@@ -82,39 +81,40 @@ import android.util.Log;
public class RequestHandler extends BaseRequestHandler
{
+ private final static Pattern RE_HTTP = Pattern.compile("^https?:");
+
private AdblockPlus application;
private String via;
- private static Pattern RE_HTTP = Pattern.compile("^https?:");
@Override
- public boolean init(Server server, String prefix)
+ public boolean init(final Server server, final String prefix)
{
super.init(server, prefix);
- application = AdblockPlus.getApplication();
- via = " " + server.hostName + ":" + server.listen.getLocalPort() + " (" + server.name + ")";
+ this.application = AdblockPlus.getApplication();
+ this.via = " " + server.hostName + ":" + server.listen.getLocalPort() + " (" + server.name + ")";
return true;
}
@Override
- public boolean respond(Request request) throws IOException
+ public boolean respond(final Request request) throws IOException
{
boolean block = false;
try
{
- block = application.matches(request.url, request.query, request.getRequestHeader("referer"), request.getRequestHeader("accept"));
+ block = this.application.matches(request.url, request.query, request.getRequestHeader("referer"), request.getRequestHeader("accept"));
}
- catch (Exception e)
+ catch (final Exception e)
{
- Log.e(prefix, "Filter error", e);
+ Log.e(this.prefix, "Filter error", e);
}
- request.log(Server.LOG_LOG, prefix, block + ": " + request.url);
+ request.log(Server.LOG_LOG, this.prefix, block + ": " + request.url);
int count = request.server.requestCount;
- if (shouldLogHeaders)
+ if (this.shouldLogHeaders)
{
System.err.println(dumpHeaders(count, request, request.headers, true));
}
@@ -139,10 +139,10 @@ public class RequestHandler extends BaseRequestHandler
}
/*
- * "Proxy-Connection" may be used (instead of just "Connection")
- * to keep alive a connection between a client and this proxy.
+ * "Proxy-Connection" may be used (instead of just "Connection") to keep
+ * alive a connection between a client and this proxy.
*/
- String pc = request.headers.get("Proxy-Connection");
+ final String pc = request.headers.get("Proxy-Connection");
if (pc != null)
{
request.connectionHeader = "Proxy-Connection";
@@ -151,24 +151,24 @@ public class RequestHandler extends BaseRequestHandler
HttpRequest.removePointToPointHeaders(request.headers, false);
- HttpRequest target = new HttpRequest(url);
+ final HttpRequest target = new HttpRequest(url);
try
{
target.setMethod(request.method);
request.headers.copyTo(target.requestHeaders);
- if (proxyHost != null)
+ if (this.proxyHost != null)
{
- target.setProxy(proxyHost, proxyPort);
- if (auth != null)
+ target.setProxy(this.proxyHost, this.proxyPort);
+ if (this.auth != null)
{
- target.requestHeaders.add("Proxy-Authorization", auth);
+ target.requestHeaders.add("Proxy-Authorization", this.auth);
}
}
if (request.postData != null)
{
- OutputStream out = target.getOutputStream();
+ final OutputStream out = target.getOutputStream();
out.write(request.postData);
out.close();
}
@@ -178,7 +178,7 @@ public class RequestHandler extends BaseRequestHandler
}
target.connect();
- if (shouldLogHeaders)
+ if (this.shouldLogHeaders)
{
System.err.println(" " + target.status + "\n" + dumpHeaders(count, request, target.responseHeaders, false));
}
@@ -188,15 +188,15 @@ public class RequestHandler extends BaseRequestHandler
target.responseHeaders.copyTo(request.responseHeaders);
try
{
- request.responseHeaders.add("Via", target.status.substring(0, 8) + via);
+ request.responseHeaders.add("Via", target.status.substring(0, 8) + this.via);
}
- catch (StringIndexOutOfBoundsException e)
+ catch (final StringIndexOutOfBoundsException e)
{
- request.responseHeaders.add("Via", via);
+ request.responseHeaders.add("Via", this.via);
}
// Detect if we need to add ElemHide filters
- String type = request.responseHeaders.get("Content-Type");
+ final String type = request.responseHeaders.get("Content-Type");
String[] selectors = null;
if (type != null && type.toLowerCase().startsWith("text/html"))
@@ -207,17 +207,17 @@ public class RequestHandler extends BaseRequestHandler
{
reqHost = (new URL(request.url)).getHost();
}
- catch (MalformedURLException e)
+ catch (final MalformedURLException e)
{
// We are transparent, it's not our deal if it's malformed.
}
- selectors = application.getSelectorsForDomain(reqHost);
+ selectors = this.application.getSelectorsForDomain(reqHost);
}
// If no filters are applicable just pass through the response
if (selectors == null || target.getResponseCode() != 200)
{
- int contentLength = target.getContentLength();
+ final int contentLength = target.getContentLength();
if (contentLength == 0)
{
// we do not use request.sendResponse to avoid arbitrary
@@ -232,7 +232,7 @@ public class RequestHandler extends BaseRequestHandler
// Insert filters otherwise
else
{
- HttpInputStream his = target.getInputStream();
+ final HttpInputStream his = target.getInputStream();
int size = target.getContentLength();
if (size < 0)
{
@@ -290,19 +290,19 @@ public class RequestHandler extends BaseRequestHandler
Charset.forName(extractedCharsetName);
charsetName = extractedCharsetName;
}
- catch (IllegalArgumentException e)
+ catch (final IllegalArgumentException e)
{
- Log.e(prefix, "Unsupported site charset, falling back to " + charsetName, e);
+ Log.e(this.prefix, "Unsupported site charset, falling back to " + charsetName, e);
}
}
}
request.sendHeaders(-1, null, -1);
- byte[] buf = new byte[Math.min(4096, size)];
+ final byte[] buf = new byte[Math.min(4096, size)];
boolean sent = selectors == null;
- BoyerMoore matcher = new BoyerMoore("<html".getBytes());
+ final BoyerMoore matcher = new BoyerMoore("<html".getBytes());
while (size > 0)
{
@@ -319,11 +319,11 @@ public class RequestHandler extends BaseRequestHandler
// Search for <html> tag
if (!sent && count > 0)
{
- List<Integer> matches = matcher.match(buf, 0, count);
+ final List<Integer> matches = matcher.match(buf, 0, count);
if (!matches.isEmpty())
{
// Add filters right before match
- int m = matches.get(0);
+ final int m = matches.get(0);
out.write(buf, 0, m);
out.write("<style type=\"text/css\">\n".getBytes());
out.write(StringUtils.join(selectors, ",\r\n").getBytes(charsetName));
@@ -335,7 +335,7 @@ public class RequestHandler extends BaseRequestHandler
}
out.write(buf, 0, count);
}
- catch (IOException e)
+ catch (final IOException e)
{
break;
}
@@ -344,36 +344,38 @@ public class RequestHandler extends BaseRequestHandler
// but we can not do it because underlying output stream is
// used later in caller code. So we use this ugly hack:
if (out instanceof ChunkedOutputStream)
- ((ChunkedOutputStream) out).writeFinalChunk();
+ {
+ ((ChunkedOutputStream)out).writeFinalChunk();
+ }
}
}
- catch (InterruptedIOException e)
+ catch (final InterruptedIOException e)
{
/*
- * Read timeout while reading from the remote side. We use a
- * read timeout in case the target never responds.
+ * Read timeout while reading from the remote side. We use a read timeout
+ * in case the target never responds.
*/
request.sendError(408, "Timeout / No response");
}
- catch (EOFException e)
+ catch (final EOFException e)
{
request.sendError(500, "No response");
}
- catch (UnknownHostException e)
+ catch (final UnknownHostException e)
{
request.sendError(500, "Unknown host");
}
- catch (ConnectException e)
+ catch (final ConnectException e)
{
request.sendError(500, "Connection refused");
}
- catch (IOException e)
+ catch (final IOException e)
{
/*
- * An IOException will happen if we can't communicate with the
- * target or the client. Rather than attempting to discriminate,
- * just send an error message to the client, and let the send
- * fail if the client was the one that was in error.
+ * An IOException will happen if we can't communicate with the target or
+ * the client. Rather than attempting to discriminate, just send an error
+ * message to the client, and let the send fail if the client was the one
+ * that was in error.
*/
String msg = "Error from proxy";
@@ -382,7 +384,7 @@ public class RequestHandler extends BaseRequestHandler
msg += ": " + e.getMessage();
}
request.sendError(500, msg);
- Log.e(prefix, msg, e);
+ Log.e(this.prefix, msg, e);
}
finally
{

Powered by Google App Engine
This is Rietveld