Search in sources :

Example 1 with WebFault

use of javax.xml.ws.WebFault in project camel by apache.

the class Soap11DataFormatAdapter method createFaultFromException.

/**
     * Creates a SOAP fault from the exception and populates the message as well
     * as the detail. The detail object is read from the method getFaultInfo of
     * the throwable if present
     * 
     * @param exception the cause exception
     * @return SOAP fault from given Throwable
     */
@SuppressWarnings("unchecked")
private JAXBElement<Fault> createFaultFromException(final Throwable exception) {
    WebFault webFault = exception.getClass().getAnnotation(WebFault.class);
    if (webFault == null || webFault.targetNamespace() == null) {
        throw new RuntimeException("The exception " + exception.getClass().getName() + " needs to have an WebFault annotation with name and targetNamespace", exception);
    }
    QName name = new QName(webFault.targetNamespace(), webFault.name());
    Object faultObject;
    try {
        Method method = exception.getClass().getMethod("getFaultInfo");
        faultObject = method.invoke(exception);
    } catch (Exception e) {
        throw new RuntimeCamelException("Exception while trying to get fault details", e);
    }
    Fault fault = new Fault();
    fault.setFaultcode(FAULT_CODE_SERVER);
    fault.setFaultstring(exception.getMessage());
    Detail detailEl = new ObjectFactory().createDetail();
    @SuppressWarnings("rawtypes") JAXBElement<?> faultDetailContent = new JAXBElement(name, faultObject.getClass(), faultObject);
    detailEl.getAny().add(faultDetailContent);
    fault.setDetail(detailEl);
    return new ObjectFactory().createFault(fault);
}
Also used : QName(javax.xml.namespace.QName) WebFault(javax.xml.ws.WebFault) Fault(org.xmlsoap.schemas.soap.envelope.Fault) Method(java.lang.reflect.Method) JAXBElement(javax.xml.bind.JAXBElement) RuntimeCamelException(org.apache.camel.RuntimeCamelException) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) WebFault(javax.xml.ws.WebFault) ObjectFactory(org.xmlsoap.schemas.soap.envelope.ObjectFactory) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Detail(org.xmlsoap.schemas.soap.envelope.Detail)

Example 2 with WebFault

use of javax.xml.ws.WebFault in project camel by apache.

the class ServiceInterfaceStrategy method addExceptions.

@SuppressWarnings("unchecked")
private void addExceptions(Method method) {
    Class<?>[] exTypes = method.getExceptionTypes();
    for (Class<?> exType : exTypes) {
        WebFault webFault = exType.getAnnotation(WebFault.class);
        if (webFault != null) {
            QName faultName = new QName(webFault.targetNamespace(), webFault.name());
            faultNameToException.put(faultName, (Class<? extends Exception>) exType);
        }
    }
}
Also used : WebFault(javax.xml.ws.WebFault) QName(javax.xml.namespace.QName)

Example 3 with WebFault

use of javax.xml.ws.WebFault in project tomee by apache.

the class EjbMethodInvoker method preEjbInvoke.

private Object preEjbInvoke(Exchange exchange, Method method, List<Object> params) {
    EjbMessageContext ctx = new EjbMessageContext(exchange.getInMessage(), Scope.APPLICATION);
    WebServiceContextImpl.setMessageContext(ctx);
    Map<String, Object> handlerProperties = removeHandlerProperties(ctx);
    exchange.put(HANDLER_PROPERTIES, handlerProperties);
    try {
        EjbInterceptor interceptor = new EjbInterceptor(params, method, this.bus, exchange);
        Object[] arguments = { ctx, interceptor };
        RpcContainer container = (RpcContainer) this.beanContext.getContainer();
        Class callInterface = this.beanContext.getServiceEndpointInterface();
        method = getMostSpecificMethod(beanContext, method, callInterface);
        Object res = container.invoke(this.beanContext.getDeploymentID(), InterfaceType.SERVICE_ENDPOINT, callInterface, method, arguments, null);
        if (exchange.isOneWay()) {
            return null;
        }
        return new MessageContentsList(res);
    } catch (ApplicationException e) {
        // when no handler is defined, EjbInterceptor will directly delegate
        // to #directEjbInvoke. So if an application exception is thrown by
        // the end user, when must consider the ApplicationException as a
        // web fault if it contains the @WebFault exception
        Throwable t = e.getCause();
        if (t != null) {
            if (RuntimeException.class.isAssignableFrom(t.getClass()) && t.getClass().isAnnotationPresent(javax.ejb.ApplicationException.class)) {
                // it's not a checked exception so it can not be a WebFault
                throw (RuntimeException) t;
            } else if (!t.getClass().isAnnotationPresent(WebFault.class)) {
                // not a web fault even if it's an EJB ApplicationException
                exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
                throw createFault(t, method, params, false);
            }
        } else {
            // may not occurs ...
            t = e;
        }
        // TODO may be we can change to FaultMode.CHECKED_APPLICATION_FAULT
        exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
        throw createFault(t, method, params, false);
    } catch (Exception e) {
        exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
        throw createFault(e, method, params, false);
    } finally {
        WebServiceContextImpl.clear();
    }
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) ApplicationException(org.apache.openejb.ApplicationException) WebFault(javax.xml.ws.WebFault) FaultMode(org.apache.cxf.message.FaultMode) RpcContainer(org.apache.openejb.RpcContainer) ApplicationException(org.apache.openejb.ApplicationException)

Example 4 with WebFault

use of javax.xml.ws.WebFault in project cxf by apache.

the class WebFaultInInterceptor method getFaultName.

private QName getFaultName(Exception webFault) {
    QName faultName = null;
    WebFault wf = webFault.getClass().getAnnotation(WebFault.class);
    if (wf != null) {
        faultName = new QName(wf.targetNamespace(), wf.name());
    }
    return faultName;
}
Also used : WebFault(javax.xml.ws.WebFault) QName(javax.xml.namespace.QName)

Example 5 with WebFault

use of javax.xml.ws.WebFault in project cxf by apache.

the class WebFaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Fault f = (Fault) message.getContent(Exception.class);
    if (f == null) {
        return;
    }
    try {
        Throwable thr = f.getCause();
        SOAPFaultException sf = null;
        if (thr instanceof SOAPFaultException) {
            sf = (SOAPFaultException) thr;
        } else if (thr.getCause() instanceof SOAPFaultException) {
            sf = (SOAPFaultException) thr.getCause();
        }
        if (sf != null) {
            SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
            if (soapVersion != null && soapVersion.getVersion() != 1.1) {
                if (f instanceof SoapFault) {
                    for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
                        ((SoapFault) f).addSubCode(it.next());
                    }
                }
                if (sf.getFault().getFaultReasonLocales().hasNext()) {
                    Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
                    String convertedLang = lang.getLanguage();
                    String country = lang.getCountry();
                    if (country.length() > 0) {
                        convertedLang = convertedLang + '-' + country;
                    }
                    f.setLang(convertedLang);
                }
            }
            message.setContent(Exception.class, f);
        }
    } catch (Exception e) {
    // do nothing;
    }
    Throwable cause = f.getCause();
    WebFault fault = null;
    if (cause != null) {
        fault = getWebFaultAnnotation(cause.getClass());
        if (fault == null && cause.getCause() != null) {
            fault = getWebFaultAnnotation(cause.getCause().getClass());
            if (fault != null || cause instanceof RuntimeException) {
                cause = cause.getCause();
            }
        }
    }
    if (cause instanceof Exception && fault != null) {
        Exception ex = (Exception) cause;
        Object faultInfo;
        try {
            Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
            faultInfo = method.invoke(cause, new Object[0]);
        } catch (NoSuchMethodException e) {
            faultInfo = createFaultInfoBean(fault, cause);
        } catch (InvocationTargetException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
        } catch (IllegalAccessException | IllegalArgumentException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
        }
        Service service = message.getExchange().getService();
        try {
            DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
                Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
                writer.setSchema(schema);
            }
            OperationInfo op = null;
            // Prevent a NPE if we can't match the operation
            if (message.getExchange().getBindingOperationInfo() != null) {
                op = message.getExchange().getBindingOperationInfo().getOperationInfo();
            }
            QName faultName = getFaultName(fault, cause.getClass(), op);
            MessagePartInfo part = op != null ? getFaultMessagePart(faultName, op) : null;
            if (f.hasDetails()) {
                writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
            } else {
                writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
                if (!f.getDetail().hasChildNodes()) {
                    f.setDetail(null);
                }
            }
            f.setMessage(ex.getMessage());
        } catch (Fault f2) {
            message.setContent(Exception.class, f2);
            super.handleMessage(message);
        } catch (Exception nex) {
            // if exception occurs while writing a fault, we'll just let things continue
            // and let the rest of the chain try handling it as is.
            LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
        }
    } else if (cause instanceof SOAPFaultException && ((SOAPFaultException) cause).getFault().hasDetail()) {
        return;
    } else if (f instanceof SoapFault && f.getCause() instanceof SOAPFaultException && ((SOAPFaultException) f.getCause()).getFault().hasDetail()) {
        return;
    } else {
        FaultMode mode = message.get(FaultMode.class);
        if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
            // only convert checked exceptions with this
            // otherwise delegate down to the normal protocol specific stuff
            super.handleMessage(message);
        }
    }
}
Also used : Locale(java.util.Locale) OperationInfo(org.apache.cxf.service.model.OperationInfo) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) Schema(javax.xml.validation.Schema) Fault(org.apache.cxf.interceptor.Fault) WebFault(javax.xml.ws.WebFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) WebFault(javax.xml.ws.WebFault) FaultMode(org.apache.cxf.message.FaultMode) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion)

Aggregations

WebFault (javax.xml.ws.WebFault)13 QName (javax.xml.namespace.QName)8 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)3 Fault (org.apache.cxf.interceptor.Fault)3 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)3 AbstractCodeGenTest (org.apache.cxf.tools.wsdlto.AbstractCodeGenTest)3 Test (org.junit.Test)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JAXBElement (javax.xml.bind.JAXBElement)2 SOAPException (javax.xml.soap.SOAPException)2 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 SoapFault (org.apache.cxf.binding.soap.SoapFault)2 FaultMode (org.apache.cxf.message.FaultMode)2 OperationInfo (org.apache.cxf.service.model.OperationInfo)2 File (java.io.File)1 Locale (java.util.Locale)1 Schema (javax.xml.validation.Schema)1