Permalink
Browse files

Add JSONP support with a callback=? query string parameter.

  • Loading branch information...
1 parent d8cd5e6 commit 7647de061968d88b90af3cb2c3b9a1b99440a2ae @mattb committed Mar 22, 2013
Showing with 7 additions and 1 deletion.
  1. +7 −1 src/main/java/com/hackdiary/geo/FlickrGeocodeServlet.java
@@ -1,6 +1,7 @@
package com.hackdiary.geo;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.util.JSONPObject;
import java.io.*;
import java.util.*;
import java.net.*;
@@ -30,7 +31,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
resp.setContentType("application/json");
resp.setHeader("Access-Control-Allow-Origin", "*");
ObjectMapper mapper = new ObjectMapper();
- mapper.writerWithDefaultPrettyPrinter().writeValue(resp.getWriter(), geocode.geocode(lat, lng));
+ Object result = geocode.geocode(lat, lng);
+ if(req.getParameter("callback") != null) {
+ result = new JSONPObject(req.getParameter("callback"), result);
+ }
+ mapper.writerWithDefaultPrettyPrinter().writeValue(resp.getWriter(), result);
}
public static void main(String[] args) throws Exception{
@@ -46,6 +51,7 @@ public static void main(String[] args) throws Exception{
}
context.addServlet(new ServletHolder(servlet),"/*");
server.start();
+ System.out.println("Tree loaded. Total memory (MB): " + (Runtime.getRuntime().totalMemory() / 1024 / 1024.0));
server.join();
}
}

0 comments on commit 7647de0

Please sign in to comment.