axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chinth...@apache.org
Subject svn commit: r486561 - in /webservices/axis2/branches/java/WSDL_2_0/modules/kernel: src/org/apache/axis2/transport/http/RESTSender.java test/org/apache/axis2/transport/http/ test/org/apache/axis2/transport/http/RestSenderTest.java
Date Wed, 13 Dec 2006 09:32:49 GMT
Author: chinthaka
Date: Wed Dec 13 01:32:48 2006
New Revision: 486561

URL: http://svn.apache.org/viewvc?view=rev&rev=486561
Log:
Making the URL templates to work. Added a test as well.

Added:
    webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/transport/http/
    webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/transport/http/RestSenderTest.java
Modified:
    webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/RESTSender.java

Modified: webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/RESTSender.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/RESTSender.java?view=diff&rev=486561&r1=486560&r2=486561
==============================================================================
--- webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/RESTSender.java
(original)
+++ webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/RESTSender.java
Wed Dec 13 01:32:48 2006
@@ -45,6 +45,8 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
 
 public class RESTSender extends AbstractHTTPSender {
     private static final Log log = LogFactory.getLog(RESTSender.class);
@@ -146,37 +148,72 @@
     }
 
     /**
-     * This will be used to support http location. User can set set of param lists and those
will be
-     * appended to the url.
+     * This will be used to support http location. User will create an OMElement to be plugged
in to
+     * SOAP body. Then he has to name the parameters that should go in the url. This method
will extract
+     * those parameters from the body first child and append to the url.
+     * <p/>
+     * In addition to that, user can set a URL template, like ?firstName={FirstName}. In
this case,
+     * first name must be taken from the body and will replace the url.
      *
      * @param messageContext
      * @param urlString
      * @return - the URL after appending the properties
      */
-    private String appendParametersToURL(MessageContext messageContext, String urlString)
{
+    protected String appendParametersToURL(MessageContext messageContext, String urlString,
String queryPart) {
         try {
             OMElement firstElement = messageContext.getEnvelope().getBody().getFirstElement();
 
+            // first process the situ where user had explicitly put some params to go in
the URL
             ArrayList httpLocationParams = (ArrayList) messageContext.getProperty(
                     Constants.Configuration.URL_HTTP_LOCATION_PARAMS_LIST);
 
             URL url = new URL(urlString);
             String path = url.getPath();
 
-            for (int i = 0; i < httpLocationParams.size(); i++) {
-                String httpLocationParam = (String) httpLocationParams.get(i);
-                OMElement httpURLParam = firstElement.getFirstChildWithName(new QName(httpLocationParam));
-                if (httpURLParam != null) {
-                    path += httpURLParam.getText();
+            if (httpLocationParams != null) {
+                for (int i = 0; i < httpLocationParams.size(); i++) {
+                    String httpLocationParam = (String) httpLocationParams.get(i);
+                    OMElement httpURLParam = firstElement.getFirstChildWithName(new QName(httpLocationParam));
+                    if (httpURLParam != null) {
+                        path += "/" + httpURLParam.getText();
+                        httpURLParam.detach();
+                    }
                 }
             }
 
-            String query = url.getQuery();
-            if (query != null && !"".equals(query)) {
-                return path + "?" + query;
-            } else {
-                return path;
+            if (queryPart != null && queryPart.length() > 0) {
+                if (queryPart.startsWith("?")) {
+                    path = urlString + queryPart;
+                } else {
+                    path = urlString + "?" + queryPart;
+                }
+            }
+
+            // now let's process URL templates.
+            String patternString = "\\{[A-Z0-9a-z._%-]+\\}";
+            Pattern pattern = Pattern.compile(patternString);
+
+            StringBuffer buffer = new StringBuffer(path);
+
+            Matcher matcher = pattern.matcher(buffer);
+
+            while (matcher.find()) {
+                String match = matcher.group();
+
+                // Get indices of matching string
+                int start = matcher.start();
+                int end = matcher.end();
+
+                CharSequence charSequence = match.subSequence(1, match.length() - 1);
+
+                buffer.delete(start, end);
+                buffer.insert(start, getOMElementValue(charSequence.toString(), firstElement));
+
             }
+
+            return buffer.toString();
+
+
         } catch (MalformedURLException e) {
             log.error("Error in processing POST request", e);
         }
@@ -184,6 +221,17 @@
         return null;
     }
 
+    private String getOMElementValue(String elementName, OMElement parentElement) {
+        OMElement httpURLParam = parentElement.getFirstChildWithName(new QName(elementName));
+
+        if (httpURLParam != null) {
+            return httpURLParam.getText();
+        }
+
+        return null;
+
+    }
+
     private void sendViaGet(MessageContext msgContext, URL url)
             throws MalformedURLException, AxisFault, IOException {
         String param = getQueryParameters(msgContext);
@@ -193,13 +241,9 @@
         }
 
         String urlString = url.getFile();
-        urlString = appendParametersToURL(msgContext, urlString);
+        urlString = appendParametersToURL(msgContext, urlString, param);
 
-        if (param != null && param.length() > 0) {
-            getMethod.setPath(urlString + "?" + param);
-        } else {
-            getMethod.setPath(urlString);
-        }
+        getMethod.setPath(urlString);
 
         // Serialization as "application/x-www-form-urlencoded"
         String charEncoding =
@@ -273,11 +317,9 @@
             reqData = createRequest(msgContext, dataout);
             String pathString = url.getPath();
 
-            appendParametersToURL(msgContext, pathString);
+            String urlString = appendParametersToURL(msgContext, pathString, reqData.urlRequest);
 
-            postMethod.setPath(pathString + ((reqData.urlRequest) != null
-                    ? ("?" + reqData.urlRequest)
-                    : ""));
+            postMethod.setPath(urlString);
 
             if (reqData.bodyRequest == null) {
                 reqData.bodyRequest = "0";

Added: webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/transport/http/RestSenderTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/transport/http/RestSenderTest.java?view=auto&rev=486561
==============================================================================
--- webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/transport/http/RestSenderTest.java
(added)
+++ webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/transport/http/RestSenderTest.java
Wed Dec 13 01:32:48 2006
@@ -0,0 +1,66 @@
+package org.apache.axis2.transport.http;
+
+import junit.framework.TestCase;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.SOAPEnvelope;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class RestSenderTest extends TestCase {
+
+    String testURL = "http://locahost:8080/paramOne/{FirstName}";
+    String queryPart = "?test=1&lastName={LastName}";
+
+
+    private MessageContext messageContext;
+
+
+    protected void setUp() throws Exception {
+        messageContext = new MessageContext();
+
+        SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
+
+        SOAPEnvelope defaultEnvelope = soapFactory.getDefaultEnvelope();
+        messageContext.setEnvelope(defaultEnvelope);
+
+        OMElement bodyFirstElement = soapFactory.createOMElement("TestOperation", null);
+        defaultEnvelope.getBody().addChild(bodyFirstElement);
+
+        soapFactory.createOMElement("FirstName", null, bodyFirstElement).setText("Foo");
+        soapFactory.createOMElement("LastName", null, bodyFirstElement).setText("Bar");
+
+    }
+
+    public void testAppendParametersToURL() {
+        RESTSender restSender = new RESTSender();
+        String modifiedURL = restSender.appendParametersToURL(messageContext, testURL, queryPart);
+
+        System.out.println("original = " + testURL + queryPart);
+        System.out.println("modifiedURL = " + modifiedURL);
+
+        String expectedURL = "http://locahost:8080/paramOne/Foo?test=1&lastName=Bar";
+
+        assertEquals(modifiedURL, expectedURL);
+
+    }
+}



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


Mime
View raw message