public void handleMessage()

in rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/WebFaultOutInterceptor.java [89:202]


    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);
            }
        }
    }