diff options
Diffstat (limited to '')
2 files changed, 78 insertions, 18 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(); diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java index 946cd60745..7c6ba6a99f 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java @@ -19,9 +19,11 @@ package org.apache.tuscany.sca.binding.jsonrpc; import java.io.ByteArrayInputStream; +import java.net.URLEncoder; import junit.framework.Assert; +import org.apache.axiom.om.util.Base64; import org.apache.tuscany.sca.node.Contribution; import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; @@ -31,6 +33,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.PostMethodWebRequest; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebRequest; @@ -75,5 +78,24 @@ public class JSONRPCServiceTestCase{ JSONObject jsonResp = new JSONObject(response.getText()); Assert.assertEquals("echo: Hello JSON-RPC", jsonResp.getString("result")); - } + } + + @Test + public void testJSONRPCBindingGET() throws Exception { + String params = Base64.encode("[\"Hello JSON-RPC\"]".getBytes()); + String queryString = "?method=echo¶ms=" + URLEncoder.encode(params,"UTF-8") + "&id=1"; + + WebConversation wc = new WebConversation(); + WebRequest request = new GetMethodWebRequest(SERVICE_URL + queryString); + WebResponse response = wc.getResource(request); + + Assert.assertEquals(200, response.getResponseCode()); + + JSONObject jsonResp = new JSONObject(response.getText()); + Assert.assertEquals("echo: Hello JSON-RPC", jsonResp.getString("result")); + } + + + + }
\ No newline at end of file |