Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6961517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:32:26+00:00 2026-05-27T15:32:26+00:00

I’m scratching my head over this: Using an Interceptor to check a few SOAP

  • 0

I’m scratching my head over this:
Using an Interceptor to check a few SOAP headers, how can I abort the interceptor chain but still respond with an error to the user?

Throwing a Fault works regarding the output, but the request is still being processed and I’d rather not have all services check for some flag in the message context.

Aborting with “message.getInterceptorChain().abort();” really aborts all processing, but then there’s also nothing returned to the client.

What’s the right way to go?

public class HeadersInterceptor extends AbstractSoapInterceptor {

    public HeadersInterceptor() {
        super(Phase.PRE_LOGICAL);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        Exchange exchange = message.getExchange();
        BindingOperationInfo bop = exchange.getBindingOperationInfo();
        Method action = ((MethodDispatcher) exchange.get(Service.class)
                .get(MethodDispatcher.class.getName())).getMethod(bop);

        if (action.isAnnotationPresent(NeedsHeaders.class)
                && !headersPresent(message)) {
            Fault fault = new Fault(new Exception("No headers Exception"));
            fault.setFaultCode(new QName("Client"));

            try {
                Document doc = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder().newDocument();
                Element detail = doc.createElementNS(Soap12.SOAP_NAMESPACE, "mynamespace");
                detail.setTextContent("Missing some headers...blah");
                fault.setDetail(detail);

            } catch (ParserConfigurationException e) {
            }

            // bad: message.getInterceptorChain().abort();
            throw fault;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:32:26+00:00Added an answer on May 27, 2026 at 3:32 pm

    Following the suggestion by Donal Fellows I’m adding an answer to my question.

    CXF heavily relies on Spring’s AOP which can cause problems of all sorts, at least here it did. I’m providing the complete code for you. Using open source projects I think it’s just fair to provide my own few lines of code for anyone who might decide not to use WS-Security (I’m expecting my services to run on SSL only). I wrote most of it by browsing the CXF sources.

    Please, comment if you think there’s a better approach.

    /**
     * Checks the requested action for AuthenticationRequired annotation and tries
     * to login using SOAP headers username/password.
     * 
     * @author Alexander Hofbauer
     */
    public class AuthInterceptor extends AbstractSoapInterceptor {
        public static final String KEY_USER = "UserAuth";
    
        @Resource
        UserService userService;
    
        public AuthInterceptor() {
            // process after unmarshalling, so that method and header info are there
            super(Phase.PRE_LOGICAL);
        }
    
        @Override
        public void handleMessage(SoapMessage message) throws Fault {
            Logger.getLogger(AuthInterceptor.class).trace("Intercepting service call");
    
            Exchange exchange = message.getExchange();
            BindingOperationInfo bop = exchange.getBindingOperationInfo();
            Method action = ((MethodDispatcher) exchange.get(Service.class)
                    .get(MethodDispatcher.class.getName())).getMethod(bop);
    
            if (action.isAnnotationPresent(AuthenticationRequired.class)
                    && !authenticate(message)) {
                Fault fault = new Fault(new Exception("Authentication failed"));
                fault.setFaultCode(new QName("Client"));
    
                try {
                    Document doc = DocumentBuilderFactory.newInstance()
                            .newDocumentBuilder().newDocument();
                    Element detail = doc.createElementNS(Soap12.SOAP_NAMESPACE, "test");
                    detail.setTextContent("Failed to authenticate.\n" +
                            "Please make sure to send correct SOAP headers username and password");
                    fault.setDetail(detail);
    
                } catch (ParserConfigurationException e) {
                }
    
                throw fault;
            }
        }
    
        private boolean authenticate(SoapMessage msg) {
            Element usernameNode = null;
            Element passwordNode = null;
    
            for (Header header : msg.getHeaders()) {
                if (header.getName().getLocalPart().equals("username")) {
                    usernameNode = (Element) header.getObject();
                } else if (header.getName().getLocalPart().equals("password")) {
                    passwordNode = (Element) header.getObject();
                }
            }
    
            if (usernameNode == null || passwordNode == null) {
                return false;
            }
            String username = usernameNode.getChildNodes().item(0).getNodeValue();
            String password = passwordNode.getChildNodes().item(0).getNodeValue();
    
            User user = null;
            try {
                user = userService.loginUser(username, password);
            } catch (BusinessException e) {
                return false;
            }
            if (user == null) {
                return false;
            }
    
            msg.put(KEY_USER, user);
            return true;
        }
    }
    

    As mentioned above, here’s the ExceptionHandler/-Logger. At first I wasn’t able to use it in combination with JAX-RS (also via CXF, JAX-WS works fine now). I don’t need JAX-RS anyway, so that problem is gone now.

    @Aspect
    public class ExceptionHandler {
        @Resource
        private Map<String, Boolean> registeredExceptions;
    
    
        /**
         * Everything in my project.
         */
        @Pointcut("within(org.myproject..*)")
        void inScope() {
        }
    
        /**
         * Every single method.
         */
        @Pointcut("execution(* *(..))")
        void anyOperation() {
        }
    
        /**
         * Log every Throwable.
         * 
         * @param t
         */
        @AfterThrowing(pointcut = "inScope() && anyOperation()", throwing = "t")
        public void afterThrowing(Throwable t) {
            StackTraceElement[] trace = t.getStackTrace();
            Logger logger = Logger.getLogger(ExceptionHandler.class);
    
            String info;
            if (trace.length > 0) {
                info = trace[0].getClassName() + ":" + trace[0].getLineNumber()
                        + " threw " + t.getClass().getName();
            } else {
                info = "Caught throwable with empty stack trace";
            }
            logger.warn(info + "\n" + t.getMessage());
            logger.debug("Stacktrace", t);
        }
    
        /**
         * Handles all exceptions according to config file.
         * Unknown exceptions are always thrown, registered exceptions only if they
         * are set to true in config file.
         * 
         * @param pjp
         * @throws Throwable
         */
        @Around("inScope() && anyOperation()")
        public Object handleThrowing(ProceedingJoinPoint pjp) throws Throwable {
            try {
                Object ret = pjp.proceed();
                return ret;
            } catch (Throwable t) {
                // We don't care about unchecked Exceptions
                if (!(t instanceof Exception)) {
                    return null;
                }
    
                Boolean throwIt = registeredExceptions.get(t.getClass().getName());
                if (throwIt == null || throwIt) {
                    throw t;
                }
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.