001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: //
021: // IBM Confidential OCO Source Material
022: // (C) COPYRIGHT International Business Machines Corp. 2006
023: // The source code for this program is not published or otherwise divested
024: // of its trade secrets, irrespective of what has been deposited with the
025: // U.S. Copyright Office.
026: //
027: // Change History:
028: // Date UserId Defect Description
029: // ----------------------------------------------------------------------------
030: // 08/28/2006 mzheng LIDB3296-46.01 New File
031: //
032: package org.apache.axis2.jaxws.sample.faultsservice;
033:
034: import javax.annotation.PostConstruct;
035: import javax.annotation.PreDestroy;
036: import javax.annotation.Resource;
037: import javax.jws.WebService;
038: import javax.xml.namespace.QName;
039: import javax.xml.soap.Detail;
040: import javax.xml.soap.DetailEntry;
041: import javax.xml.soap.SOAPBody;
042: import javax.xml.soap.SOAPException;
043: import javax.xml.soap.SOAPFactory;
044: import javax.xml.soap.SOAPFault;
045: import javax.xml.ws.Holder;
046: import javax.xml.ws.WebServiceContext;
047: import javax.xml.ws.WebServiceException;
048: import javax.xml.ws.soap.SOAPFaultException;
049:
050: import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
051: import org.apache.axis2.saaj.SOAPEnvelopeImpl;
052: import org.apache.axis2.jaxws.TestLogger;
053: import org.test.polymorphicfaults.BaseFault;
054: import org.test.polymorphicfaults.ComplexFault;
055: import org.test.polymorphicfaults.DerivedFault1;
056: import org.test.polymorphicfaults.DerivedFault2;
057:
058: /**
059: * This class provides server side implementation for the
060: * FaultsServicePortType.
061: */
062: @WebService(targetNamespace="http://org/test/polymorphicfaults",wsdlLocation="WEB-INF/wsdl/FaultsService.wsdl",serviceName="FaultsService",portName="FaultsPort",endpointInterface="org.apache.axis2.jaxws.sample.faultsservice.FaultsServicePortType")
063: public class FaultsServiceSoapBindingImpl implements
064: FaultsServicePortType {
065:
066: private WebServiceContext ctx = null;
067: private boolean init = false;
068:
069: /**
070: * Throws wrapper exceptions for fault beans
071: */
072: public float getQuote(String tickerSymbol)
073: throws BaseFault_Exception, DerivedFault1_Exception,
074: DerivedFault2_Exception, InvalidTickerFault_Exception,
075: SimpleFault {
076: TestLogger.logger.debug("\nIn getQuote(): " + tickerSymbol
077: + "\n");
078: if (tickerSymbol.equals("SMPL")) {
079: throw new SimpleFault("Server throws SimpleFault", 100);
080: } else if (tickerSymbol.equals("LEGC")) {
081: throw new InvalidTickerFault_Exception(
082: "Server throws InvalidTickerFault_Exception",
083: tickerSymbol, 123);
084: } else if (tickerSymbol.equals("DF1")) {
085: DerivedFault1 df = new DerivedFault1();
086: df.setA(100);
087: df.setB(tickerSymbol);
088: throw new DerivedFault1_Exception(
089: "Server throws DerivedFault1_Exception", df);
090: } else if (tickerSymbol.equals("DF2")) {
091: DerivedFault2 df = new DerivedFault2();
092: df.setA(200);
093: df.setB(tickerSymbol);
094: df.setC(80.0F);
095: throw new DerivedFault2_Exception(
096: "Server throws DerivedFault2_Exception", df);
097: } else if (tickerSymbol.equals("BASE")) {
098: BaseFault bf = new BaseFault();
099: bf.setA(400);
100: throw new BaseFault_Exception(
101: "Server throws BaseFault_Exception", bf);
102: } else if (tickerSymbol.equals("INJECTION")) {
103: if (ctx != null && init) {
104: // Only return this value if the context is injected and
105: // the initialization method is invoked
106: return 1234567;
107: }
108: }
109: return 100;
110: }
111:
112: /**
113: * Throws wrapper exceptions for derived fault beans
114: */
115: public int throwFault(int a, String b, float c)
116: throws BaseFault_Exception, ComplexFault_Exception {
117: if (b.equals("Complex")) {
118: ComplexFault cf = new ComplexFault();
119: cf.setA(a);
120: cf.setB(b);
121: cf.setC(c);
122: cf.setD(5);
123: throw new ComplexFault_Exception(
124: "Server throws ComplexFault_Exception", cf);
125: } else if (b.equals("BaseFault")) {
126: BaseFault bf = new BaseFault();
127: bf.setA(a);
128: throw new BaseFault_Exception(
129: "Server throws BaseFault_Exception", bf);
130: } else if (b.equals("DerivedFault1")) {
131: DerivedFault1 df = new DerivedFault1();
132: df.setA(a);
133: df.setB(b);
134: throw new BaseFault_Exception(
135: "Server throws BaseFault_Exception", df);
136: } else if (b.equals("DerivedFault2")) {
137: DerivedFault2 df = new DerivedFault2();
138: df.setA(a);
139: df.setB(b);
140: df.setC(c);
141: throw new BaseFault_Exception(
142: "Server throws BaseFault_Exception", df);
143: } else if (b.equals("SOAPFaultException")) {
144: try {
145: SOAPFault soapFault = createSOAPFault();
146: soapFault.setFaultString("hello world");
147: soapFault.setFaultActor("actor");
148: throw new SOAPFaultException(soapFault);
149: } catch (SOAPException se) {
150: }
151: } else if (b.equals("SOAPFaultException2")) {
152: try {
153: SOAPFault soapFault = createSOAPFault();
154: soapFault.setFaultString("hello world2");
155: soapFault.setFaultActor("actor2");
156: Detail detail = soapFault.addDetail();
157: DetailEntry de = detail.addDetailEntry(new QName(
158: "urn://sample", "detailEntry"));
159: de.setValue("Texas");
160: throw new SOAPFaultException(soapFault);
161: } catch (SOAPException se) {
162: }
163: } else if (b.equals("NPE")) {
164: throw new NullPointerException();
165: } else if (b.equals("NPE2")) {
166: // Throw NPE with a message
167: throw new NullPointerException(
168: "Null Pointer Exception occurred");
169: } else if (b.equals("WSE")) {
170: WebServiceException wsf = new WebServiceException(
171: "This is a WebServiceException");
172: throw wsf;
173: }
174: return 0;
175: }
176:
177: /**
178: * Returns a fault bean or throws a wrapper exception
179: */
180: public void returnFault(int a, String b, float c,
181: Holder<DerivedFault1> fault) throws EqualFault {
182:
183: DerivedFault2 newFault = new DerivedFault2();
184: newFault.setA(fault.value.getA());
185: newFault.setB(fault.value.getB());
186: newFault.setC(c);
187:
188: if (fault.value.getB().equals("fault")) {
189: fault.value = newFault;
190: return;
191: } else if (fault.value.getB().equals("exception")) {
192: throw new EqualFault("Server throws EqualFault", newFault);
193: }
194: DerivedFault1 df = new DerivedFault1();
195: df.setA(a + 1);
196: df.setB("Server: " + b);
197: throw new EqualFault("Server throws EqualFault", df);
198: }
199:
200: SOAPFault createSOAPFault() throws SOAPException {
201: SOAPFault soapFault = null;
202:
203: // REVIEW: The following does not work due to Axis2 SAAJ problems.
204: //
205: // SOAPFactory soapFactory = SOAPFactory.newInstance();
206: // SOAPFault soapFault = soapFactory.createFault();
207:
208: // Alternate Approach
209: org.apache.axiom.soap.SOAPFactory asf = DOOMAbstractFactory
210: .getSOAP11Factory();
211: org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl axiomEnv = (org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl) asf
212: .createSOAPEnvelope();
213: javax.xml.soap.SOAPEnvelope env = new SOAPEnvelopeImpl(axiomEnv);
214: SOAPBody body = env.addBody();
215: soapFault = body.addFault();
216: return soapFault;
217: }
218:
219: @PostConstruct
220: public void initialize() {
221: //Called after resource injection and before a method is called.
222: TestLogger.logger.debug("Calling PostConstruct to Initialize");
223: this .init = true;
224: }
225:
226: @PreDestroy
227: public void distructor() {
228: //Called before the scope of request or session or application ends.
229:
230: TestLogger.logger.debug("Calling PreDestroy ");
231:
232: }
233:
234: @Resource
235: private void setCtx(WebServiceContext ctx) {
236: // The setter is private. This should not matter because the engine
237: // should still make it accessible.
238: this.ctx = ctx;
239: }
240: }
|