3

I'm trying to return fault SOAP-message with Apache CXF, but only one way I could find it is custom exceptions (@WebFault). For example (this is my @WebService's method):

@Override
public String getAuthKey(String username, String password) throws BadCredeintialsException {        
    UserDetails ud = userDetailsService.loadUserByUsername(username);
    String pwd = passwordEncoder.encode(password);

    if (pwd.equals(ud.getPassword())) {
        return authKeyService.generateAuthKey(ud.getUsername()).getKey();
    }
    else {
        throw new BadCredeintialsException("Wrong username or password", new FaultInfoBean());
    }
}

BadCredeintialsException here is annotated by @WebFault and extends Exception (with 2 needed constructors and getFaultInfo() method).

Problem: When exception throws, server prints stack trace of exception into log, but I don't need this, this case (wrong login or pwd) is too low for garbage my server log, I need just return fault as SOAP-message, don't need to throw a real exception.

How can I do this? Thank you.

marshall
  • 265
  • 3
  • 11
  • If I remember correctly cxf throws the exception as a warning, which logging framework you are using? – Karthik Prasad May 14 '15 at 12:13
  • @KarthikPrasad, I'm using JBoss Wildfly server, which is using log4j by default. And cxf throws exception as [ERROR] in my case, after that it prints [INFO] message with same text (but already without stacktrace). Now I just created Exception with `writableStackTrace=false`, seems better, but still I get duplicate messages ([ERROR] + [INFO]). – marshall May 14 '15 at 13:46

1 Answers1

1

I worked around this by defining a FaultHandler Interceptor, and mark exception as known, expected exception. In this case CXF didn't print WARN and exception trace.

@Override
public void handleFault(Message message) {

    FaultMode mode = message.get(FaultMode.class);
    Exception exception = message.getContent(Exception.class);

    if (exception != null && exception.getCause() != null) {
        if (mode != FaultMode.CHECKED_APPLICATION_FAULT) {
            if (exception.getCause() instanceof NotificationFailedException) {
                message.put(FaultMode.class, FaultMode.CHECKED_APPLICATION_FAULT);
            }
        }
    }
}

Printed log was below:

INFO    Application {http:<url>}NotificationListener#{http://<url>}Notify has thrown exception, unwinding now: NotificationFailedException: Strange Error
hsnkhrmn
  • 961
  • 7
  • 20