You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by sn...@apache.org on 2002/09/16 17:26:26 UTC
cvs commit: xml-soap/java/src/org/apache/soap/encoding/soapenc PropertyBagSerializer.java
snichol 2002/09/16 08:26:26
Modified: java/docs changes.html
Added: java/samples/propertybag DeploymentDescriptor.xml
PropertyBagClient.java PropertyBagService.java
README testit.cmd testit.sh
java/src/org/apache/soap/encoding/soapenc
PropertyBagSerializer.java
Log:
Add a serializer (PropertyBagSerializer) that can read SOAP compound
types into a Hashtable where the element names are the keys and the
element contents are the values, which means beans do not have to be created
for each compound type being deserialized. Conversely, it can write a
Map similary, providing an alternative to the default Apache SOAP serialization
of Maps.
Revision Changes Path
1.46 +6 -0 xml-soap/java/docs/changes.html
Index: changes.html
===================================================================
RCS file: /home/cvs/xml-soap/java/docs/changes.html,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -r1.45 -r1.46
--- changes.html 6 Sep 2002 06:14:10 -0000 1.45
+++ changes.html 16 Sep 2002 15:26:25 -0000 1.46
@@ -80,6 +80,12 @@
are specified in the deployment descriptor. The container must be
configured for authentication through its configuration and/or the Apache
SOAP web.xml deployment descriptor.</li>
+ <li>Add a serializer (PropertyBagSerializer) that can read SOAP compound
+ types into a Hashtable where the element names are the keys and the
+ element contents are the values, which means beans do not have to be created
+ for each compound type being deserialized. Conversely, it can write a
+ Map similary, providing an alternative to the default Apache SOAP serialization
+ of Maps.</li>
</ul>
</li>
</ul>
1.1 xml-soap/java/samples/propertybag/DeploymentDescriptor.xml
Index: DeploymentDescriptor.xml
===================================================================
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:property-bag-sample">
<isd:provider type="java"
scope="Application"
methods="analyze">
<isd:java class="samples.propertybag.PropertyBagService" static="false"/>
<isd:option key="gzip" value="false"/>
<isd:option key="SessionRequired" value="false"/>
</isd:provider>
<isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener>
<isd:mappings>
<isd:map encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:x="urn:property-bag-sample" qname="x:PropertyBag"
javaType="java.util.Map"
xml2JavaClassName="org.apache.soap.encoding.soapenc.PropertyBagSerializer"/>
</isd:mappings>
</isd:service>
1.1 xml-soap/java/samples/propertybag/PropertyBagClient.java
Index: PropertyBagClient.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "SOAP" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2000, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package samples.propertybag;
import java.net.URL;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.Header;
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.encoding.soapenc.PropertyBagSerializer;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import org.apache.soap.rpc.SOAPContext;
import org.apache.soap.util.xml.QName;
/**
* See README for info.
*
* @author Scott Nichol (snichol@computer.org)
*/
public class PropertyBagClient {
public static void main (String[] args) throws Exception {
if (args.length != 1) {
System.err.println ("Usage: java " + PropertyBagClient.class.getName() +
" SOAP-router-URL");
System.exit(1);
}
// Process the arguments.
URL url = new URL(args[0]);
// Create the bag (something that looks like a "book bean"
Hashtable bag = new Hashtable(7);
bag.put("AuthorFirstName", "Michael");
bag.put("AuthorLastName", "Chabon");
bag.put("Title", "The Amazing Adventures of Kavalier & Clay");
bag.put("Pages", new Integer(639));
bag.put("CopyrightYear", new Integer(2000));
bag.put("Publisher", "Random House");
bag.put("DateAdded", new Date());
// Create the mapping
PropertyBagSerializer ser = new PropertyBagSerializer();
SOAPMappingRegistry smr = new SOAPMappingRegistry();
smr.mapTypes(Constants.NS_URI_SOAP_ENC,
new QName("urn:property-bag-sample", "PropertyBag"),
Map.class, ser, null);
// Build the call.
Header header = new Header();
SOAPContext ctx = new SOAPContext();
ctx.setGzip(false);
ctx.setDocLitSerialization(false);
Vector params = new Vector();
params.addElement(new Parameter("bag", Map.class, bag, null));
Call call = new Call("urn:property-bag-sample",
"analyze",
params,
header,
Constants.NS_URI_SOAP_ENC,
ctx);
call.setSOAPMappingRegistry(smr);
// Invoke the call and handle the response.
Response resp = call.invoke(url, "");
if (resp.generatedFault()) {
Fault fault = resp.getFault();
System.err.println("Generated fault: " + fault);
} else {
Parameter result = resp.getReturnValue();
Hashtable hash = (Hashtable) result.getValue();
System.out.println("keys: " + hash.get("keys"));
System.out.println("values: " + hash.get("values"));
}
}
}
1.1 xml-soap/java/samples/propertybag/PropertyBagService.java
Index: PropertyBagService.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "SOAP" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2000, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package samples.propertybag;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Tests PropertyBagSerializer.
*
* @author Scott Nichol (snichol@computer.org)
*/
public class PropertyBagService {
/**
* "Analyzes" a map.
*/
public Hashtable analyze(Map map) {
Hashtable hash = new Hashtable(2);
Set keySet = map.keySet();
StringBuffer keys = new StringBuffer(1024);
for (Iterator i = keySet.iterator(); i.hasNext(); ) {
if (keys.length() != 0)
keys.append('~');
keys.append(i.next().toString());
}
hash.put("keys", keys.toString());
Collection valuesC = map.values();
StringBuffer values = new StringBuffer(1024);
for (Iterator i = valuesC.iterator(); i.hasNext(); ) {
if (values.length() != 0)
values.append('~');
values.append(i.next().toString());
}
hash.put("values", values.toString());
return hash;
}
}
1.1 xml-soap/java/samples/propertybag/README
Index: README
===================================================================
Service:
-------
To install this service on an Apache-SOAP listener, you need to make
the samples.propertybag package available on the Apache-SOAP listener's
classpath. Then deploy this service by filling in the deployment
template using the info in the deployment descriptor in this
directory or by using the service manager client:
java org.apache.soap.server.ServiceManagerClient routerURL deploy dd.xml
where routerURL is the URL of the SOAP RPC router and dd.xml is the
name of the deployment descriptor file. For example:
java org.apache.soap.server.ServiceManagerClient \
http://localhost:8080/soap/servlet/rpcrouter deploy DeploymentDescriptor.xml
Client:
------
There is one HTTP client that sends a "book bean" property bag to the service.
The service method "analyzes" this, returning the list of keys and list of values.
Additional Client Classpath Requirements:
----------------------------------------
../..
Explanation:
-----------
This demonstrates and tests the PropertyBagSerializer class. A property bag is
meant to be used as an alternative to a Java bean with no behavior. It is
especially useful to receive complex types received in SOAP messages without
having to create a bean for each type.
Sample Usage:
------------
java samples.gzip.PropertyBagClient \
http://localhost:8080/soap/servlet/rpcrouter
Wire Dump:
---------
*** REQUEST ***
POST /soap/servlet/rpcrouter HTTP/1.0
Host: localhost:81
Content-Type: text/xml; charset=utf-8
Content-Length: 892
SOAPAction: ""
Accept-Encoding: x-gzip
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:analyze xmlns:ns1="urn:property-bag-sample" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<bag xsi:type="ns1:PropertyBag">
<Publisher xsi:type="xsd:string">Random House</Publisher>
<AuthorFirstName xsi:type="xsd:string">Michael</AuthorFirstName>
<CopyrightYear xsi:type="xsd:int">2000</CopyrightYear>
<AuthorLastName xsi:type="xsd:string">Chabon</AuthorLastName>
<DateAdded xsi:type="xsd:dateTime">2002-09-16T15:16:33.832Z</DateAdded>
<Title xsi:type="xsd:string">The Amazing Adventures of Kavalier & Clay</Title>
<Pages xsi:type="xsd:int">639</Pages>
</bag>
</ns1:analyze>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*** RESPONSE ***
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 890
Date: Mon, 16 Sep 2002 15:16:34 GMT
Accept-Encoding: x-gzip
Server: Apache Tomcat/4.0.1 (HTTP/1.1 Connector)
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:analyzeResponse xmlns:ns1="urn:property-bag-sample" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xmlns:ns2="http://xml.apache.org/xml-soap" xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">keys</key>
<value xsi:type="xsd:string">AuthorLastName~AuthorFirstName~DateAdded~CopyrightYear~Title~Pages~Publisher</value>
</item>
<item>
<key xsi:type="xsd:string">values</key>
<value xsi:type="xsd:string">Chabon~Michael~Mon Sep 16 11:16:33 EDT 2002~2000~The Amazing Adventures of Kavalier & Clay~639~Random House</value>
</item>
</return>
</ns1:analyzeResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
1.1 xml-soap/java/samples/propertybag/testit.cmd
Index: testit.cmd
===================================================================
@echo off
echo This test assumes a server URL of http://localhost:8080/soap/servlet/rpcrouter
echo Deploying the propertybag service...
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter deploy DeploymentDescriptor.xml
echo .
echo Verify that it's there
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list
echo .
echo Running the propertybag test
java samples.propertybag.PropertyBagClient http://localhost:8080/soap/servlet/rpcrouter
echo .
echo Undeploy it now
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter undeploy urn:property-bag-sample
echo .
echo Verify that it's gone
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list
1.1 xml-soap/java/samples/propertybag/testit.sh
Index: testit.sh
===================================================================
echo This test assumes a server URL of http://localhost:8080/soap/servlet/rpcrouter
echo Deploying the propertybag service...
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter deploy DeploymentDescriptor.xml
echo
echo Verify that it\'s there
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list
echo
echo Running the propertybag test
rm return.dat
java samples.propertybag.PropertyBagClient http://localhost:8080/soap/servlet/rpcrouter
echo
echo Undeploy it now
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter undeploy urn:property-bag-sample
echo
echo Verify that it\'s gone
java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list
1.1 xml-soap/java/src/org/apache/soap/encoding/soapenc/PropertyBagSerializer.java
Index: PropertyBagSerializer.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "SOAP" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2000, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.soap.encoding.soapenc;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.apache.soap.Constants;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.RPCConstants;
import org.apache.soap.rpc.SOAPContext;
import org.apache.soap.util.Bean;
import org.apache.soap.util.StringUtils;
import org.apache.soap.util.xml.Deserializer;
import org.apache.soap.util.xml.DOMUtils;
import org.apache.soap.util.xml.NSStack;
import org.apache.soap.util.xml.QName;
import org.apache.soap.util.xml.Serializer;
import org.apache.soap.util.xml.XMLJavaMappingRegistry;
/**
* A <code>PropertyBagSerializer</code> can be used to serialize and
* deserialize Maps using the <code>SOAP-ENC</code>
* encoding style. It differs from <code>MapSerializer</code> in the
* actual XML representation, which here looks more like a complex type.
*
* @author Scott Nichol (snichol@computer.org)
*/
public class PropertyBagSerializer implements Serializer, Deserializer {
public void marshall(String inScopeEncStyle, Class javaType, Object src,
Object context, Writer sink, NSStack nsStack,
XMLJavaMappingRegistry xjmr, SOAPContext ctx)
throws IllegalArgumentException, IOException {
if (src == null) {
SoapEncUtils.generateNullStructure(inScopeEncStyle,
javaType,
context,
sink,
nsStack,
xjmr,
ctx);
} else if (src instanceof Map) {
SoapEncUtils.generateStructureHeader(inScopeEncStyle,
javaType,
context,
sink,
nsStack,
xjmr,
ctx);
sink.write(StringUtils.lineSeparator);
Map map = (Map) src;
Set keySet = map.keySet();
for (Iterator keys = keySet.iterator(); keys.hasNext(); ) {
Object key = keys.next();
Object value = map.get(key);
// Serialize the property.
Parameter param = new Parameter(key.toString(), value.getClass(), value, null);
xjmr.marshall(Constants.NS_URI_SOAP_ENC, Parameter.class, param,
null, sink, nsStack, ctx);
sink.write(StringUtils.lineSeparator);
}
sink.write("</" + context + '>');
} else {
throw new IllegalArgumentException("Tried to pass a '" +
src.getClass().toString() +
"' to PropertyBagSerializer");
}
}
public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src,
XMLJavaMappingRegistry xjmr, SOAPContext ctx) {
Element root = (Element)src;
String name = root.getTagName();
if (SoapEncUtils.isNull(root))
return new Bean(Map.class, null);
Hashtable hash = new Hashtable();
Element tempEl = DOMUtils.getFirstChildElement(root);
while (tempEl != null) {
Bean paramBean = xjmr.unmarshall(inScopeEncStyle,
RPCConstants.Q_ELEM_PARAMETER,
tempEl, ctx);
Parameter param = (Parameter) paramBean.value;
hash.put(param.getName(), param.getValue());
tempEl = DOMUtils.getNextSiblingElement(tempEl);
}
return new Bean(Map.class, hash);
}
}
--
To unsubscribe, e-mail: <ma...@xml.apache.org>
For additional commands, e-mail: <ma...@xml.apache.org>