summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2012-06-16 18:13:58 +0000
committerlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2012-06-16 18:13:58 +0000
commit9b335d1eec8f9844d481999c7d3a9ea4dc2d0c89 (patch)
tree35afd4075fde4d5d448f739961909ddc0397ef8e
parent6eff900741dfdc75dd8e89485339b6193ab95fc4 (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
-rw-r--r--sca-java-2.x/trunk/modules/implementation-widget-runtime/pom.xml40
-rw-r--r--sca-java-2.x/trunk/modules/implementation-widget-runtime/src/main/java/org/apache/tuscany/sca/implementation/widget/provider/WidgetImplementationInvoker.java54
2 files changed, 88 insertions, 6 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-widget-runtime/pom.xml b/sca-java-2.x/trunk/modules/implementation-widget-runtime/pom.xml
index 4e22897a69..f9b0f1615c 100644
--- a/sca-java-2.x/trunk/modules/implementation-widget-runtime/pom.xml
+++ b/sca-java-2.x/trunk/modules/implementation-widget-runtime/pom.xml
@@ -50,18 +50,58 @@
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-common-http</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-http</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-binding-http</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version> <!-- to keep compatible with older servlet containers -->
<scope>provided</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-node-impl</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-implementation-java-runtime</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-host-jetty</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
+ <groupId>httpunit</groupId>
+ <artifactId>httpunit</artifactId>
+ <version>1.7</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
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();
+ }
}