diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-05 07:22:02 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-05 07:22:02 +0000 |
commit | c5e4337f496dd4a9ef2ef2dac9a783e2e2be2d66 (patch) | |
tree | bbea8c49dd9fd18b2ac0c53c6509eb18c123888f /branches/sca-java-1.x/modules/binding-http-new-runtime/src/main | |
parent | 841b4c7adf6c13158b17909b8208c59da793e376 (diff) |
TUSCANY-2968 - JSONRPC invocation using operation selector and wire formats working end to end
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@771601 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches/sca-java-1.x/modules/binding-http-new-runtime/src/main')
-rw-r--r-- | branches/sca-java-1.x/modules/binding-http-new-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPRRBListenerServlet.java | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/branches/sca-java-1.x/modules/binding-http-new-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPRRBListenerServlet.java b/branches/sca-java-1.x/modules/binding-http-new-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPRRBListenerServlet.java index 89bea9f75a..605e2c6211 100644 --- a/branches/sca-java-1.x/modules/binding-http-new-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPRRBListenerServlet.java +++ b/branches/sca-java-1.x/modules/binding-http-new-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPRRBListenerServlet.java @@ -19,16 +19,18 @@ package org.apache.tuscany.sca.binding.http.provider; +import java.io.BufferedReader; +import java.io.CharArrayWriter; import java.io.IOException; +import java.io.InputStreamReader; import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.http.HTTPBindingContext; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.invocation.MessageFactory; @@ -36,7 +38,7 @@ import org.apache.tuscany.sca.invocation.MessageFactory; public class HTTPRRBListenerServlet extends HttpServlet { private static final long serialVersionUID = 6688524143716091739L; - + transient private Binding binding; transient private Invoker bindingInvoker; transient private MessageFactory messageFactory; @@ -49,18 +51,52 @@ public class HTTPRRBListenerServlet extends HttpServlet { this.bindingInvoker = bindingInvoker; this.messageFactory = messageFactory; } - - public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - + + public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // 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 + // although here we can correctly handle data from other clients + // that do not escape non ASCII data + String charset = request.getCharacterEncoding(); + 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); + } + + HTTPBindingContext bindingContext = new HTTPBindingContext(); + bindingContext.setHttpRequest(request); + bindingContext.setHttpResponse(response); + // Dispatch the service interaction to the service invoker Message requestMessage = messageFactory.createMessage(); - requestMessage.setBody(new Object[]{request, response}); + requestMessage.setBindingContext(bindingContext); + requestMessage.setBody(new Object[]{data}); + Message responseMessage = bindingInvoker.invoke(requestMessage); + + // return response to client if (responseMessage.isFault()) { // Turn a fault into an exception //throw new ServletException((Throwable)responseMessage.getBody()); Throwable e = (Throwable)responseMessage.getBody(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); - } + } else { + byte[] bout; + bout = responseMessage.<Object>getBody().toString().getBytes("UTF-8"); + response.getOutputStream().write(bout); + response.getOutputStream().flush(); + response.getOutputStream().close(); + } } + } |