Can send data to the server
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 09 May 2013 20:53:00 +0200
changeset 7541fc4499ba6a
parent 74 b4d67c7a8d83
child 76 b646af8e9c2a
Can send data to the server
json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
json/src/main/java/org/apidesign/html/json/impl/JSON.java
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java
json/src/main/java/org/apidesign/html/json/spi/JSONCall.java
json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java
ko-fx/src/main/java/org/apidesign/html/kofx/LoadJSON.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Thu May 09 20:30:25 2013 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Thu May 09 20:53:00 2013 +0200
     1.3 @@ -180,7 +180,7 @@
     1.4          int line = res.indexOf('\n');
     1.5          String msg;
     1.6          if (line >= 0) {
     1.7 -            msg = res.substring(0, line);
     1.8 +            msg = res.substring(line + 1);
     1.9              res = res.substring(0, line);
    1.10          } else {
    1.11              msg = res;
     2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Thu May 09 20:30:25 2013 +0200
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Thu May 09 20:53:00 2013 +0200
     2.3 @@ -94,11 +94,15 @@
     2.4          Context c, Runnable whenDone, Object[] result, 
     2.5          String urlBefore, String urlAfter
     2.6      ) {
     2.7 -        loadJSON(c, whenDone, result, urlBefore, urlAfter, null);
     2.8 +        loadJSON(c, whenDone, result, urlBefore, urlAfter, null, null);
     2.9      }
    2.10  
    2.11 -    public static void loadJSON(Context c, Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method) {
    2.12 -        JSONCall call = PropertyBindingAccessor.createCall(whenDone, result, urlBefore, urlAfter, method);
    2.13 +    public static void loadJSON(
    2.14 +        Context c, Runnable whenDone, Object[] result,
    2.15 +        String urlBefore, String urlAfter, String method,
    2.16 +        Object data
    2.17 +    ) {
    2.18 +        JSONCall call = PropertyBindingAccessor.createCall(whenDone, result, urlBefore, urlAfter, method, data);
    2.19          Transfer t = ContextAccessor.findTransfer(c);
    2.20          t.loadJSON(call);
    2.21      }
     3.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Thu May 09 20:30:25 2013 +0200
     3.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Thu May 09 20:53:00 2013 +0200
     3.3 @@ -849,8 +849,13 @@
     3.4              } else {
     3.5                  body.append("null");
     3.6              }
     3.7 -            if (!"GET".equals(onR.method())) {
     3.8 +            if (!"GET".equals(onR.method()) || dataMirror != null) {
     3.9                  body.append(", \"").append(onR.method()).append('"');
    3.10 +                if (dataMirror != null) {
    3.11 +                    body.append(", data");
    3.12 +                } else {
    3.13 +                    body.append(", null");
    3.14 +                }
    3.15              }
    3.16              body.append(");\n");
    3.17  //            body.append("  ").append(clazz.getSimpleName()).append(".").append(n).append("(");
     4.1 --- a/json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java	Thu May 09 20:30:25 2013 +0200
     4.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/PropertyBindingAccessor.java	Thu May 09 20:53:00 2013 +0200
     4.3 @@ -43,7 +43,7 @@
     4.4      protected abstract <M> PropertyBinding newBinding(PBData<M> d);
     4.5      protected abstract <M> FunctionBinding newFunction(FBData<M> d);
     4.6      protected abstract JSONCall newCall(
     4.7 -        Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method);
     4.8 +        Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method, Object data);
     4.9  
    4.10      
    4.11      static <M> PropertyBinding create(PBData<M> d) {
    4.12 @@ -53,8 +53,8 @@
    4.13          return DEFAULT.newFunction(d);
    4.14      }
    4.15      static JSONCall createCall(
    4.16 -        Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method) {
    4.17 -        return DEFAULT.newCall(whenDone, result, urlBefore, urlAfter, method);
    4.18 +        Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method, Object data) {
    4.19 +        return DEFAULT.newCall(whenDone, result, urlBefore, urlAfter, method, data);
    4.20      }
    4.21  
    4.22      public static final class PBData<M> {
     5.1 --- a/json/src/main/java/org/apidesign/html/json/spi/JSONCall.java	Thu May 09 20:30:25 2013 +0200
     5.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/JSONCall.java	Thu May 09 20:53:00 2013 +0200
     5.3 @@ -21,6 +21,9 @@
     5.4  
     5.5  package org.apidesign.html.json.spi;
     5.6  
     5.7 +import java.io.IOException;
     5.8 +import java.io.OutputStream;
     5.9 +
    5.10  /** Description of a JSON call request that is supposed to be processed
    5.11   * by {@link Transfer#loadJSON(org.apidesign.html.json.spi.JSONCall)} implementors.
    5.12   *
    5.13 @@ -32,13 +35,32 @@
    5.14      private final String urlBefore;
    5.15      private final String urlAfter;
    5.16      private final String method;
    5.17 +    private final Object data;
    5.18  
    5.19 -    JSONCall(Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method) {
    5.20 +    JSONCall(Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method, Object data) {
    5.21          this.whenDone = whenDone;
    5.22          this.result = result;
    5.23          this.urlBefore = urlBefore;
    5.24          this.urlAfter = urlAfter;
    5.25          this.method = method;
    5.26 +        this.data = data;
    5.27 +    }
    5.28 +    
    5.29 +    /** Do we have some data to send? Can the {@link #writeData(java.io.OutputStream)} method be 
    5.30 +     * called?
    5.31 +     * 
    5.32 +     * @return true, if the call has some data to send
    5.33 +     */
    5.34 +    public boolean isDoOutput() {
    5.35 +        return this.data != null;
    5.36 +    }
    5.37 +    
    5.38 +    public void writeData(OutputStream os) throws IOException {
    5.39 +        if (this.data == null) {
    5.40 +            throw new IOException("No data!");
    5.41 +        }
    5.42 +        os.write(this.data.toString().getBytes("UTF-8"));
    5.43 +        os.flush();
    5.44      }
    5.45      
    5.46      public String getMethod() {
     6.1 --- a/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Thu May 09 20:30:25 2013 +0200
     6.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/PropertyBinding.java	Thu May 09 20:53:00 2013 +0200
     6.3 @@ -49,8 +49,8 @@
     6.4              }
     6.5  
     6.6              @Override
     6.7 -            protected JSONCall newCall(Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method) {
     6.8 -                return new JSONCall(whenDone, result, urlBefore, urlAfter, method);
     6.9 +            protected JSONCall newCall(Runnable whenDone, Object[] result, String urlBefore, String urlAfter, String method, Object data) {
    6.10 +                return new JSONCall(whenDone, result, urlBefore, urlAfter, method, data);
    6.11              }
    6.12          };
    6.13      }
     7.1 --- a/ko-fx/src/main/java/org/apidesign/html/kofx/LoadJSON.java	Thu May 09 20:30:25 2013 +0200
     7.2 +++ b/ko-fx/src/main/java/org/apidesign/html/kofx/LoadJSON.java	Thu May 09 20:53:00 2013 +0200
     7.3 @@ -23,6 +23,7 @@
     7.4  import java.io.IOException;
     7.5  import java.io.InputStream;
     7.6  import java.io.InputStreamReader;
     7.7 +import java.io.OutputStream;
     7.8  import java.io.PushbackInputStream;
     7.9  import java.io.Reader;
    7.10  import java.net.HttpURLConnection;
    7.11 @@ -42,7 +43,6 @@
    7.12  import org.json.JSONException;
    7.13  import org.json.JSONObject;
    7.14  import org.json.JSONTokener;
    7.15 -import org.openide.util.Exceptions;
    7.16  
    7.17  /** This is an implementation package - just
    7.18   * include its JAR on classpath and use official {@link Context} API
    7.19 @@ -100,6 +100,12 @@
    7.20                  if (call.getMethod() != null) {
    7.21                      huc.setRequestMethod(call.getMethod());
    7.22                  }
    7.23 +                if (call.isDoOutput()) {
    7.24 +                    huc.setDoOutput(true);
    7.25 +                    final OutputStream os = huc.getOutputStream();
    7.26 +                    call.writeData(os);
    7.27 +                    os.flush();
    7.28 +                }
    7.29              }
    7.30              final PushbackInputStream is = new PushbackInputStream(
    7.31                  conn.getInputStream(), 1