Search in sources :

Example 16 with FaultInfo

use of org.apache.cxf.service.model.FaultInfo in project cxf by apache.

the class ClientFaultConverter method processFaultDetail.

protected void processFaultDetail(Fault fault, Message msg) {
    Element exDetail = (Element) DOMUtils.getChild(fault.getDetail(), Node.ELEMENT_NODE);
    if (exDetail == null) {
        return;
    }
    QName qname = new QName(exDetail.getNamespaceURI(), exDetail.getLocalName());
    FaultInfo faultWanted = null;
    MessagePartInfo part = null;
    BindingOperationInfo boi = msg.getExchange().getBindingOperationInfo();
    if (boi == null) {
        return;
    }
    if (boi.isUnwrapped()) {
        boi = boi.getWrappedOperation();
    }
    for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
        for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
            if (qname.equals(mpi.getConcreteName())) {
                faultWanted = faultInfo;
                part = mpi;
                break;
            }
        }
        if (faultWanted != null) {
            break;
        }
    }
    if (faultWanted == null) {
        // did not find it using the proper qualified names, we'll try again with just the localpart
        for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
            for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
                if (qname.getLocalPart().equals(mpi.getConcreteName().getLocalPart())) {
                    faultWanted = faultInfo;
                    part = mpi;
                    break;
                }
            }
            if (faultWanted != null) {
                break;
            }
        }
    }
    if (faultWanted == null) {
        return;
    }
    Service s = msg.getExchange().getService();
    DataBinding dataBinding = s.getDataBinding();
    Object e = null;
    if (isDOMSupported(dataBinding)) {
        DataReader<Node> reader = this.getNodeDataReader(msg);
        reader.setProperty(DataReader.FAULT, fault);
        e = reader.read(part, exDetail);
    } else {
        DataReader<XMLStreamReader> reader = this.getDataReader(msg);
        XMLStreamReader xsr = new W3CDOMStreamReader(exDetail);
        try {
            xsr.nextTag();
        } catch (XMLStreamException e1) {
            throw new Fault(e1);
        }
        reader.setProperty(DataReader.FAULT, fault);
        e = reader.read(part, xsr);
    }
    if (!(e instanceof Exception)) {
        try {
            Class<?> exClass = faultWanted.getProperty(Class.class.getName(), Class.class);
            if (exClass == null) {
                return;
            }
            if (e == null) {
                Constructor<?> constructor = exClass.getConstructor(String.class);
                e = constructor.newInstance(fault.getMessage());
            } else {
                try {
                    Constructor<?> constructor = getConstructor(exClass, e);
                    e = constructor.newInstance(fault.getMessage(), e);
                } catch (NoSuchMethodException e1) {
                    // Use reflection to convert fault bean to exception
                    e = convertFaultBean(exClass, e, fault);
                }
            }
            msg.setContent(Exception.class, e);
        } catch (Exception e1) {
            LogUtils.log(LOG, Level.INFO, "EXCEPTION_WHILE_CREATING_EXCEPTION", e1, e1.getMessage());
        }
    } else {
        if (fault.getMessage() != null) {
            Field f;
            try {
                f = Throwable.class.getDeclaredField("detailMessage");
                ReflectionUtil.setAccessible(f);
                f.set(e, fault.getMessage());
            } catch (Exception e1) {
            // ignore
            }
        }
        msg.setContent(Exception.class, e);
    }
}
Also used : FaultInfo(org.apache.cxf.service.model.FaultInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Service(org.apache.cxf.service.Service) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) XMLStreamException(javax.xml.stream.XMLStreamException) Field(java.lang.reflect.Field) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) DataBinding(org.apache.cxf.databinding.DataBinding)

Example 17 with FaultInfo

use of org.apache.cxf.service.model.FaultInfo in project cxf by apache.

the class FaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Fault f = (Fault) message.getContent(Exception.class);
    if (f == null) {
        return;
    }
    Throwable cause = f.getCause();
    if (cause == null) {
        return;
    }
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop == null) {
        return;
    }
    FaultInfo fi = getFaultForClass(bop, cause.getClass());
    if (cause instanceof Exception && fi != null) {
        Exception ex = (Exception) cause;
        Object bean = getFaultBean(cause, fi, message);
        Service service = message.getExchange().getService();
        MessagePartInfo part = fi.getFirstMessagePart();
        DataBinding db = service.getDataBinding();
        try {
            if (isDOMSupported(db)) {
                DataWriter<Node> writer = db.createWriter(Node.class);
                if (f.hasDetails()) {
                    writer.write(bean, part, f.getDetail());
                } else {
                    writer.write(bean, part, f.getOrCreateDetail());
                    if (!f.getDetail().hasChildNodes()) {
                        f.setDetail(null);
                    }
                }
            } else {
                if (f.hasDetails()) {
                    XMLStreamWriter xsw = new W3CDOMStreamWriter(f.getDetail());
                    DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
                    writer.write(bean, part, xsw);
                } else {
                    XMLStreamWriter xsw = new W3CDOMStreamWriter(f.getOrCreateDetail());
                    DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
                    writer.write(bean, part, xsw);
                    if (!f.getDetail().hasChildNodes()) {
                        f.setDetail(null);
                    }
                }
            }
            f.setMessage(ex.getMessage());
        } catch (Exception fex) {
            // ignore - if any exceptions occur here, we'll ignore them
            // and let the default fault handling of the binding convert
            // the fault like it was an unchecked exception.
            LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", fex);
        }
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo) FaultInfo(org.apache.cxf.service.model.FaultInfo) Node(org.w3c.dom.Node) Service(org.apache.cxf.service.Service) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DataBinding(org.apache.cxf.databinding.DataBinding)

Example 18 with FaultInfo

use of org.apache.cxf.service.model.FaultInfo in project cxf by apache.

the class JaxWsServiceFactoryBean method buildWSAActions.

private void buildWSAActions(OperationInfo operation, Method method) {
    // nothing
    if (method == null) {
        return;
    }
    Action action = method.getAnnotation(Action.class);
    Addressing addressing = method.getDeclaringClass().getAnnotation(Addressing.class);
    if (action == null && addressing == null) {
        return;
    }
    WebMethod wm = method.getAnnotation(WebMethod.class);
    String inputAction = "";
    if (action != null) {
        inputAction = action.input();
    }
    if (wm != null && StringUtils.isEmpty(inputAction)) {
        inputAction = wm.action();
    }
    if (StringUtils.isEmpty(inputAction)) {
        inputAction = computeAction(operation, "Request");
    }
    if (action == null && addressing != null) {
        operation.getInput().addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, inputAction);
        operation.getInput().addExtensionAttribute(JAXWSAConstants.WSAW_ACTION_QNAME, inputAction);
        if (operation.getOutput() != null) {
            operation.getOutput().addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, computeAction(operation, "Response"));
            operation.getOutput().addExtensionAttribute(JAXWSAConstants.WSAW_ACTION_QNAME, computeAction(operation, "Response"));
        }
    } else {
        MessageInfo input = operation.getInput();
        input.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, inputAction);
        if (!StringUtils.isEmpty(action.input())) {
            input.addExtensionAttribute(JAXWSAConstants.WSAW_ACTION_QNAME, inputAction);
        }
        MessageInfo output = operation.getOutput();
        if (output != null && !StringUtils.isEmpty(action.output())) {
            output.addExtensionAttribute(JAXWSAConstants.WSAW_ACTION_QNAME, action.output());
            output.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, action.output());
        } else if (output != null) {
            output.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, computeAction(operation, "Response"));
        }
        FaultAction[] faultActions = action.fault();
        if (faultActions != null && faultActions.length > 0 && operation.getFaults() != null) {
            for (FaultAction faultAction : faultActions) {
                FaultInfo faultInfo = getFaultInfo(operation, faultAction.className());
                if (faultInfo != null && !StringUtils.isEmpty(faultAction.value())) {
                    faultInfo.addExtensionAttribute(JAXWSAConstants.WSAW_ACTION_QNAME, faultAction.value());
                    faultInfo.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, faultAction.value());
                }
                if (operation.isUnwrappedCapable()) {
                    faultInfo = getFaultInfo(operation.getUnwrappedOperation(), faultAction.className());
                    if (faultInfo != null && !StringUtils.isEmpty(faultAction.value())) {
                        faultInfo.addExtensionAttribute(JAXWSAConstants.WSAW_ACTION_QNAME, faultAction.value());
                        faultInfo.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, faultAction.value());
                    }
                }
            }
        }
    }
    for (FaultInfo fi : operation.getFaults()) {
        if (fi.getExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME) == null) {
            String f = "/Fault/" + fi.getName().getLocalPart();
            fi.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, computeAction(operation, f));
            if (operation.isUnwrappedCapable()) {
                fi = operation.getUnwrappedOperation().getFault(fi.getName());
                fi.addExtensionAttribute(JAXWSAConstants.WSAM_ACTION_QNAME, computeAction(operation, f));
            }
        }
    }
}
Also used : WebMethod(javax.jws.WebMethod) FaultAction(javax.xml.ws.FaultAction) Action(javax.xml.ws.Action) FaultAction(javax.xml.ws.FaultAction) FaultInfo(org.apache.cxf.service.model.FaultInfo) Addressing(javax.xml.ws.soap.Addressing) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Example 19 with FaultInfo

use of org.apache.cxf.service.model.FaultInfo in project cxf by apache.

the class JaxWsServiceFactoryBeanTest method testEndpoint.

@Test
public void testEndpoint() throws Exception {
    ReflectionServiceFactoryBean bean = new JaxWsServiceFactoryBean();
    URL resource = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(resource);
    bean.setWsdlURL(resource.toString());
    Bus bus = getBus();
    bean.setBus(bus);
    bean.setServiceClass(GreeterImpl.class);
    BeanInvoker invoker = new BeanInvoker(new GreeterImpl());
    bean.setInvoker(invoker);
    Service service = bean.create();
    String ns = "http://apache.org/hello_world_soap_http";
    assertEquals("SOAPService", service.getName().getLocalPart());
    assertEquals(ns, service.getName().getNamespaceURI());
    InterfaceInfo intf = service.getServiceInfos().get(0).getInterface();
    OperationInfo op = intf.getOperation(new QName(ns, "sayHi"));
    Class<?> wrapper = op.getInput().getMessageParts().get(0).getTypeClass();
    assertNotNull(wrapper);
    wrapper = op.getOutput().getMessageParts().get(0).getTypeClass();
    assertNotNull(wrapper);
    assertEquals(invoker, service.getInvoker());
    op = intf.getOperation(new QName(ns, "testDocLitFault"));
    Collection<FaultInfo> faults = op.getFaults();
    assertEquals(2, faults.size());
    FaultInfo f = op.getFault(new QName(ns, "BadRecordLitFault"));
    assertNotNull(f);
    Class<?> c = f.getProperty(Class.class.getName(), Class.class);
    assertNotNull(c);
    assertEquals(1, f.getMessageParts().size());
    MessagePartInfo mpi = f.getMessagePartByIndex(0);
    assertNotNull(mpi.getTypeClass());
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) Bus(org.apache.cxf.Bus) FaultInfo(org.apache.cxf.service.model.FaultInfo) QName(javax.xml.namespace.QName) BeanInvoker(org.apache.cxf.service.invoker.BeanInvoker) Service(org.apache.cxf.service.Service) WebService(javax.jws.WebService) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) URL(java.net.URL) GreeterImpl(org.apache.hello_world_soap_http.GreeterImpl) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) ReflectionServiceFactoryBean(org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean) Test(org.junit.Test) AbstractJaxWsTest(org.apache.cxf.jaxws.AbstractJaxWsTest)

Example 20 with FaultInfo

use of org.apache.cxf.service.model.FaultInfo in project cxf by apache.

the class PolicyInterceptorsTest method testServerPolicyOutFaultInterceptorGetBindingFaultInfo.

@Test
public void testServerPolicyOutFaultInterceptorGetBindingFaultInfo() {
    ServerPolicyOutFaultInterceptor interceptor = new ServerPolicyOutFaultInterceptor();
    message = control.createMock(Message.class);
    Exception ex = new UnsupportedOperationException(new RuntimeException());
    boi = control.createMock(BindingOperationInfo.class);
    EasyMock.expect(message.get(BindingFaultInfo.class)).andReturn(null);
    BindingFaultInfo bfi = control.createMock(BindingFaultInfo.class);
    Collection<BindingFaultInfo> bfis = CastUtils.cast(Collections.EMPTY_LIST);
    EasyMock.expect(boi.getFaults()).andReturn(bfis);
    BindingOperationInfo wrappedBoi = control.createMock(BindingOperationInfo.class);
    EasyMock.expect(boi.getWrappedOperation()).andReturn(wrappedBoi).times(2);
    Collection<BindingFaultInfo> wrappedBfis = CastUtils.cast(Collections.singletonList(bfi));
    EasyMock.expect(wrappedBoi.getFaults()).andReturn(wrappedBfis);
    FaultInfo fi = control.createMock(FaultInfo.class);
    EasyMock.expect(bfi.getFaultInfo()).andReturn(fi);
    EasyMock.expect(fi.getProperty(Class.class.getName(), Class.class)).andReturn(RuntimeException.class);
    message.put(BindingFaultInfo.class, bfi);
    EasyMock.expectLastCall();
    control.replay();
    assertSame(bfi, interceptor.getBindingFaultInfo(message, ex, boi));
    control.verify();
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo) FaultInfo(org.apache.cxf.service.model.FaultInfo) Message(org.apache.cxf.message.Message) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo) Test(org.junit.Test)

Aggregations

FaultInfo (org.apache.cxf.service.model.FaultInfo)27 QName (javax.xml.namespace.QName)12 BindingFaultInfo (org.apache.cxf.service.model.BindingFaultInfo)11 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)11 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)11 OperationInfo (org.apache.cxf.service.model.OperationInfo)10 MessageInfo (org.apache.cxf.service.model.MessageInfo)8 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Service (org.apache.cxf.service.Service)3 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)3 UnwrappedOperationInfo (org.apache.cxf.service.model.UnwrappedOperationInfo)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 BindingFault (javax.wsdl.BindingFault)2 Fault (javax.wsdl.Fault)2 Input (javax.wsdl.Input)2 Output (javax.wsdl.Output)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2