snichol 2004/06/22 20:28:10
Added: java/samples/excfault DeploymentDescriptor.xml ExcFaultClient.java ExcFaultException.java ExcFaultExceptionSerializer.java ExcFaultService.java README testit.cmd testit.sh Log: New sample demonstrating ExceptionFaultListener. Revision Changes Path 1.1 ws-soap/java/samples/excfault/DeploymentDescriptor.xml Index: DeploymentDescriptor.xml =================================================================== <isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:exc-fault-sample"> <isd:provider type="java" scope="Application" methods="throw1"> <isd:java class="samples.excfault.ExcFaultService" static="false"/> <isd:option key="gzip" value="false"/> <isd:option key="SessionRequired" value="false"/> </isd:provider> <isd:faultListener>org.apache.soap.server.ExceptionFaultListener</isd:faultListener> <isd:mappings> <isd:map encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:x="urn:exc-fault-sample" qname="x:ExcFaultException" javaType="samples.excfault.ExcFaultException" java2XMLClassName="samples.excfault.ExcFaultExceptionSerializer" xml2JavaClassName="samples.excfault.ExcFaultExceptionSerializer"/> </isd:mappings> </isd:service> 1.1 ws-soap/java/samples/excfault/ExcFaultClient.java Index: ExcFaultClient.java =================================================================== /** * * Copyright 2000-2004 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. */ package samples.excfault; import java.net.URL; 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.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 ([EMAIL PROTECTED]) */ public class ExcFaultClient { private static void usage() { System.err.println ("Usage: java " + ExcFaultClient.class.getName() + " [-9|-0] SOAP-router-URL"); System.exit(1); } public static void main (String[] args) throws Exception { // Process the arguments. URL url = null; String schemaURI = null; if (args.length == 1) { url = new URL(args[0]); schemaURI = Constants.NS_URI_2001_SCHEMA_XSD; } else if (args.length == 2) { if (args[0].equals("-9")) schemaURI = Constants.NS_URI_1999_SCHEMA_XSD; else if (args[0].equals("-0")) schemaURI = Constants.NS_URI_2000_SCHEMA_XSD; else usage(); url = new URL(args[1]); } else { usage(); } // Create the mapping ExcFaultExceptionSerializer ser = new ExcFaultExceptionSerializer(); SOAPMappingRegistry smr = new SOAPMappingRegistry(null, schemaURI); smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("urn:exc-fault-sample", "ExcFaultException"), ExcFaultException.class, ser, ser); // Build the call. Header header = new Header(); SOAPContext ctx = new SOAPContext(); ctx.setGzip(false); ctx.setDocLitSerialization(false); Vector params = new Vector(); Call call = new Call("urn:exc-fault-sample", "throw1", 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); Vector v = fault.getDetailEntries(); for (int i = 0; i < v.size(); i++) { Object o = v.elementAt(i); if (o instanceof Parameter) { Parameter p = (Parameter) o; if (p.getType().equals(ExcFaultException.class)) { ExcFaultException exc = (ExcFaultException) p.getValue(); System.err.println(" ExcFaultException >>>>>"); System.err.println(" Code: " + exc.getCode()); System.err.println(" Message: " + exc.getMessage()); exc.printStackTrace(); } } } } else { Parameter result = resp.getReturnValue(); Object o = result.getValue(); System.out.println("return: " + o); } } } 1.1 ws-soap/java/samples/excfault/ExcFaultException.java Index: ExcFaultException.java =================================================================== /** * * Copyright 2000-2004 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. */ package samples.excfault; import java.io.PrintStream; import java.io.PrintWriter; public class ExcFaultException extends Exception { protected int code; protected String stackTrace; public ExcFaultException(int code, String msg) { this(code, msg, (String) null); } public ExcFaultException(int code, String msg, String stackTrace) { super(msg); this.code = code; this.stackTrace = stackTrace; } public int getCode() { return code; } public void printStackTrace() { printStackTrace(System.err); } public void printStackTrace(PrintStream s) { PrintWriter pw = new PrintWriter(s); printStackTrace(pw); pw.close(); } public void printStackTrace(PrintWriter w) { if (stackTrace != null) { w.print(stackTrace); } else { super.printStackTrace(w); } } } 1.1 ws-soap/java/samples/excfault/ExcFaultExceptionSerializer.java Index: ExcFaultExceptionSerializer.java =================================================================== /** * * Copyright 2000-2004 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. */ package samples.excfault; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.apache.soap.Constants; import org.apache.soap.Envelope; import org.apache.soap.encoding.soapenc.SoapEncUtils; import org.apache.soap.rpc.SOAPContext; import org.apache.soap.util.Bean; 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; /** * Serializes and deserializes an ExcFaultException. * * @author Scott Nichol ([EMAIL PROTECTED]) */ public class ExcFaultExceptionSerializer 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 { nsStack.pushScope(); if (src == null) { SoapEncUtils.generateNullStructure(inScopeEncStyle, javaType, context, sink, nsStack, xjmr, ctx); } else { SoapEncUtils.generateStructureHeader(inScopeEncStyle, javaType, context, sink, nsStack, xjmr, ctx); sink.write(Envelope.LINE_SEPARATOR); ExcFaultException exc = (ExcFaultException) src; nsStack.pushScope(); int code = exc.getCode(); xjmr.marshall(Constants.NS_URI_SOAP_ENC, int.class, new Integer(code), "code", sink, nsStack, ctx); sink.write(Envelope.LINE_SEPARATOR); nsStack.popScope(); nsStack.pushScope(); String msg = exc.getMessage(); if (msg == null) { SoapEncUtils.generateNullStructure(Constants.NS_URI_SOAP_ENC, String.class, "message", sink, nsStack, xjmr, ctx); } else { xjmr.marshall(Constants.NS_URI_SOAP_ENC, String.class, msg, "message", sink, nsStack, ctx); } sink.write(Envelope.LINE_SEPARATOR); nsStack.popScope(); nsStack.pushScope(); StringWriter sw = new StringWriter(1024); PrintWriter pw = new PrintWriter(sw); exc.printStackTrace(pw); pw.close(); xjmr.marshall(Constants.NS_URI_SOAP_ENC, String.class, sw.toString(), "stackTrace", sink, nsStack, ctx); sink.write(Envelope.LINE_SEPARATOR); nsStack.popScope(); sink.write("</" + context + '>'); } nsStack.popScope(); } public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src, XMLJavaMappingRegistry xjmr, SOAPContext ctx) throws IllegalArgumentException { Element root = (Element)src; if (SoapEncUtils.isNull(root)) { return new Bean(ExcFaultException.class, null); } int code = -1; String msg = null; String stackTrace = null; Element tempEl = DOMUtils.getFirstChildElement(root); while (tempEl != null) { String declEncStyle = DOMUtils.getAttributeNS(tempEl, Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); String actualEncStyle = declEncStyle != null ? declEncStyle : inScopeEncStyle; // If it's a local reference, follow it. String href = tempEl.getAttribute(Constants.ATTR_REFERENCE); Element actualEl = tempEl; if (href != null && !href.equals("") && (href.charAt(0) == '#')) { href = href.substring(1); actualEl = DOMUtils.getElementByID(src.getOwnerDocument().getDocumentElement(),href); if (actualEl == null) { throw new IllegalArgumentException("No such ID '" + href + "'"); } } QName declItemType = SoapEncUtils.getTypeQName(actualEl); QName actualItemType = declItemType; Bean b = xjmr.unmarshall(actualEncStyle, actualItemType, actualEl, ctx); if (actualEl.getLocalName().equals("code")) { code = ((Integer) b.value).intValue(); } else if (actualEl.getLocalName().equals("message")) { msg = (String) b.value; } else if (actualEl.getLocalName().equals("stackTrace")) { stackTrace = (String) b.value; } else { throw new IllegalArgumentException("Unknown element '" + actualEl.getLocalName() + "'"); } tempEl = DOMUtils.getNextSiblingElement(tempEl); } ExcFaultException exc = new ExcFaultException(code, msg, stackTrace); return new Bean(ExcFaultException.class, exc); } } 1.1 ws-soap/java/samples/excfault/ExcFaultService.java Index: ExcFaultService.java =================================================================== /** * * Copyright 2000-2004 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. */ package samples.excfault; /** * Tests ExceptionFaultListener. * * @author Scott Nichol ([EMAIL PROTECTED]) */ public class ExcFaultService { /** * Throws an exception. */ public void throw1() throws ExcFaultException { throw new ExcFaultException(98, "This is thrown from ExcFaultService#throw1"); } } 1.1 ws-soap/java/samples/excfault/README Index: README =================================================================== Service: ------- To install this service on an Apache-SOAP listener, you need to make the samples.excfault 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 calls the throw1 service method. The service method throws an exception, which will be processed by the ExceptionFaultListener configured in the deployment descriptor. Additional Client Classpath Requirements: ---------------------------------------- ../.. Explanation: ----------- This demonstrates and tests the ExceptionFaultListener class. Sample Usage: ------------ java samples.excfault.ExcFaultClient \ 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: 398 SOAPAction: "" Accept-Encoding: gzip <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <ns1:throw1 xmlns:ns1="urn:exc-fault-sample" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> </ns1:throw1> </SOAP-ENV:Body> </SOAP-ENV:Envelope> *** RESPONSE *** HTTP/1.1 500 Internal Server Error Accept-Encoding: gzip Content-Type: text/xml;charset=utf-8 Content-Length: 3715 Date: Wed, 23 Jun 2004 03:24:15 GMT Server: Apache-Coyote/1.1 Connection: close <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Server</faultcode> <faultstring>Exception from service object: This is thrown from ExcFaultService#throw1</faultstring> <faultactor>/soap/servlet/rpcrouter</faultactor> <detail> <detailEntry xmlns:ns1="urn:exc-fault-sample" xsi:type="ns1:ExcFaultException" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <code xsi:type="xsd:int">98</code> <message xsi:type="xsd:string">This is thrown from ExcFaultService#throw1</message> <stackTrace xsi:type="xsd:string">samples.excfault.ExcFaultException: This is thrown from ExcFaultService#throw1 at samples.excfault.ExcFaultService.throw1(ExcFaultService.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.apache.soap.server.RPCRouter.invoke(RPCRouter.java:165) at org.apache.soap.providers.RPCJavaProvider.invoke(RPCJavaProvider.java:89) at org.apache.soap.server.http.RPCRouterServlet.doPost(RPCRouterServlet.java:348) at javax.servlet.http.HttpServlet.service(HttpServlet.java:763) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156) at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732) at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688) at java.lang.Thread.run(Thread.java:534) </stackTrace> </detailEntry> </detail> </SOAP-ENV:Fault> </SOAP-ENV:Body> </SOAP-ENV:Envelope> 1.1 ws-soap/java/samples/excfault/testit.cmd Index: testit.cmd =================================================================== @echo off echo This test assumes a server URL of http://localhost:8080/soap/servlet/rpcrouter echo Deploying the excfault 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 excfault test java samples.excfault.ExcFaultClient http://localhost:8080/soap/servlet/rpcrouter echo . echo Running the excfault test with the 1999 schema java samples.excfault.ExcFaultClient -9 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:exc-fault-sample echo . echo Verify that it's gone java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list 1.1 ws-soap/java/samples/excfault/testit.sh Index: testit.sh =================================================================== echo This test assumes a server URL of http://localhost:8080/soap/servlet/rpcrouter echo Deploying the excfault 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 excfault test java samples.excfault.ExcFaultClient http://localhost:8080/soap/servlet/rpcrouter echo echo Running the excfault test with the 1999 schema java samples.excfault.ExcFaultClient -9 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:exc-fault-sample echo echo Verify that it\'s gone java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list