Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

LinkResolver Documentation and Cleanup

  • Loading branch information...
commit 08a61cc1b1f599013abf25c592d7de01f8c835a3 1 parent 4f68d36
@javajawa authored
Showing with 23 additions and 0 deletions.
  1. +23 −0 src/uk/co/harcourtprogramming/docitten/LinkResolver.java
View
23 src/uk/co/harcourtprogramming/docitten/LinkResolver.java
@@ -28,9 +28,24 @@ public void uncaughtException(Thread t, Throwable e)
}
};
+ /**
+ * <p>Logger shared with {@link LinkService} and with all other LinkResolver
+ * instances</p>
+ */
private final static Logger LOG = Logger.getLogger("DoCitten.LinkService");
+ /**
+ * <p>Regex pattern to test whether a string has an http or https protocol
+ * section</p>
+ * <p>The URI class in Java requires this in order to create an instance</p>
+ */
private final static Pattern PROTOCOL = Pattern.compile("^https?://.+");
+ /**
+ * <p>Max time to wait for any one hop before giving up</p>
+ */
private final static int TIMEOUT = 2000;
+ /**
+ * <p>The maximum number of redirects to follow</p>
+ */
private final static int MAX_HOPS = 5;
/**
@@ -98,6 +113,7 @@ public void run()
HttpURLConnection conn;
boolean resolved = false;
int hops = 0;
+
while (true)
{
conn = (HttpURLConnection)curr.openConnection();
@@ -117,6 +133,7 @@ public void run()
case HttpURLConnection.HTTP_NOT_MODIFIED:
resolved = true;
break;
+
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
case HttpURLConnection.HTTP_MULT_CHOICE:
@@ -124,9 +141,11 @@ public void run()
if (conn.getHeaderField("Location") == null) return;
curr = URI.create(curr.toExternalForm()).resolve(conn.getHeaderField("Location")).toURL();
break;
+
default:
return;
}
+
conn.disconnect();
if (interrupted()) return;
if (resolved) break;
@@ -182,10 +201,12 @@ private String getTitle(HttpURLConnection conn) throws IOException
String line;
boolean reading = false;
String title = "[No Title Set]";
+
while (true)
{
line = pageData.readLine();
if (line == null) break;
+
if (line.contains("<title>"))
{
reading = true;
@@ -200,7 +221,9 @@ private String getTitle(HttpURLConnection conn) throws IOException
if (line.contains("</head>") || line.contains("<body>")) break;
if (reading) title += line;
}
+
pageData.close();
+
return title.trim().replaceAll("\\s\\s+", " ");
}
Please sign in to comment.
Something went wrong with that request. Please try again.