Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for @WebFault #1392

Open
loganmzz opened this issue Nov 29, 2023 · 0 comments
Open

Support for @WebFault #1392

loganmzz opened this issue Nov 29, 2023 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@loganmzz
Copy link

loganmzz commented Nov 29, 2023

I have generated classes from WSDL. Fault messages are converted into an Exception with a faultInfo (for detail element) and annoted with @WebFault

When I throw the business exception on server side, it's just converted into a generic javax.xml.ws.soap.SOAPFaultException with just exception message on client side.

Here is my quick&dirty exception resolver:

public class WebFaultAnnotationExceptionResolver implements EndpointExceptionResolver {
    private final ConcurrentMap<Class<?>, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class<?>, JAXBContext>();

    protected Marshaller createMarshaller(Object target) throws JAXBException {
        JAXBContext jaxbContext = jaxbContexts.computeIfAbsent(target.getClass(), clazz -> {
            try {
                return JAXBContext.newInstance(clazz);
            } catch (JAXBException e) {
                throw new RuntimeException(e);
            }
        });
        return jaxbContext.createMarshaller();
    }

    @Override
    public boolean resolveException(MessageContext messageContext, Object endpoint, Exception ex) {
        Annotation webFault = ex.getClass().getAnnotation(WebFault.class);
        if (webFault == null) {
            return false;
        }

        SoapBody soapBody = ((SoapMessage) messageContext.getResponse()).getSoapBody();
        SoapFault soapFault = soapBody.addServerOrReceiverFault(ex.getMessage(), null);

        try {
            Method getFaultInfo = ex.getClass().getMethod("getFaultInfo");
            try {
                Object detail = getFaultInfo.invoke(ex);
                if (detail == null) {
                    return true;
                }
                Marshaller marshaller = createMarshaller(detail);
                marshaller.marshal(detail, soapFault.addFaultDetail().getResult());
                return true;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } catch (NoSuchMethodException e) {
            return true;
        }
    }
}
@snicoll snicoll added the status: waiting-for-triage An issue we've not yet triaged label Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants