1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Mon Apr 08 15:14:29 2013 +0200
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Mon Apr 08 16:51:30 2013 +0200
1.3 @@ -73,7 +73,32 @@
1.4 private static Object getProperty(Object object, String property) {
1.5 return null;
1.6 }
1.7 +
1.8 + public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
1.9 + int h = whenDone.hashCode();
1.10 + String name;
1.11 + for (;;) {
1.12 + name = "jsonp" + Integer.toHexString(h);
1.13 + if (defineIfUnused(name, jsonResult, whenDone)) {
1.14 + return name;
1.15 + }
1.16 + h++;
1.17 + }
1.18 + }
1.19
1.20 + @JavaScriptBody(args = { "name", "arr", "run" }, body =
1.21 + "if (window[name]) return false;\n "
1.22 + + "window[name] = function(data) {\n "
1.23 + + " arr[0] = data;\n"
1.24 + + " run.run__V();\n"
1.25 + + " delete window[name];\n"
1.26 + + "};"
1.27 + + "return true;\n"
1.28 + )
1.29 + private static boolean defineIfUnused(String name, Object[] arr, Runnable run) {
1.30 + return true;
1.31 + }
1.32 +
1.33 @JavaScriptBody(args = { "url", "arr", "callback" }, body = ""
1.34 + "var request = new XMLHttpRequest();\n"
1.35 + "request.open('GET', url, true);\n"
1.36 @@ -89,11 +114,33 @@
1.37 + "};"
1.38 + "request.send();"
1.39 )
1.40 - public static void loadJSON(
1.41 + private static void loadJSON(
1.42 String url, Object[] jsonResult, Runnable whenDone
1.43 ) {
1.44 }
1.45
1.46 + public static void loadJSON(
1.47 + String url, Object[] jsonResult, Runnable whenDone, String jsonp
1.48 + ) {
1.49 + if (jsonp == null) {
1.50 + loadJSON(url, jsonResult, whenDone);
1.51 + } else {
1.52 + loadJSONP(url, jsonp);
1.53 + }
1.54 + }
1.55 +
1.56 + @JavaScriptBody(args = { "url", "jsonp" }, body =
1.57 + "var scrpt = window.document.createElement('script');\n "
1.58 + + "scrpt.setAttribute('src', url);\n "
1.59 + + "scrpt.setAttribute('id', jsonp);\n "
1.60 + + "scrpt.setAttribute('type', 'text/javascript');\n "
1.61 + + "var body = document.getElementsByTagName('body')[0];\n "
1.62 + + "body.appendChild(scrpt);\n"
1.63 + )
1.64 + private static void loadJSONP(String url, String jsonp) {
1.65 +
1.66 + }
1.67 +
1.68 public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
1.69 for (int i = 0; i < props.length; i++) {
1.70 values[i] = getProperty(jsonObject, props[i]);
2.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Apr 08 15:14:29 2013 +0200
2.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Apr 08 16:51:30 2013 +0200
2.3 @@ -901,13 +901,26 @@
2.4 String n = e.getSimpleName().toString();
2.5 body.append("public void ").append(n).append("(");
2.6 StringBuilder assembleURL = new StringBuilder();
2.7 + String jsonpVarName = null;
2.8 {
2.9 String sep = "";
2.10 + boolean skipJSONP = onR.jsonp().isEmpty();
2.11 for (String p : findParamNames(e, onR.url(), assembleURL)) {
2.12 + if (!skipJSONP && p.equals(onR.jsonp())) {
2.13 + skipJSONP = true;
2.14 + jsonpVarName = p;
2.15 + continue;
2.16 + }
2.17 body.append(sep);
2.18 body.append("String ").append(p);
2.19 sep = ", ";
2.20 }
2.21 + if (!skipJSONP) {
2.22 + err().printMessage(Diagnostic.Kind.ERROR,
2.23 + "Name of jsonp attribute ('" + onR.jsonp() +
2.24 + "') is not used in url attribute '" + onR.url() + "'"
2.25 + );
2.26 + }
2.27 }
2.28 body.append(") {\n");
2.29 body.append(" final Object[] result = { null };\n");
2.30 @@ -944,9 +957,14 @@
2.31 " }\n" +
2.32 " }\n"
2.33 );
2.34 + body.append(" ProcessResult pr = new ProcessResult();\n");
2.35 + if (jsonpVarName != null) {
2.36 + body.append(" String ").append(jsonpVarName).
2.37 + append(" = org.apidesign.bck2brwsr.htmlpage.ConvertTypes.createJSONP(result, pr);\n");
2.38 + }
2.39 body.append(" org.apidesign.bck2brwsr.htmlpage.ConvertTypes.loadJSON(\n ");
2.40 body.append(assembleURL);
2.41 - body.append(", result, new ProcessResult()\n );\n");
2.42 + body.append(", result, pr, ").append(jsonpVarName).append("\n );\n");
2.43 // body.append(" ").append(clazz.getSimpleName()).append(".").append(n).append("(");
2.44 // body.append(wrapParams(e, null, className, "ev", "data"));
2.45 // body.append(");\n");
3.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnReceive.java Mon Apr 08 15:14:29 2013 +0200
3.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnReceive.java Mon Apr 08 16:51:30 2013 +0200
3.3 @@ -38,4 +38,17 @@
3.4 * @return the (possibly parametrized) url to connect to
3.5 */
3.6 String url();
3.7 +
3.8 + /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
3.9 + * a callback from the server generated page to a function defined in the
3.10 + * system. The name of such function is usually specified as a property
3.11 + * (of possibly different names). By defining the <code>jsonp</code> attribute
3.12 + * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
3.13 + * transmission and specifies the name of the property. The property should
3.14 + * also be used in the {@link #url()} attribute on appropriate place.
3.15 + *
3.16 + * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
3.17 + * callback function.
3.18 + */
3.19 + String jsonp() default "";
3.20 }
4.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Mon Apr 08 15:14:29 2013 +0200
4.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Mon Apr 08 16:51:30 2013 +0200
4.3 @@ -171,6 +171,34 @@
4.4 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
4.5 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
4.6 }
4.7 +
4.8 + @OnReceive(url="/{url}?callme={me}", jsonp = "me")
4.9 + static void fetchViaJSONP(Person p, JSONik model) {
4.10 + model.setFetched(p);
4.11 + }
4.12 +
4.13 + @Http(@Http.Resource(
4.14 + content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
4.15 + path="/person.json",
4.16 + mimeType = "application/javascript",
4.17 + parameters = { "callme" }
4.18 + ))
4.19 + @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
4.20 + if (js == null) {
4.21 + js = new JSONik();
4.22 + js.applyBindings();
4.23 +
4.24 + js.fetchViaJSONP("person.json");
4.25 + }
4.26 +
4.27 + Person p = js.getFetched();
4.28 + if (p == null) {
4.29 + throw new InterruptedException();
4.30 + }
4.31 +
4.32 + assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
4.33 + // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
4.34 + }
4.35
4.36 @Http(@Http.Resource(
4.37 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
5.1 --- a/javaquery/demo-twitter/src/main/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClient.java Mon Apr 08 15:14:29 2013 +0200
5.2 +++ b/javaquery/demo-twitter/src/main/java/org/apidesign/bck2brwsr/demo/twitter/TwitterClient.java Mon Apr 08 16:51:30 2013 +0200
5.3 @@ -57,7 +57,7 @@
5.4 public static final class TwttrQr {
5.5 }
5.6
5.7 - @OnReceive(url="{url}")
5.8 + @OnReceive(url="{root}/search.json?{query}&callback={me}", jsonp="me")
5.9 static void queryTweets(TwitterModel page, TwitterQuery q) {
5.10 page.getCurrentTweets().clear();
5.11 page.getCurrentTweets().addAll(q.getResults());
5.12 @@ -67,7 +67,7 @@
5.13 static void refreshTweets(TwitterModel model) {
5.14 Tweeters people = model.getActiveTweeters();
5.15 StringBuilder sb = new StringBuilder();
5.16 - sb.append("http://search.twitter.com/search.json?rpp=25&q=");
5.17 + sb.append("rpp=25&q=");
5.18 String sep = "";
5.19 for (String p : people.getUserNames()) {
5.20 sb.append(sep);
5.21 @@ -75,7 +75,7 @@
5.22 sb.append(p);
5.23 sep = " OR ";
5.24 }
5.25 - model.queryTweets(sb.toString());
5.26 + model.queryTweets("http://search.twitter.com", sb.toString());
5.27 }
5.28
5.29 private static Tweeters tweeters(String listName, String... userNames) {
6.1 --- a/javaquery/demo-twitter/src/test/java/org/apidesign/bck2brwsr/demo/twitter/TwitterProtocolTest.java Mon Apr 08 15:14:29 2013 +0200
6.2 +++ b/javaquery/demo-twitter/src/test/java/org/apidesign/bck2brwsr/demo/twitter/TwitterProtocolTest.java Mon Apr 08 16:51:30 2013 +0200
6.3 @@ -29,9 +29,10 @@
6.4 public class TwitterProtocolTest {
6.5 private TwitterModel page;
6.6 @Http(@Http.Resource(
6.7 - path = "/test.tweet",
6.8 + path = "/search.json",
6.9 mimeType = "application/json",
6.10 - content = "{\"completed_in\":0.04,\"max_id\":320055706885689344,\"max_id_str\""
6.11 + parameters = {"callback"},
6.12 + content = "$0({\"completed_in\":0.04,\"max_id\":320055706885689344,\"max_id_str\""
6.13 + ":\"320055706885689344\",\"page\":1,\"query\":\"from%3AJaroslavTulach\",\"refresh_url\":"
6.14 + "\"?since_id=320055706885689344&q=from%3AJaroslavTulach\","
6.15 + "\"results\":[{\"created_at\":\"Fri, 05 Apr 2013 06:10:01 +0000\","
6.16 @@ -68,13 +69,13 @@
6.17 + "\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1656828312\\/jst_normal.gif\","
6.18 + "\"source\":\"<a href="http:\\/\\/twitter.com\\/">web<\\/a>\",\"text\":"
6.19 + "\"Math proofs without words. Ingenious: http:\\/\\/t.co\\/sz7yVbfpGw\"}],\"results_per_page\":100,"
6.20 - + "\"since_id\":0,\"since_id_str\":\"0\"}"
6.21 + + "\"since_id\":0,\"since_id_str\":\"0\"})"
6.22 ))
6.23 @BrwsrTest public void readFromTwttr() throws InterruptedException {
6.24 if (page == null) {
6.25 page = new TwitterModel();
6.26 page.applyBindings();
6.27 - page.queryTweets("/test.tweet");
6.28 + page.queryTweets("", "q=xyz");
6.29 }
6.30
6.31 if (page.getCurrentTweets().isEmpty()) {
7.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Mon Apr 08 15:14:29 2013 +0200
7.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Mon Apr 08 16:51:30 2013 +0200
7.3 @@ -178,7 +178,15 @@
7.4 LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
7.5 response.setContentType(r.httpType);
7.6 r.httpContent.reset();
7.7 - copyStream(r.httpContent, response.getOutputStream(), null);
7.8 + String[] params = null;
7.9 + if (r.parameters.length != 0) {
7.10 + params = new String[r.parameters.length];
7.11 + for (int i = 0; i < r.parameters.length; i++) {
7.12 + params[i] = request.getParameter(r.parameters[i]);
7.13 + }
7.14 + }
7.15 +
7.16 + copyStream(r.httpContent, response.getOutputStream(), null, params);
7.17 }
7.18 }
7.19 }
7.20 @@ -316,7 +324,7 @@
7.21 }
7.22 if (ch == '$' && params.length > 0) {
7.23 int cnt = is.read() - '0';
7.24 - if (cnt == 'U' - '0') {
7.25 + if (baseURL != null && cnt == 'U' - '0') {
7.26 os.write(baseURL.getBytes("UTF-8"));
7.27 } else {
7.28 if (cnt >= 0 && cnt < params.length) {
8.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Mon Apr 08 15:14:29 2013 +0200
8.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Mon Apr 08 16:51:30 2013 +0200
8.3 @@ -55,11 +55,11 @@
8.4 /** HTTP resource to be available during execution. An invocation may
8.5 * perform an HTTP query and obtain a resource relative to the page.
8.6 */
8.7 - public void addHttpResource(String relativePath, String mimeType, InputStream content) {
8.8 - if (relativePath == null || mimeType == null || content == null) {
8.9 + public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
8.10 + if (relativePath == null || mimeType == null || content == null || parameters == null) {
8.11 throw new NullPointerException();
8.12 }
8.13 - resources.add(new Resource(content, mimeType, relativePath));
8.14 + resources.add(new Resource(content, mimeType, relativePath, parameters));
8.15 }
8.16
8.17 /** Invokes the associated method.
8.18 @@ -100,12 +100,16 @@
8.19 final InputStream httpContent;
8.20 final String httpType;
8.21 final String httpPath;
8.22 + final String[] parameters;
8.23
8.24 - Resource(InputStream httpContent, String httpType, String httpPath) {
8.25 + Resource(InputStream httpContent, String httpType, String httpPath,
8.26 + String[] parameters
8.27 + ) {
8.28 httpContent.mark(Integer.MAX_VALUE);
8.29 this.httpContent = httpContent;
8.30 this.httpType = httpType;
8.31 this.httpPath = httpPath;
8.32 + this.parameters = parameters;
8.33 }
8.34 }
8.35 }
9.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java Mon Apr 08 15:14:29 2013 +0200
9.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java Mon Apr 08 16:51:30 2013 +0200
9.3 @@ -53,5 +53,10 @@
9.4 String resource() default "";
9.5 /** mime type of the resource */
9.6 String mimeType();
9.7 + /** query parameters. Can be referenced from the {@link #content} as
9.8 + * <code>$0</code>, <code>$1</code>, etc. The values will be extracted
9.9 + * from URL parameters of the request.
9.10 + */
9.11 + String[] parameters() default {};
9.12 }
9.13 }
10.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Mon Apr 08 15:14:29 2013 +0200
10.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Mon Apr 08 16:51:30 2013 +0200
10.3 @@ -65,10 +65,10 @@
10.4 for (Http.Resource r : http) {
10.5 if (!r.content().isEmpty()) {
10.6 InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
10.7 - c.addHttpResource(r.path(), r.mimeType(), is);
10.8 + c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
10.9 } else {
10.10 InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
10.11 - c.addHttpResource(r.path(), r.mimeType(), is);
10.12 + c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
10.13 }
10.14 }
10.15 }