You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by sa...@apache.org on 2012/02/20 10:13:24 UTC

svn commit: r1291158 - in /axis/axis2/java/core/trunk/modules: integration/test/org/apache/axis2/rest/ kernel/src/org/apache/axis2/description/ kernel/src/org/apache/axis2/dispatchers/ kernel/src/org/apache/axis2/namespace/ kernel/src/org/apache/axis2/...

Author: sagara
Date: Mon Feb 20 09:13:23 2012
New Revision: 1291158

URL: http://svn.apache.org/viewvc?rev=1291158&view=rev
Log:
Applied patch for AXIS2-5247.

Added:
    axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/
    axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/RESTfulServiceTest.java   (with props)
    axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/Stock.java   (with props)
    axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/StockService.java   (with props)
Modified:
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/WSDL2Constants.java
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/dispatchers/RequestURIBasedServiceDispatcher.java
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/namespace/Constants.java
    axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/wsdl/WSDLUtil.java

Added: axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/RESTfulServiceTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/RESTfulServiceTest.java?rev=1291158&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/RESTfulServiceTest.java (added)
+++ axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/RESTfulServiceTest.java Mon Feb 20 09:13:23 2012
@@ -0,0 +1,188 @@
+/*
+* 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.axis2.rest;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.ConfigurationContextFactory;
+import org.apache.axis2.description.AxisBinding;
+import org.apache.axis2.description.AxisBindingOperation;
+import org.apache.axis2.description.AxisEndpoint;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.description.WSDL2Constants;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.axis2.integration.UtilServer;
+import org.apache.axis2.integration.UtilServerBasedTestCase;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+
+import javax.xml.namespace.QName;
+import java.util.Comparator;
+import java.util.Map;
+import java.util.TreeMap;
+
+public class RESTfulServiceTest extends UtilServerBasedTestCase {
+
+    ConfigurationContext configContext;
+    ConfigurationContext clinetConfigurationctx;
+
+    public static Test suite() {
+        return getTestSetup(new TestSuite(RESTfulServiceTest.class));
+    }
+
+    protected void setUp() throws Exception {
+        configContext = UtilServer.getConfigurationContext();
+        clinetConfigurationctx = ConfigurationContextFactory.
+                createConfigurationContextFromFileSystem(null, null);
+    }
+
+    public void testServiceCreate() throws AxisFault {
+        AxisConfiguration axisConfig = configContext.getAxisConfiguration();
+        AxisService axisService =
+                AxisService.createService("org.apache.axis2.rest.StockService", axisConfig);
+        assertNotNull(axisService);
+
+        assertNotNull(axisService.getOperation(new QName("addStock")));
+        assertNotNull(axisService.getOperation(new QName("getStockValue")));
+
+        axisService.setTargetNamespace("http://rest.axis2.apache.org");
+
+        Map httpLocationTable = new TreeMap(new Comparator() {
+            public int compare(Object o1, Object o2) {
+                return (-1 * ((Comparable) o1).compareTo(o2));
+            }
+        });
+        // StockServiceHttpBinding for StockService
+        AxisBinding binding = new AxisBinding();
+        binding.setName(new QName("StockServiceHttpBinding"));
+        binding.setType("http://www.w3.org/ns/wsdl/http");
+        binding.setProperty(WSDL2Constants.ATTR_WHTTP_METHOD_DEFAULT,
+                            Constants.Configuration.HTTP_METHOD_GET);
+
+        // AxisBindingOperation for addStock
+        AxisBindingOperation bindingOperation1 = new AxisBindingOperation();
+        bindingOperation1.setAxisOperation(axisService.getOperation(new QName("addStock")));
+        bindingOperation1.setName(axisService.getOperation(new QName("addStock")).getName());
+        bindingOperation1.setParent(binding);
+        bindingOperation1.setProperty(WSDL2Constants.ATTR_WHTTP_METHOD,
+                                      Constants.Configuration.HTTP_METHOD_GET);
+        bindingOperation1.setProperty(WSDL2Constants.ATTR_WHTTP_LOCATION,
+                                      "add/{name}/value/{value}");
+        httpLocationTable.put(Constants.Configuration.HTTP_METHOD_GET + "/add/",
+                              axisService.getOperation(new QName("addStock")));
+        bindingOperation1.setProperty(WSDL2Constants.ATTR_WHTTP_INPUT_SERIALIZATION,
+                                      Constants.MIME_CT_APPLICATION_URL_ENCODED);
+        bindingOperation1.setProperty(WSDL2Constants.ATTR_WHTTP_OUTPUT_SERIALIZATION,
+                                      Constants.MIME_CT_APPLICATION_XML);
+        binding.addChild(bindingOperation1);
+
+        assertNotNull(binding.getChild(bindingOperation1.getName()));
+
+        // AxisBindingOperation for getStockValue
+        AxisBindingOperation bindingOperation2 = new AxisBindingOperation();
+        bindingOperation2.setAxisOperation(axisService.getOperation(new QName("getStockValue")));
+        bindingOperation2.setName(axisService.getOperation(new QName("getStockValue")).getName());
+        bindingOperation2.setParent(binding);
+        bindingOperation2.setProperty(WSDL2Constants.ATTR_WHTTP_METHOD,
+                                      Constants.Configuration.HTTP_METHOD_GET);
+        bindingOperation2.setProperty(WSDL2Constants.ATTR_WHTTP_LOCATION, "get/{name}");
+        httpLocationTable.put(Constants.Configuration.HTTP_METHOD_GET + "/get/",
+                              axisService.getOperation(new QName("getStockValue")));
+        bindingOperation2.setProperty(WSDL2Constants.ATTR_WHTTP_INPUT_SERIALIZATION,
+                                      Constants.MIME_CT_APPLICATION_URL_ENCODED);
+        bindingOperation2.setProperty(WSDL2Constants.ATTR_WHTTP_OUTPUT_SERIALIZATION,
+                                      Constants.MIME_CT_APPLICATION_XML);
+        binding.addChild(bindingOperation2);
+
+        assertNotNull(binding.getChild(bindingOperation2.getName()));
+        binding.setProperty(WSDL2Constants.HTTP_LOCATION_TABLE, httpLocationTable);
+
+        // adding Http AxisEndpoint, HttpBinding to service
+        AxisEndpoint axisEndpoint = new AxisEndpoint();
+        axisEndpoint.setBinding(binding);
+        axisEndpoint.setName("StockServiceHttpEndpoint");
+        axisService.addEndpoint("StockServiceHttpEndpoint", axisEndpoint);
+        axisService.setEndpointName("StockServiceHttpEndpoint");
+        axisService.setBindingName("StockServiceHttpBinding");
+        axisService.setEndpointURL("http://127.0.0.1:" + (UtilServer.TESTING_PORT) +
+                                   "/axis2/services/StockService.StockServiceHttpEndpoint/");
+        assertNotNull(axisService.getEndpoint("StockServiceHttpEndpoint"));
+        axisConfig.addService(axisService);
+        assertEquals("StockService", axisService.getName());
+
+    }
+
+    public void testRESTMethods() throws AxisFault {
+
+        HttpClient httpClient = new HttpClient();
+
+        String url1 = "http://127.0.0.1:" + (UtilServer.TESTING_PORT)
+                      + "/axis2/services/StockService/add/IBM/value/34.7";
+
+        GetMethod method1 = new GetMethod(url1);
+
+        try {
+            int statusCode = httpClient.executeMethod(method1);
+            if (statusCode != HttpStatus.SC_OK) {
+                System.err.println("Method failed: " + method1.getStatusLine());
+            }
+            OMElement response = AXIOMUtil.stringToOM(new String(method1.getResponseBody()));
+            OMElement returnElem = response.getFirstChildWithName(new QName("return"));
+            assertEquals("IBM stock added with value : 34.7", returnElem.getText());
+
+        } catch (Exception e) {
+            System.err.println("Error occurred while trying to invoke method: " + e.getMessage());
+            e.printStackTrace();
+            fail("Caught exception " + e.toString());
+        } finally {
+            method1.releaseConnection();
+        }
+
+
+        String url2 = "http://127.0.0.1:" + (UtilServer.TESTING_PORT)
+                             + "/axis2/services/StockService/get/IBM";
+        GetMethod method2 = new GetMethod(url2);
+
+        try {
+            int statusCode = httpClient.executeMethod(method2);
+
+            if (statusCode != HttpStatus.SC_OK) {
+                System.err.println("Method failed: " + method2.getStatusLine());
+            }
+            OMElement response = AXIOMUtil.stringToOM(new String(method2.getResponseBody()));
+            OMElement returnElem = response.getFirstChildWithName(new QName("return"));
+            assertEquals("34.7", returnElem.getText());
+
+        } catch (Exception e) {
+            System.err.println("Error occurred while trying to invoke method: " + e.getMessage());
+            e.printStackTrace();
+            fail("Caught exception " + e.toString());
+        } finally {
+            method2.releaseConnection();
+        }
+
+    }
+}

Propchange: axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/RESTfulServiceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/Stock.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/Stock.java?rev=1291158&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/Stock.java (added)
+++ axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/Stock.java Mon Feb 20 09:13:23 2012
@@ -0,0 +1,43 @@
+/*
+* 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.axis2.rest;
+
+
+public class Stock {
+    private String name;
+    private float value;
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+
+    public void setValue(float value) {
+        this.value = value;
+    }
+
+    public float getValue() {
+        return this.value;
+    }
+
+}

Propchange: axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/Stock.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/StockService.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/StockService.java?rev=1291158&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/StockService.java (added)
+++ axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/StockService.java Mon Feb 20 09:13:23 2012
@@ -0,0 +1,52 @@
+/*
+* 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.axis2.rest;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+public class StockService {
+
+    private static Map<String, Stock> map = new HashMap<String, Stock>();
+
+
+    public float getStockValue(String name) {
+        float value = 0;
+        try {
+            value = map.get(name).getValue();
+        } catch (Exception e) {
+        }
+        return value;
+    }
+
+    public String addStock(String name, float value) {
+        Stock stock = new Stock();
+        stock.setName(name);
+        stock.setValue(value);
+        map.put(name, stock);
+        return name+ " stock added with value : "+value;
+    }
+
+    public void removeStock(String name) {
+        map.remove(name);
+    }
+
+}

Propchange: axis/axis2/java/core/trunk/modules/integration/test/org/apache/axis2/rest/StockService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/WSDL2Constants.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/WSDL2Constants.java?rev=1291158&r1=1291157&r2=1291158&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/WSDL2Constants.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/description/WSDL2Constants.java Mon Feb 20 09:13:23 2012
@@ -109,6 +109,7 @@ public interface WSDL2Constants {
     String ATTR_WHTTP_LOCATION = "whttp:location";
     String ATTR_WHTTP_HEADER = "whttp:header";
     String ATTR_WHTTP_METHOD = "whttp:method";
+    String ATTR_WHTTP_METHOD_DEFAULT = "whttp:methodDefault";
     String ATTR_WHTTP_CODE = "whttp:code";
     String ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR = "whttp:queryParameterSeparator";
     String ATTR_WHTTP_IGNORE_UNCITED = "whttp:ignoreUncited";

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/dispatchers/RequestURIBasedServiceDispatcher.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/dispatchers/RequestURIBasedServiceDispatcher.java?rev=1291158&r1=1291157&r2=1291158&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/dispatchers/RequestURIBasedServiceDispatcher.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/dispatchers/RequestURIBasedServiceDispatcher.java Mon Feb 20 09:13:23 2012
@@ -24,12 +24,15 @@ import org.apache.axis2.Constants;
 import org.apache.axis2.addressing.EndpointReference;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisEndpoint;
 import org.apache.axis2.description.AxisService;
 import org.apache.axis2.description.HandlerDescription;
+import org.apache.axis2.description.TransportInDescription;
 import org.apache.axis2.description.WSDL2Constants;
 import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.axis2.util.LoggingControl;
 import org.apache.axis2.util.Utils;
+import org.apache.axis2.wsdl.WSDLUtil;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -104,9 +107,17 @@ public class RequestURIBasedServiceDispa
                                 messageContext.setProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME,
                                         endpoints.get(endpointName));
                             }
-            			}
-            		}
-            	}
+                            String endpointName = temp[0].substring(temp[0].indexOf(".") + 1);
+                            AxisEndpoint endpoint = (AxisEndpoint) endpoints.get(endpointName);
+                            if (endpoint != null) {
+                                messageContext.setProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME,
+                                                           endpoint);
+                            } else {
+                                inferEndpoint(messageContext, axisService);
+                            }
+                        }
+                    }
+                }
 
             	return axisService;
             } else {
@@ -128,4 +139,29 @@ public class RequestURIBasedServiceDispa
     public void initDispatcher() {
         init(new HandlerDescription(NAME));
     }
+
+    private void inferEndpoint(MessageContext msgCtx, AxisService service) {
+        if (!msgCtx.isServerSide()) {
+            return;
+        }
+        String transport = null;
+        TransportInDescription transportIn = msgCtx.getTransportIn();
+        if (transportIn != null) {
+            transport = transportIn.getName();
+            if (transport == null) {
+                return;
+            }
+        }
+        AxisEndpoint endpoint = null;
+        Map endpointMapping = service.getEndpoints();
+        String serviceName = service.getName();
+
+        if (msgCtx.isDoingREST()) {
+            endpoint = (AxisEndpoint) endpointMapping.get(WSDLUtil.
+                    getEndpointName(serviceName, transport));
+        }
+        if (endpoint != null) {
+            msgCtx.setProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME, endpoint);
+        }
+    }
 }

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/namespace/Constants.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/namespace/Constants.java?rev=1291158&r1=1291157&r2=1291158&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/namespace/Constants.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/namespace/Constants.java Mon Feb 20 09:13:23 2012
@@ -357,6 +357,7 @@ public class Constants {
     public static final String MIME_CT_IMAGE_GIF = "image/gif";
     public static final String MIME_CT_TEXT_XML = "text/xml";
     public static final String MIME_CT_APPLICATION_XML = "application/xml";
+    public static final String MIME_CT_APPLICATION_URL_ENCODED = "application/x-www-form-urlencoded";
     public static final String MIME_CT_MULTIPART_PREFIX = "multipart/";
 
     public static final QName BASE_64_CONTENT_QNAME =

Modified: axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/wsdl/WSDLUtil.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/wsdl/WSDLUtil.java?rev=1291158&r1=1291157&r2=1291158&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/wsdl/WSDLUtil.java (original)
+++ axis/axis2/java/core/trunk/modules/kernel/src/org/apache/axis2/wsdl/WSDLUtil.java Mon Feb 20 09:13:23 2012
@@ -90,4 +90,22 @@ public class WSDLUtil {
         return httpMethod + httpLocation;
     }
 
+    /**
+     * This method will return the EndPointName for a service with give transport protocol
+     * ex : StudentServiceHttpEndpoint
+     *
+     * @param serviceName
+     * @param protocol transport protocol
+     * @return
+     */
+    public static String getEndpointName(String serviceName, String protocol) {
+
+        StringBuilder buffer = new StringBuilder();
+        buffer.append(serviceName);
+        buffer.append(protocol.substring(0, 1).toUpperCase());
+        buffer.append(protocol.substring(1, protocol.length()).toLowerCase());
+        buffer.append("Endpoint");
+        return buffer.toString();
+    }
+
 }