summaryrefslogtreecommitdiff
path: root/altosuilib/AltosSiteMapCache.java
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2014-05-28 00:42:24 -0700
committerKeith Packard <keithp@keithp.com>2014-05-28 00:42:24 -0700
commit3773e89c47d356c4df58edc5725c33bca89b9605 (patch)
treecd5dfe6f87cad5b5b58d6949e5776859d7aff7ab /altosuilib/AltosSiteMapCache.java
parente6cfa25702b3dc1d492c5f1a4d0b4ba4831d30bd (diff)
altosuilib: Add google maps API key, configured with -with-google-key
This places the actual key outside of the repository, allowing the user to configure the name of the file containing the key. By default, this pulls the key from $HOME/altusmetrumllc/google-maps-api-key. With the key present, there are no longer any rate limits to loading map data. Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'altosuilib/AltosSiteMapCache.java')
-rw-r--r--altosuilib/AltosSiteMapCache.java143
1 files changed, 78 insertions, 65 deletions
diff --git a/altosuilib/AltosSiteMapCache.java b/altosuilib/AltosSiteMapCache.java
index 6e6046bc..c4316dab 100644
--- a/altosuilib/AltosSiteMapCache.java
+++ b/altosuilib/AltosSiteMapCache.java
@@ -45,6 +45,67 @@ public class AltosSiteMapCache {
static private Object fetch_lock = new Object();
+ private static int fetch_one(File file, String url) {
+ URL u;
+
+ System.out.printf("Loading URL %s\n", url);
+ try {
+ u = new URL(url);
+ } catch (java.net.MalformedURLException e) {
+ return bad_request;
+ }
+
+ byte[] data;
+ URLConnection uc = null;
+ try {
+ uc = u.openConnection();
+ String type = uc.getContentType();
+ int contentLength = uc.getContentLength();
+ if (uc instanceof HttpURLConnection) {
+ int response = ((HttpURLConnection) uc).getResponseCode();
+ switch (response) {
+ case HttpURLConnection.HTTP_FORBIDDEN:
+ case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
+ case HttpURLConnection.HTTP_UNAUTHORIZED:
+ forbidden_time = System.nanoTime();
+ forbidden_set = true;
+ return forbidden;
+ }
+ }
+ InputStream in = new BufferedInputStream(uc.getInputStream());
+ int bytesRead = 0;
+ int offset = 0;
+ data = new byte[contentLength];
+ while (offset < contentLength) {
+ bytesRead = in.read(data, offset, data.length - offset);
+ if (bytesRead == -1)
+ break;
+ offset += bytesRead;
+ }
+ in.close();
+
+ if (offset != contentLength)
+ return failed;
+
+ } catch (IOException e) {
+ return failed;
+ }
+
+ try {
+ FileOutputStream out = new FileOutputStream(file);
+ out.write(data);
+ out.flush();
+ out.close();
+ } catch (FileNotFoundException e) {
+ return bad_request;
+ } catch (IOException e) {
+ if (file.exists())
+ file.delete();
+ return bad_request;
+ }
+ return success;
+ }
+
public static int fetch_map(File file, String url) {
if (file.exists())
return success;
@@ -52,75 +113,27 @@ public class AltosSiteMapCache {
if (forbidden_set && (System.nanoTime() - forbidden_time) < forbidden_interval)
return forbidden;
- synchronized (fetch_lock) {
- URL u;
- long startTime = System.nanoTime();
-
- try {
- u = new URL(url);
- } catch (java.net.MalformedURLException e) {
- return bad_request;
- }
-
- byte[] data;
- URLConnection uc = null;
- try {
- uc = u.openConnection();
- String type = uc.getContentType();
- int contentLength = uc.getContentLength();
- if (uc instanceof HttpURLConnection) {
- int response = ((HttpURLConnection) uc).getResponseCode();
- switch (response) {
- case HttpURLConnection.HTTP_FORBIDDEN:
- case HttpURLConnection.HTTP_PAYMENT_REQUIRED:
- case HttpURLConnection.HTTP_UNAUTHORIZED:
- forbidden_time = System.nanoTime();
- forbidden_set = true;
- return forbidden;
+ int status = bad_request;
+
+ if (!AltosUIVersion.has_google_maps_api_key()) {
+ synchronized (fetch_lock) {
+ long startTime = System.nanoTime();
+ status = fetch_one(file, url);
+ if (status == success) {
+ long duration_ms = (System.nanoTime() - startTime) / 1000000;
+ if (duration_ms < google_maps_ratelimit_ms) {
+ try {
+ Thread.sleep(google_maps_ratelimit_ms - duration_ms);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
}
}
- InputStream in = new BufferedInputStream(uc.getInputStream());
- int bytesRead = 0;
- int offset = 0;
- data = new byte[contentLength];
- while (offset < contentLength) {
- bytesRead = in.read(data, offset, data.length - offset);
- if (bytesRead == -1)
- break;
- offset += bytesRead;
- }
- in.close();
-
- if (offset != contentLength)
- return failed;
-
- } catch (IOException e) {
- return failed;
- }
-
- try {
- FileOutputStream out = new FileOutputStream(file);
- out.write(data);
- out.flush();
- out.close();
- } catch (FileNotFoundException e) {
- return bad_request;
- } catch (IOException e) {
- if (file.exists())
- file.delete();
- return bad_request;
- }
-
- long duration_ms = (System.nanoTime() - startTime) / 1000000;
- if (duration_ms < google_maps_ratelimit_ms) {
- try {
- Thread.sleep(google_maps_ratelimit_ms - duration_ms);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
}
- return success;
+ } else {
+ status = fetch_one(file, url);
}
+ return status;
}
static final int min_cache_size = 9;