Package org.apache.cxf.systest.interceptor

Source Code of org.apache.cxf.systest.interceptor.InterceptorFaultTest$Server

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.cxf.systest.interceptor;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import javax.xml.namespace.QName;
import javax.xml.ws.Endpoint;
import javax.xml.ws.WebServiceException;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.binding.soap.SoapFault;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.greeter_control.Control;
import org.apache.cxf.greeter_control.ControlImpl;
import org.apache.cxf.greeter_control.ControlService;
import org.apache.cxf.greeter_control.FaultThrowingInterceptor;
import org.apache.cxf.greeter_control.Greeter;
import org.apache.cxf.greeter_control.GreeterService;
import org.apache.cxf.greeter_control.PingMeFault;
import org.apache.cxf.greeter_control.types.FaultLocation;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.phase.PhaseComparator;
import org.apache.cxf.phase.PhaseManager;
import org.apache.cxf.test.TestUtilities;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.cxf.ws.addressing.MAPAggregator;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
*/
public class InterceptorFaultTest extends AbstractBusClientServerTestBase {
    public static final String PORT = allocatePort(Server.class);
   
    private static final Logger LOG = LogUtils.getLogger(InterceptorFaultTest.class);

    private static final QName SOAP_FAULT_CODE = new QName("http://schemas.xmlsoap.org/soap/envelope/",
                                                           "Server");
    private static final String FAULT_MESSAGE = "Could not send Message.";
   
    private static final String CONTROL_PORT_ADDRESS =
        "http://localhost:" + PORT + "/SoapContext/ControlPort";
   
    private static int decoupledEndpointPort = 1;
    private static String decoupledEndpoint;
   

    /**
     * Tests that a fault thrown by a server side interceptor is reported back to
     * the client in appropriate form (plain Fault in case of one way requests,
     * SoapFault in case of two way requests).
     * Also demonstrates how an interceptor on the server out fault chain can
     * distinguish different fault modes (the capability to do so is crucial to
     * QOS interceptors such as the RM, addressing and policy interceptors).
     *
     */
    public static class Server extends AbstractBusTestServerBase {

        protected void run() {
            SpringBusFactory factory = new SpringBusFactory();
            Bus bus = factory.createBus();
            BusFactory.setDefaultBus(bus);
            setBus(bus);

            ControlImpl implementor = new ControlImpl();
            implementor.setAddress("http://localhost:" + PORT + "/SoapContext/GreeterPort");
            GreeterImpl greeterImplementor = new GreeterImpl();
            greeterImplementor.setThrowAlways(true);
            greeterImplementor.useLastOnewayArg(true);
            implementor.setImplementor(greeterImplementor);
            Endpoint.publish(CONTROL_PORT_ADDRESS, implementor);
            LOG.fine("Published control endpoint.");
        }

        public static void main(String[] args) {
            try {
                Server s = new Server();
                s.start();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.exit(-1);
            } finally {
                System.out.println("done!");
            }
        }
    }
   
    private Bus controlBus;
    private Control control;
    private Bus greeterBus;
    private Greeter greeter;
    private List<Phase> inPhases;
    private PhaseComparator comparator;
    private Phase preLogicalPhase;
   
   

    @BeforeClass
    public static void startServers() throws Exception {
        TestUtilities.setKeepAliveSystemProperty(false);
        assertTrue("server did not launch correctly", launchServer(Server.class, true));
    }
   
    @AfterClass
    public static void cleanup() {
        TestUtilities.recoverKeepAliveSystemProperty();
    }
   
    @After
    public void tearDown() {
        if (null != greeter) {
            assertTrue("Failed to stop greeter.", control.stopGreeter(null));
            greeterBus.shutdown(true);
            greeterBus = null;
        }
        if (null != control) { 
            assertTrue("Failed to stop greeter", control.stopGreeter(null));
            controlBus.shutdown(true);
        }
    }

    @Test
    public void testWithoutAddressing() throws Exception {
        testWithoutAddressing(false);
    }

    @Test
    public void testRobustWithoutAddressing() throws Exception {
        testWithoutAddressing(true);
    }

    private void testWithoutAddressing(boolean robust) throws Exception {
       
        setupGreeter("org/apache/cxf/systest/interceptor/no-addr.xml", false);

        control.setRobustInOnlyMode(robust);

        // all interceptors pass
        testInterceptorsPass(robust);

        // behaviour is identicial for all phases
        FaultLocation location = new org.apache.cxf.greeter_control.types.ObjectFactory()
            .createFaultLocation();       
       
        // test failure occuring before and after logical addressing interceptor
        // won't get a fault in case of oneways non-robust for the latter (partial response already sent)
        testInterceptorFail(inPhases, location, robust);
    }
   
    @Test
    public void testWithAddressingAnonymousReplies() throws Exception {
        testWithAddressingAnonymousReplies(false);
    }

    @Test
    public void testRobustWithAddressingAnonymousReplies() throws Exception {
        testWithAddressingAnonymousReplies(true);
    }

    private void testWithAddressingAnonymousReplies(boolean robust) throws Exception {
        setupGreeter("org/apache/cxf/systest/interceptor/addr.xml", false);
       
        control.setRobustInOnlyMode(robust);
       
        // all interceptors pass
        testInterceptorsPass(robust);
       
        // test failure in phases before Phase.PRE_LOGICAL
        FaultLocation location = new org.apache.cxf.greeter_control.types.ObjectFactory()
            .createFaultLocation();
        location.setAfter(MAPAggregator.class.getName());
       
        // test failure occuring before and after logical addressing interceptor
        // won't get a fault in case of oneways non-robust for the latter (partial response already sent)
        testInterceptorFail(inPhases, location, robust);
    }
   
  
    private void testInterceptorFail(List<Phase> phases, FaultLocation location, boolean robust)
        throws PingMeFault {
        for (Phase p : phases) {
            location.setPhase(p.getName());
            if (Phase.PRE_LOGICAL.equals(p.getName())) {
                continue;
            } else if (Phase.POST_INVOKE.equals(p.getName())) {
                break;
            }  
            testFail(location, true, robust);
        }
    }

    private void testInterceptorsPass(boolean robust) {
        greeter.greetMeOneWay("one");
       
        // wait 5 seconds for the non-robust case
        if (!robust) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // ignore
            }
        }
        // verify both the previous greetMeOneWay call and this greetMe call
        assertEquals("one", greeter.greetMe("two"));
        try {
            greeter.pingMe();
            fail("Expected PingMeFault not thrown.");
        } catch (PingMeFault f) {
            assertEquals(20, f.getFaultInfo().getMajor());
            assertEquals(10, f.getFaultInfo().getMinor());
        }
    }

    private void testFail(FaultLocation location, boolean usingAddressing, boolean robust)
        throws PingMeFault {
        // System.out.print("Test interceptor failing in phase: " + location.getPhase());
       
        control.setFaultLocation(location);      

        // oneway reports a plain fault (although server sends a soap fault)

        boolean expectOnewayFault = robust
            || comparator.compare(preLogicalPhase, getPhase(location.getPhase())) > 0;
       
        try {
            greeter.greetMeOneWay("oneway");
            if (expectOnewayFault) {
                fail("Oneway operation unexpectedly succeded for phase " + location.getPhase());
            }
        } catch (WebServiceException ex) {
            if (!expectOnewayFault) {
                fail("Oneway operation unexpectedly failed.");
            }
            assertEquals(FAULT_MESSAGE, ex.getMessage());
        }
       
        String expectedMsg = getExpectedInterceptorFaultMessage(location.getPhase());
        try {
            greeter.greetMe("cxf");
            fail("Twoway operation unexpectedly succeded.");
        } catch (WebServiceException ex) {
            Throwable cause = ex.getCause();
            SoapFault sf = (SoapFault)cause;
           
            assertEquals(expectedMsg, sf.getReason());
            assertEquals(SOAP_FAULT_CODE, sf.getFaultCode());
        }
       
        try {
            greeter.pingMe();
            fail("Expected PingMeFault not thrown.");
        } catch (WebServiceException ex) {
            Throwable cause = ex.getCause();
            SoapFault sf = (SoapFault)cause;
            assertEquals(expectedMsg, sf.getReason());
            assertEquals(SOAP_FAULT_CODE, sf.getFaultCode());
       
    }
  
   
   
    private void setupGreeter(String cfgResource, boolean useDecoupledEndpoint)
        throws NumberFormatException, MalformedURLException {
       
        SpringBusFactory bf = new SpringBusFactory();
       
        controlBus = bf.createBus();
        BusFactory.setDefaultBus(controlBus);

        ControlService cs = new ControlService();
        control = cs.getControlPort();
        updateAddressPort(control, PORT);
       
        assertTrue("Failed to start greeter", control.startGreeter(cfgResource));
       
        greeterBus = bf.createBus(cfgResource);
        BusFactory.setDefaultBus(greeterBus);
        LOG.fine("Initialised greeter bus with configuration: " + cfgResource);
       
        if (null == comparator) {
            comparator = new PhaseComparator();
        }
        if (null == inPhases) {
            inPhases = new ArrayList<Phase>();
            inPhases.addAll(greeterBus.getExtension(PhaseManager.class).getInPhases());
            Collections.sort(inPhases, comparator);
        }       
        if (null == preLogicalPhase) {
            preLogicalPhase = getPhase(Phase.PRE_LOGICAL);
        }
      
        GreeterService gs = new GreeterService();

        greeter = gs.getGreeterPort();
        updateAddressPort(greeter, PORT);
        LOG.fine("Created greeter client.");
      
        if (!useDecoupledEndpoint) {
            return;
        }

        // programatically configure decoupled endpoint that is guaranteed to
        // be unique across all test cases
        decoupledEndpointPort++;
        decoupledEndpoint = "http://localhost:"
            + allocatePort("decoupled-" + decoupledEndpointPort)
            + "/decoupled_endpoint";

        Client c = ClientProxy.getClient(greeter);
        HTTPConduit hc = (HTTPConduit)(c.getConduit());
        HTTPClientPolicy cp = hc.getClient();
        cp.setDecoupledEndpoint(decoupledEndpoint);

        LOG.fine("Using decoupled endpoint: " + cp.getDecoupledEndpoint());
    }

    private String getExpectedInterceptorFaultMessage(String phase) {
        return FaultThrowingInterceptor.MESSAGE_FORMAT.format(new Object[] {phase}).toUpperCase();
    }
   
    private Phase getPhase(String name) {
        for (Phase p : inPhases) {
            if (p.getName().equals(name)) {
                return p;
            }
        }
        return null;
    }
}
TOP

Related Classes of org.apache.cxf.systest.interceptor.InterceptorFaultTest$Server

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.