You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2006/12/02 22:43:30 UTC

svn commit: r481633 - in /incubator/tuscany/cpp/sca/samples/RestCustomer: sample.customer.restclient/ sample.customer/

Author: jsdelfino
Date: Sat Dec  2 13:43:28 2006
New Revision: 481633

URL: http://svn.apache.org/viewvc?view=rev&rev=481633
Log:
Cleaned up the REST sample and added a command style service to show the XML/HTTP command style support.

Added:
    incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py   (with props)
Removed:
    incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResource.py
Modified:
    incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/CustomerRestClient.py
    incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/sample.customer.restclient.composite
    incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/sample.customer.composite

Modified: incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/CustomerRestClient.py
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/CustomerRestClient.py?view=diff&rev=481633&r1=481632&r2=481633
==============================================================================
--- incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/CustomerRestClient.py (original)
+++ incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/CustomerRestClient.py Sat Dec  2 13:43:28 2006
@@ -25,30 +25,62 @@
 from xml.etree import ElementTree as et
 import sca
 
-# Locate the customer service
-customerService = sca.locateservice("Customer")
+# Locate the customer resource service
+customerResource = sca.locateservice("CustomerResource")
 
-# Invoke the CRUD operations on the customer resource
+# Show how to invoke CRUD operations on the customer resource
+# The CRUD operations translate to HTTP POST, GET, PUT and DELETE
+# according to the REST pattern
 
-customer = customerService.retrieve("2345")
-print "Retrieved customer " + et.tostring(customer)
+customer = customerResource.retrieve("2345")
+print "Rest - Retrieved customer " + et.tostring(customer)
 
 customer = et.fromstring("""<customer xmlns="http://sample.customer"><id>1234</id><firstName>Jane</firstName><lastName>Doe</lastName></customer>""")
-url = customerService.create(customer)
-print "Created customer " + url
+url = customerResource.create(customer)
+print "Rest - Created customer " + url
 
-customer = customerService.retrieve("1234")
-print "Retrieved customer " + et.tostring(customer)
+customer = customerResource.retrieve("1234")
+print "Rest - Retrieved customer " + et.tostring(customer)
 
-customer = customerService.retrieve(url)
-print "Retrieved by url " + et.tostring(customer)
+customer = customerResource.retrieve(url)
+print "Rest - Retrieved by url " + et.tostring(customer)
 
 customer.find("{http://sample.customer}lastName").text="Smith"
-customerService.update("1234", customer)
-print "Updated customer 1234"
+customerResource.update("1234", customer)
+print "Rest - Updated customer 1234"
 
-customer = customerService.retrieve("1234")
-print "Retrieved customer " + et.tostring(customer)
+customer = customerResource.retrieve("1234")
+print "Rest - Retrieved customer " + et.tostring(customer)
 
-customerService.delete("1234")
-print "Deleted customer 1234"
+customerResource.delete("1234")
+print "Rest - Deleted customer 1234"
+
+# Also show how to use REST binding to invoke remote commands
+# using HTTP GET and XML over HTTP POST, the REST binding
+# uses that command pattern when you don't declare a REST interface
+# on your SCA reference
+
+# Locate the customer command service
+customerCommand = sca.locateservice("CustomerCommand")
+
+# Invoke operations on the customer command service
+customer = customerCommand.retrieve("2345")
+print "Command  - Retrieved customer " + et.tostring(customer)
+
+customer = et.fromstring("""<customer xmlns="http://sample.customer"><id>1234</id><firstName>Jane</firstName><lastName>Doe</lastName></customer>""")
+url = customerCommand.create(customer)
+print "Command  - Created customer " + url
+
+customer = customerCommand.retrieve("1234")
+print "Command  - Retrieved customer " + et.tostring(customer)
+
+customer.find("{http://sample.customer}lastName").text="Smith"
+# commented out for now as application/form-data does not work yet
+#customerCommand.update("1234", customer)
+#print "Command  - Updated customer 1234"
+
+customer = customerCommand.retrieve("1234")
+print "Command  - Retrieved customer " + et.tostring(customer)
+
+customerCommand.delete("1234")
+print "Command  - Deleted customer 1234"

Modified: incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/sample.customer.restclient.composite
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/sample.customer.restclient.composite?view=diff&rev=481633&r1=481632&r2=481633
==============================================================================
--- incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/sample.customer.restclient.composite (original)
+++ incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer.restclient/sample.customer.restclient.composite Sat Dec  2 13:43:28 2006
@@ -21,9 +21,13 @@
 <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" 
 	name="sample.customer.restclient">
 
-	<reference name="Customer">
+	<reference name="CustomerResource">
 		<interface.rest/>
-		<binding.rest uri="http://localhost:9090/rest/sample.customer.CustomerComponent/Customer"/>
+		<binding.rest uri="http://localhost:9190/rest/sample.customer.CustomerComponent/CustomerResource"/>
+	</reference>
+
+	<reference name="CustomerCommand">
+		<binding.rest uri="http://localhost:9190/rest/sample.customer.CustomerComponent/CustomerCommand"/>
 	</reference>
 
 </composite>

Added: incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py?view=auto&rev=481633
==============================================================================
--- incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py (added)
+++ incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py Sat Dec  2 13:43:28 2006
@@ -0,0 +1,59 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#
+
+# 
+# This Python code implements a simple SCA component that
+# manages Customer resources.
+# 
+
+import os
+import shutil
+from xml.etree import ElementTree as et
+
+qname = "{http://sample.customer}id"
+dir = os.environ["CUSTOMER_DIR"]
+
+class CustomerResourceImpl:
+    "A class that manages Customer resources"
+    
+    def create(self, customer):
+        id = customer.findtext(qname)
+        print "Python - CustomerResourceImpl.create " + id
+        f = open(dir + "/" + id + ".xml", "w")
+        f.write(et.tostring(customer))
+        f.close()
+        return id
+
+    def retrieve(self, id):
+        print "Python - CustomerResourceImpl.retrieve " + id
+        f = open(dir + "/" + id + ".xml", "r")
+        customer = et.fromstring(f.read())
+        f.close()
+        return customer
+
+    def update(self, id, customer):
+        print "Python - CustomerResourceImpl.update " + id
+        f = open(dir + "/" + id + ".xml", "w")
+        f.write(et.tostring(customer))
+        f.close()
+
+    def delete(self, id):
+        print "Python - CustomerResourceImpl.delete " + id
+        shutil.copyfile(dir + "/" + id + ".xml", dir + "/" + id + "-bak.xml")
+        os.remove(dir + "/" + id + ".xml")

Propchange: incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/CustomerResourceImpl.py
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/sample.customer.composite
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/sample.customer.composite?view=diff&rev=481633&r1=481632&r2=481633
==============================================================================
--- incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/sample.customer.composite (original)
+++ incubator/tuscany/cpp/sca/samples/RestCustomer/sample.customer/sample.customer.composite Sat Dec  2 13:43:28 2006
@@ -21,14 +21,19 @@
 <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" 
 	name="sample.customer">
 
-	<service name="Customer">
+	<service name="CustomerResource">
 		<interface.rest/>
 		<binding.rest/>
 		<reference>CustomerComponent</reference>
 	</service>
 
+	<service name="CustomerCommand">
+		<binding.rest/>
+		<reference>CustomerComponent</reference>
+	</service>
+
 	<component name="CustomerComponent">
-		<implementation.python module="CustomerResource" class="CustomerResourceImpl"/>
+		<implementation.python module="CustomerResourceImpl" class="CustomerResourceImpl"/>
 	</component>
         
 </composite>



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org