From c5e532242f6f7f4bcc6938edd79022da2cb14cc3 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 27 Dec 2010 05:59:31 +0000 Subject: Port REST support improvements to AppEngine Python integration scripts and add a sample. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1053004 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/wsgi/atomutil.py | 2 +- sca-cpp/trunk/modules/wsgi/composite.py | 21 ++++-- sca-cpp/trunk/modules/wsgi/http-test | 5 -- sca-cpp/trunk/modules/wsgi/httputil.py | 125 +++++++++++++++++++++++++++++--- sca-cpp/trunk/modules/wsgi/util.py | 13 ++++ sca-cpp/trunk/modules/wsgi/xmlutil.py | 6 +- 6 files changed, 145 insertions(+), 27 deletions(-) (limited to 'sca-cpp/trunk/modules/wsgi') diff --git a/sca-cpp/trunk/modules/wsgi/atomutil.py b/sca-cpp/trunk/modules/wsgi/atomutil.py index 1e6a7c31b5..106e69c3c6 100644 --- a/sca-cpp/trunk/modules/wsgi/atomutil.py +++ b/sca-cpp/trunk/modules/wsgi/atomutil.py @@ -43,7 +43,7 @@ def readATOMEntry(l): return () return entryElementsToValues(car(e)) -# Convert a list of values representy an ATOM entry to a value +# Convert a list of values representing an ATOM entry to a value def entryValue(e): v = elementsToValues((caddr(e),)) return cons(car(e), (cadr(e), cdr(car(v)))) diff --git a/sca-cpp/trunk/modules/wsgi/composite.py b/sca-cpp/trunk/modules/wsgi/composite.py index 7044483f70..ffd971fef5 100755 --- a/sca-cpp/trunk/modules/wsgi/composite.py +++ b/sca-cpp/trunk/modules/wsgi/composite.py @@ -177,20 +177,25 @@ def application(e, r): if m == "GET": v = comp("get", id) - # Write returned content-type / content pair - if not isinstance(cadr(v), basestring): + # Write content-type / content-list pair + if isString(car(v)) and isList(cadr(v)): return result(e, r, 200, (("Content-type", car(v)),), cadr(v)) # Write an ATOM feed or entry - if isNil(id): - return result(e, r, 200, (("Content-type", "application/atom+xml;type=feed"),), writeATOMFeed(feedValuesToElements(v))) - return result(e, r, 200, (("Content-type", "application/atom+xml;type=entry"),), writeATOMEntry(entryValuesToElements(v))) + if isString(car(v)) and isString(cadr(v)): + if isNil(id): + return result(e, r, 200, (("Content-type", "application/atom+xml;type=feed"),), writeATOMFeed(feedValuesToElements(v))) + return result(e, r, 200, (("Content-type", "application/atom+xml;type=entry"),), writeATOMEntry(entryValuesToElements(v))) + + # Write a JSON value + return result(e, r, 200, (("Content-type", "application/json"),), writeJSON(valuesToElements(v))) if m == "POST": ct = requestContentType(e) # Handle a JSON-RPC function call - if ct.find("application/json-rpc") != -1 or ct.find("text/plain") != -1 or ct.find("application/x-www-form-urlencoded") != -1: + if contains(ct, "application/json-rpc") or contains(ct, "text/plain") or contains(ct, "application/x-www-form-urlencoded"): + print >> stderr, "Handling JSON-RPC request" json = elementsToValues(readJSON(requestBody(e))) args = postArgs(json) jid = cadr(assoc("'id", args)) @@ -200,7 +205,7 @@ def application(e, r): return result(e, r, 200, (("Content-type", "application/json-rpc"),), jsonResult(jid, v)) # Handle an ATOM entry POST - if ct.find("application/atom+xml") != -1: + if contains(ct, "application/atom+xml"): ae = entryValue(readATOMEntry(requestBody(e))) v = comp("post", id, ae) if isNil(v): @@ -237,7 +242,7 @@ def main(): # Handle the WSGI request with the WSGI runtime st = serverType(environ) - if st.find("App Engine") != -1 or st.find("Development") != -1: + if contains(st, "App Engine") or contains(st, "Development"): from google.appengine.ext.webapp.util import run_wsgi_app run_wsgi_app(application) elif st != "": diff --git a/sca-cpp/trunk/modules/wsgi/http-test b/sca-cpp/trunk/modules/wsgi/http-test index 6676f6514c..5499399b31 100755 --- a/sca-cpp/trunk/modules/wsgi/http-test +++ b/sca-cpp/trunk/modules/wsgi/http-test @@ -17,11 +17,6 @@ # specific language governing permissions and limitations # under the License. -uri=$1 -if [ "$uri" = "" ]; then - uri="http://localhost:8090" -fi - # Setup mkdir -p tmp ./wsgi-start target 8090 2>/dev/null diff --git a/sca-cpp/trunk/modules/wsgi/httputil.py b/sca-cpp/trunk/modules/wsgi/httputil.py index 723e1a7284..ea00f3f9fe 100644 --- a/sca-cpp/trunk/modules/wsgi/httputil.py +++ b/sca-cpp/trunk/modules/wsgi/httputil.py @@ -26,8 +26,11 @@ from string import strip from base64 import b64encode from sys import stderr from util import * -from atomutil import * -from jsonutil import * +from elemutil import * +import atomutil +import jsonutil +import rssutil +import xmlutil # JSON request id id = 1 @@ -38,23 +41,116 @@ class client: self.url = urlparse(url) def __call__(self, func, *args): + print >> stderr, "Client proxy call", func, args # Connect to the configured URL - print >> stderr, "Client POST", self.url.geturl() c, headers = connect(self.url) - # POST a JSON-RPC request + # handle a GET request + if func == "get": + u = requesturi(self.url, car(args)) + print >> stderr, "Client GET request", u + c.request("GET", u, None, headers) + res = c.getresponse() + print >> stderr, "Client status", res.status + if res.status != 200: + return None + ct = res.getheader("Content-type", "text/plain") + ls = (res.read(),) + + if contains(ct, "application/atom+xml;type=entry"): + # Read an ATOM entry + v = atomutil.entryValue(atomutil.readATOMEntry(ls)) + print >> stderr, "Client result", v + return v + + if contains(ct, "application/atom+xml;type=feed"): + # Read an ATOM feed + v = atomutil.feedValues(atomutil.readATOMFeed(ls)) + print >> stderr, "Client result", v + return v + + if contains(ct, "application/rss+xml") or rssutil.isRSSFeed(ls): + # Read an RSS feed + v = rssutil.feedValues(rssutil.readRSSFeed(ls)) + print >> stderr, "Client result", v + return v + + if contains(ct, "text/javascript") or contains(ct, "application/json") or jsonutil.isJSON(ls): + # Read a JSON document + v = elementsToValues(jsonutil.readJSON(ls)) + print >> stderr, "Client result", v + return v + + if contains(ct, "text/xml") or contains(ct, "application/xml") or xmlutil.isXML(ls): + # Read an XML document + v = elementsToValues(xmlutil.readXML(ls)) + print >> stderr, "Client result", v + return v + + # Return the content type and a content list + v = (ct, ls) + print >> stderr, "Client result", v + return v + + # handle a POST request + if func == "post": + u = requesturi(self.url, car(args)) + print >> stderr, "Client POST request", u + req = StringIO() + writeStrings(atomutil.writeATOMEntry(atomutil.entryValuesToElements(cadr(args))), req) + headers["Content-type"] = "application/atom+xml" + c.request("POST", u, req.getvalue(), headers) + res = c.getresponse() + print >> stderr, "Client status", res.status + if res.status != 200 and res.status != 201: + return None + loc = res.getheader("Location") + if loc == None: + return None + return loc[(loc.rfind('/') + 1):] + + # handle a PUT request + if func == "put": + u = requesturi(self.url, car(args)) + print >> stderr, "Client PUT request", u + req = StringIO() + writeStrings(atomutil.writeATOMEntry(atomutil.entryValuesToElements(cadr(args))), req) + headers["Content-type"] = "application/atom+xml" + c.request("PUT", u, req.getvalue(), headers) + res = c.getresponse() + print >> stderr, "Client status", res.status + if res.status != 200: + return None + return True + + # handle a DELETE request + if func == "delete": + u = requesturi(self.url, car(args)) + print >> stderr, "Client DELETE request", u + c.request("DELETE", u, None, headers) + res = c.getresponse() + print >> stderr, "Client status", res.status + if res.status != 200: + return None + return True + + # handle a JSON-RPC request + u = requesturi(self.url, ()) + print >> stderr, "Client JSON-RPC request", u global id req = StringIO() - writeStrings(jsonRequest(id, func, args), req) + writeStrings(jsonutil.jsonRequest(id, func, args), req) id = id + 1 headers["Content-type"] = "application/json-rpc" - c.request("POST", self.url.path, req.getvalue(), headers) + c.request("POST", u, req.getvalue(), headers) res = c.getresponse() print >> stderr, "Client status", res.status if res.status != 200: return None - return jsonResultValue((res.read(),)) + v = jsonutil.jsonResultValue((res.read(),)) + print >> stderr, "Client result", v + return v def __getattr__(self, name): if name[0] == '_': @@ -82,12 +178,19 @@ def connect(url): else: c = HTTPSConnection(url.hostname, 443 if url.port == None else url.port) - # For HTTP basic authentication the user and password are + # For HTTP basic authentication the user and password may be # provided by htpasswd.py - import htpasswd - auth = 'Basic ' + b64encode(htpasswd.user + ':' + htpasswd.passwd) - return c, {"Authorization": auth} + try: + import htpasswd + auth = 'Basic ' + b64encode(htpasswd.user + ':' + htpasswd.passwd) + return c, {"Authorization": auth} + except: + return c, {} else: c = HTTPConnection(url.hostname, 80 if url.port == None else url.port) return c, {} +# Convert a URL and arg to a request URI +def requesturi(url, arg): + return url.path + path(arg) + ("" if url.query == "" else "?" + url.query) + diff --git a/sca-cpp/trunk/modules/wsgi/util.py b/sca-cpp/trunk/modules/wsgi/util.py index 560101e32d..80bc7db101 100644 --- a/sca-cpp/trunk/modules/wsgi/util.py +++ b/sca-cpp/trunk/modules/wsgi/util.py @@ -59,6 +59,9 @@ def isNil(l): def isSymbol(v): return isinstance(v, basestring) and v[0:1] == "'" +def isString(v): + return isinstance(v, basestring) and v[0:1] != "'" + def isList(v): if getattr(v, '__iter__', False) == False: return False @@ -132,10 +135,20 @@ def assoc(k, l): def curry(f, *args): return lambda *a: f(*(args + a)) +# Convert a path represented as a list of values to a string +def path(p): + if isNil(p): + return "" + return "/" + car(p) + path(cdr(p)) + # Split a path into a list of segments def tokens(path): return tuple(filter(lambda s: len(s) != 0, path.split("/"))) +# Return true if s1 contains s2 +def contains(s1, s2): + return s1.find(s2) != -1 + # Write a list of strings to a stream def writeStrings(l, os): if l == (): diff --git a/sca-cpp/trunk/modules/wsgi/xmlutil.py b/sca-cpp/trunk/modules/wsgi/xmlutil.py index 35ccb7f803..83b1cf2f92 100644 --- a/sca-cpp/trunk/modules/wsgi/xmlutil.py +++ b/sca-cpp/trunk/modules/wsgi/xmlutil.py @@ -31,8 +31,10 @@ def readAttributes(a): # Read an XML element def readElement(e): - l = (element, "'" + e.tag) + readAttributes(tuple(e.items())) + readElements(tuple(e.getchildren())) - if e.text == None: + a = tuple(e.items()) + c = tuple(e.getchildren()) + l = (element, "'" + e.tag) + readAttributes(a) + readElements(c) + if e.text == None or c != (): return l return l + (e.text,) -- cgit v1.2.3