Package org.apache.cxf.binding.xml.interceptor

Source Code of org.apache.cxf.binding.xml.interceptor.XMLFaultInterceptorsTest

/**
* 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.binding.xml.interceptor;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.cxf.binding.xml.XMLFault;
import org.apache.cxf.interceptor.AbstractInDatabindingInterceptor;
import org.apache.cxf.interceptor.ClientFaultConverter;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.staxutils.StaxUtils;
import org.apache.hello_world_xml_http.bare.types.MyComplexStructType;

public class XMLFaultInterceptorsTest extends TestBase {

    public void testRuntimeExceptionOfImpl() throws Exception {

        String ns = "http://apache.org/hello_world_xml_http/wrapped";
        common("/wsdl/hello_world_xml_wrapped.wsdl", new QName(ns, "XMLPort"), MyComplexStructType.class);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        xmlMessage.setContent(OutputStream.class, baos);
        xmlMessage.setContent(XMLStreamWriter.class, StaxUtils.createXMLStreamWriter(baos));
        xmlMessage.setContent(Exception.class, new Fault(new RuntimeException("dummy exception")));
        XMLFaultOutInterceptor xfo = new XMLFaultOutInterceptor();
        xfo.setPhase("phase1");
        chain.add(xfo);
        InHelpInterceptor ih = new InHelpInterceptor();
        ClientFaultConverter cfc = new ClientFaultConverter();
        XMLFaultInInterceptor xfi = new XMLFaultInInterceptor();
        ih.setPhase("phase2");
        cfc.setPhase("phase3");
        xfi.setPhase("phase3");
        chain.add(ih);
        chain.add(cfc);
        chain.add(xfi);
        chain.doIntercept(xmlMessage);
        assertNotNull(xmlMessage.getContent(Exception.class));
        assertTrue(xmlMessage.getContent(Exception.class) instanceof XMLFault);
        XMLFault xfault = (XMLFault) xmlMessage.getContent(Exception.class);
        assertTrue("check message expected - dummy exception",
                xfault.getMessage().indexOf("dummy exception") >= 0);
    }

    private class InHelpInterceptor extends AbstractInDatabindingInterceptor {

        public void handleMessage(Message message) {

            try {
                ByteArrayOutputStream baos = (ByteArrayOutputStream) message.getContent(OutputStream.class);
                ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
                message.setContent(InputStream.class, bis);
                XMLStreamReader xsr = StaxUtils.createXMLStreamReader(bis);
                xsr.nextTag();
                message.setContent(XMLStreamReader.class, xsr);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

    }
}
TOP

Related Classes of org.apache.cxf.binding.xml.interceptor.XMLFaultInterceptorsTest

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.