altosuilib: Add google maps API key, configured with -with-google-key
authorKeith Packard <keithp@keithp.com>
Wed, 28 May 2014 07:42:24 +0000 (00:42 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 28 May 2014 07:42:24 +0000 (00:42 -0700)
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>
altosuilib/AltosSiteMap.java
altosuilib/AltosSiteMapCache.java
altosuilib/AltosUIVersion.java.in
configure.ac

index ee9b8c051658cdfdae26743200c4f2ccc4ba97b0..d9ea564ce1c6dde2d34e510009f8920cddba6856 100644 (file)
@@ -263,8 +263,6 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay, Mous
                AltosSiteMap asm = new AltosSiteMap(true);
                asm.centre = asm.getBaseLocation(lat, lng);
 
-               //Point2D.Double p = new Point2D.Double();
-               //Point2D.Double p2;
                int dx = -w/2, dy = -h/2;
                for (int y = dy; y < h+dy; y++) {
                        for (int x = dx; x < w+dx; x++) {
@@ -334,8 +332,10 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay, Mous
        private void initMaps(double lat, double lng) {
                setBaseLocation(lat, lng);
 
-               for (AltosSiteMapTile tile : mapTiles.values())
+               for (AltosSiteMapTile tile : mapTiles.values()) {
                        tile.clearMap();
+                       tile.set_status(AltosSiteMapCache.loading);
+               }
                Thread thread = new Thread() {
                                public void run() {
                                        for (Point k : mapTiles.keySet())
@@ -369,8 +369,13 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay, Mous
                        format_string = "jpg";
                else
                        format_string = "png32";
-               return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s",
-                                    lat, lng, zoom, px_size, px_size, maptype_names[maptype], format_string);
+
+               if (AltosUIVersion.has_google_maps_api_key())
+                       return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s&key=%s",
+                                            lat, lng, zoom, px_size, px_size, maptype_names[maptype], format_string, AltosUIVersion.google_maps_api_key);
+               else
+                       return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s",
+                                            lat, lng, zoom, px_size, px_size, maptype_names[maptype], format_string);
        }
 
        boolean initialised = false;
index 6e6046bc7f3f31d071f956f48eb5ac1298a1e7b0..c4316dabf4f5628cc26c3bddcbfcdab0b960e279 100644 (file)
@@ -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;
index 3f6295609514e88f2eb2c5bc1200b347afda8d0a..0edb5c04b3ae2fd7d58fea4452a23bf57e7a1ced 100644 (file)
@@ -19,4 +19,10 @@ package org.altusmetrum.altosuilib_2;
 
 public class AltosUIVersion {
        public final static String version = "@VERSION@";
+
+       public final static String google_maps_api_key = @GOOGLEKEY@;
+
+       static boolean has_google_maps_api_key() {
+               return google_maps_api_key != null && google_maps_api_key.length() > 1;
+       }
 }
index d2d87754f3fcca52f53d38024edaa6fdcebadcc6..dfbe59bcc0e96543218c465a2930067dbdcf3250 100644 (file)
@@ -162,6 +162,20 @@ AM_CONDITIONAL(FATINSTALL, [test "x$FATDIR" != "xnone"])
 
 AC_SUBST(FATDIR)
 
+AC_ARG_WITH(google-key, AS_HELP_STRING([--with-google-key=PATH],
+           [Set the file to read the google maps API key from (defaults to ~/altusmetrumllc/google-maps-api-key)]),
+           [GOOGLEKEYFILE=$withval], [GOOGLEKEYFILE=$HOME/altusmetrumllc/google-maps-api-key])
+
+if test -r "$GOOGLEKEYFILE" -a -s "$GOOGLEKEYFILE"; then
+       GOOGLEKEY='"'`cat "$GOOGLEKEYFILE"`'"'
+       HAVE_GOOGLE_KEY="yes"
+else
+       GOOGLEKEY='null'
+       HAVE_GOOGLE_KEY="no"
+fi
+
+AC_SUBST(GOOGLEKEY)
+
 AC_PROG_CC
 AC_PROG_INSTALL
 AC_PROG_LN_S
@@ -537,11 +551,12 @@ echo "    STlink support..............: ${HAVE_STLINK}"
 echo "    Local pdclib................: ${HAVE_PDCLIB}"
 echo "    i386 and amd64 libaltos.....: ${MULTI_ARCH}"
 echo ""
-echo "  Java paths"
+echo "  Java"
 echo "    freetts.....................: ${FREETTS}"
 echo "    jfreechart..................: ${JFREECHART}"
 echo "    jcommon.....................: ${JCOMMON}"
 echo "    JVM include.................: ${JVM_INCLUDE}"
+echo "    Google maps API key.........: ${HAVE_GOOGLE_KEY}"
 if test x${ANDROID_SDK} != "xno"; then
 echo ""
 echo "  Android path"