Skip to content

Commit

Permalink
Enhancing widget invoker to handled more details of what resource is …
Browse files Browse the repository at this point in the history
…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: https://svn.apache.org/repos/asf/tuscany/sca-java-2.x/trunk@1350974 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lresende committed Jun 16, 2012
1 parent 1b9c627 commit ce1cafa
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
40 changes: 40 additions & 0 deletions modules/implementation-widget-runtime/pom.xml
Expand Up @@ -48,19 +48,59 @@
<version>2.0-SNAPSHOT</version>
</dependency>

<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>
Expand Down
Expand Up @@ -23,10 +23,16 @@
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;
Expand Down Expand Up @@ -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)) {

Expand All @@ -77,15 +98,17 @@ public Message invoke(Message msg) {

InputStream is = new ByteArrayInputStream(bos.toByteArray());

msg.setBody(is);
//msg.setBody(is);
writeResponse(bindingContext, is);

} else {

// Return an input stream for a resource inside the
// widget folder
URL url = new URL(widgetFolderURL +'/' + id);
InputStream is = url.openStream();
msg.setBody(is);
//msg.setBody(is);
writeResponse(bindingContext, is);
}
} catch (MalformedURLException e) {

Expand All @@ -99,4 +122,23 @@ public Message invoke(Message msg) {
}
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();
}
}

0 comments on commit ce1cafa

Please sign in to comment.