You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ch...@apache.org on 2006/12/15 15:10:34 UTC
svn commit: r487560 - in
/webservices/axis2/branches/java/WSDL_2_0/modules/kernel:
src/org/apache/axis2/transport/http/util/ src/org/apache/axis2/util/
test/org/apache/axis2/util/
Author: chinthaka
Date: Fri Dec 15 06:10:32 2006
New Revision: 487560
URL: http://svn.apache.org/viewvc?view=rev&rev=487560
Log:
Handling URL templates in the server side. Now we can support all the scenarios defined in http://dev.w3.org/cvsweb/~checkout~/2002/ws/desc/test-suite/documents/good/LocationTemplate-1G/SOAPservice.wsdl?rev=1.2&content-type=text/plain.
Added:
webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/MultipleEntryHashMap.java
Modified:
webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/util/RESTUtil.java
webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/SchemaUtil.java
webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/util/SchemaUtilTest.java
Modified: webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/util/RESTUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/util/RESTUtil.java?view=diff&rev=487560&r1=487559&r2=487560
==============================================================================
--- webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/util/RESTUtil.java (original)
+++ webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/transport/http/util/RESTUtil.java Fri Dec 15 06:10:32 2006
@@ -30,6 +30,7 @@
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.engine.AxisEngine;
import org.apache.axis2.engine.RequestURIBasedDispatcher;
+import org.apache.axis2.engine.SOAPMessageBodyBasedDispatcher;
import org.apache.axis2.transport.TransportUtils;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.util.SchemaUtil;
Added: webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/MultipleEntryHashMap.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/MultipleEntryHashMap.java?view=auto&rev=487560
==============================================================================
--- webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/MultipleEntryHashMap.java (added)
+++ webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/MultipleEntryHashMap.java Fri Dec 15 06:10:32 2006
@@ -0,0 +1,63 @@
+package org.apache.axis2.util;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.ArrayList;
+/*
+ * 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.
+ */
+
+/**
+ * This will make a hash map which can contain multiple entries for the same hash value.
+ */
+public class MultipleEntryHashMap {
+
+ private Map table;
+
+ public MultipleEntryHashMap() {
+ this.table = new HashMap(1);
+ }
+
+ /**
+ * If you call get once in this, it will remove that item from the map
+ *
+ * @param key
+ * @return
+ */
+ public Object get(Object key) {
+ ArrayList list = (ArrayList) table.get(key);
+ if (list != null) {
+ Object o = list.get(0);
+ list.remove(0);
+ return o;
+ }
+
+ return null;
+
+ }
+
+ public Object put(Object key, Object value) {
+ ArrayList list = (ArrayList) table.get(key);
+ if (list == null) {
+ ArrayList listToBeAdded = new ArrayList();
+ table.put(key, listToBeAdded);
+ listToBeAdded.add(value);
+ } else {
+ list.add(value);
+ }
+
+ return value;
+ }
+}
Modified: webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/SchemaUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/SchemaUtil.java?view=diff&rev=487560&r1=487559&r2=487560
==============================================================================
--- webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/SchemaUtil.java (original)
+++ webservices/axis2/branches/java/WSDL_2_0/modules/kernel/src/org/apache/axis2/util/SchemaUtil.java Fri Dec 15 06:10:32 2006
@@ -90,15 +90,17 @@
* @param soapFactory
* @throws AxisFault
*/
+
public static SOAPEnvelope handleMediaTypeURLEncoded(MessageContext msgCtxt,
HttpServletRequest request,
XmlSchemaElement xmlSchemaElement,
SOAPFactory soapFactory) throws AxisFault {
- Map requestParameterMap = request.getParameterMap();
SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
SOAPBody body = soapEnvelope.getBody();
+ Map requestParameterMap = request.getParameterMap();
+
if (xmlSchemaElement == null) {
// if there is no schema its piece of cake !! add these to the soap body in any order you like.
// Note : if there are parameters in the path of the URL, there is no way this can add them
@@ -129,7 +131,8 @@
}
OMElement bodyFirstChild = soapFactory.createOMElement(bodyFirstChildQName, body);
- // we handle only GET and POST methods. Do a sanity check here, first
+ // we handle only GET and POST methods. Do a sanity check here, first.
+
String httpMethod = request.getMethod();
if (org.apache.axis2.transport.http.HTTPConstants.HTTP_METHOD_POST.equals(httpMethod)
|| (org.apache.axis2.transport.http.HTTPConstants.HTTP_METHOD_GET.equals(httpMethod)))
@@ -154,7 +157,7 @@
// create a variable wit the default as "&"
String queryParameterSeparator = "&";
- Map httpLocationParameterMap = new HashMap(1);
+ MultipleEntryHashMap httpLocationParameterMap = new MultipleEntryHashMap();
if (axisBindingOperation != null) {
// now check whether we have a query parameter separator defined in this
@@ -170,12 +173,10 @@
// parameter names can be different from the element name in the schema, due
// to http location. Let's filter the parameter names from it.
- httpLocationParameterMap = createHttpLocationParameterMap(httpLocation, queryParameterSeparator);
+ httpLocationParameterMap = createHttpLocationParameterMap(httpLocation, queryParameterSeparator, request);
}
- Map parameterMap = request.getParameterMap();
-
while (iterator.hasNext()) {
XmlSchemaElement innerElement = (XmlSchemaElement) iterator.next();
QName qName = innerElement.getQName();
@@ -187,19 +188,27 @@
name = mappingParamName;
}
- String[] parameterValuesArray = (String[]) parameterMap.get(name);
+ String[] parameterValuesArray = (String[]) requestParameterMap.get(name);
+ String value = null;
if (parameterValuesArray != null &&
!"".equals(parameterValuesArray[0]) && parameterValuesArray[0] != null)
{
- OMNamespace ns = (qName == null || qName.getNamespaceURI() == null || qName.getNamespaceURI().length() == 0) ?
- null :
- soapFactory.createOMNamespace(qName.getNamespaceURI(), null);
- soapFactory.createOMElement(name, ns,
- bodyFirstChild).setText(parameterValuesArray[0]);
- } else {
+ value = parameterValuesArray[0];
+
+ } else if (httpLocationParameterMap.get(name) != null) {
+
+ value = (String) httpLocationParameterMap.get(name);
+
+ } else if (xmlSchemaElement.getMinOccurs() != 0) {
throw new AxisFault("Required element " + qName +
" defined in the schema can not be found in the request");
}
+
+ OMNamespace ns = (qName == null || qName.getNamespaceURI() == null || qName.getNamespaceURI().length() == 0) ?
+ null :
+ soapFactory.createOMNamespace(qName.getNamespaceURI(), null);
+ soapFactory.createOMElement(name, ns,
+ bodyFirstChild).setText(value);
}
}
}
@@ -223,16 +232,31 @@
* @param httpLocation
* @param queryParameterSeparator
*/
- protected static Map createHttpLocationParameterMap(String httpLocation, String queryParameterSeparator) {
-
- Map httpLocationParameterMap = new HashMap();
- // if there is a questions mark in the front, remove it
- if (httpLocation.startsWith("?")) {
- httpLocation = httpLocation.substring(1, httpLocation.length());
+ protected static MultipleEntryHashMap createHttpLocationParameterMap(String httpLocation,
+ String queryParameterSeparator,
+ HttpServletRequest request) {
+
+ MultipleEntryHashMap httpLocationParameterMap = new MultipleEntryHashMap();
+
+ // let's handle query parameters and the path separately
+ String[] urlParts = httpLocation.split("\\?");
+ String templatedPath = urlParts[0];
+
+ if (urlParts.length > 1) {
+ String templatedQueryParams = urlParts[1];
+ // first extract parameters from the query part
+ extractParametersFromQueryPart(templatedQueryParams, queryParameterSeparator, httpLocationParameterMap, request.getParameterMap());
}
-// now let's tokenize the string with query parameter separator
- String[] nameValuePairs = httpLocation.split(queryParameterSeparator);
+ // now let's do the difficult part, extract parameters from the path element.
+ extractParametersFromPath(templatedPath, httpLocationParameterMap, request.getRequestURL());
+
+ return httpLocationParameterMap;
+ }
+
+ protected static void extractParametersFromQueryPart(String templatedQueryParams, String queryParameterSeparator, MultipleEntryHashMap httpLocationParameterMap, Map parameterMap) {
+ // now let's tokenize the string with query parameter separator
+ String[] nameValuePairs = templatedQueryParams.split(queryParameterSeparator);
for (int i = 0; i < nameValuePairs.length; i++) {
StringBuffer buffer = new StringBuffer(nameValuePairs[i]);
// this name value pair will be either name=value or
@@ -241,12 +265,84 @@
if (buffer.indexOf("{") > 0 && buffer.indexOf("}") > 0) {
String parameterName = buffer.substring(0, buffer.indexOf("="));
String schemaElementName = buffer.substring(buffer.indexOf("=") + 2, buffer.length() - 1);
- httpLocationParameterMap.put(schemaElementName, parameterName);
-
+ httpLocationParameterMap.put(schemaElementName, parameterMap.get(parameterName));
}
}
+ }
- return httpLocationParameterMap;
+ /**
+ * Here is what I will try to do here. I will first try to identify the location of the first
+ * template element in the request URI. I am trying to deduce the location of that location
+ * using the httpLocation element of the binding (it is passed in to this
+ * method).
+ * If there is a contant part in the httpLocation, then I will identify it. For this, I get
+ * the index of {, from httpLocation param, and whatever to the left of it is the contant part.
+ * Then I search for this constant part inside the url. This will give us the access to the first
+ * template parameter.
+ * To find the end of this parameter, we need to get the index of the next constant, from
+ * httpLocation attribute. Likewise we keep on discovering parameters.
+ * <p/>
+ * Assumptions :
+ * 1. User will always append the value of httpLocation to the address given in the
+ * endpoint.
+ * 2. I was talking about the constants in the httpLocation. Those constants will not occur,
+ * to a reasonable extend, before the constant we are looking for.
+ *
+ * @param templatedPath
+ * @param httpLocationParameterMap
+ */
+ protected static void extractParametersFromPath(String templatedPath, MultipleEntryHashMap httpLocationParameterMap, StringBuffer requestURIBuffer) {
+
+
+ if (templatedPath != null && !"".equals(templatedPath) && templatedPath.indexOf("{") > -1) {
+ StringBuffer pathTemplate = new StringBuffer(templatedPath);
+
+ // this will hold the index, from which we need to process the request URI
+ int startIndex = 0;
+ int templateStartIndex = 0;
+ int templateEndIndex = 0;
+ int indexOfNextConstant = 0;
+
+ while (startIndex < requestURIBuffer.length()) {
+ // this will always hold the starting index of a template parameter
+ templateStartIndex = pathTemplate.indexOf("{", templateStartIndex);
+
+ if (templateStartIndex > 0) {
+ // get the preceding constant part from the template
+ String constantPart = pathTemplate.substring(templateEndIndex + 1, templateStartIndex);
+
+ // get the index of the end of this template param
+ templateEndIndex = pathTemplate.indexOf("}", templateStartIndex);
+
+ String parameterName = pathTemplate.substring(templateStartIndex + 1, templateEndIndex);
+ // next try to find the next constant
+ templateStartIndex = pathTemplate.indexOf("{", templateEndIndex);
+
+ int endIndexOfConstant = requestURIBuffer.indexOf(constantPart, indexOfNextConstant) + constantPart.length();
+
+ if (templateEndIndex == pathTemplate.length() - 1 || templateStartIndex == -1) {
+
+ constantPart = pathTemplate.substring(templateEndIndex + 1, pathTemplate.length());
+ indexOfNextConstant = requestURIBuffer.indexOf(constantPart, endIndexOfConstant);
+
+ httpLocationParameterMap.put(parameterName, requestURIBuffer.substring(endIndexOfConstant, indexOfNextConstant));
+ startIndex = requestURIBuffer.length();
+ } else {
+
+ // this is the next constant from the template
+ constantPart = pathTemplate.substring(templateEndIndex + 1, templateStartIndex);
+
+ indexOfNextConstant = requestURIBuffer.indexOf(constantPart, endIndexOfConstant);
+ httpLocationParameterMap.put(parameterName, requestURIBuffer.substring(endIndexOfConstant, indexOfNextConstant));
+ startIndex = indexOfNextConstant;
+
+ }
+
+ }
+
+ }
+
+ }
}
}
Modified: webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/util/SchemaUtilTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/util/SchemaUtilTest.java?view=diff&rev=487560&r1=487559&r2=487560
==============================================================================
--- webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/util/SchemaUtilTest.java (original)
+++ webservices/axis2/branches/java/WSDL_2_0/modules/kernel/test/org/apache/axis2/util/SchemaUtilTest.java Fri Dec 15 06:10:32 2006
@@ -2,7 +2,7 @@
import junit.framework.TestCase;
-import java.util.Map;
+import java.util.*;
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
@@ -21,12 +21,74 @@
public class SchemaUtilTest extends TestCase {
- String httpLocation = "?test=1&last={LastName}&firstNamePart={FirstName}";
+ private Map parameterMap;
+
+ protected void setUp() throws Exception {
+ parameterMap = new HashMap();
+
+ /**
+ * <year>2006</year>
+ <month>11</month>
+ <day>18</day>
+ <time>16:40:00</time>
+ <time>20:00:42</time>
+ */
+
+ }
public void testCreateHttpLocationParameterMap() {
- Map httpLocationParameterMap = SchemaUtil.createHttpLocationParameterMap(httpLocation, "&");
- assertEquals("firstNamePart", httpLocationParameterMap.get("FirstName"));
- assertEquals("last", httpLocationParameterMap.get("LastName"));
- assertTrue(httpLocationParameterMap.size() == 2);
+
+ parameterMap.put("firstNamePart", "ABCD");
+ parameterMap.put("last", "DCBA");
+
+ String httpLocation = "test=1&last={LastName}&firstNamePart={FirstName}";
+
+ MultipleEntryHashMap multipleEntryHashMap = new MultipleEntryHashMap();
+
+ SchemaUtil.extractParametersFromQueryPart(httpLocation, "&", multipleEntryHashMap, parameterMap);
+ assertEquals("ABCD", multipleEntryHashMap.get("FirstName"));
+ assertEquals("DCBA", multipleEntryHashMap.get("LastName"));
}
+
+ public void testServerSideURLTemplateHandlingOne() {
+ String template = "datespace/{year}/{month}/{day}/{time}-{time}.html";
+
+ StringBuffer actualPath = new StringBuffer("http://example.org/datespace/2006/11/18/16:40:00-20:00:42.html");
+
+ MultipleEntryHashMap multipleEntryHashMap = new MultipleEntryHashMap();
+
+ SchemaUtil.extractParametersFromPath(template, multipleEntryHashMap, actualPath);
+
+ assertEquals(multipleEntryHashMap.get("year"), "2006");
+ assertEquals(multipleEntryHashMap.get("month"), "11");
+ assertEquals(multipleEntryHashMap.get("day"), "18");
+ assertEquals(multipleEntryHashMap.get("time"), "16:40:00");
+ assertEquals(multipleEntryHashMap.get("time"), "20:00:42");
+ }
+
+ public void testServerSideURLTemplateHandlingTwo() {
+
+ parameterMap.put("day", "18");
+ parameterMap.put("start", "16:40:00");
+ parameterMap.put("end", "20:00:42");
+ String template = "datespace/{year}/{month}.html?day={day};start={time};end={time}";
+
+ StringBuffer actualPath = new StringBuffer("http://example.org/datespace/2006/11.html?day=18;start=16:40:00;end=20:00:42");
+
+ MultipleEntryHashMap multipleEntryHashMap = new MultipleEntryHashMap();
+
+ String[] urlParts = template.split("\\?");
+ String templatedPath = urlParts[0];
+ String templatedQueryParams = urlParts[1];
+
+ SchemaUtil.extractParametersFromPath(templatedPath, multipleEntryHashMap, actualPath);
+ SchemaUtil.extractParametersFromQueryPart(templatedQueryParams, ";", multipleEntryHashMap, parameterMap);
+
+ assertEquals(multipleEntryHashMap.get("year"), "2006");
+ assertEquals(multipleEntryHashMap.get("month"), "11");
+ assertEquals(multipleEntryHashMap.get("day"), "18");
+ assertEquals(multipleEntryHashMap.get("time"), "16:40:00");
+ assertEquals(multipleEntryHashMap.get("time"), "20:00:42");
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org