2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.ko2brwsr;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import org.apidesign.html.json.spi.JSONCall;
25 import org.apidesign.html.json.spi.Transfer;
26 import org.apidesign.html.json.spi.WSTransfer;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 final class BrwsrCtxImpl implements Transfer, WSTransfer<LoadWS> {
33 private BrwsrCtxImpl() {}
35 public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
38 public void extract(Object obj, String[] props, Object[] values) {
39 ConvertTypes.extractJSON(obj, props, values);
43 public void loadJSON(final JSONCall call) {
44 class R implements Runnable {
45 final boolean success;
47 public R(boolean success) {
48 this.success = success;
51 Object[] arr = { null };
55 call.notifySuccess(arr[0]);
58 if (arr[0] instanceof Throwable) {
59 t = (Throwable) arr[0];
62 t = new IOException();
64 t = new IOException(arr[0].toString());
71 R success = new R(true);
72 R failure = new R(false);
74 String me = ConvertTypes.createJSONP(success.arr, success);
75 ConvertTypes.loadJSONP(call.composeURL(me), me);
78 if (call.isDoOutput()) {
80 ByteArrayOutputStream bos = new ByteArrayOutputStream();
82 data = new String(bos.toByteArray(), "UTF-8");
83 } catch (IOException ex) {
87 ConvertTypes.loadJSON(call.composeURL(null), success.arr, success, failure, call.getMethod(), data);
92 public Object toJSON(InputStream is) throws IOException {
93 StringBuilder sb = new StringBuilder();
94 InputStreamReader r = new InputStreamReader(is);
102 return ConvertTypes.parse(sb.toString());
106 public LoadWS open(String url, JSONCall callback) {
107 return new LoadWS(callback, url);
111 public void send(LoadWS socket, JSONCall data) {
116 public void close(LoadWS socket) {