diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2012-06-16 18:13:58 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2012-06-16 18:13:58 +0000 |
commit | 9b335d1eec8f9844d481999c7d3a9ea4dc2d0c89 (patch) | |
tree | 35afd4075fde4d5d448f739961909ddc0397ef8e /sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main | |
parent | 6eff900741dfdc75dd8e89485339b6193ab95fc4 (diff) |
Enhancing widget invoker to handled more details of what resource is being requested. This logic was present on a specific servlet from the binding, but makes more sense to be together with the widget implementation
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1350974 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main')
-rw-r--r-- | sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java b/sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java index b92c35aa8d..394f12c8b1 100644 --- a/sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java +++ b/sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java @@ -23,10 +23,16 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLDecoder; +import javax.servlet.http.HttpServletRequest; + +import org.apache.tuscany.sca.common.http.HTTPContext; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -54,18 +60,33 @@ class WidgetImplementationInvoker implements Invoker { } public Message invoke(Message msg) { + HTTPContext bindingContext = (HTTPContext) msg.getBindingContext(); + HttpServletRequest request = bindingContext.getHttpRequest(); + + + // Get the request path + String pathInfo = request.getPathInfo(); + String path = null; + if(pathInfo != null) { + try { + path = URLDecoder.decode(pathInfo, "UTF-8"); + } catch (UnsupportedEncodingException uee) { + //ignore for now + } + } + // Get the resource id from the request message - - String id = msg.getBody() == null ? "" : (String)((Object[])msg.getBody())[0]; + String id = path == null ? "" : path.substring(1); try { if (id.length() == 0) { // Return an input stream for the widget resource URL url = new URL(widgetLocationURL); - InputStream is = url.openStream(); - msg.setBody(is); + //InputStream is = url.openStream(); + //msg.setBody(is); + writeResponse(bindingContext, url.openStream()); } else if (id.equals(widgetName)) { @@ -77,7 +98,8 @@ class WidgetImplementationInvoker implements Invoker { InputStream is = new ByteArrayInputStream(bos.toByteArray()); - msg.setBody(is); + //msg.setBody(is); + writeResponse(bindingContext, is); } else { @@ -85,7 +107,8 @@ class WidgetImplementationInvoker implements Invoker { // widget folder URL url = new URL(widgetFolderURL +'/' + id); InputStream is = url.openStream(); - msg.setBody(is); + //msg.setBody(is); + writeResponse(bindingContext, is); } } catch (MalformedURLException e) { @@ -99,4 +122,23 @@ class WidgetImplementationInvoker implements Invoker { } return msg; } + + /** + * Write the widget response to the http response outputstream + * @param bindingContext + * @param is + * @throws IOException + */ + private static void writeResponse(HTTPContext bindingContext, InputStream is) throws IOException { + OutputStream os = bindingContext.getHttpResponse().getOutputStream(); + byte[] buffer = new byte[2048]; + for (;;) { + int n = is.read(buffer); + if (n <= 0) + break; + os.write(buffer, 0, n); + } + os.flush(); + os.close(); + } } |