Author: jliu
Date: Tue Sep 19 00:12:34 2006
New Revision: 447767
URL: http://svn.apache.org/viewvc?view=rev&rev=447767
Log:
Added RESTful client test using HTTP GET.
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
(with props)
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerTest.java
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProvider.java
incubator/cxf/trunk/testutils/src/main/resources/wsdl/hello_world_xml_wrapped.wsdl
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerTest.java?view=diff&rev=447767&r1=447766&r2=447767
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerTest.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerTest.java
Tue Sep 19 00:12:34 2006
@@ -19,11 +19,19 @@
package org.apache.cxf.systest.rest;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
+import java.util.Properties;
import javax.xml.namespace.QName;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+//import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Service;
@@ -41,17 +49,17 @@
import org.apache.hello_world_xml_http.wrapped.XMLService;
public class RestClientServerTest extends ClientServerTestBase {
- private final QName serviceName = new QName(
- "http://apache.org/hello_world_xml_http/wrapped", "XMLService");
+ private final QName serviceName = new
QName("http://apache.org/hello_world_xml_http/wrapped",
+ "XMLService");
- private final QName portName = new QName(
- "http://apache.org/hello_world_xml_http/wrapped",
"RestProviderPort");
+ private final QName portName = new
QName("http://apache.org/hello_world_xml_http/wrapped",
+ "RestProviderPort");
public static class Server extends TestServerBase {
protected void run() {
Object implementor = new RestSourcePayloadProvider();
- String address =
"http://localhost:9023/XMLService/RestProviderPort";
+ String address =
"http://localhost:9023/XMLService/RestProviderPort/Customer";
Endpoint.publish(address, implementor);
}
@@ -72,36 +80,78 @@
TestSuite suite = new TestSuite(RestClientServerTest.class);
return new ClientServerSetupBase(suite) {
public void startServers() throws Exception {
- assertTrue("server did not launch correctly",
- launchServer(Server.class));
+ assertTrue("server did not launch correctly",
launchServer(Server.class));
}
};
}
- public void testDOMSourcePAYLOAD() throws Exception {
+ public void testHttpPOSTDispatch() throws Exception {
URL wsdl =
getClass().getResource("/wsdl/hello_world_xml_wrapped.wsdl");
assertNotNull(wsdl);
XMLService service = new XMLService(wsdl, serviceName);
assertNotNull(service);
- InputStream is = getClass().getResourceAsStream(
- "resources/CustomerJohnReq.xml");
+ InputStream is =
getClass().getResourceAsStream("resources/CustomerJohnReq.xml");
Document doc = XMLUtils.parse(is);
DOMSource reqMsg = new DOMSource(doc);
assertNotNull(reqMsg);
- Dispatch<DOMSource> disp = service.createDispatch(portName,
- DOMSource.class, Service.Mode.PAYLOAD);
+ Dispatch<DOMSource> disp = service.createDispatch(portName,
DOMSource.class, Service.Mode.PAYLOAD);
DOMSource result = disp.invoke(reqMsg);
assertNotNull(result);
Node respDoc = result.getNode();
assertEquals("Customer", respDoc.getFirstChild().getLocalName());
+ }
+
+ public void testHttpGET() throws Exception {
+ String endpointAddress =
"http://localhost:9023/XMLService/RestProviderPort/Customer";
+ URL url = new URL(endpointAddress + "?name=john&address=20");
+ InputStream in = url.openStream();
+ assertNotNull(in);
+ //StreamSource source = new StreamSource(in);
+ //printSource(source);
+
/*
- * assertEquals("TestXMLBindingProviderMessage",
respDoc.getFirstChild()
- * .getTextContent());
+ * url = new URL(endpointAddress + "/num1/10/num2/20");
+ * System.out.println("Invoking URL=" + url); process(url);
*/
+ }
+
+ // Service.addPort() is not supported yet
+ /*
+ * public void testHttpGETDispatcher() throws Exception { String
+ * endpointAddress =
+ * "http://localhost:9023/XMLService/RestProviderPort/Customer"; Service
+ * service = Service.create(serviceName); URI endpointURI = new
+ * URI(endpointAddress.toString()); String path = null; String query =
null;
+ * if (endpointURI != null){ path = endpointURI.getPath(); query =
+ * endpointURI.getQuery(); } service.addPort(portName,
+ * HTTPBinding.HTTP_BINDING, endpointAddress.toString()); Dispatch<Source>
+ * d = service.createDispatch(portName, Source.class,
Service.Mode.PAYLOAD);
+ * Map<String, Object> requestContext = d.getRequestContext();
+ * requestContext.put(Message.HTTP_REQUEST_METHOD, new String("GET"));
+ * requestContext.put(Message.QUERY_STRING, "id=1"); //this is the original
+ * path part of uri requestContext.put(Message.PATH_INFO, path);
+ * System.out.println ("Invoking Restful GET Request with query string ");
+ * Source result = d.invoke(null); printSource(result); }
+ */
+
+ private void printSource(Source source) {
+ try {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ StreamResult sr = new StreamResult(bos);
+ Transformer trans =
TransformerFactory.newInstance().newTransformer();
+ Properties oprops = new Properties();
+ oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ trans.setOutputProperties(oprops);
+ trans.transform(source, sr);
+ System.out.println("**** Response ******" + bos.toString());
+ bos.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
}
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProvider.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProvider.java?view=diff&rev=447767&r1=447766&r2=447767
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProvider.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProvider.java
Tue Sep 19 00:12:34 2006
@@ -36,17 +36,16 @@
import org.apache.cxf.message.Message;
-
-//The following wsdl file is used.
-//wsdlLocation =
"/trunk/testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
[EMAIL PROTECTED](portName = "XMLProviderPort",
- serviceName = "SOAPServiceRPCLit2",
- targetNamespace = "http://apache.org/hello_world_rpclit",
- wsdlLocation = "/wsdl/hello_world_rpc_lit.wsdl")
+// The following wsdl file is used.
+// wsdlLocation =
+// "/trunk/testutils/src/main/resources/wsdl/hello_world_rpc_lit.wsdl"
[EMAIL PROTECTED](portName = "XMLProviderPort",
+ serviceName = "SOAPServiceRPCLit2",
+ targetNamespace = "http://apache.org/hello_world_rpclit",
+ wsdlLocation = "/wsdl/hello_world_rpc_lit.wsdl")
@ServiceMode(value = Service.Mode.PAYLOAD)
@javax.xml.ws.BindingType(value = "http://cxf.apache.org/bindings/xformat")
-public class RestSourcePayloadProvider implements
- Provider<DOMSource> {
+public class RestSourcePayloadProvider implements Provider<DOMSource> {
@Resource
protected WebServiceContext wsContext;
@@ -56,17 +55,41 @@
public DOMSource invoke(DOMSource request) {
MessageContext mc = wsContext.getMessageContext();
- //String path1 = (String) mc.get(MessageContext.PATH_INFO);
- String path = (String) mc.get(Message.PATH_INFO);
-
- if (path.equals("/XMLService/RestProviderPort")) {
+ String path = (String)mc.get(Message.PATH_INFO);
+ String query = (String)mc.get(Message.QUERY_STRING);
+ String httpMethod = (String)mc.get(Message.HTTP_REQUEST_METHOD);
+
+ /*
+ * System.out.println("--path--- " + path);
+ * System.out.println("--query--- " + query);
+ * System.out.println("--httpMethod--- " + httpMethod);
+ */
+ if (httpMethod.equalsIgnoreCase("POST")) {
+ // TBD: parse query info from DOMSource
+ // System.out.println("--POST: getAllCustomers--- ");
return getCustomer(null);
+ } else if (httpMethod.equalsIgnoreCase("GET")) {
+ if (path.equals("/XMLService/RestProviderPort/Customer") && query
== null) {
+ // System.out.println("--GET:getAllCustomers--- ");
+ return getAllCustomers();
+ } else if (path.equals("/XMLService/RestProviderPort/Customer") &&
query != null) {
+ // System.out.println("--GET:getCustomer--- ");
+ return getCustomer(query);
+ }
}
return null;
}
-
+
+ private DOMSource getAllCustomers() {
+ return createDOMSource("resources/CustomerAllResp.xml");
+ }
+
private DOMSource getCustomer(String customerID) {
+ return createDOMSource("resources/CustomerJohnResp.xml");
+ }
+
+ private DOMSource createDOMSource(String fileName) {
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document document = null;
@@ -76,14 +99,13 @@
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
builder = factory.newDocumentBuilder();
- InputStream greetMeResponse = getClass().getResourceAsStream(
- "resources/CustomerJohnResp.xml");
+ InputStream greetMeResponse =
getClass().getResourceAsStream(fileName);
document = builder.parse(greetMeResponse);
response = new DOMSource(document);
} catch (Exception e) {
e.printStackTrace();
}
- return response;
+ return response;
}
}
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml?view=auto&rev=447767
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
(added)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
Tue Sep 19 00:12:34 2006
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<ns4:Customer xmlns:ns4="http://apache.org/hello_world_soap_http/types">
+ <ns4:CustomerName>John</ns4:CustomerName>
+ <ns4:CustomerID>123456</ns4:CustomerID>
+</ns4:Customer>
+<ns4:Customer xmlns:ns4="http://apache.org/hello_world_soap_http/types">
+ <ns4:CustomerName>Eric</ns4:CustomerName>
+ <ns4:CustomerID>123457</ns4:CustomerID>
+</ns4:Customer>
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/CustomerAllResp.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Modified:
incubator/cxf/trunk/testutils/src/main/resources/wsdl/hello_world_xml_wrapped.wsdl
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/testutils/src/main/resources/wsdl/hello_world_xml_wrapped.wsdl?view=diff&rev=447767&r1=447766&r2=447767
==============================================================================
---
incubator/cxf/trunk/testutils/src/main/resources/wsdl/hello_world_xml_wrapped.wsdl
(original)
+++
incubator/cxf/trunk/testutils/src/main/resources/wsdl/hello_world_xml_wrapped.wsdl
Tue Sep 19 00:12:34 2006
@@ -176,7 +176,7 @@
<wsdl:port binding="tns:Greeter_XMLBinding"
name="RestProviderPort">
<http:address
-
location="http://localhost:9023/XMLService/RestProviderPort" />
+
location="http://localhost:9023/XMLService/RestProviderPort/Customer" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>