diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-29 05:29:23 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2010-03-29 05:29:23 +0000 |
commit | 0b7e1327b4c79940f7626e9127ae213a76540eb4 (patch) | |
tree | 345b6bb68448755c937b08d81395a102addbd960 /sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc | |
parent | 3f309fc3c335346e601458b65949f5efcba467c2 (diff) |
TUSCANY-3516 - Adding support for invoking JSON-RPC services via GET HTTP method
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@928594 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc')
-rw-r--r-- | sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java | 72 |
1 files changed, 55 insertions, 17 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java index b3e2e39c19..00d9c9c8dd 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java @@ -24,6 +24,9 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.Calendar; +import java.util.Date; import java.util.List; import javax.servlet.ServletConfig; @@ -44,6 +47,8 @@ import org.json.JSONException; import org.json.JSONObject; import org.oasisopen.sca.ServiceRuntimeException; +import com.sun.xml.internal.messaging.saaj.util.Base64; + /** * Servlet that handles JSON-RPC requests invoking SCA services. * @@ -105,14 +110,6 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { } private void handleServiceRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { - // Encode using UTF-8, although We are actually ASCII clean as - // all unicode data is JSON escaped using backslash u. This is - // less data efficient for foreign character sets but it is - // needed to support naughty browsers such as Konqueror and Safari - // which do not honour the charset set in the response - response.setContentType("text/plain;charset=utf-8"); - OutputStream out = response.getOutputStream(); - // Decode using the charset in the request if it exists otherwise // use UTF-8 as this is what all browser implementations use. // The JSON-RPC-Java JavaScript client is ASCII clean so it @@ -122,18 +119,46 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { if (charset == null) { charset = "UTF-8"; } - BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset)); - - // Read the request + CharArrayWriter data = new CharArrayWriter(); - char[] buf = new char[4096]; - int ret; - while ((ret = in.read(buf, 0, 4096)) != -1) { - data.write(buf, 0, ret); - } + if (request.getMethod().equals("GET")) { + // if using GET Support (see http://groups.google.com/group/json-rpc/web/json-rpc-over-http) + + //parse the GET QueryString + try { + String params = Base64.base64Decode(URLDecoder.decode(request.getParameter("params"),charset)); + + StringBuffer sb = new StringBuffer(); + sb.append("{"); + sb.append("\"method\": \"" + request.getParameter("method") + "\","); + sb.append("\"params\": " + params + ","); + sb.append("\"id\":" + request.getParameter("id")); + sb.append("}"); + + data.write(sb.toString().toCharArray(), 0, sb.length()); + } catch (Exception e) { + //FIXME Exceptions are not handled correctly here + // They should be reported to the client JavaScript as proper + // JavaScript exceptions. + throw new RuntimeException("Unable to parse request", e); + } + + } else { + // default POST style + BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset)); + // Read the request into charArray + char[] buf = new char[4096]; + int ret; + while ((ret = in.read(buf, 0, 4096)) != -1) { + data.write(buf, 0, ret); + } + + } + JSONObject jsonReq = null; String method = null; + //parse the JSON payload try { jsonReq = new JSONObject(data.toString()); method = jsonReq.getString("method"); @@ -144,7 +169,6 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { throw new RuntimeException("Unable to parse request", e); } - // check if it's a system request // or a method invocation byte[] bout; @@ -159,6 +183,20 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { } // Send response to client + // Encode using UTF-8, although We are actually ASCII clean as + // all unicode data is JSON escaped using backslash u. This is + // less data efficient for foreign character sets but it is + // needed to support naughty browsers such as Konqueror and Safari + // which do not honour the charset set in the response + response.setContentType("text/plain;charset=utf-8"); + + //set Cache-Control to no-cache to avoid intermediary + //proxy/reverse-proxy caches and always hit the server + //that would identify if the value was current or not + response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Expires", new Date(0).toGMTString()); + + OutputStream out = response.getOutputStream(); out.write(bout); out.flush(); out.close(); |