| | @@ -871,11 +871,63 @@ |
| 871 | 871 | { |
| 872 | 872 | args.errorMessage = e.toString(); |
| 873 | 873 | WhAjaj.Connector.sendHelper.onSendError( request, args ); |
| 874 | 874 | return undefined; |
| 875 | 875 | } |
| 876 | | - }/*jQuery*/ |
| 876 | + }/*jQuery*/, |
| 877 | + /** |
| 878 | + This is a concrete implementation of |
| 879 | + WhAjaj.Connector.prototype.sendImpl() which uses the rhino |
| 880 | + Java API to send requests and fetch the responses. |
| 881 | + |
| 882 | + Limitations vis-a-vis the interface: |
| 883 | + |
| 884 | + - timeouts are not supported. |
| 885 | + |
| 886 | + - asynchronous mode is not supported because implementing it |
| 887 | + requires the ability to kill a running thread (which is deprecated |
| 888 | + in the Java API). |
| 889 | + */ |
| 890 | + rhino:function(request,args) |
| 891 | + { |
| 892 | + var data = request || undefined; |
| 893 | + if( data ) { |
| 894 | + if('string'!==typeof data) { |
| 895 | + try { |
| 896 | + data = JSON.stringify(data); |
| 897 | + } |
| 898 | + catch(e) { |
| 899 | + WhAjaj.Connector.sendHelper.onSendError( request, args ); |
| 900 | + return; |
| 901 | + } |
| 902 | + } |
| 903 | + } |
| 904 | + var url = new java.net.URL( args.url ); |
| 905 | + var con; |
| 906 | + var IO = new JavaImporter(java.io); |
| 907 | + var wr; |
| 908 | + var rd, ln, json = []; |
| 909 | + try{ |
| 910 | + con = url.openConnection(); |
| 911 | + con.setDoOutput( true ); |
| 912 | + wr = new IO.OutputStreamWriter(con.getOutputStream()) |
| 913 | + wr.write(data); |
| 914 | + wr.flush(); |
| 915 | + rd = new IO.BufferedReader(new IO.InputStreamReader(con.getInputStream())); |
| 916 | + while ((line = rd.readLine()) != null) { |
| 917 | + json.push(line); |
| 918 | + } |
| 919 | + |
| 920 | + }catch(e){ |
| 921 | + args.errorMessage = e.toString(); |
| 922 | + WhAjaj.Connector.sendHelper.onSendError( request, args ); |
| 923 | + return undefined; |
| 924 | + } |
| 925 | + try { wr.close(); } catch(e) { /*ignore*/} |
| 926 | + try { rd.close(); } catch(e) { /*ignore*/} |
| 927 | + WhAjaj.Connector.sendHelper.onSendSuccess( request, json.join(''), args ); |
| 928 | + } |
| 877 | 929 | }; |
| 878 | 930 | |
| 879 | 931 | /** |
| 880 | 932 | An internal function which takes an object containing properties |
| 881 | 933 | for a WhAjaj.Connector network request. This function creates a new |
| | @@ -1029,6 +1081,7 @@ |
| 1029 | 1081 | can be replaced with a custom implementation if one follows the rules |
| 1030 | 1082 | described throughout this API. See WhAjaj.Connector.sendImpls for |
| 1031 | 1083 | the concrete implementations included with this API. |
| 1032 | 1084 | */ |
| 1033 | 1085 | WhAjaj.Connector.prototype.sendImpl = WhAjaj.Connector.sendImpls.XMLHttpRequest; |
| 1086 | +//WhAjaj.Connector.prototype.sendImpl = WhAjaj.Connector.sendImpls.rhino; |
| 1034 | 1087 | //WhAjaj.Connector.prototype.sendImpl = WhAjaj.Connector.sendImpls.jQuery; |
| 1035 | 1088 | |