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: */package org.apache.cxf.systest.interceptor;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.logging.Logger;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.ws.Endpoint;
028: import javax.xml.ws.WebServiceException;
029:
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.BusFactory;
032: import org.apache.cxf.binding.soap.SoapFault;
033: import org.apache.cxf.bus.spring.SpringBusFactory;
034: import org.apache.cxf.endpoint.Client;
035: import org.apache.cxf.frontend.ClientProxy;
036: import org.apache.cxf.greeter_control.Control;
037: import org.apache.cxf.greeter_control.ControlImpl;
038: import org.apache.cxf.greeter_control.ControlService;
039: import org.apache.cxf.greeter_control.FaultThrowingInterceptor;
040: import org.apache.cxf.greeter_control.Greeter;
041: import org.apache.cxf.greeter_control.GreeterService;
042: import org.apache.cxf.greeter_control.PingMeFault;
043: import org.apache.cxf.greeter_control.types.FaultLocation;
044: import org.apache.cxf.interceptor.Fault;
045: import org.apache.cxf.phase.Phase;
046: import org.apache.cxf.phase.PhaseComparator;
047: import org.apache.cxf.phase.PhaseManager;
048: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
049: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
050: import org.apache.cxf.transport.http.HTTPConduit;
051: import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
052: import org.apache.cxf.ws.addressing.MAPAggregator;
053: import org.junit.After;
054: import org.junit.BeforeClass;
055: import org.junit.Test;
056:
057: /**
058: *
059: */
060: public class InterceptorFaultTest extends
061: AbstractBusClientServerTestBase {
062: private static final Logger LOG = Logger
063: .getLogger(InterceptorFaultTest.class.getName());
064:
065: private static final QName SOAP_FAULT_CODE = new QName(
066: "http://schemas.xmlsoap.org/soap/envelope/", "Server");
067: private static final String FAULT_CODE = "COULD_NOT_SEND";
068: private static final String FAULT_MESSAGE = "Could not send Message.";
069:
070: private static final String CONTROL_PORT_ADDRESS = "http://localhost:9001/SoapContext/ControlPort";
071:
072: private static int decoupledEndpointPort = 10000;
073: private static String decoupledEndpoint;
074:
075: /**
076: * Tests that a fault thrown by a server side interceptor is reported back to
077: * the client in appropriate form (plain Fault in case of one way requests,
078: * SoapFault in case of two way requests).
079: * Also demonstrates how an interceptor on the server out fault chain can
080: * distinguish different fault modes (the capability to do so is crucial to
081: * QOS interceptors such as the RM, addressing and policy interceptors).
082: *
083: */
084: public static class Server extends AbstractBusTestServerBase {
085:
086: protected void run() {
087: SpringBusFactory factory = new SpringBusFactory();
088: Bus bus = factory.createBus();
089: BusFactory.setDefaultBus(bus);
090: setBus(bus);
091:
092: ControlImpl implementor = new ControlImpl();
093: GreeterImpl greeterImplementor = new GreeterImpl();
094: greeterImplementor.setThrowAlways(true);
095: implementor.setImplementor(greeterImplementor);
096: Endpoint.publish(CONTROL_PORT_ADDRESS, implementor);
097: LOG.fine("Published control endpoint.");
098: }
099:
100: public static void main(String[] args) {
101: try {
102: Server s = new Server();
103: s.start();
104: } catch (Exception ex) {
105: ex.printStackTrace();
106: System.exit(-1);
107: } finally {
108: System.out.println("done!");
109: }
110: }
111: }
112:
113: private Bus controlBus;
114: private Control control;
115: private Bus greeterBus;
116: private Greeter greeter;
117: private List<Phase> inPhases;
118: private PhaseComparator comparator;
119: private Phase preLogicalPhase;
120:
121: @BeforeClass
122: public static void startServers() throws Exception {
123: assertTrue("server did not launch correctly", launchServer(
124: Server.class, true));
125: }
126:
127: @After
128: public void tearDown() {
129: if (null != greeter) {
130: assertTrue("Failed to stop greeter.", control
131: .stopGreeter(null));
132: greeterBus.shutdown(true);
133: greeterBus = null;
134: }
135: if (null != control) {
136: assertTrue("Failed to stop greeter", control
137: .stopGreeter(null));
138: controlBus.shutdown(true);
139: }
140: }
141:
142: @Test
143: public void testWithoutAddressing() throws Exception {
144:
145: setupGreeter("org/apache/cxf/systest/interceptor/no-addr.xml",
146: false);
147:
148: // all interceptors pass
149:
150: /*
151: greeter.greetMeOneWay("one");
152: assertEquals("TWO", greeter.greetMe("two"));
153: try {
154: greeter.pingMe();
155: fail("Expected PingMeFault not thrown.");
156: } catch (PingMeFault f) {
157: assertEquals(20, (int)f.getFaultInfo().getMajor());
158: assertEquals(10, (int)f.getFaultInfo().getMinor());
159: }
160: */
161:
162: // behaviour is identicial for all phases
163: Iterator<Phase> it = inPhases.iterator();
164: Phase p = null;
165: FaultLocation location = new org.apache.cxf.greeter_control.types.ObjectFactory()
166: .createFaultLocation();
167:
168: while (it.hasNext()) {
169: p = it.next();
170: location.setPhase(p.getName());
171: if (Phase.INVOKE.equals(p.getName())) {
172: break;
173: }
174: testFail(location);
175: }
176: }
177:
178: @Test
179: public void testWithAddressingAnonymousReplies() throws Exception {
180: setupGreeter("org/apache/cxf/systest/interceptor/addr.xml",
181: false);
182:
183: // all interceptors pass
184:
185: greeter.greetMeOneWay("one");
186: assertEquals("TWO", greeter.greetMe("two"));
187: try {
188: greeter.pingMe();
189: fail("Expected PingMeFault not thrown.");
190: } catch (PingMeFault f) {
191: assertEquals(20, (int) f.getFaultInfo().getMajor());
192: assertEquals(10, (int) f.getFaultInfo().getMinor());
193: }
194:
195: // test failure in phases before Phase.PRE_LOGICAL
196:
197: Iterator<Phase> it = inPhases.iterator();
198: Phase p = null;
199: FaultLocation location = new org.apache.cxf.greeter_control.types.ObjectFactory()
200: .createFaultLocation();
201: location.setAfter(MAPAggregator.class.getName());
202:
203: // test failure occuring before logical addressing interceptor
204:
205: while (it.hasNext()) {
206: p = it.next();
207: location.setPhase(p.getName());
208: if (Phase.PRE_LOGICAL.equals(p.getName())) {
209: break;
210: }
211: testFail(location, true);
212: }
213:
214: // test failure occuring after logical addressing interceptor -
215: // won't get a fault in case of oneways (partial response already sent)
216:
217: do {
218: location.setPhase(p.getName());
219: if (Phase.INVOKE.equals(p.getName())) {
220: break;
221: }
222: testFail(location, true);
223: p = it.hasNext() ? it.next() : null;
224: } while (null != p);
225: }
226:
227: private void testFail(FaultLocation location) throws PingMeFault {
228: testFail(location, false);
229: }
230:
231: private void testFail(FaultLocation location,
232: boolean usingAddressing) throws PingMeFault {
233: // System.out.print("Test interceptor failing in phase: " + location.getPhase());
234:
235: control.setFaultLocation(location);
236:
237: String expectedMsg = getExpectedInterceptorFaultMessage(location
238: .getPhase());
239:
240: // oneway reports a plain fault (although server sends a soap fault)
241:
242: boolean expectOnewayFault = !usingAddressing
243: || comparator.compare(preLogicalPhase,
244: getPhase(location.getPhase())) > 0;
245:
246: try {
247: greeter.greetMeOneWay("oneway");
248: if (expectOnewayFault) {
249: fail("Oneway operation unexpectedly succeded.");
250: }
251: } catch (WebServiceException ex) {
252: if (!expectOnewayFault) {
253: fail("Oeway operation unexpectedly failed.");
254: }
255: Throwable cause = ex.getCause();
256: Fault f = (Fault) cause;
257: assertEquals(FAULT_CODE, f.getCode());
258: assertEquals(FAULT_MESSAGE, f.getMessage());
259: }
260:
261: try {
262: greeter.greetMe("cxf");
263: fail("Twoway operation unexpectedly succeded.");
264: } catch (WebServiceException ex) {
265: Throwable cause = ex.getCause();
266: SoapFault sf = (SoapFault) cause;
267: assertEquals(expectedMsg, sf.getReason());
268: assertEquals(SOAP_FAULT_CODE, sf.getFaultCode());
269: }
270:
271: try {
272: greeter.pingMe();
273: fail("Expected PingMeFault not thrown.");
274: } catch (WebServiceException ex) {
275: Throwable cause = ex.getCause();
276: SoapFault sf = (SoapFault) cause;
277: assertEquals(expectedMsg, sf.getReason());
278: assertEquals(SOAP_FAULT_CODE, sf.getFaultCode());
279: }
280: }
281:
282: private void setupGreeter(String cfgResource,
283: boolean useDecoupledEndpoint) {
284:
285: SpringBusFactory bf = new SpringBusFactory();
286:
287: controlBus = bf.createBus();
288: BusFactory.setDefaultBus(controlBus);
289:
290: ControlService cs = new ControlService();
291: control = cs.getControlPort();
292:
293: assertTrue("Failed to start greeter", control
294: .startGreeter(cfgResource));
295:
296: greeterBus = bf.createBus(cfgResource);
297: BusFactory.setDefaultBus(greeterBus);
298: LOG.fine("Initialised greeter bus with configuration: "
299: + cfgResource);
300:
301: if (null == comparator) {
302: comparator = new PhaseComparator();
303: }
304: if (null == inPhases) {
305: inPhases = new ArrayList<Phase>();
306: inPhases.addAll(greeterBus.getExtension(PhaseManager.class)
307: .getInPhases());
308: Collections.sort(inPhases, comparator);
309: }
310: if (null == preLogicalPhase) {
311: preLogicalPhase = getPhase(Phase.PRE_LOGICAL);
312: }
313:
314: GreeterService gs = new GreeterService();
315:
316: greeter = gs.getGreeterPort();
317: LOG.fine("Created greeter client.");
318:
319: if (!useDecoupledEndpoint) {
320: return;
321: }
322:
323: // programatically configure decoupled endpoint that is guaranteed to
324: // be unique across all test cases
325:
326: decoupledEndpointPort--;
327: decoupledEndpoint = "http://localhost:" + decoupledEndpointPort
328: + "/decoupled_endpoint";
329:
330: Client c = ClientProxy.getClient(greeter);
331: HTTPConduit hc = (HTTPConduit) (c.getConduit());
332: HTTPClientPolicy cp = hc.getClient();
333: cp.setDecoupledEndpoint(decoupledEndpoint);
334:
335: LOG.fine("Using decoupled endpoint: "
336: + cp.getDecoupledEndpoint());
337: }
338:
339: private String getExpectedInterceptorFaultMessage(String phase) {
340: return FaultThrowingInterceptor.MESSAGE_FORMAT.format(
341: new Object[] { phase }).toUpperCase();
342: }
343:
344: private Phase getPhase(String name) {
345: for (Phase p : inPhases) {
346: if (p.getName().equals(name)) {
347: return p;
348: }
349: }
350: return null;
351: }
352: }
|