Package org.skyway.spring.util.webservice.cxf

Source Code of org.skyway.spring.util.webservice.cxf.WSSecurityOutInterceptor

/**
* Copyright 2007 - 2011 Skyway Software, Inc.
*/
package org.skyway.spring.util.webservice.cxf;

import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.skyway.spring.util.webservice.WebServiceInvocationCredentials;
import org.skyway.spring.util.webservice.WebServiceInvocationCredentialsHolder;

/**
* Intercepts and applies credentials (if necessary) to outgoing web service calls.
* This handles WS-Security authentication.
*
* @author jperkins
*
*/
public class WSSecurityOutInterceptor extends AbstractPhaseInterceptor<Message> {
  public WSSecurityOutInterceptor() {
    super(Phase.PRE_PROTOCOL);
  }

  public WSSecurityOutInterceptor(String s) {
    super(Phase.PRE_PROTOCOL);
  }

  @SuppressWarnings("unchecked")
  public void handleMessage(Message message) throws Fault {
    /*
     * If we are the requestor, then this means we are making an external web service
     * call and need to check if we need to set credentials before the call is made.
     */
    if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
      WebServiceInvocationCredentials parameters = WebServiceInvocationCredentialsHolder.get();

      if (parameters != null && parameters.isAuthenticationWsSecurity()) {
        Map props = new HashMap();
        props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        props.put(WSHandlerConstants.USER, parameters.getUserId());
        props.put(WSHandlerConstants.PASSWORD_TYPE, parameters.isHashPassword() ? WSConstants.PW_DIGEST : WSConstants.PW_TEXT);
        props.put(WSHandlerConstants.PW_CALLBACK_REF, new ApplyCredentialsHandler());

        WSS4JOutInterceptor wss4jInHandler = new WSS4JOutInterceptor(props);

        message.getInterceptorChain().add(wss4jInHandler);
        message.getInterceptorChain().add(new SAAJOutInterceptor());
      }
    }
  }
}
TOP

Related Classes of org.skyway.spring.util.webservice.cxf.WSSecurityOutInterceptor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.