|
| 1 | +package com.google.appengine.demos.asyncrest; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.math.BigDecimal; |
| 5 | +import java.math.RoundingMode; |
| 6 | +import java.net.URLEncoder; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Queue; |
| 10 | + |
| 11 | +import javax.servlet.ServletConfig; |
| 12 | +import javax.servlet.ServletException; |
| 13 | +import javax.servlet.UnavailableException; |
| 14 | +import javax.servlet.http.HttpServlet; |
| 15 | +import javax.servlet.http.HttpServletRequest; |
| 16 | +import javax.servlet.http.HttpServletResponse; |
| 17 | + |
| 18 | +/** |
| 19 | + * AbstractRestServlet. |
| 20 | + * |
| 21 | + */ |
| 22 | +public class AbstractRestServlet extends HttpServlet { |
| 23 | + |
| 24 | + protected final static int MAX_RESULTS = 5; |
| 25 | + |
| 26 | + protected final static String STYLE = "<style type='text/css'>" |
| 27 | + + " img.thumb:hover {height:50px}" |
| 28 | + + " img.thumb {vertical-align:text-top}" |
| 29 | + + " span.red {color: #ff0000}" |
| 30 | + + " span.green {color: #00ff00}" |
| 31 | + + " iframe {border: 0px}" + "</style>"; |
| 32 | + |
| 33 | + protected final static String APPKEY = "com.google.appengine.demos.asyncrest.appKey"; |
| 34 | + protected final static String APPKEY_ENV = "PLACES_APPKEY"; |
| 35 | + protected final static String LOC_PARAM = "loc"; |
| 36 | + protected final static String ITEMS_PARAM = "items"; |
| 37 | + protected final static String LATITUDE_PARAM = "lat"; |
| 38 | + protected final static String LONGITUDE_PARAM = "long"; |
| 39 | + protected final static String RADIUS_PARAM = "radius"; |
| 40 | + protected String key; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void init(ServletConfig servletConfig) throws ServletException { |
| 44 | + //first try the servlet context init-param |
| 45 | + String source = "InitParameter"; |
| 46 | + key = servletConfig.getInitParameter(APPKEY); |
| 47 | + if (key == null || key.startsWith("${")) { |
| 48 | + source = "System Property"; |
| 49 | + key = System.getProperty(APPKEY); |
| 50 | + } |
| 51 | + if (key == null || key.startsWith("${")) { |
| 52 | + source = "Environment Variable"; |
| 53 | + key = System.getenv(APPKEY_ENV); |
| 54 | + } |
| 55 | + if (key == null) { |
| 56 | + throw new UnavailableException("Places App Key not set"); |
| 57 | + } |
| 58 | + if (key.startsWith("${")) { |
| 59 | + throw new UnavailableException("Places App Key not expanded from " + source); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static String sanitize(String s) { |
| 64 | + if (s == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + return s.replace("<", "?").replace("&", "?").replace("\n", "?"); |
| 68 | + } |
| 69 | + |
| 70 | + protected String restQuery(String coordinates, String radius, String item) { |
| 71 | + try { |
| 72 | + return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key + "&location=" |
| 73 | + + URLEncoder.encode(coordinates, "UTF-8") + "&types=" + URLEncoder.encode(item, "UTF-8") |
| 74 | + + "&radius=" + URLEncoder.encode(radius, "UTF-8"); |
| 75 | + |
| 76 | + } catch (Exception e) { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public String generateResults(Queue<Map<String, Object>> results) { |
| 82 | + StringBuilder thumbs = new StringBuilder(); |
| 83 | + int resultCount = 0; |
| 84 | + Iterator<Map<String, Object>> itor = results.iterator(); |
| 85 | + |
| 86 | + while (resultCount < MAX_RESULTS && itor.hasNext()) { |
| 87 | + Map m = (Map) itor.next(); |
| 88 | + String name = (String) m.get("name"); |
| 89 | + Object[] photos = (Object[]) m.get("photos"); |
| 90 | + if (photos != null && photos.length > 0) { |
| 91 | + resultCount++; |
| 92 | + thumbs.append("<img class='thumb' border='1px' height='40px'" + " src='" |
| 93 | + + getPhotoURL((String) (((Map) photos[0]).get("photo_reference"))) + "'" + " title='" + name |
| 94 | + + "'" + "/>"); |
| 95 | + thumbs.append("</a> "); |
| 96 | + } |
| 97 | + } |
| 98 | + return thumbs.toString(); |
| 99 | + } |
| 100 | + |
| 101 | + public String getPhotoURL(String photoref) { |
| 102 | + return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref |
| 103 | + + "&maxheight=40"; |
| 104 | + } |
| 105 | + |
| 106 | + protected String ms(long nano) { |
| 107 | + BigDecimal dec = new BigDecimal(nano); |
| 108 | + return dec.divide(new BigDecimal(1000000L)).setScale(1, RoundingMode.UP).toString(); |
| 109 | + } |
| 110 | + |
| 111 | + protected int width(long nano) { |
| 112 | + int w = (int) ((nano + 999999L) / 5000000L); |
| 113 | + if (w == 0) { |
| 114 | + w = 2; |
| 115 | + } |
| 116 | + return w; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 121 | + throws ServletException, IOException { |
| 122 | + doGet(request, response); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments