cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r486073 - in /incubator/cxf/trunk: rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/ systests/src/test/java/org/apache/cxf/systest/rest/
Date Tue, 12 Dec 2006 09:31:14 GMT
Author: jliu
Date: Tue Dec 12 01:31:12 2006
New Revision: 486073

URL: http://svn.apache.org/viewvc?view=rev&rev=486073
Log:
Update restful system test to use Http Binding instead of XML binding, this removes the dependency
on the presence of WSDL file 

Added:
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/HttpBindingServer.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerHttpBindingTest.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProviderHttpBinding.java
Modified:
    incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/EndpointImpl.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/EndpointImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/EndpointImpl.java?view=diff&rev=486073&r1=486072&r2=486073
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/EndpointImpl.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/EndpointImpl.java
Tue Dec 12 01:31:12 2006
@@ -29,6 +29,7 @@
 import javax.xml.ws.Provider;
 import javax.xml.ws.WebServiceException;
 import javax.xml.ws.handler.Handler;
+import javax.xml.ws.http.HTTPBinding;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.binding.xml.XMLBindingInfoFactoryBean;
@@ -208,7 +209,8 @@
         
         // TODO: Replace with discovery mechanism!!
         AbstractBindingInfoFactoryBean bindingFactory = null;
-        if (XMLConstants.NS_XML_FORMAT.equals(bindingURI)) {
+        if (XMLConstants.NS_XML_FORMAT.equals(bindingURI)
+            || HTTPBinding.HTTP_BINDING.equals(bindingURI)) {
             bindingFactory = new XMLBindingInfoFactoryBean();
         } else {
             // Just assume soap otherwise...

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/HttpBindingServer.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/HttpBindingServer.java?view=auto&rev=486073
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/HttpBindingServer.java
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/HttpBindingServer.java
Tue Dec 12 01:31:12 2006
@@ -0,0 +1,46 @@
+/**
+ * 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.
+ */
+
+package org.apache.cxf.systest.rest;
+
+import javax.xml.ws.Endpoint;
+import javax.xml.ws.http.HTTPBinding;
+
+import org.apache.cxf.systest.common.TestServerBase;
+
+public class HttpBindingServer extends TestServerBase {
+
+    protected void run() {
+        Endpoint e = Endpoint.create(HTTPBinding.HTTP_BINDING, new RestSourcePayloadProviderHttpBinding());
+        String address = "http://localhost:9024/XMLService/RestProviderPort/Customer";
+        e.publish(address);
+    }        
+
+    public static void main(String[] args) {
+        try {
+            HttpBindingServer s = new HttpBindingServer();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }    
+}

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerHttpBindingTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerHttpBindingTest.java?view=auto&rev=486073
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerHttpBindingTest.java
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerHttpBindingTest.java
Tue Dec 12 01:31:12 2006
@@ -0,0 +1,131 @@
+/**
+ * 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.
+ */
+
+package org.apache.cxf.systest.rest;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
+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.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.ws.Dispatch;
+
+import javax.xml.ws.Service;
+import javax.xml.ws.handler.MessageContext;
+import javax.xml.ws.http.HTTPBinding;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.cxf.systest.common.ClientServerSetupBase;
+import org.apache.cxf.systest.common.ClientServerTestBase;
+
+public class RestClientServerHttpBindingTest extends ClientServerTestBase {
+    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 String endpointAddress =
+        "http://localhost:9024/XMLService/RestProviderPort/Customer"; 
+   
+    public static Test suite() throws Exception {
+        TestSuite suite = new TestSuite(RestClientServerHttpBindingTest.class);
+        return new ClientServerSetupBase(suite) {
+            public void startServers() throws Exception {
+                assertTrue("server did not launch correctly", launchServer(HttpBindingServer.class));
+            }
+        };
+    }    
+   
+    public void testHttpGET() throws Exception {
+        URL url = new URL(endpointAddress + "?name=john&address=20");
+        InputStream in = url.openStream();
+        assertNotNull(in);       
+    }
+
+    public void testHttpPOSTDispatchHTTPBinding() throws Exception {
+        Service service = Service.create(serviceName);
+        service.addPort(portName, HTTPBinding.HTTP_BINDING, endpointAddress);
+        Dispatch<Source> dispatcher = service.createDispatch(portName, Source.class,
Service.Mode.MESSAGE);
+        Map<String, Object> requestContext = dispatcher.getRequestContext();
+        requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "POST");
+        InputStream is = getClass().getResourceAsStream("resources/CustomerJohnReq.xml");
+        Source result = dispatcher.invoke(new StreamSource(is));
+        String tempstring = source2String(result);
+        assertTrue("Result should start with Customer", tempstring.startsWith("<ns4:Customer"));
+        assertTrue("Result should have CustomerID", tempstring.lastIndexOf("CustomerID>123456<")
> 0);
+    }
+    
+    @SuppressWarnings("unchecked")
+    public void testHttpGETDispatchHTTPBinding() throws Exception { 
+        Service service = Service.create(serviceName); 
+        URI endpointURI = new URI(endpointAddress);
+        String path = null; 
+        if (endpointURI != null) { 
+            path = endpointURI.getPath(); 
+        } 
+        service.addPort(portName, HTTPBinding.HTTP_BINDING, endpointAddress);
+        Dispatch<Source> d = service.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
+        Map<String, Object> requestContext = d.getRequestContext();
+        Map<String, Object> responseContext = d.getResponseContext();
+        
+        requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "GET");
+        requestContext.put(MessageContext.QUERY_STRING, "id=1"); 
+        //this is the original path part of uri 
+        requestContext.put(MessageContext.PATH_INFO, path);        
+        Source result = d.invoke(null);
+        
+        // varify the responseContext;
+        Map<String, List<String>> responseHeader =
+            (Map<String, List<String>>)responseContext.get(MessageContext.HTTP_RESPONSE_HEADERS);
+        assertNotNull("the response header should not be null", responseHeader);
+        
+        List<String> values = responseHeader.get("REST");
+        assertNotNull("the response rest header should not be null", values);
+        assertEquals("the list size wrong", 2, values.size());        
+        assertNotNull("result shoud not be null", result);        
+        String tempstring = source2String(result);
+        assertTrue("Result should start with Customer", tempstring.startsWith("<ns4:Customer"));
+        assertTrue("Result should have CustomerID", tempstring.lastIndexOf("CustomerID>123456<")
> 0);
+    }
+    
+    private String source2String(Source source) throws Exception {
+        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);
+        return bos.toString();
+    }
+}

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=486073&r1=486072&r2=486073
==============================================================================
--- 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 Dec 12 01:31:12 2006
@@ -60,7 +60,9 @@
     private final QName portName = new QName("http://apache.org/hello_world_xml_http/wrapped",
                                              "RestProviderPort");
 
-    
+    private final String endpointAddress =
+        "http://localhost:9023/XMLService/RestProviderPort/Customer"; 
+   
     public static Test suite() throws Exception {
         TestSuite suite = new TestSuite(RestClientServerTest.class);
         return new ClientServerSetupBase(suite) {
@@ -70,7 +72,7 @@
         };
     }    
    
-    public void testHttpPOSTDispatchWithWSDL() throws Exception {
+    public void testHttpPOSTDispatchXMLBinding() throws Exception {
         URL wsdl = getClass().getResource("/wsdl/hello_world_xml_wrapped.wsdl");
         assertNotNull(wsdl);
 
@@ -91,17 +93,14 @@
     }
 
     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);
-       
+        assertNotNull(in);       
     }
 
-    public void testHttpPOSTDispatch() throws Exception {
+    public void testHttpPOSTDispatchHTTPBinding() throws Exception {
         Service service = Service.create(serviceName);
-        service.addPort(portName, HTTPBinding.HTTP_BINDING,
-                        "http://localhost:9023/XMLService/RestProviderPort/Customer");
+        service.addPort(portName, HTTPBinding.HTTP_BINDING, endpointAddress);
         Dispatch<Source> dispatcher = service.createDispatch(portName, Source.class,
Service.Mode.MESSAGE);
         Map<String, Object> requestContext = dispatcher.getRequestContext();
         requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "POST");
@@ -113,23 +112,19 @@
     }
     
     @SuppressWarnings("unchecked")
-    public void testHttpGETDispatcher() throws Exception { 
-        String endpointAddress =
-            "http://localhost:9023/XMLService/RestProviderPort/Customer"; 
+    public void testHttpGETDispatchHTTPBinding() throws Exception { 
         Service service = Service.create(serviceName); 
-        URI endpointURI = new URI(endpointAddress.toString());
+        URI endpointURI = new URI(endpointAddress);
         String path = null; 
-        //String query = null;
         if (endpointURI != null) { 
             path = endpointURI.getPath(); 
-            //query = endpointURI.getQuery();
         } 
-        service.addPort(portName, HTTPBinding.HTTP_BINDING, endpointAddress.toString());
+        service.addPort(portName, HTTPBinding.HTTP_BINDING, endpointAddress);
         Dispatch<Source> d = service.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
         Map<String, Object> requestContext = d.getRequestContext();
         Map<String, Object> responseContext = d.getResponseContext();
         
-        requestContext.put(MessageContext.HTTP_REQUEST_METHOD, new String("GET"));
+        requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "GET");
         requestContext.put(MessageContext.QUERY_STRING, "id=1"); 
         //this is the original path part of uri 
         requestContext.put(MessageContext.PATH_INFO, path);        

Added: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProviderHttpBinding.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProviderHttpBinding.java?view=auto&rev=486073
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProviderHttpBinding.java
(added)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestSourcePayloadProviderHttpBinding.java
Tue Dec 12 01:31:12 2006
@@ -0,0 +1,111 @@
+/**
+ * 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.
+ */
+
+package org.apache.cxf.systest.rest;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.Resource;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.ws.Provider;
+import javax.xml.ws.Service;
+import javax.xml.ws.ServiceMode;
+import javax.xml.ws.WebServiceContext;
+import javax.xml.ws.WebServiceProvider;
+import javax.xml.ws.handler.MessageContext;
+
+import org.w3c.dom.Document;
+
+@WebServiceProvider()
+@ServiceMode(value = Service.Mode.PAYLOAD)
+public class RestSourcePayloadProviderHttpBinding implements Provider<DOMSource> {
+
+    @Resource
+    protected WebServiceContext wsContext;
+
+    public RestSourcePayloadProviderHttpBinding() {
+    }
+
+    @SuppressWarnings("unchecked")
+    public DOMSource invoke(DOMSource request) {
+        MessageContext mc = wsContext.getMessageContext();
+        String path = (String)mc.get(MessageContext.PATH_INFO);
+        String query = (String)mc.get(MessageContext.QUERY_STRING);
+        String httpMethod = (String)mc.get(MessageContext.HTTP_REQUEST_METHOD);
+        
+        Map<String, List<String>> responseHeader =
+            (Map<String, List<String>>)mc.get(MessageContext.HTTP_RESPONSE_HEADERS);
+        List<String> values = new ArrayList<String>();
+        values.add("hello1");
+        values.add("hello2");
+        responseHeader.put("REST", values);
+//        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 ("/XMLService/RestProviderPort/Customer".equals(path) && query ==
null) {
+                // System.out.println("--GET:getAllCustomers--- ");
+                return getAllCustomers();
+            } else if ("/XMLService/RestProviderPort/Customer".equals(path) && 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;
+        DOMSource response = null;
+
+        try {
+            factory = DocumentBuilderFactory.newInstance();
+            factory.setValidating(true);
+            builder = factory.newDocumentBuilder();
+            InputStream greetMeResponse = getClass().getResourceAsStream(fileName);
+
+            document = builder.parse(greetMeResponse);
+            response = new DOMSource(document);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return response;
+    }       
+}



Mime
View raw message