Sunday, October 14, 2007

Dummy Functions of Javascript Library

Currently, I have built all dummy javascript functions based on JavaCOG. The classes in JavaCOG are mapped to objects in javascript and all methods are mapped as well. However, there is a problem. In Java, a function can be defined multiply times so long as the function signatures are different. In javascript, function overriding is not supported. As a result, in the future we must distinguish the different cases by passing specified parameters.

Current focus is how to make javascript interact with the server. I have several ideas:

(1) Using xmlhttprequest in Javascript
   When a user needs to send request to server, we can use xmlhttprequestobject. However, for security reason, xmlhttprequest has limitations. For example, xmlhttprequest can only send requests to the domain from which the javascript comes. If a javascript program from google.com wants to send requests to a host in microsoft.com domain by using xmlhttprequest object, it is prohibited by default. Of course, uses can allow the access by modifying their browser security configurations. However, I don't think lots of users are willing to do that. As a result, only one server can exist to provide service.
    Advantages: programmers can choose any format they like to encode the requests. JSON is a choice which is better than XML.

(2) Using IFrame element
   By dynamically modifying the source URL of IFrame element, clients can send http GET requests to server. If form is embedded in the IFrame element, clients can send HTTP POST requests. If GET method is used, we can customize query part of the URL so that request parameters are included. If POST method is used, we can specify parameters by setting values of fields.
   Advantages: it does not have the domain access limitation described above.
   Disadvantages: If we use GET method, there are some limitations imposed on the URL such as length, characters.... If we use POST method, we can not control the encoding of the submitted data.

    Of course, we can use basic form to submit the requests. However, we want to build a Web 2.0 Javascript library, so ...

    Actually, javascript at client side invokes the corresponding functions at server side. So I call it Javascript RPC. There exist two main formats of messages transmitted between client and server: XML-RPC and JSON-RPC.
    Ajax Pattern(http://ajaxpatterns.org/wiki/index.php?title=AJAXFrameworks) lists many Ajax-related frameworks among which some satisfy our requirements.

(1) XML-RPC
    http://www.xmlrpc.com/
    I found serveral javascript libraries which support XML-RPC.
    xml-rpc http://www.scottandrew.com/xml-rpc/
    @tomic http://atomic.jbrisbin.com/ Actually, it is a Web 2.x Ajax Framework.
    vcXMLRPC: http://www.vcdn.org/Public/XMLRPC/ The latest version was released in 2001.
    jsxmlrpc:  http://kuriositaet.de/javascript/jsxmlrpc.html

(2) JSON-RPC

http://json-rpc.org/  On this website, there is a specification about JSON-RPC. In addition, some implementations written in different languages are listed.
Besides the client-side support of JSON-RPC, some frameworks go further to provide server-side support.
    JSON-RPC-Java:  http://oss.metaparadigm.com/jsonrpc/
    "JSON-RPC-Java is a key piece of Java web application middleware that allows JavaScript DHTML web applications to call remote methods in a Java Application Server without the need for page reloading (now refered to as AJAX)."
    Now, JSON-RPC-Java has been merged with Jabsorb http://code.google.com/p/jabsorb/.

Some libraries implement both XML-RPC and JSON-RPC:
    JSXML-RPC: http://phpxmlrpc.sourceforge.net/jsxmlrpc/
    JS o lait: http://jsolait.net/. Actually, this library provides more than RPC. It provides codec, crypto, HTML forms ...  Part of JSON-RPC-Java is based on Js o lait.

I have not investigated what format the following frameworks use:
    JSRS -- Javascript Remote Scripting http://www.ashleyit.com/rs/main.htm 
    DWR -- Direct Web Remoting https://dwr.dev.java.net/

No comments: